Skip to content

Commit

Permalink
Fix more namespace issues
Browse files Browse the repository at this point in the history
  • Loading branch information
mtfishman committed Mar 27, 2024
1 parent 0c4e4b7 commit 77155bc
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 28 deletions.
40 changes: 22 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@
> In short, use this package with caution, and don't expect the interface to be stable
> or for us to clearly announce parts of the code we are changing.


# ITensorNetworks

A package to provide general network data structures and tools to use with ITensors.jl.



## Installation

You can install this package through the Julia package manager:
Expand Down Expand Up @@ -99,13 +103,13 @@ and 4 edge(s):

with vertex data:
4-element Dictionary{Tuple{Int64, Int64}, Any}
(1, 1) │ ((dim=2|id=74|"1×1,2×1"), (dim=2|id=723|"1×1,1×2"))
(2, 1) │ ((dim=2|id=74|"1×1,2×1"), (dim=2|id=823|"2×1,2×2"))
(1, 2) │ ((dim=2|id=723|"1×1,1×2"), (dim=2|id=712|"1×2,2×2"))
(2, 2) │ ((dim=2|id=823|"2×1,2×2"), (dim=2|id=712|"1×2,2×2"))
(1, 1) │ ((dim=2|id=712|"1×1,2×1"), (dim=2|id=598|"1×1,1×2"))
(2, 1) │ ((dim=2|id=712|"1×1,2×1"), (dim=2|id=457|"2×1,2×2"))
(1, 2) │ ((dim=2|id=598|"1×1,1×2"), (dim=2|id=683|"1×2,2×2"))
(2, 2) │ ((dim=2|id=457|"2×1,2×2"), (dim=2|id=683|"1×2,2×2"))

julia> tn[1, 1]
ITensor ord=2 (dim=2|id=74|"1×1,2×1") (dim=2|id=723|"1×1,1×2")
ITensor ord=2 (dim=2|id=712|"1×1,2×1") (dim=2|id=598|"1×1,1×2")
NDTensors.EmptyStorage{NDTensors.EmptyNumber, NDTensors.Dense{NDTensors.EmptyNumber, Vector{NDTensors.EmptyNumber}}}

julia> neighbors(tn, (1, 1))
Expand All @@ -129,8 +133,8 @@ and 1 edge(s):

with vertex data:
2-element Dictionary{Tuple{Int64, Int64}, Any}
(1, 1) │ ((dim=2|id=74|"1×1,2×1"), (dim=2|id=723|"1×1,1×2"))
(1, 2) │ ((dim=2|id=723|"1×1,1×2"), (dim=2|id=712|"1×2,2×2"))
(1, 1) │ ((dim=2|id=712|"1×1,2×1"), (dim=2|id=598|"1×1,1×2"))
(1, 2) │ ((dim=2|id=598|"1×1,1×2"), (dim=2|id=683|"1×2,2×2"))

julia> tn_2 = subgraph(v -> v[1] == 2, tn)
ITensorNetwork{Tuple{Int64, Int64}} with 2 vertices:
Expand All @@ -143,8 +147,8 @@ and 1 edge(s):

with vertex data:
2-element Dictionary{Tuple{Int64, Int64}, Any}
(2, 1) │ ((dim=2|id=74|"1×1,2×1"), (dim=2|id=823|"2×1,2×2"))
(2, 2) │ ((dim=2|id=823|"2×1,2×2"), (dim=2|id=712|"1×2,2×2"))
(2, 1) │ ((dim=2|id=712|"1×1,2×1"), (dim=2|id=457|"2×1,2×2"))
(2, 2) │ ((dim=2|id=457|"2×1,2×2"), (dim=2|id=683|"1×2,2×2"))
```


Expand All @@ -166,9 +170,9 @@ and 2 edge(s):

with vertex data:
3-element Dictionary{Int64, Vector{Index}}
1 │ Index[(dim=2|id=598|"S=1/2,Site,n=1")]
2 │ Index[(dim=2|id=457|"S=1/2,Site,n=2")]
3 │ Index[(dim=2|id=683|"S=1/2,Site,n=3")]
1 │ Index[(dim=2|id=830|"S=1/2,Site,n=1")]
2 │ Index[(dim=2|id=369|"S=1/2,Site,n=2")]
3 │ Index[(dim=2|id=558|"S=1/2,Site,n=3")]

and edge data:
0-element Dictionary{NamedEdge{Int64}, Vector{Index}}
Expand All @@ -186,9 +190,9 @@ and 2 edge(s):

with vertex data:
3-element Dictionary{Int64, Any}
1 │ ((dim=2|id=598|"S=1/2,Site,n=1"), (dim=2|id=123|"1,2"))
2 │ ((dim=2|id=457|"S=1/2,Site,n=2"), (dim=2|id=123|"1,2"), (dim=2|id=656|"2,3…
3 │ ((dim=2|id=683|"S=1/2,Site,n=3"), (dim=2|id=656|"2,3"))
1 │ ((dim=2|id=830|"S=1/2,Site,n=1"), (dim=2|id=186|"1,2"))
2 │ ((dim=2|id=369|"S=1/2,Site,n=2"), (dim=2|id=186|"1,2"), (dim=2|id=430|"2,3…
3 │ ((dim=2|id=558|"S=1/2,Site,n=3"), (dim=2|id=430|"2,3"))
julia> tn2 = ITensorNetwork(s; link_space=2)
ITensorNetwork{Int64} with 3 vertices:
Expand All @@ -203,9 +207,9 @@ and 2 edge(s):
with vertex data:
3-element Dictionary{Int64, Any}
1 │ ((dim=2|id=598|"S=1/2,Site,n=1"), (dim=2|id=382|"1,2"))
2 │ ((dim=2|id=457|"S=1/2,Site,n=2"), (dim=2|id=382|"1,2"), (dim=2|id=190|"2,3
3 │ ((dim=2|id=683|"S=1/2,Site,n=3"), (dim=2|id=190|"2,3"))
1 │ ((dim=2|id=830|"S=1/2,Site,n=1"), (dim=2|id=994|"1,2"))
2 │ ((dim=2|id=369|"S=1/2,Site,n=2"), (dim=2|id=994|"1,2"), (dim=2|id=978|"2,3
3 │ ((dim=2|id=558|"S=1/2,Site,n=3"), (dim=2|id=978|"2,3"))

julia> @visualize tn1;
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
Expand Down
12 changes: 12 additions & 0 deletions examples/README.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
#' > [!WARNING]
#' > This is a pre-release software. There are no guarantees that functionality won't break
#' > from version to version, though we will try our best to indicate breaking changes
#' > following [semantic versioning](https://semver.org/) (semver) by bumping the minor
#' > version of the package. We are biasing heavily towards "moving fast and breaking things"
#' > during this stage of development, which will allow us to more quickly develop the package
#' > and bring it to a point where we have enough features and are happy enough with the external
#' > interface to officially release it for general public use.
#' >
#' > In short, use this package with caution, and don't expect the interface to be stable
#' > or for us to clearly announce parts of the code we are changing.

#' # ITensorNetworks
#'
#' A package to provide general network data structures and tools to use with ITensors.jl.
Expand Down
22 changes: 12 additions & 10 deletions src/treetensornetworks/opsum_to_ttn.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using ITensors.ITensorMPS: ITensorMPS

# convert ITensors.OpSum to TreeTensorNetwork

#
Expand Down Expand Up @@ -41,7 +43,7 @@ Hamiltonian, compressing shared interaction channels.
"""
function ttn_svd(os::OpSum, sites::IndsNetwork, root_vertex; kwargs...)
# Function barrier to improve type stability
coefficient_type = ITensors.determineValType(terms(os))
coefficient_type = ITensorMPS.determineValType(terms(os))
return ttn_svd(coefficient_type, os, sites, root_vertex; kwargs...)
end

Expand Down Expand Up @@ -100,7 +102,7 @@ function ttn_svd(
end
end
inbond_coefs = Dict(
e => Dict{QN,Vector{ITensors.MatElem{coefficient_type}}}() for e in es
e => Dict{QN,Vector{ITensorMPS.MatElem{coefficient_type}}}() for e in es
) # bond coefficients for incoming edge channels
site_coef_done = Prod{Op}[] # list of terms for which the coefficient has been added to a site factor
# temporary symbolic representation of TTN Hamiltonian
Expand Down Expand Up @@ -158,21 +160,21 @@ function ttn_svd(
coutmap = get!(outmaps, edge_in => not_incoming_qn, Dict{Vector{Op},Int}())
cinmap = get!(inmaps, edge_in => -incoming_qn, Dict{Vector{Op},Int}())

bond_row = ITensors.posInLink!(cinmap, incoming)
bond_col = ITensors.posInLink!(coutmap, not_incoming) # get incoming channel
bond_row = ITensorMPS.posInLink!(cinmap, incoming)
bond_col = ITensorMPS.posInLink!(coutmap, not_incoming) # get incoming channel
bond_coef = convert(coefficient_type, ITensors.coefficient(term))
q_inbond_coefs = get!(
inbond_coefs[edge_in], incoming_qn, ITensors.MatElem{coefficient_type}[]
inbond_coefs[edge_in], incoming_qn, ITensorMPS.MatElem{coefficient_type}[]
)
push!(q_inbond_coefs, ITensors.MatElem(bond_row, bond_col, bond_coef))
push!(q_inbond_coefs, ITensorMPS.MatElem(bond_row, bond_col, bond_coef))
T_inds[dim_in] = bond_col
T_qns[dim_in] = -incoming_qn
end
for dout in dims_out
coutmap = get!(
outmaps, edges[dout] => outgoing_qns[edges[dout]], Dict{Vector{Op},Int}()
)
T_inds[dout] = ITensors.posInLink!(coutmap, outgoing[edges[dout]]) # add outgoing channel
T_inds[dout] = ITensorMPS.posInLink!(coutmap, outgoing[edges[dout]]) # add outgoing channel
T_qns[dout] = outgoing_qns[edges[dout]]
end
# if term starts at this site, add its coefficient as a site factor
Expand All @@ -197,11 +199,11 @@ function ttn_svd(
push!(tempTTN[v], el)
end

ITensors.remove_dups!(tempTTN[v])
ITensorMPS.remove_dups!(tempTTN[v])
# manual truncation: isometry on incoming edge
if !isnothing(dim_in) && !isempty(inbond_coefs[edges[dim_in]])
for (q, mat) in inbond_coefs[edges[dim_in]]
M = ITensors.toMatrix(mat)
M = ITensorMPS.toMatrix(mat)
U, S, V = svd(M)
P = S .^ 2
truncate!(P; maxdim, cutoff, mindim)
Expand Down Expand Up @@ -489,7 +491,7 @@ function TTN(

os = deepcopy(os)
os = sorteachterm(os, sites, root_vertex)
os = ITensors.sortmergeterms(os) # not exported
os = ITensorMPS.sortmergeterms(os) # not exported
if algorithm == "svd"
T = ttn_svd(os, sites, root_vertex; kwargs...)
else
Expand Down

0 comments on commit 77155bc

Please sign in to comment.