Skip to content

Commit 6e3c3b3

Browse files
mtfishmanclaude
andauthored
Move test-only helpers (random_tensornetwork, random_ttn, random_mps, ModelHamiltonians) out of src/ (#339)
## Summary Pull non-basic constructors that are only used by tests out of `src/` and into a new `test/utils.jl`. Each test that needs them does `include("utils.jl")` inside its gensym module. The file is intentionally named `utils.jl` so the `ITensorPkgSkeleton.runtests` runner does not auto-pick it up (the runner only globs `test_*.jl`). **What moved to `test/utils.jl`:** - `random_tensornetwork` (12 method specializations, including the `Distribution`- and `IsUnderlyingGraph`-based variants) — moved from `src/specialitensornetworks.jl`. - `random_ttn` and `random_mps` — moved from `src/treetensornetworks/treetensornetwork.jl`. These thin wrappers around `random_tensornetwork` had no real `src/` consumers (only docstring examples) and a single test reference. - `ModelHamiltonians.tight_binding`, `heisenberg`, `ising` plus the internal helpers `to_callable`, `nth_nearest_neighbors`, `next_nearest_neighbors` — moved from `src/lib/ModelHamiltonians/` into a `ModelHamiltonians` submodule of `test/utils.jl`. Test sites continue to call them as `ModelHamiltonians.ising(...)` etc. **What was deleted entirely (no consumers):** - `delta_network` (and the rest of `src/specialitensornetworks.jl`) — only consumer was `ModelNetworks.ising_network`, which itself had no consumers. - `ModelHamiltonians.hubbard` — no consumers. - `src/lib/ModelNetworks/` (`ising_network`, `ising_network_state`) — no consumers; the lone `using ITensorNetworks.ModelNetworks: ModelNetworks` in `test/test_belief_propagation.jl` was an unused import and was dropped. - `random_ttn` / `random_mps` jldoctest examples in `src/normalize.jl`, `src/expect.jl`, `src/treetensornetworks/abstracttreetensornetwork.jl`, plus `[ref]` links in `See also:` lists. Consistent with not having doc examples that depend on functions being removed. **Construction pattern** The `random_tensornetwork` helpers in `test/utils.jl` are factored through a private `_random_tensornetwork(s::IndsNetwork, build_tensor; link_space)` that uses the explicit Dict-of-ITensor pattern: build a `Dict{Edge, Index}` of fresh link indices over `edges(s)` (mirrored under `reverse(e)`); for each vertex, gather the site indices from `vertex_data(s)[v]` (or empty if `v` has no entry) plus the link indices on incident edges; let the caller-supplied `build_tensor` produce the per-vertex `ITensor`; wrap with the basic `ITensorNetwork(::Dict{V, ITensor})` constructor. The 12 user-facing `random_tensornetwork` method specializations and their `IsUnderlyingGraph` trait wrappers route through this helper. **Documentation** - `docs/src/{computing_properties,solvers}.md` setup blocks now build a small inline `random_state(g, s; link_space)` helper using the same Dict-of-ITensor pattern, instead of calling `random_ttn`. `link_space` is required (no default), passed explicitly at the call sites. - `docs/src/tree_tensor_networks.md` drops the `random_ttn` / `random_mps` constructor examples and autodoc entries. - `docs/src/reference.md`'s `@autodocs` listing trimmed to just `ITensorNetworks` (no longer pulling in the removed `ModelNetworks` / `ModelHamiltonians` submodules). **Incidental import cleanups** A few imports were previously available inside `ITensorNetworks` as a side effect of the deleted files' `using` clauses; now imported directly: - `ITensors: delta` — `src/formnetworks/bilinearformnetwork.jl`, `src/caches/abstractbeliefpropagationcache.jl` **Version + test deps** - Bump to `0.19.0` (pre-1.0 minor bump = breaking, public API removed). - Update pinned `ITensorNetworks` compat in `test/`, `docs/`, `examples/` Project.toml. - Add `SimpleTraits` to `test/Project.toml` so `test/utils.jl` can use `@traitfn` / `IsUnderlyingGraph`. --------- Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 31bb354 commit 6e3c3b3

33 files changed

Lines changed: 291 additions & 496 deletions

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "ITensorNetworks"
22
uuid = "2919e153-833c-4bdc-8836-1ea460a35fc7"
3-
version = "0.18.1"
3+
version = "0.19.0"
44
authors = ["Matthew Fishman <mfishman@flatironinstitute.org>, Joseph Tindall <jtindall@flatironinstitute.org> and contributors"]
55

66
[workspace]

docs/Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ path = ".."
1616
Documenter = "1.10"
1717
Graphs = "1"
1818
ITensorFormatter = "0.2.27"
19-
ITensorNetworks = "0.18"
19+
ITensorNetworks = "0.19"
2020
ITensors = "0.9"
2121
Literate = "2.20.1"
2222
NamedGraphs = "0.8.2"

docs/src/computing_properties.md

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,29 @@
11
# Computing Properties
22

33
```@setup main
4-
using Graphs: vertices
5-
using ITensorNetworks: expect, inner, loginner, normalize, random_ttn, siteinds
4+
using Graphs: edges, vertices
5+
using ITensorNetworks: ITensorNetwork, expect, inner, loginner, normalize, siteinds, ttn
6+
using ITensors: Index, random_itensor
67
using LinearAlgebra: norm
8+
using NamedGraphs.GraphsExtensions: incident_edges
79
using NamedGraphs.NamedGraphGenerators: named_grid
810
11+
function random_state(g, s; link_space)
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)
19+
end
20+
921
g = named_grid((4,))
1022
s = siteinds("S=1/2", g)
11-
phi = normalize(random_ttn(s; link_space = 2))
12-
psi = normalize(random_ttn(s; link_space = 2))
13-
x = normalize(random_ttn(s; link_space = 2))
14-
y = normalize(random_ttn(s; link_space = 2))
23+
phi = normalize(ttn(random_state(g, s; link_space = 2)))
24+
psi = normalize(ttn(random_state(g, s; link_space = 2)))
25+
x = normalize(ttn(random_state(g, s; link_space = 2)))
26+
y = normalize(ttn(random_state(g, s; link_space = 2)))
1527
v = first(vertices(psi))
1628
```
1729

docs/src/reference.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
# API Reference
22

3-
Complete listing of all documented public functions and types in ITensorNetworks.jl,
4-
ITensorNetworks.ModelNetworks, and ITensorNetworks.ModelHamiltonians.
3+
Complete listing of all documented public functions and types in ITensorNetworks.jl.
54

65
```@autodocs
7-
Modules = [ITensorNetworks, ITensorNetworks.ModelNetworks, ITensorNetworks.ModelHamiltonians]
6+
Modules = [ITensorNetworks]
87
```

docs/src/solvers.md

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

1818
```@example main
19-
using ITensorNetworks: dmrg, dst, edges, normalize, random_ttn, siteinds, src, ttn
20-
using ITensors: OpSum
19+
using Graphs: vertices
20+
using ITensorNetworks: ITensorNetwork, dmrg, dst, edges, normalize, siteinds, src, ttn
21+
using ITensors: Index, OpSum, random_itensor
22+
using NamedGraphs.GraphsExtensions: incident_edges
2123
using NamedGraphs.NamedGraphGenerators: named_comb_tree
2224
25+
function random_state(g, s; link_space)
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)
33+
end
34+
2335
# Build a Heisenberg Hamiltonian on a comb tree
2436
g = named_comb_tree((3, 2))
2537
s = siteinds("S=1/2", g)
@@ -33,7 +45,7 @@ H = let h = OpSum()
3345
end
3446
3547
# Random initial state (normalise first!)
36-
psi0 = normalize(random_ttn(s; link_space = 2))
48+
psi0 = normalize(ttn(random_state(g, s; link_space = 2)))
3749
3850
# Run DMRG
3951
energy, psi = dmrg(H, psi0;

docs/src/tree_tensor_networks.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ and provides a convenient interface for 1D calculations.
2121
```@example main
2222
using Graphs: vertices
2323
using ITensorNetworks: ITensorNetwork, TreeTensorNetwork, mps, ortho_region, orthogonalize,
24-
random_mps, random_ttn, siteinds, ttn
24+
siteinds, ttn
2525
using ITensors: ITensors
2626
using LinearAlgebra: norm
2727
using NamedGraphs.NamedGraphGenerators: named_comb_tree
@@ -33,20 +33,14 @@ sites = siteinds("S=1/2", g)
3333
psi = ttn(sites) # zero-initialised
3434
psi = ttn(v -> "Up", sites) # product state
3535
36-
# Random TTN
37-
psi = random_ttn(sites; link_space = 2)
38-
3936
# 1D MPS
4037
s1d = siteinds("S=1/2", 6)
4138
mps_state = mps(v -> "Up", s1d) # product MPS
42-
mps_state = random_mps(s1d; link_space = 2)
4339
```
4440

4541
```@docs; canonical=false
4642
ITensorNetworks.ttn
4743
ITensorNetworks.mps
48-
ITensorNetworks.random_ttn
49-
ITensorNetworks.random_mps
5044
```
5145

5246
### The `TreeTensorNetwork` type and conversion

examples/Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ ITensorNetworks = "2919e153-833c-4bdc-8836-1ea460a35fc7"
55
path = ".."
66

77
[compat]
8-
ITensorNetworks = "0.18"
8+
ITensorNetworks = "0.19"

src/ITensorNetworks.jl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ include("contraction_sequences.jl")
1414
include("tebd.jl")
1515
include("itensornetwork.jl")
1616
include("contract.jl")
17-
include("specialitensornetworks.jl")
1817
include("partitioneditensornetwork.jl")
1918
include("edge_sequences.jl")
2019
include("caches/abstractbeliefpropagationcache.jl")
@@ -63,7 +62,5 @@ include("normalize.jl")
6362
include("expect.jl")
6463
include("environment.jl")
6564
include("exports.jl")
66-
include("lib/ModelHamiltonians/src/ModelHamiltonians.jl")
67-
include("lib/ModelNetworks/src/ModelNetworks.jl")
6865

6966
end

src/caches/abstractbeliefpropagationcache.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using Adapt: Adapt, adapt, adapt_structure
22
using Graphs: Graphs, IsDirected
3-
using ITensors: dir
3+
using ITensors: delta, dir
44
using LinearAlgebra: diag, dot
55
using NDTensors: NDTensors
66
using NamedGraphs.PartitionedGraphs: PartitionedGraph, PartitionedGraphs, QuotientVertex,

src/expect.jl

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -97,23 +97,6 @@ vertex of `ψ`.
9797
9898
A `Dictionary` mapping each vertex of `ψ` to its expectation value.
9999
100-
# Example
101-
102-
```jldoctest
103-
julia> using NamedGraphs.NamedGraphGenerators: named_grid
104-
105-
julia> g = named_grid((4,));
106-
107-
julia> s = siteinds("S=1/2", g);
108-
109-
julia> psi = random_ttn(s; link_space = 2);
110-
111-
julia> sz = expect(psi, "Sz");
112-
113-
julia> sz_exact = expect(psi, "Sz"; alg = "exact");
114-
115-
```
116-
117100
See also: [`expect(ψ, op::String, vertices)`](@ref),
118101
[`expect(operator, state::AbstractTreeTensorNetwork)`](@ref).
119102
"""

0 commit comments

Comments
 (0)