@@ -5,106 +5,101 @@ using NamedDimsArrays:
55using NamedGraphs. GraphsExtensions: all_edges, incident_edges
66using Random: Random
77
8- # === MessageCache constructors keyed to the norm network ⟨tn|tn⟩ ===
8+ # === Norm-network environment constructors ===
9+ #
10+ # `*_norm_message_env(tn)` builds a `MessageCache` shaped to act as the BP environment
11+ # for the norm network ⟨tn|tn⟩, with each entry filled per the leading verb (`identity`,
12+ # `ones`, `randn`, `rand`). The `_env` suffix is reserved for the high-level
13+ # environment-builder interface; the low-level `MessageCache` / `messagecache(...)`
14+ # constructors are used internally. A parallel `*_norm_ctm_env` family is planned for
15+ # CTMRG environments.
916
1017"""
11- similar_norm_messagecache(tn) -> MessageCache
12-
13- Allocate a `MessageCache` of square operator messages with **undefined** data, one per
14- directed edge of the undirected graph of `tn` (both directions on every undirected edge).
15- Each message's codomain is the link axes on that edge in `tn`; the domain has dual axes
16- with fresh `randname`-generated names. The element type and backend are inherited from
17- the factor tensors of `tn` via `Base.similar`.
18-
19- This is the allocator that backs the filled-cache constructors
20- (`identity_norm_messagecache`, `ones_norm_messagecache`, `randn_norm_messagecache`).
21- Use it directly to construct caches with custom message data, e.g. by mutating each
22- entry after allocation.
18+ similar_norm_message_env(tn) -> MessageCache
19+
20+ Allocate a BP environment for the norm network ⟨tn|tn⟩ with **undefined** message data:
21+ one square operator message per directed edge of `tn` (both directions on every
22+ undirected edge). Each message's codomain is the link axes on that edge in `tn`; the
23+ domain has dual axes with fresh `randname`-generated names. Element type and backend are
24+ inherited from the factor tensors of `tn` via `Base.similar`.
25+
26+ Used internally by [`norm_message_env`](@ref) and the filled environment constructors
27+ ([`identity_norm_message_env`](@ref), [`ones_norm_message_env`](@ref),
28+ [`randn_norm_message_env`](@ref), [`rand_norm_message_env`](@ref)). Use it directly to
29+ construct environments with custom message data, e.g. by mutating each entry after
30+ allocation.
2331"""
24- function similar_norm_messagecache (tn)
32+ function similar_norm_message_env (tn)
2533 return messagecache (all_edges (tn)) do e
2634 return similar_operator (tn[src (e)], linkinds (tn, e))
2735 end
2836end
2937
3038"""
31- identity_norm_messagecache( tn) -> MessageCache
39+ norm_message_env(f, tn) -> MessageCache
3240
33- Allocate a `MessageCache` of identity-operator messages, one per directed edge of `tn`.
34- Each message acts as the identity map on the link axis for its edge — the
35- "uncorrelated environment" starting point for belief-propagation simple-update gauging
36- on the norm network ⟨tn|tn⟩.
37-
38- See also: [`ones_norm_messagecache`](@ref), [`randn_norm_messagecache`](@ref),
39- [`rand_norm_messagecache`](@ref), [`similar_norm_messagecache`](@ref).
40- """
41- function identity_norm_messagecache (tn)
42- m = similar_norm_messagecache (tn)
43- # `one_operator` is held locally in `tensoralgebra.jl` and would become
44- # `Base.one(::AbstractNamedDimsOperator)` once that lands upstream.
45- # TODO : replace with `map(one_operator, m)` once `map` is defined on `MessageCache`.
46- foreach (e -> m[e] = one_operator (m[e]), edges (m))
47- return m
41+ Allocate a norm-network BP environment via [`similar_norm_message_env`](@ref) and apply
42+ `f` to each operator-message entry. Shared building block for the filled-environment
43+ constructors.
44+ """
45+ function norm_message_env (f, tn)
46+ env = similar_norm_message_env (tn)
47+ # TODO : replace with `map(f, env)` once `map` is defined on `MessageCache`.
48+ foreach (e -> env[e] = f (env[e]), edges (env))
49+ return env
4850end
4951
5052"""
51- ones_norm_messagecache (tn) -> MessageCache
53+ identity_norm_message_env (tn) -> MessageCache
5254
53- Allocate a `MessageCache` whose per-edge messages have every entry equal to `1`. Each
54- message is the rank-1 outer product of all-ones vectors on the (codomain, domain) link
55- axes .
55+ Build a norm-network BP environment with identity-operator messages on every edge — the
56+ "uncorrelated environment" starting point for belief-propagation simple-update gauging
57+ on ⟨tn|tn⟩ .
5658
57- See also: [`identity_norm_messagecache `](@ref), [`randn_norm_messagecache `](@ref),
58- [`rand_norm_messagecache `](@ref).
59+ See also: [`ones_norm_message_env `](@ref), [`randn_norm_message_env `](@ref),
60+ [`rand_norm_message_env`](@ref), [`similar_norm_message_env `](@ref).
5961"""
60- function ones_norm_messagecache (tn)
61- m = similar_norm_messagecache (tn)
62- # TODO : replace with `map(msg -> fill!(msg, one(eltype(msg))), m)` once `map`
63- # is defined on `MessageCache`.
64- foreach (e -> m[e] = fill! (m[e], one (eltype (m[e]))), edges (m))
65- return m
66- end
62+ identity_norm_message_env (tn) = norm_message_env (one_operator, tn)
63+
64+ """
65+ ones_norm_message_env(tn) -> MessageCache
66+
67+ Build a norm-network BP environment whose per-edge messages have every entry equal to
68+ `1` — the rank-1 outer product of all-ones vectors on each (codomain, domain) pair.
69+
70+ See also: [`identity_norm_message_env`](@ref), [`randn_norm_message_env`](@ref),
71+ [`rand_norm_message_env`](@ref).
72+ """
73+ ones_norm_message_env (tn) = norm_message_env (msg -> fill! (msg, one (eltype (msg))), tn)
6774
68- randn_norm_messagecache (tn) = randn_norm_messagecache (Random. default_rng (), tn)
75+ randn_norm_message_env (tn) = randn_norm_message_env (Random. default_rng (), tn)
6976
7077"""
71- randn_norm_messagecache ([rng], tn) -> MessageCache
78+ randn_norm_message_env ([rng], tn) -> MessageCache
7279
73- Allocate a `MessageCache` whose per-edge messages have entries drawn from a standard
74- normal distribution. `rng` defaults to `Random.default_rng()`.
80+ Build a norm-network BP environment whose per-edge messages have entries drawn from a
81+ standard normal distribution. `rng` defaults to `Random.default_rng()`.
7582
76- See also: [`rand_norm_messagecache `](@ref), [`identity_norm_messagecache `](@ref),
77- [`ones_norm_messagecache `](@ref).
83+ See also: [`rand_norm_message_env `](@ref), [`identity_norm_message_env `](@ref),
84+ [`ones_norm_message_env `](@ref).
7885"""
79- function randn_norm_messagecache (rng:: Random.AbstractRNG , tn)
80- m = similar_norm_messagecache (tn)
81- # `randn_operator!` is held locally in `tensoralgebra.jl`; would become a
82- # method of `Random.randn!` once that lands upstream.
83- # TODO : replace with `map(msg -> randn_operator!(rng, msg), m)` once `map` is
84- # defined on `MessageCache`.
85- foreach (e -> randn_operator! (rng, m[e]), edges (m))
86- return m
86+ function randn_norm_message_env (rng:: Random.AbstractRNG , tn)
87+ return norm_message_env (msg -> randn_operator! (rng, msg), tn)
8788end
8889
89- rand_norm_messagecache (tn) = rand_norm_messagecache (Random. default_rng (), tn)
90+ rand_norm_message_env (tn) = rand_norm_message_env (Random. default_rng (), tn)
9091
9192"""
92- rand_norm_messagecache ([rng], tn) -> MessageCache
93+ rand_norm_message_env ([rng], tn) -> MessageCache
9394
94- Allocate a `MessageCache` whose per-edge messages have entries drawn from a uniform
95- distribution on `[0, 1)`. `rng` defaults to `Random.default_rng()`.
95+ Build a norm-network BP environment whose per-edge messages have entries drawn from a
96+ uniform distribution on `[0, 1)`. `rng` defaults to `Random.default_rng()`.
9697
97- See also: [`randn_norm_messagecache `](@ref), [`identity_norm_messagecache `](@ref),
98- [`ones_norm_messagecache `](@ref).
98+ See also: [`randn_norm_message_env `](@ref), [`identity_norm_message_env `](@ref),
99+ [`ones_norm_message_env `](@ref).
99100"""
100- function rand_norm_messagecache (rng:: Random.AbstractRNG , tn)
101- m = similar_norm_messagecache (tn)
102- # `rand_operator!` is held locally in `tensoralgebra.jl`; would become a
103- # method of `Random.rand!` once that lands upstream.
104- # TODO : replace with `map(msg -> rand_operator!(rng, msg), m)` once `map` is
105- # defined on `MessageCache`.
106- foreach (e -> rand_operator! (rng, m[e]), edges (m))
107- return m
101+ function rand_norm_message_env (rng:: Random.AbstractRNG , tn)
102+ return norm_message_env (msg -> rand_operator! (rng, msg), tn)
108103end
109104
110105# === Double-layer construction and BP wrapper ===
144139
145140Run belief propagation on the norm network `⟨tn|tn⟩` (treating `tn` as the ket),
146141starting from a pre-built operator `MessageCache` `messages` (e.g. from
147- [`identity_norm_messagecache `](@ref) or any of the other `*_norm_messagecache `
142+ [`identity_norm_message_env `](@ref) or any of the other `*_norm_message_env `
148143constructors).
149144
150145The norm network built by [`normnetwork`](@ref) is the source of truth for bra-link
0 commit comments