Skip to content

Commit 2e35ed7

Browse files
committed
Add tests
1 parent ac83e66 commit 2e35ed7

2 files changed

Lines changed: 233 additions & 0 deletions

File tree

‎test/toolbox/mpo_terms.jl‎

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
using Test
2+
using Random
3+
using TensorKit
4+
using PEPSKit
5+
using PEPSKit: MPOTerm, TensorProductTerm, gate_to_mpo, add_term!
6+
import TensorKitTensors.SpinOperators as SO
7+
8+
# `LocalOperator` terms given as a matrix product operator, one tensor per site acted on,
9+
# rather than as the dense rank-2N tensor. `gate_to_mpo` turns a dense operator into one.
10+
#
11+
# A tensor product term is the trivial-bond case, and is the narrower type, so both are
12+
# vectors of tensors and dispatch separates them by whether the factors are rank-2.
13+
14+
"""
15+
Largest bond dimension across the cuts of an MPO term, i.e. the Schmidt rank of the operator
16+
it represents. The outgoing bond of a factor is its last domain index, except for a rank-2
17+
factor, which carries no bond at all.
18+
"""
19+
function mpobond(term)
20+
length(term) == 1 && return 1
21+
return maximum(1:(length(term) - 1)) do i
22+
W = term[i]
23+
numind(W) == 2 && return 1
24+
return dim(space(W, numind(W)))
25+
end
26+
end
27+
28+
const Dbond, χenv = 2, 8
29+
30+
X, Z = SO.σˣ(ComplexF64, Trivial), SO.σᶻ(ComplexF64, Trivial)
31+
const lattice11 = fill(ComplexSpace(2), 1, 1)
32+
33+
@testset "MPO factors follow the standard convention" begin
34+
# rank-2 at the ends of the chain, rank-4 in the bulk
35+
W1, W2 = gate_to_mpo(X ⊗ X + Z ⊗ Z)
36+
@test (numout(W1), numin(W1)) == (1, 2)
37+
@test (numout(W2), numin(W2)) == (2, 1)
38+
39+
Ws = gate_to_mpo(X ⊗ X ⊗ X + Z ⊗ Z ⊗ Z)
40+
@test length(Ws) == 3
41+
@test (numout(Ws[1]), numin(Ws[1])) == (1, 2)
42+
@test (numout(Ws[2]), numin(Ws[2])) == (2, 2)
43+
@test (numout(Ws[3]), numin(Ws[3])) == (2, 1)
44+
45+
# the bond is the Schmidt rank across each cut
46+
@test mpobond(gate_to_mpo(X ⊗ X)) == 1 # a pure product
47+
@test mpobond(gate_to_mpo(X ⊗ X + Z ⊗ Z)) == 2
48+
@test mpobond(gate_to_mpo(X ⊗ X ⊗ X + Z ⊗ Z ⊗ Z)) == 2
49+
@test mpobond([X]) == 1 # a lone site has no bond
50+
51+
# Heisenberg XYZ has Schmidt rank 3 at every spin
52+
for spin in (1 // 2, 1, 3 // 2)
53+
term = SO.S_x_S_x(ComplexF64, Trivial; spin) +
54+
SO.S_y_S_y(ComplexF64, Trivial; spin) +
55+
SO.S_z_S_z(ComplexF64, Trivial; spin)
56+
@test mpobond(gate_to_mpo(term)) == 3
57+
end
58+
end
59+
60+
@testset "Dispatch separates products from MPOs" begin
61+
# a vector of rank-2 operators is both, and the narrower type wins where it matters
62+
@test [X, Z] isa TensorProductTerm
63+
@test [X, Z] isa MPOTerm
64+
65+
# anything carrying a bond is only an MPO term
66+
for O in (X ⊗ Z, X ⊗ X + Z ⊗ Z, X ⊗ X ⊗ X + Z ⊗ Z ⊗ Z)
67+
Ws = gate_to_mpo(O)
68+
@test Ws isa MPOTerm
69+
@test !(Ws isa TensorProductTerm)
70+
end
71+
end
72+
73+
@testset "MPO terms reproduce dense terms ($(name))" for (name, inds, O) in (
74+
("NN horizontal", [(1, 1), (1, 2)], -(X ⊗ X) + Z ⊗ Z),
75+
("NN vertical", [(1, 1), (2, 1)], -(X ⊗ X) + Z ⊗ Z),
76+
("NNN diagonal", [(1, 1), (2, 2)], X ⊗ Z),
77+
("3 site line", [(1, 1), (1, 2), (1, 3)], X ⊗ X ⊗ X + Z ⊗ Z ⊗ Z),
78+
)
79+
Random.seed!(2985721)
80+
peps = InfinitePEPS(ComplexSpace(2), ComplexSpace(Dbond))
81+
env = CTMRGEnv(randn, ComplexF64, peps, ComplexSpace(χenv))
82+
83+
H_dense = LocalOperator(lattice11, inds => O)
84+
H_mpo = LocalOperator(lattice11, inds => gate_to_mpo(O))
85+
@test expectation_value(peps, H_mpo, env) ≈ expectation_value(peps, H_dense, env) rtol =
86+
1.0e-9
87+
end
88+
89+
@testset "MPO terms reproduce the Heisenberg XYZ Hamiltonian" begin
90+
Random.seed!(2985721)
91+
H = heisenberg_XYZ(InfiniteSquare())
92+
H_mpo = LocalOperator(
93+
physicalspace(H), (inds => gate_to_mpo(op) for (inds, op) in H.terms)...
94+
)
95+
@test all(t -> t isa MPOTerm, values(H_mpo.terms))
96+
@test all(t -> mpobond(t) == 3, values(H_mpo.terms))
97+
98+
peps = InfinitePEPS(ComplexSpace(2), ComplexSpace(Dbond))
99+
env, = leading_boundary(
100+
CTMRGEnv(peps, ComplexSpace(χenv)), peps; tol = 1.0e-8, verbosity = 0
101+
)
102+
E = expectation_value(peps, H, env)
103+
@test expectation_value(peps, H_mpo, env) ≈ E rtol = 1.0e-9
104+
105+
# scaling an MPO term scales the term, not each of its factors
106+
α = 0.37
107+
@test expectation_value(peps, α * H_mpo, env) ≈ α * E rtol = 1.0e-9
108+
109+
# the real part is taken factor by factor
110+
H_real = real(H_mpo)
111+
@test all(t -> t isa MPOTerm, values(H_real.terms))
112+
@test all(all(W -> norm(imag(W)) < 1.0e-14, t) for t in values(H_real.terms))
113+
end
114+
115+
@testset "MPO term bookkeeping" begin
116+
# the factors are ordered along the chain, so the sites may not be permuted
117+
@test_throws ArgumentError LocalOperator(
118+
lattice11, [(1, 2), (1, 1)] => gate_to_mpo(X ⊗ Z)
119+
)
120+
121+
# a mismatch between sites and factors is caught
122+
@test_throws ArgumentError LocalOperator(
123+
lattice11, [(1, 1), (1, 2), (1, 3)] => gate_to_mpo(X ⊗ Z)
124+
)
125+
126+
# factor ranks must match their position in the chain
127+
@test_throws ArgumentError LocalOperator(lattice11, [(1, 1)] => [X ⊗ X])
128+
@test_throws ArgumentError LocalOperator(lattice11, [(1, 1)] => gate_to_mpo(X ⊗ Z))
129+
130+
# an MPO is not summed in place, since the bond dimension of a sum is not the same
131+
O = LocalOperator(lattice11, [(1, 1), (1, 2)] => gate_to_mpo(X ⊗ Z))
132+
@test_throws ArgumentError add_term!(
133+
O, [CartesianIndex(1, 1), CartesianIndex(1, 2)], gate_to_mpo(Z ⊗ X)
134+
)
135+
136+
# physical spaces are checked
137+
@test_throws SpaceMismatch LocalOperator(
138+
lattice11, [(1, 1), (1, 2)] => gate_to_mpo(
139+
SO.S_x_S_x(ComplexF64, Trivial; spin = 1)
140+
)
141+
)
142+
end
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
using Test
2+
using Random
3+
using TensorKit
4+
using PEPSKit
5+
using PEPSKit: TensorProductTerm, add_term!, _scale_local_term
6+
import TensorKitTensors.SpinOperators as SO
7+
8+
# `LocalOperator` terms given as an explicit tensor product of single-site operators, one
9+
# per site acted on, rather than as the tensor product formed up front.
10+
#
11+
# The transverse-field Ising model is the natural test case: with trivial symmetry every set
12+
# of sites carries exactly one term, so the whole Hamiltonian is a sum of tensor products,
13+
# a two-factor product per nearest neighbor bond and a one-factor product per site. This
14+
# requires trivial symmetry, since a single-site σᶻ is not symmetric under the Z₂ symmetry
15+
# of the model.
16+
17+
"""
18+
Transverse-field Ising Hamiltonian built from tensor product terms, matching the conventions
19+
of [`transverse_field_ising`](@ref).
20+
"""
21+
function transverse_field_ising_tensorproduct(
22+
T::Type{<:Number}, lattice::InfiniteSquare; J = 1.0, g = 1.0
23+
)
24+
Z, X = SO.σᶻ(T, Trivial), SO.σˣ(T, Trivial)
25+
spaces = fill(domain(X)[1], (lattice.Nrows, lattice.Ncols))
26+
return LocalOperator(
27+
spaces,
28+
(neighbor => [-J * Z, copy(Z)] for neighbor in nearest_neighbours(lattice))...,
29+
([idx] => [(-J * g) * X] for idx in vertices(lattice))...,
30+
)
31+
end
32+
transverse_field_ising_tensorproduct(lattice::InfiniteSquare; kwargs...) =
33+
transverse_field_ising_tensorproduct(ComplexF64, lattice; kwargs...)
34+
35+
const Dbond, χenv, g = 2, 8, 3.1
36+
37+
@testset "Tensor product terms reproduce the two-site Ising Hamiltonian ($uc)" for uc in
38+
((1, 1), (2, 2))
39+
Random.seed!(2985721)
40+
H = transverse_field_ising(InfiniteSquare(uc...); g)
41+
H_prod = transverse_field_ising_tensorproduct(InfiniteSquare(uc...); g)
42+
43+
# one term per site set: a two-factor product per bond, a one-factor product per site
44+
@test length(H_prod.terms) == length(H.terms)
45+
@test all(t -> t isa TensorProductTerm, values(H_prod.terms))
46+
@test sort(length.(collect(values(H_prod.terms)))) ==
47+
sort(vcat(fill(1, prod(uc)), fill(2, 2 * prod(uc))))
48+
49+
peps = InfinitePEPS(ComplexSpace(2), ComplexSpace(Dbond); unitcell = uc)
50+
env, = leading_boundary(CTMRGEnv(peps, ComplexSpace(χenv)), peps; tol = 1.0e-8, verbosity = 0)
51+
52+
@test expectation_value(peps, H_prod, env) ≈ expectation_value(peps, H, env) rtol = 1.0e-9
53+
54+
# scaling a product term scales the term, not each of its factors
55+
α = 0.37
56+
@test expectation_value(peps, α * H_prod, env) ≈ α * expectation_value(peps, H_prod, env) rtol =
57+
1.0e-9
58+
end
59+
60+
@testset "Tensor product term bookkeeping" begin
61+
lattice = fill(ComplexSpace(2), 1, 1)
62+
X, Z = SO.σˣ(ComplexF64, Trivial), SO.σᶻ(ComplexF64, Trivial)
63+
64+
# a sum of tensor products is not a tensor product, so terms may not be accumulated
65+
O = LocalOperator(lattice, [(1, 1), (1, 2)] => [X, X])
66+
@test_throws ArgumentError add_term!(O, [(1, 1), (1, 2)], [Z, Z])
67+
68+
# factors are reordered along with their sites
69+
O1 = LocalOperator(lattice, [(1, 1), (1, 2)] => [X, Z])
70+
O2 = LocalOperator(lattice, [(1, 2), (1, 1)] => [Z, X])
71+
@test only(keys(O1.terms)) == only(keys(O2.terms))
72+
@test all(map(≈, only(values(O1.terms)), only(values(O2.terms))))
73+
74+
# each factor must be a single-site operator, and match the physical space
75+
@test_throws ArgumentError LocalOperator(lattice, [(1, 1), (1, 2)] => [X]) # arity
76+
@test_throws ArgumentError LocalOperator(lattice, [(1, 1)] => [X ⊗ X]) # not single-site
77+
@test_throws SpaceMismatch LocalOperator(
78+
lattice, [(1, 1)] => [SO.S_x(ComplexF64, Trivial; spin = 1)]
79+
)
80+
81+
# scaling touches exactly one factor
82+
scaled = _scale_local_term([X, Z], 3.0)
83+
@test scaled[1] ≈ 3.0 * X
84+
@test scaled[2] ≈ Z
85+
86+
# the real part of a product is the product of the real parts of its factors
87+
O3 = LocalOperator(lattice, [(1, 1), (1, 2)] => [(2.0 + 3.0im) * Z, copy(Z)])
88+
re = only(values(real(O3).terms))
89+
@test re[1] ≈ real((2.0 + 3.0im) * Z)
90+
@test re[2] ≈ real(Z)
91+
end

0 commit comments

Comments
 (0)