Skip to content

Commit c634c01

Browse files
mtfishmanclaude
andcommitted
Add empty ITensorNetwork / TreeTensorNetwork constructors
Mirror the `Vector()` / `Dictionary()` convention by providing parameterless and `{V}`-only constructors that yield an empty network: - `ITensorNetwork()` / `ITensorNetwork{V}()` - `TreeTensorNetwork()` / `TreeTensorNetwork{V}()` The default vertex type is `Any`. The body of `ITensorNetwork{V}(tensors)` now seeds from `ITensorNetwork{V}()` and writes each tensor through `setindex!`, removing the explicit field call and the prior `set_vertex_data!` call at this layer. Also drop the redundant `ITensorNetwork(::TreeTensorNetwork)` and the `ITensorNetwork(::AbstractTTN)` "not implemented" placeholder — the generic `ITensorNetwork(tensors)` now handles a `TreeTensorNetwork` via `keytype` and `setindex!`. Update the docs to drop the dangling references. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 7c3eb04 commit c634c01

5 files changed

Lines changed: 26 additions & 29 deletions

File tree

docs/src/experimental_methods.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,8 @@ Methods which still need to be discussed, modified, or deprecated.
3737

3838
#### AbstractTreeTensorNetwork Type
3939

40-
* Required-to-implement abstract interface — `TreeTensorNetwork` provides all three (`treetensornetworks/abstracttreetensornetwork.jl`):
40+
* Required-to-implement abstract interface — `TreeTensorNetwork` provides both (`treetensornetworks/abstracttreetensornetwork.jl`):
4141
```julia
42-
ITensorNetwork(tn::AbstractTTN)
4342
ortho_region(tn::AbstractTTN)
4443
set_ortho_region(tn::AbstractTTN, new_region)
4544
```
@@ -134,11 +133,6 @@ Methods which still need to be discussed, modified, or deprecated.
134133

135134
#### TreeTensorNetwork Type
136135

137-
* Get the underlying `ITensorNetwork` of a `TTN` (drops orthogonality metadata) (`treetensornetworks/treetensornetwork.jl`):
138-
```julia
139-
ITensorNetwork(tn::TTN)
140-
```
141-
142136
* Get the current orthogonality region — the set of vertices forming the gauge center (`treetensornetworks/treetensornetwork.jl`):
143137
```julia
144138
ortho_region(tn::TTN)

docs/src/tree_tensor_networks.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ itn_again = ITensorNetwork(psi) # TTN → ITensorNetwork
6565

6666
```@docs; canonical=false
6767
ITensorNetworks.TreeTensorNetwork
68-
ITensorNetworks.ITensorNetwork(::ITensorNetworks.TreeTensorNetwork)
6968
```
7069

7170
## Orthogonal Gauge

src/itensornetwork.jl

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,24 @@ end
6969
# Constructors
7070
#
7171

72-
# Construct by feeding `tensors` through `set_vertex_data!` one vertex
73-
# at a time — this centralizes the reverse-map registration, edge
74-
# inference, and hypergraph check in a single place (the `setindex!`
75-
# code path). Walking `keys(tensors)` in order makes the resulting
76-
# `neighbors(g, v)` / `edges(g)` iteration order deterministic in the
77-
# input order. An empty `tensors` (`Dict{V, ITensor}()`, etc.) yields
78-
# an empty network — there is no separate empty-arg constructor.
72+
# Empty network with no vertices. Mirrors the `Vector()` / `Dictionary()`
73+
# convention; vertex type defaults to `Any` when unspecified.
74+
function ITensorNetwork{V}() where {V}
75+
return ITensorNetwork{V}(
76+
NamedGraph{V}(), Dictionary{V, ITensor}(), Dict{Index, Set{V}}()
77+
)
78+
end
79+
ITensorNetwork() = ITensorNetwork{Any}()
80+
81+
# Construct by writing each tensor into a freshly empty network via
82+
# `setindex!`. The `setindex!` code path centralizes reverse-map
83+
# registration, edge inference, and the hypergraph check, and walking
84+
# `keys(tensors)` in order makes the resulting `neighbors(g, v)` /
85+
# `edges(g)` iteration order deterministic in the input order.
7986
function ITensorNetwork{V}(tensors) where {V}
80-
tn = ITensorNetwork{V}(NamedGraph{V}(), Dictionary{V, ITensor}(), Dict{Index, Set{V}}())
87+
tn = ITensorNetwork{V}()
8188
for v in keys(tensors)
82-
set_vertex_data!(tn, tensors[v], v)
89+
tn[v] = tensors[v]
8390
end
8491
return tn
8592
end

src/treetensornetworks/abstracttreetensornetwork.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ function DataGraphs.underlying_graph_type(::Type{<:AbstractTTN{V}}) where {V}
1616
return NamedGraph{V}
1717
end
1818

19-
ITensorNetwork(tn::AbstractTTN) = error("Not implemented")
20-
2119
#
2220
# Field access
2321
#

src/treetensornetworks/treetensornetwork.jl

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ struct TreeTensorNetwork{V} <: AbstractTreeTensorNetwork{V}
2626
ortho_region::Indices{V}
2727
end
2828

29+
# Empty TTN with no vertices. The is-a-tree invariant holds trivially.
30+
function TreeTensorNetwork{V}() where {V}
31+
itn = ITensorNetwork{V}()
32+
return TreeTensorNetwork{V}(
33+
itn.graph, itn.vertex_data, itn.ind_to_vertices, Indices{V}()
34+
)
35+
end
36+
TreeTensorNetwork() = TreeTensorNetwork{Any}()
37+
2938
"""
3039
TreeTensorNetwork(tensors; ortho_region=nothing) -> TreeTensorNetwork
3140
@@ -69,16 +78,6 @@ end
6978
const TTN = TreeTensorNetwork
7079

7180
# Field access
72-
"""
73-
ITensorNetwork(tn::TreeTensorNetwork) -> ITensorNetwork
74-
75-
Convert a `TreeTensorNetwork` to a plain `ITensorNetwork`, discarding orthogonality
76-
metadata. The returned network shares the same underlying tensor data.
77-
78-
See also: [`TreeTensorNetwork`](@ref).
79-
"""
80-
ITensorNetwork(tn::TTN) = ITensorNetwork(map(copy, vertex_data(tn)))
81-
8281
"""
8382
ortho_region(tn::TreeTensorNetwork) -> Indices
8483

0 commit comments

Comments
 (0)