Skip to content

Commit 14ac593

Browse files
mtfishmanclaude
andcommitted
Rename *_norm_messagecache to *_norm_message_env; add norm_message_env
Reserve `messagecache` (and `MessageCache`) for the low-level data-structure constructors; use `_message_env` for the domain-level environment builders. Renames apply to all five constructors: similar_norm_messagecache -> similar_norm_message_env identity_norm_messagecache -> identity_norm_message_env ones_norm_messagecache -> ones_norm_message_env randn_norm_messagecache -> randn_norm_message_env rand_norm_messagecache -> rand_norm_message_env Introduce norm_message_env(f, tn) as the shared filler: it allocates via similar_norm_message_env, applies f to each entry, returns the cache. The identity / ones / randn / rand variants are now one-liners delegating to it. The eventual interface is `*_message_env(NormNetwork(tn))`; for now the network is encoded in the `_norm_` infix until the NormNetwork type lands. A parallel `*_norm_ctm_env` family is planned for CTMRG. Test imports, in-test constructor list, and testset name updated. beliefpropagation_normnetwork docstring cross-refs updated. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
1 parent 8fc52e5 commit 14ac593

2 files changed

Lines changed: 77 additions & 82 deletions

File tree

src/beliefpropagation/normnetwork.jl

Lines changed: 67 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -5,106 +5,101 @@ using NamedDimsArrays:
55
using NamedGraphs.GraphsExtensions: all_edges, incident_edges
66
using 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
2836
end
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
4850
end
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)
8788
end
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)
108103
end
109104

110105
# === Double-layer construction and BP wrapper ===
@@ -144,7 +139,7 @@ end
144139
145140
Run belief propagation on the norm network `⟨tn|tn⟩` (treating `tn` as the ket),
146141
starting 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`
148143
constructors).
149144
150145
The norm network built by [`normnetwork`](@ref) is the source of truth for bra-link

test/test_apply_operator.jl

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import NamedDimsArrays as NDA
33
import TensorAlgebra as TA
44
using ITensorBase: Index
55
using ITensorNetworksNext: TensorNetwork, apply_operator, apply_operators,
6-
beliefpropagation_normnetwork, identity_norm_messagecache, ones_norm_messagecache,
7-
rand_norm_messagecache, randn_norm_messagecache, similar_norm_messagecache
6+
beliefpropagation_normnetwork, identity_norm_message_env, ones_norm_message_env,
7+
rand_norm_message_env, randn_norm_message_env, similar_norm_message_env
88
using MatrixAlgebraKit: truncrank
99
using NamedDimsArrays: name, operator, randname, setname
1010
using NamedGraphs.GraphsExtensions: incident_edges
@@ -48,7 +48,7 @@ end
4848
site_axes = Dict(v => Index(d) for v in Graphs.vertices(g))
4949
state = random_tensornetwork(g, link_axes, site_axes)
5050
env = beliefpropagation_normnetwork(
51-
state, ones_norm_messagecache(state);
51+
state, ones_norm_message_env(state);
5252
stopping_criterion = (; maxiter = 100, tol = 1.0e-13)
5353
)
5454
# Without truncation the gate is applied exactly, so the gated network
@@ -67,7 +67,7 @@ end
6767
site_axes = Dict(v => Index(d) for v in Graphs.vertices(g))
6868
state = random_tensornetwork(g, link_axes, site_axes)
6969
env = beliefpropagation_normnetwork(
70-
state, ones_norm_messagecache(state);
70+
state, ones_norm_message_env(state);
7171
stopping_criterion = (; maxiter = 100, tol = 1.0e-13)
7272
)
7373
gate = randn_operator((site_axes[2], site_axes[3]))
@@ -85,7 +85,7 @@ end
8585
site_axes = Dict(v => Index(d) for v in Graphs.vertices(g))
8686
state = random_tensornetwork(g, link_axes, site_axes)
8787
env = beliefpropagation_normnetwork(
88-
state, ones_norm_messagecache(state);
88+
state, ones_norm_message_env(state);
8989
stopping_criterion = (; maxiter = 100, tol = 1.0e-13)
9090
)
9191
# Gates on neighboring edges sharing site 3, applied in sequence.
@@ -95,7 +95,7 @@ end
9595
@test prod(gated) NDA.apply(g2, NDA.apply(g1, prod(state)))
9696
end
9797

98-
@testset "norm-messagecache constructors" begin
98+
@testset "norm-message-env constructors" begin
9999
link_axes = Dict(e => Index(χ) for e in Graphs.edges(g))
100100
site_axes = Dict(v => Index(d) for v in Graphs.vertices(g))
101101
state = random_tensornetwork(g, link_axes, site_axes)
@@ -104,9 +104,9 @@ end
104104
# undirected edge of the state.
105105
n_directed = 2 * length(collect(Graphs.edges(g)))
106106
for ctor in (
107-
similar_norm_messagecache, identity_norm_messagecache,
108-
ones_norm_messagecache, randn_norm_messagecache,
109-
rand_norm_messagecache,
107+
similar_norm_message_env, identity_norm_message_env,
108+
ones_norm_message_env, randn_norm_message_env,
109+
rand_norm_message_env,
110110
)
111111
cache = ctor(state)
112112
@test length(collect(Graphs.edges(cache))) == n_directed
@@ -115,7 +115,7 @@ end
115115
# Identity env reproduces the gauge-invariant exact-gate property: an
116116
# untruncated gate gives the exact result regardless of which valid env we
117117
# gauge against.
118-
env = identity_norm_messagecache(state)
118+
env = identity_norm_message_env(state)
119119
for gate in (
120120
randn_operator((site_axes[2],)),
121121
randn_operator((site_axes[2], site_axes[3])),

0 commit comments

Comments
 (0)