Skip to content

Commit 09cfdb7

Browse files
authored
Add definitions of delta and Ising networks (#18)
1 parent 255d482 commit 09cfdb7

File tree

9 files changed

+154
-31
lines changed

9 files changed

+154
-31
lines changed

Project.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
name = "ITensorNetworksNext"
22
uuid = "302f2e75-49f0-4526-aef7-d8ba550cb06c"
33
authors = ["ITensor developers <[email protected]> and contributors"]
4-
version = "0.1.12"
4+
version = "0.1.13"
55

66
[deps]
77
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
88
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
99
BackendSelection = "680c2d7c-f67a-4cc9-ae9c-da132b1447a5"
1010
DataGraphs = "b5a273c3-7e6c-41f6-98bd-8d7f1525a36a"
11+
DiagonalArrays = "74fd4be6-21e2-4f6f-823a-4360d37c7a77"
1112
Dictionaries = "85a47980-9c8c-11e8-2b9f-f7ca1fa99fb4"
1213
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
1314
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
@@ -31,6 +32,7 @@ AbstractTrees = "0.4.5"
3132
Adapt = "4.3"
3233
BackendSelection = "0.1.6"
3334
DataGraphs = "0.2.7"
35+
DiagonalArrays = "0.3.23"
3436
Dictionaries = "0.4.5"
3537
Graphs = "1.13.1"
3638
LinearAlgebra = "1.10"

docs/src/reference.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Reference
22

33
```@autodocs
4-
Modules = [ITensorNetworksNext]
4+
Modules = [ITensorNetworksNext, ITensorNetworksNext.TensorNetworkGenerators]
55
```

src/ITensorNetworksNext.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module ITensorNetworksNext
33
include("lazynameddimsarrays.jl")
44
include("abstracttensornetwork.jl")
55
include("tensornetwork.jl")
6+
include("TensorNetworkGenerators/TensorNetworkGenerators.jl")
67
include("contract_network.jl")
78
include("abstract_problem.jl")
89
include("iterators.jl")
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module TensorNetworkGenerators
2+
3+
export delta_network, ising_network
4+
5+
include("delta_network.jl")
6+
include("ising_network.jl")
7+
8+
end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using DiagonalArrays: δ
2+
using Graphs: AbstractGraph
3+
using ..ITensorNetworksNext: TensorNetwork
4+
using NamedGraphs.GraphsExtensions: incident_edges
5+
6+
"""
7+
delta_network(f, elt::Type = Float64, g::AbstractGraph)
8+
9+
Construct a TensorNetwork on the graph `g` with element type `elt` that has delta tensors
10+
on each vertex. Link dimensions are defined using the function `f(e)` that should take an
11+
edge `e` as an input and should output the link index on that edge.
12+
"""
13+
function delta_network(f, elt::Type, g::AbstractGraph)
14+
return tn = TensorNetwork(g) do v
15+
is = Tuple(f.(incident_edges(g, v)))
16+
return δ(elt, is)
17+
end
18+
end
19+
function delta_network(f, g::AbstractGraph)
20+
return delta_network(f, Float64, g)
21+
end
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using DiagonalArrays: DiagonalArray
2+
using Graphs: degree, dst, edges, src
3+
using LinearAlgebra: Diagonal, eigen
4+
using NamedDimsArrays: apply, dename, inds, operator, randname
5+
6+
function sqrt_ising_bond(β; h1 = zero(typeof(β)), h2 = zero(typeof(β)))
7+
f11 = exp* (1 + h1 + h2))
8+
f12 = exp* (-1 + h1 - h2))
9+
f21 = exp* (-1 - h1 + h2))
10+
f22 = exp* (1 - h1 - h2))
11+
= eltype(β)[f11 f12; f21 f22]
12+
d², v = eigen(m²)
13+
d = sqrt.(d²)
14+
return v * Diagonal(d) * inv(v)
15+
end
16+
17+
"""
18+
ising_network(f, β::Number, g::AbstractGraph)
19+
20+
Construct a TensorNetwork on the graph `g` with inverse temperature `β` that has Ising
21+
partition function tensors on each vertex. Link dimensions are defined using the function
22+
`f(e)` that should take an edge `e` as an input and should output the link index on that
23+
edge.
24+
"""
25+
function ising_network(
26+
f, β::Number, g::AbstractGraph; h::Number = zero(eltype(β)), sz_vertices = []
27+
)
28+
elt = typeof(β)
29+
= Dict(e => randname(f(e)) for e in edges(g))
30+
(e) = get(() -> l̃[reverse(e)], l̃, e)
31+
tn = delta_network(f̃, elt, g)
32+
for v in sz_vertices
33+
a = DiagonalArray(elt[1, -1], dename.(inds(tn[v])))
34+
tn[v] = a[inds(tn[v])...]
35+
end
36+
for e in edges(tn)
37+
v1 = src(e)
38+
v2 = dst(e)
39+
deg1 = degree(tn, v1)
40+
deg2 = degree(tn, v2)
41+
m = sqrt_ising_bond(β; h1 = h / deg1, h2 = h / deg2)
42+
t = operator(m, ((e),), (f(e),))
43+
tn[v1] = apply(t, tn[v1])
44+
tn[v2] = apply(t, tn[v2])
45+
end
46+
return tn
47+
end

src/abstracttensornetwork.jl

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,16 @@
11
using Adapt: Adapt, adapt, adapt_structure
22
using BackendSelection: @Algorithm_str, Algorithm
3-
using DataGraphs:
4-
DataGraphs,
5-
AbstractDataGraph,
6-
edge_data,
7-
underlying_graph,
8-
underlying_graph_type,
9-
vertex_data
3+
using DataGraphs: DataGraphs, AbstractDataGraph, edge_data, underlying_graph,
4+
underlying_graph_type, vertex_data
105
using Dictionaries: Dictionary
11-
using Graphs:
12-
Graphs,
13-
AbstractEdge,
14-
AbstractGraph,
15-
Graph,
16-
add_edge!,
17-
add_vertex!,
18-
bfs_tree,
19-
center,
20-
dst,
21-
edges,
22-
edgetype,
23-
ne,
24-
neighbors,
25-
nv,
26-
rem_edge!,
27-
src,
28-
vertices
6+
using Graphs: Graphs, AbstractEdge, AbstractGraph, Graph, add_edge!, add_vertex!,
7+
bfs_tree, center, dst, edges, edgetype, ne, neighbors, nv, rem_edge!, src, vertices
298
using LinearAlgebra: LinearAlgebra, factorize
309
using MacroTools: @capture
3110
using NamedDimsArrays: dimnames, inds
3211
using NamedGraphs: NamedGraphs, NamedGraph, not_implemented, steiner_tree
33-
using NamedGraphs.GraphsExtensions:
34-
, directed_graph, incident_edges, rem_edges!, rename_vertices, vertextype
12+
using NamedGraphs.GraphsExtensions: , directed_graph, incident_edges, rem_edges!,
13+
rename_vertices, vertextype
3514
using SplitApplyCombine: flatten
3615

3716
abstract type AbstractTensorNetwork{V, VD} <: AbstractDataGraph{V, VD, Nothing} end
@@ -202,7 +181,6 @@ end
202181
# Fix the edges of the TensorNetwork `tn` to match
203182
# the tensor connectivity at vertex `v`.
204183
function fix_edges!(tn::AbstractTensorNetwork, v)
205-
rem_incident_edges!(tn, v)
206184
rem_edges!(tn, incident_edges(tn, v))
207185
add_missing_edges!(tn, v)
208186
return tn

test/Project.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
[deps]
22
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
33
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
4+
DiagonalArrays = "74fd4be6-21e2-4f6f-823a-4360d37c7a77"
45
Dictionaries = "85a47980-9c8c-11e8-2b9f-f7ca1fa99fb4"
56
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
67
ITensorBase = "4795dd04-0d67-49bb-8f44-b89c448a1dc7"
78
ITensorNetworksNext = "302f2e75-49f0-4526-aef7-d8ba550cb06c"
89
NamedDimsArrays = "60cbd0c0-df58-4cb7-918c-6f5607b73fde"
910
NamedGraphs = "678767b0-92e7-4007-89e4-4527a8725b19"
11+
QuadGK = "1fd47b50-473d-5c70-9696-f719f8f3bcdc"
1012
SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"
1113
Suppressor = "fd094767-a336-5f1f-9728-57cf17d0bbfb"
1214
TensorOperations = "6aa20fa7-93e2-5fca-9bc0-fbd0db3c71a2"
@@ -17,15 +19,17 @@ WrappedUnions = "325db55a-9c6c-5b90-b1a2-ec87e7a38c44"
1719
[compat]
1820
AbstractTrees = "0.4.5"
1921
Aqua = "0.8.14"
22+
DiagonalArrays = "0.3.23"
2023
Dictionaries = "0.4.5"
2124
Graphs = "1.13.1"
2225
ITensorBase = "0.3"
2326
ITensorNetworksNext = "0.1.1"
2427
NamedDimsArrays = "0.8"
2528
NamedGraphs = "0.6.8, 0.7"
29+
QuadGK = "2.11.2"
2630
SafeTestsets = "0.1"
2731
Suppressor = "0.2.8"
28-
TermInterface = "2"
2932
TensorOperations = "5.3.1"
33+
TermInterface = "2"
3034
Test = "1.10"
3135
WrappedUnions = "0.3"
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using DiagonalArrays: δ
2+
using Graphs: edges, ne, nv, vertices
3+
using ITensorBase: Index
4+
using ITensorNetworksNext: contract_network
5+
using ITensorNetworksNext.TensorNetworkGenerators: delta_network, ising_network
6+
using NamedDimsArrays: inds
7+
using NamedGraphs.GraphsExtensions: arranged_edges, incident_edges
8+
using NamedGraphs.NamedGraphGenerators: named_grid
9+
using Test: @test, @testset
10+
11+
module TestUtils
12+
using QuadGK: quadgk
13+
# Exact critical inverse temperature for 2D square lattice Ising model.
14+
βc() = 0.5 * log(1 + 2)
15+
# Exact infinite volume free energy density for 2D square lattice Ising model.
16+
function ising_free_energy_density::Real)
17+
κ = 2sinh(2β) / cosh(2β)^2
18+
integrand(θ) = log(0.5 * (1 + sqrt(abs(1 -* sin(θ))^2))))
19+
integral, _ = quadgk(integrand, 0, π)
20+
return (-log(2cosh(2β)) - (1 / (2π)) * integral) / β
21+
end
22+
end
23+
24+
@testset "TensorNetworkGenerators" begin
25+
@testset "Delta Network" begin
26+
dims = (3, 3)
27+
g = named_grid(dims)
28+
ldict = Dict(e => Index(2) for e in edges(g))
29+
l(e) = get(() -> ldict[reverse(e)], ldict, e)
30+
tn = delta_network(l, g)
31+
@test nv(tn) == 9
32+
@test ne(tn) == ne(g)
33+
@test issetequal(vertices(tn), vertices(g))
34+
@test issetequal(arranged_edges(tn), arranged_edges(g))
35+
for v in vertices(tn)
36+
is = l.(incident_edges(g, v))
37+
@test tn[v] == δ(Tuple(is))
38+
end
39+
end
40+
@testset "Ising Network" begin
41+
dims = (4, 4)
42+
β = TestUtils.βc()
43+
g = named_grid(dims; periodic = true)
44+
ldict = Dict(e => Index(2) for e in edges(g))
45+
l(e) = get(() -> ldict[reverse(e)], ldict, e)
46+
tn = ising_network(l, β, g)
47+
@test nv(tn) == 16
48+
@test ne(tn) == ne(g)
49+
@test issetequal(vertices(tn), vertices(g))
50+
@test issetequal(arranged_edges(tn), arranged_edges(g))
51+
for v in vertices(tn)
52+
is = l.(incident_edges(g, v))
53+
@test issetequal(is, inds(tn[v]))
54+
@test tn[v] δ(Tuple(is))
55+
end
56+
# TODO: Use eager contraction sequence finding.
57+
z = contract_network(tn; alg = "exact")[]
58+
f = -log(z) /* nv(g))
59+
f_inf = TestUtils.ising_free_energy_density(β)
60+
@test f f_inf rtol = 1.0e-1
61+
end
62+
end

0 commit comments

Comments
 (0)