|
| 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 |
0 commit comments