Skip to content

Commit 291a04a

Browse files
mtfishmanclaude
andcommitted
docs: rebuild random_state helper using basic Dict-of-ITensor constructor
Switch the `@setup` / `@example` blocks in `docs/src/computing_properties.md` and `docs/src/solvers.md` from the function-callback `ITensorNetwork(s; link_space, kwargs...) do v ... end` form to an explicit construction: 1. build a `Dict{Edge, Index}` of link indices over `edges(g)` (mirrored under `reverse(e)`), 2. build an `ITensor` for each vertex from the site index plus the link indices on that vertex's incident edges, 3. wrap with the basic `ITensorNetwork(::Dict{V, ITensor})` constructor. Mirrors the construction pattern used in the ITensorNetworksNext.jl tests and avoids leaning on the `do v ... end` higher-level constructor that we intend to phase out. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent ae7018a commit 291a04a

2 files changed

Lines changed: 27 additions & 14 deletions

File tree

docs/src/computing_properties.md

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
11
# Computing Properties
22

33
```@setup main
4-
using Graphs: vertices
4+
using Graphs: edges, vertices
55
using ITensorNetworks: ITensorNetwork, expect, inner, loginner, normalize, siteinds, ttn
6-
using ITensors: itensor
7-
using ITensors.NDTensors: dim
6+
using ITensors: Index, random_itensor
87
using LinearAlgebra: norm
8+
using NamedGraphs.GraphsExtensions: incident_edges
99
using NamedGraphs.NamedGraphGenerators: named_grid
1010
11-
random_state(s) = ITensorNetwork(s; link_space = 2) do v
12-
return inds -> itensor(randn(Float64, dim.(inds)...), inds)
11+
function random_state(g, s; link_space = 2)
12+
l = Dict(e => Index(link_space, "Link") for e in edges(g))
13+
l = merge(l, Dict(reverse(e) => l[e] for e in edges(g)))
14+
ts = Dict(
15+
v => random_itensor(only(s[v]), (l[e] for e in incident_edges(g, v))...)
16+
for v in vertices(g)
17+
)
18+
return ITensorNetwork(ts)
1319
end
1420
1521
g = named_grid((4,))
1622
s = siteinds("S=1/2", g)
17-
phi = normalize(ttn(random_state(s)))
18-
psi = normalize(ttn(random_state(s)))
19-
x = normalize(ttn(random_state(s)))
20-
y = normalize(ttn(random_state(s)))
23+
phi = normalize(ttn(random_state(g, s)))
24+
psi = normalize(ttn(random_state(g, s)))
25+
x = normalize(ttn(random_state(g, s)))
26+
y = normalize(ttn(random_state(g, s)))
2127
v = first(vertices(psi))
2228
```
2329

docs/src/solvers.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,20 @@ variational sweep algorithm.
1616
[`dmrg`](@ref ITensorNetworks.dmrg) is an alias for `eigsolve`.
1717

1818
```@example main
19+
using Graphs: vertices
1920
using ITensorNetworks: ITensorNetwork, dmrg, dst, edges, normalize, siteinds, src, ttn
20-
using ITensors: OpSum, itensor
21-
using ITensors.NDTensors: dim
21+
using ITensors: Index, OpSum, random_itensor
22+
using NamedGraphs.GraphsExtensions: incident_edges
2223
using NamedGraphs.NamedGraphGenerators: named_comb_tree
2324
24-
random_state(s) = ITensorNetwork(s; link_space = 2) do v
25-
return inds -> itensor(randn(Float64, dim.(inds)...), inds)
25+
function random_state(g, s; link_space = 2)
26+
l = Dict(e => Index(link_space, "Link") for e in edges(g))
27+
l = merge(l, Dict(reverse(e) => l[e] for e in edges(g)))
28+
ts = Dict(
29+
v => random_itensor(only(s[v]), (l[e] for e in incident_edges(g, v))...)
30+
for v in vertices(g)
31+
)
32+
return ITensorNetwork(ts)
2633
end
2734
2835
# Build a Heisenberg Hamiltonian on a comb tree
@@ -38,7 +45,7 @@ H = let h = OpSum()
3845
end
3946
4047
# Random initial state (normalise first!)
41-
psi0 = normalize(ttn(random_state(s)))
48+
psi0 = normalize(ttn(random_state(g, s)))
4249
4350
# Run DMRG
4451
energy, psi = dmrg(H, psi0;

0 commit comments

Comments
 (0)