Skip to content

Commit ae4ad2c

Browse files
authored
Name changes (#154)
1 parent 8efd015 commit ae4ad2c

38 files changed

+524
-601
lines changed

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ITensorNetworks"
22
uuid = "2919e153-833c-4bdc-8836-1ea460a35fc7"
33
authors = ["Matthew Fishman <[email protected]> and contributors"]
4-
version = "0.5.1"
4+
version = "0.6"
55

66
[deps]
77
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
@@ -57,7 +57,7 @@ ITensors = "0.3.58"
5757
IsApprox = "0.1"
5858
IterTools = "1.4.0"
5959
KrylovKit = "0.6.0"
60-
NamedGraphs = "0.1.20"
60+
NamedGraphs = "0.1.23"
6161
Observers = "0.2"
6262
PackageExtensionCompat = "1"
6363
Requires = "1.3"

src/ITensorNetworks.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ include("sitetype.jl")
1414
include("abstractitensornetwork.jl")
1515
include("contraction_sequences.jl")
1616
include("expect.jl")
17-
include("models.jl")
1817
include("tebd.jl")
1918
include("itensornetwork.jl")
2019
include("mincut.jl")
@@ -67,6 +66,8 @@ include("solvers/sweep_plans/sweep_plans.jl")
6766
include("apply.jl")
6867
include("environment.jl")
6968
include("exports.jl")
69+
include("ModelHamiltonians/ModelHamiltonians.jl")
70+
include("ModelNetworks/ModelNetworks.jl")
7071

7172
using PackageExtensionCompat: @require_extensions
7273
using Requires: @require
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
module ModelHamiltonians
2+
using Dictionaries: AbstractDictionary
3+
using Graphs: AbstractGraph, dst, edges, edgetype, neighborhood, path_graph, src, vertices
4+
using ITensors.Ops: OpSum
5+
6+
to_callable(value::Type) = value
7+
to_callable(value::Function) = value
8+
to_callable(value::AbstractDict) = Base.Fix1(getindex, value)
9+
to_callable(value::AbstractDictionary) = Base.Fix1(getindex, value)
10+
function to_callable(value::AbstractArray{<:Any,N}) where {N}
11+
getindex_value(x::Integer) = value[x]
12+
getindex_value(x::Tuple{Vararg{Integer,N}}) = value[x...]
13+
getindex_value(x::CartesianIndex{N}) = value[x]
14+
return getindex_value
15+
end
16+
to_callable(value) = Returns(value)
17+
18+
# TODO: Move to `NamedGraphs.jl` or `GraphsExtensions.jl`.
19+
# TODO: Add a tet for this.
20+
function nth_nearest_neighbors(g, v, n::Int)
21+
isone(n) && return neighborhood(g, v, 1)
22+
return setdiff(neighborhood(g, v, n), neighborhood(g, v, n - 1))
23+
end
24+
25+
# TODO: Move to `NamedGraphs.jl` or `GraphsExtensions.jl`.
26+
# TODO: Add a tet for this.
27+
next_nearest_neighbors(g, v) = nth_nearest_neighbors(g, v, 2)
28+
29+
function tight_binding(g::AbstractGraph; t=1, tp=0, h=0)
30+
(; t, tp, h) = map(to_callable, (; t, tp, h))
31+
h = to_callable(h)
32+
= OpSum()
33+
for e in edges(g)
34+
-= t(e), "Cdag", src(e), "C", dst(e)
35+
-= t(e), "Cdag", dst(e), "C", src(e)
36+
end
37+
for v in vertices(g)
38+
for nn in next_nearest_neighbors(g, v)
39+
e = edgetype(g)(v, nn)
40+
-= tp(e), "Cdag", src(e), "C", dst(e)
41+
-= tp(e), "Cdag", dst(e), "C", src(e)
42+
end
43+
end
44+
for v in vertices(g)
45+
-= h(v), "N", v
46+
end
47+
return
48+
end
49+
50+
"""
51+
t-t' Hubbard Model g,i,v
52+
"""
53+
function hubbard(g::AbstractGraph; U=0, t=1, tp=0, h=0)
54+
(; U, t, tp, h) = map(to_callable, (; U, t, tp, h))
55+
= OpSum()
56+
for e in edges(g)
57+
-= t(e), "Cdagup", src(e), "Cup", dst(e)
58+
-= t(e), "Cdagup", dst(e), "Cup", src(e)
59+
-= t(e), "Cdagdn", src(e), "Cdn", dst(e)
60+
-= t(e), "Cdagdn", dst(e), "Cdn", src(e)
61+
end
62+
for v in vertices(g)
63+
for nn in next_nearest_neighbors(g, v)
64+
e = edgetype(g)(v, nn)
65+
-= tp(e), "Cdagup", src(e), "Cup", dst(e)
66+
-= tp(e), "Cdagup", dst(e), "Cup", src(e)
67+
-= tp(e), "Cdagdn", src(e), "Cdn", dst(e)
68+
-= tp(e), "Cdagdn", dst(e), "Cdn", src(e)
69+
end
70+
end
71+
for v in vertices(g)
72+
-= h(v), "Sz", v
73+
+= U(v), "Nupdn", v
74+
end
75+
return
76+
end
77+
78+
"""
79+
Random field J1-J2 Heisenberg model on a general graph
80+
"""
81+
function heisenberg(g::AbstractGraph; J1=1, J2=0, h=0)
82+
(; J1, J2, h) = map(to_callable, (; J1, J2, h))
83+
= OpSum()
84+
for e in edges(g)
85+
+= J1(e) / 2, "S+", src(e), "S-", dst(e)
86+
+= J1(e) / 2, "S-", src(e), "S+", dst(e)
87+
+= J1(e), "Sz", src(e), "Sz", dst(e)
88+
end
89+
for v in vertices(g)
90+
for nn in next_nearest_neighbors(g, v)
91+
e = edgetype(g)(v, nn)
92+
+= J2(e) / 2, "S+", src(e), "S-", dst(e)
93+
+= J2(e) / 2, "S-", src(e), "S+", dst(e)
94+
+= J2(e), "Sz", src(e), "Sz", dst(e)
95+
end
96+
end
97+
for v in vertices(g)
98+
+= h(v), "Sz", v
99+
end
100+
return
101+
end
102+
103+
"""
104+
Random field J1-J2 Heisenberg model on a chain of length N
105+
"""
106+
heisenberg(N::Integer; kwargs...) = heisenberg(path_graph(N); kwargs...)
107+
108+
"""
109+
Next-to-nearest-neighbor Ising model (ZZX) on a general graph
110+
"""
111+
function ising(g::AbstractGraph; J1=-1, J2=0, h=0)
112+
(; J1, J2, h) = map(to_callable, (; J1, J2, h))
113+
= OpSum()
114+
for e in edges(g)
115+
+= J1(e), "Sz", src(e), "Sz", dst(e)
116+
end
117+
for v in vertices(g)
118+
for nn in next_nearest_neighbors(g, v)
119+
e = edgetype(g)(v, nn)
120+
# TODO: Try removing this if-statement. This
121+
# helps to avoid constructing next-nearest
122+
# neighbor gates, which `apply` can't handle
123+
# right now. We could skip zero terms in gate
124+
# construction.
125+
if !iszero(J2(e))
126+
+= J2(e), "Sz", src(e), "Sz", dst(e)
127+
end
128+
end
129+
end
130+
for v in vertices(g)
131+
+= h(v), "Sx", v
132+
end
133+
return
134+
end
135+
136+
"""
137+
Next-to-nearest-neighbor Ising model (ZZX) on a chain of length N
138+
"""
139+
ising(N::Integer; kwargs...) = ising(path_graph(N); kwargs...)
140+
end

src/ModelNetworks/ModelNetworks.jl

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
module ModelNetworks
2+
using Graphs: degree, dst, edges, src
3+
using ..ITensorNetworks: IndsNetwork, delta_network, insert_missing_internal_inds, itensor
4+
using ITensors: commoninds, diagITensor, inds, noprime
5+
using LinearAlgebra: Diagonal, eigen
6+
using NamedGraphs: NamedGraph
7+
8+
"""
9+
BUILD Z OF CLASSICAL ISING MODEL ON A GIVEN GRAPH AT INVERSE TEMP BETA
10+
H = -\\sum_{(v,v') \\in edges}\\sigma^{z}_{v}\\sigma^{z}_{v'}
11+
OPTIONAL ARGUMENT:
12+
h: EXTERNAL MAGNETIC FIELD
13+
szverts: A LIST OF VERTICES OVER WHICH TO APPLY A SZ.
14+
THE RESULTANT NETWORK CAN THEN BE CONTRACTED AND DIVIDED BY THE ACTUAL PARTITION FUNCTION TO GET THAT OBSERVABLE
15+
INDSNETWORK IS ASSUMED TO BE BUILT FROM A GRAPH (NO SITE INDS) AND OF LINK SPACE 2
16+
"""
17+
function ising_network(
18+
eltype::Type, s::IndsNetwork, beta::Number; h::Number=0.0, szverts=nothing
19+
)
20+
s = insert_missing_internal_inds(s, edges(s); internal_inds_space=2)
21+
tn = delta_network(eltype, s)
22+
if (szverts != nothing)
23+
for v in szverts
24+
tn[v] = diagITensor(eltype[1, -1], inds(tn[v]))
25+
end
26+
end
27+
for edge in edges(tn)
28+
v1 = src(edge)
29+
v2 = dst(edge)
30+
i = commoninds(tn[v1], tn[v2])[1]
31+
deg_v1 = degree(tn, v1)
32+
deg_v2 = degree(tn, v2)
33+
f11 = exp(beta * (1 + h / deg_v1 + h / deg_v2))
34+
f12 = exp(beta * (-1 + h / deg_v1 - h / deg_v2))
35+
f21 = exp(beta * (-1 - h / deg_v1 + h / deg_v2))
36+
f22 = exp(beta * (1 - h / deg_v1 - h / deg_v2))
37+
q = eltype[f11 f12; f21 f22]
38+
w, V = eigen(q)
39+
w = map(sqrt, w)
40+
sqrt_q = V * Diagonal(w) * inv(V)
41+
t = itensor(sqrt_q, i, i')
42+
tn[v1] = tn[v1] * t
43+
tn[v1] = noprime(tn[v1])
44+
t = itensor(sqrt_q, i', i)
45+
tn[v2] = tn[v2] * t
46+
tn[v2] = noprime(tn[v2])
47+
end
48+
return tn
49+
end
50+
51+
function ising_network(s::IndsNetwork, beta::Number; h::Number=0.0, szverts=nothing)
52+
return ising_network(typeof(beta), s, beta; h, szverts)
53+
end
54+
55+
function ising_network(
56+
eltype::Type, g::NamedGraph, beta::Number; h::Number=0.0, szverts=nothing
57+
)
58+
return ising_network(eltype, IndsNetwork(g; link_space=2), beta; h, szverts)
59+
end
60+
61+
function ising_network(g::NamedGraph, beta::Number; h::Number=0.0, szverts=nothing)
62+
return ising_network(eltype(beta), g, beta; h, szverts)
63+
end
64+
65+
"""Build the wavefunction whose norm is equal to Z of the classical ising model
66+
s needs to have site indices in this case!"""
67+
function ising_network_state(eltype::Type, s::IndsNetwork, beta::Number; h::Number=0.0)
68+
return ising_network(eltype, s, 0.5 * beta; h)
69+
end
70+
71+
function ising_network_state(eltype::Type, g::NamedGraph, beta::Number; h::Number=0.0)
72+
return ising_network(eltype, IndsNetwork(g, 2, 2), 0.5 * beta; h)
73+
end
74+
75+
function ising_network_state(s::IndsNetwork, beta::Number; h::Number=0.0)
76+
return ising_network_state(typeof(beta), s, beta; h)
77+
end
78+
79+
function ising_network_state(g::NamedGraph, beta::Number; h::Number=0.0)
80+
return ising_network(typeof(beta), IndsNetwork(g, 2, 2), 0.5 * beta; h)
81+
end
82+
end

src/approx_itensornetwork/approx_itensornetwork.jl

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Approximate a `binary_tree_partition` into an output ITensorNetwork
44
with the same binary tree structure. `root` is the root vertex of the
55
pre-order depth-first-search traversal used to perform the truncations.
66
"""
7-
function approx_itensornetwork(
7+
function approx_tensornetwork(
88
::Algorithm"density_matrix",
99
binary_tree_partition::DataGraph;
1010
root,
@@ -33,7 +33,7 @@ function approx_itensornetwork(
3333
)
3434
end
3535

36-
function approx_itensornetwork(
36+
function approx_tensornetwork(
3737
::Algorithm"ttn_svd",
3838
binary_tree_partition::DataGraph;
3939
root,
@@ -60,7 +60,7 @@ Approximate a given ITensorNetwork `tn` into an output ITensorNetwork
6060
with a binary tree structure. The binary tree structure is defined based
6161
on `inds_btree`, which is a directed binary tree DataGraph of indices.
6262
"""
63-
function approx_itensornetwork(
63+
function approx_tensornetwork(
6464
alg::Union{Algorithm"density_matrix",Algorithm"ttn_svd"},
6565
tn::ITensorNetwork,
6666
inds_btree::DataGraph;
@@ -70,7 +70,7 @@ function approx_itensornetwork(
7070
contraction_sequence_kwargs=(;),
7171
)
7272
par = _partition(tn, inds_btree; alg="mincut_recursive_bisection")
73-
output_tn, log_root_norm = approx_itensornetwork(
73+
output_tn, log_root_norm = approx_tensornetwork(
7474
alg,
7575
par;
7676
root=_root(inds_btree),
@@ -95,7 +95,7 @@ end
9595
Approximate a given ITensorNetwork `tn` into an output ITensorNetwork with `output_structure`.
9696
`output_structure` outputs a directed binary tree DataGraph defining the desired graph structure.
9797
"""
98-
function approx_itensornetwork(
98+
function approx_tensornetwork(
9999
alg::Union{Algorithm"density_matrix",Algorithm"ttn_svd"},
100100
tn::ITensorNetwork,
101101
output_structure::Function=path_graph_structure;
@@ -105,7 +105,7 @@ function approx_itensornetwork(
105105
contraction_sequence_kwargs=(;),
106106
)
107107
inds_btree = output_structure(tn)
108-
return approx_itensornetwork(
108+
return approx_tensornetwork(
109109
alg,
110110
tn,
111111
inds_btree;
@@ -117,7 +117,7 @@ function approx_itensornetwork(
117117
end
118118

119119
# interface
120-
function approx_itensornetwork(
120+
function approx_tensornetwork(
121121
partitioned_tn::DataGraph;
122122
alg::String,
123123
root,
@@ -126,7 +126,7 @@ function approx_itensornetwork(
126126
contraction_sequence_alg="optimal",
127127
contraction_sequence_kwargs=(;),
128128
)
129-
return approx_itensornetwork(
129+
return approx_tensornetwork(
130130
Algorithm(alg),
131131
partitioned_tn;
132132
root,
@@ -137,7 +137,7 @@ function approx_itensornetwork(
137137
)
138138
end
139139

140-
function approx_itensornetwork(
140+
function approx_tensornetwork(
141141
tn::ITensorNetwork,
142142
inds_btree::DataGraph;
143143
alg::String,
@@ -146,7 +146,7 @@ function approx_itensornetwork(
146146
contraction_sequence_alg="optimal",
147147
contraction_sequence_kwargs=(;),
148148
)
149-
return approx_itensornetwork(
149+
return approx_tensornetwork(
150150
Algorithm(alg),
151151
tn,
152152
inds_btree;
@@ -157,7 +157,7 @@ function approx_itensornetwork(
157157
)
158158
end
159159

160-
function approx_itensornetwork(
160+
function approx_tensornetwork(
161161
tn::ITensorNetwork,
162162
output_structure::Function=path_graph_structure;
163163
alg::String,
@@ -166,7 +166,7 @@ function approx_itensornetwork(
166166
contraction_sequence_alg="optimal",
167167
contraction_sequence_kwargs=(;),
168168
)
169-
return approx_itensornetwork(
169+
return approx_tensornetwork(
170170
Algorithm(alg),
171171
tn,
172172
output_structure;

src/approx_itensornetwork/ttn_svd.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ using IterTools: partition
22
"""
33
Approximate a `partition` into an output ITensorNetwork
44
with the binary tree structure defined by `out_tree` by
5-
first transforming the partition into a TTN, then truncating
5+
first transforming the partition into a ttn, then truncating
66
the ttn using a sequence of SVDs.
77
"""
88
function _approx_itensornetwork_ttn_svd!(
@@ -22,7 +22,7 @@ function _approx_itensornetwork_ttn_svd!(
2222
contraction_sequence_kwargs=contraction_sequence_kwargs,
2323
)
2424
end
25-
truncate_ttn = truncate(TTN(tn); cutoff=cutoff, maxdim=maxdim, root_vertex=root)
25+
truncate_ttn = truncate(ttn(tn); cutoff=cutoff, maxdim=maxdim, root_vertex=root)
2626
out_tn = ITensorNetwork(truncate_ttn)
2727
root_tensor = out_tn[root]
2828
root_norm = norm(root_tensor)

0 commit comments

Comments
 (0)