Skip to content

Commit bee93ed

Browse files
authored
More Ising tests (#19)
1 parent 09cfdb7 commit bee93ed

File tree

3 files changed

+90
-36
lines changed

3 files changed

+90
-36
lines changed

Project.toml

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

66
[deps]
77
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"

src/TensorNetworkGenerators/ising_network.jl

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@ using DiagonalArrays: DiagonalArray
22
using Graphs: degree, dst, edges, src
33
using LinearAlgebra: Diagonal, eigen
44
using NamedDimsArrays: apply, dename, inds, operator, randname
5+
using NamedGraphs.GraphsExtensions: vertextype
56

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)
7+
function sqrt_ising_bond(β; J = one(β), h = zero(β), deg1::Integer, deg2::Integer)
8+
h1 = h / deg1
9+
h2 = h / deg2
10+
m = [
11+
exp* (J + h1 + h2)) exp* (-J + h1 - h2));
12+
exp* (-J - h1 + h2)) exp* (J - h1 - h2));
13+
]
14+
d, v = eigen(m)
15+
return v * (Diagonal(d)) * inv(v)
1516
end
1617

1718
"""
@@ -23,7 +24,8 @@ partition function tensors on each vertex. Link dimensions are defined using the
2324
edge.
2425
"""
2526
function ising_network(
26-
f, β::Number, g::AbstractGraph; h::Number = zero(eltype(β)), sz_vertices = []
27+
f, β::Number, g::AbstractGraph; J::Number = one(β), h::Number = zero(β),
28+
sz_vertices = vertextype(g)[]
2729
)
2830
elt = typeof(β)
2931
= Dict(e => randname(f(e)) for e in edges(g))
@@ -38,7 +40,7 @@ function ising_network(
3840
v2 = dst(e)
3941
deg1 = degree(tn, v1)
4042
deg2 = degree(tn, v2)
41-
m = sqrt_ising_bond(β; h1 = h / deg1, h2 = h / deg2)
43+
m = sqrt_ising_bond(β; J, h, deg1, deg2)
4244
t = operator(m, ((e),), (f(e),))
4345
tn[v1] = apply(t, tn[v1])
4446
tn[v2] = apply(t, tn[v2])

test/test_tensornetworkgenerators.jl

Lines changed: 76 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,41 @@ using Test: @test, @testset
1111
module TestUtils
1212
using QuadGK: quadgk
1313
# Exact critical inverse temperature for 2D square lattice Ising model.
14-
βc() = 0.5 * log(1 + 2)
14+
βc_2d_ising(elt::Type{<:Number} = Float64) = elt(log(1 + 2) / 2)
1515
# 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))))
16+
function f_2d_ising::Real; J::Real = one(β))
17+
κ = 2sinh(2β * J) / cosh(2β * J)^2
18+
integrand(θ) = log((1 + (abs(1 -* sin(θ))^2))) / 2)
1919
integral, _ = quadgk(integrand, 0, π)
20-
return (-log(2cosh(2β)) - (1 / (2π)) * integral) / β
20+
return (-log(2cosh(2β * J)) - (1 / (2π)) * integral) / β
21+
end
22+
function f_1d_ising::Real; J::Real = one(β), h::Real = zero(β))
23+
λ⁺ = exp* J) * (cosh* h) + (sinh* h)^2 + exp(-4β * J)))
24+
return -(log(λ⁺) / β)
25+
end
26+
function f_1d_ising::Real, N::Integer; periodic::Bool = true, kwargs...)
27+
return if periodic
28+
f_1d_ising_periodic(β, N; kwargs...)
29+
else
30+
f_1d_ising_open(β, N; kwargs...)
31+
end
32+
end
33+
function f_1d_ising_periodic::Real, N::Integer; J::Real = one(β), h::Real = zero(β))
34+
r = (sinh* h)^2 + exp(-4β * J))
35+
λ⁺ = exp* J) * (cosh* h) + r)
36+
λ⁻ = exp* J) * (cosh* h) - r)
37+
Z = λ⁺^N + λ⁻^N
38+
return -(log(Z) /* N))
39+
end
40+
function f_1d_ising_open::Real, N::Integer; J::Real = one(β), h::Real = zero(β))
41+
isone(N) && return 2cosh* h)
42+
T = [
43+
exp* (J + h)) exp(-β * J);
44+
exp(-β * J) exp* (J - h));
45+
]
46+
b = [exp* h / 2), exp(-β * h / 2)]
47+
Z = (b' * (T^(N - 1)) * b)[]
48+
return -(log(Z) /* N))
2149
end
2250
end
2351

@@ -38,25 +66,49 @@ end
3866
end
3967
end
4068
@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))
69+
@testset "1D Ising (periodic = $periodic)" for periodic in (false, true)
70+
dims = (4,)
71+
β = 0.4
72+
g = named_grid(dims; periodic)
73+
ldict = Dict(e => Index(2) for e in edges(g))
74+
l(e) = get(() -> ldict[reverse(e)], ldict, e)
75+
tn = ising_network(l, β, g)
76+
@test nv(tn) == 4
77+
@test ne(tn) == ne(g)
78+
@test issetequal(vertices(tn), vertices(g))
79+
@test issetequal(arranged_edges(tn), arranged_edges(g))
80+
for v in vertices(tn)
81+
is = l.(incident_edges(g, v))
82+
@test issetequal(is, inds(tn[v]))
83+
@test tn[v] δ(Tuple(is))
84+
end
85+
# TODO: Use eager contraction sequence finding.
86+
z = contract_network(tn; alg = "exact")[]
87+
f = -log(z) /* nv(g))
88+
f_analytic = TestUtils.f_1d_ising(β, 4; periodic)
89+
@test f f_analytic
90+
end
91+
@testset "2D Ising" begin
92+
dims = (4, 4)
93+
β = TestUtils.βc_2d_ising()
94+
g = named_grid(dims; periodic = true)
95+
ldict = Dict(e => Index(2) for e in edges(g))
96+
l(e) = get(() -> ldict[reverse(e)], ldict, e)
97+
tn = ising_network(l, β, g)
98+
@test nv(tn) == 16
99+
@test ne(tn) == ne(g)
100+
@test issetequal(vertices(tn), vertices(g))
101+
@test issetequal(arranged_edges(tn), arranged_edges(g))
102+
for v in vertices(tn)
103+
is = l.(incident_edges(g, v))
104+
@test issetequal(is, inds(tn[v]))
105+
@test tn[v] δ(Tuple(is))
106+
end
107+
# TODO: Use eager contraction sequence finding.
108+
z = contract_network(tn; alg = "exact")[]
109+
f = -log(z) /* nv(g))
110+
f_inf = TestUtils.f_2d_ising(β)
111+
@test f f_inf rtol = 1.0e-1
55112
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
61113
end
62114
end

0 commit comments

Comments
 (0)