|
| 1 | +using Test, TestExtras |
| 2 | +using Adapt |
| 3 | +using TensorOperations, JLArrays |
| 4 | +using Random |
| 5 | +using TensorKit |
| 6 | +using TensorKit: type_repr |
| 7 | +using TensorKit: PlanarTrivial, ℙ |
| 8 | +using TensorKit: planaradd!, planartrace!, planarcontract! |
| 9 | + |
| 10 | +Random.seed!(123456) |
| 11 | + |
| 12 | +spacelist = default_spacelist(fast_tests) |
| 13 | + |
| 14 | +for V in spacelist |
| 15 | + I = sectortype(first(V)) |
| 16 | + Istr = type_repr(I) |
| 17 | + BraidingStyle(I) isa NoBraiding && continue |
| 18 | + @timedtestset "Braiding tensor + CUDA with symmetry: $Istr" verbose = true begin |
| 19 | + W = V[1] ⊗ V[2] ← V[2] ⊗ V[1] |
| 20 | + T = isreal(sectortype(W)) ? Float64 : ComplexF64 |
| 21 | + t1 = @constinferred BraidingTensor{T, spacetype(V[2]), JLVector{T}}(W) |
| 22 | + @test space(t1) == W |
| 23 | + @test codomain(t1) == codomain(W) |
| 24 | + @test domain(t1) == domain(W) |
| 25 | + @test scalartype(t1) == (isreal(sectortype(W)) ? Float64 : ComplexF64) |
| 26 | + @test storagetype(t1) == JLVector{scalartype(t1)} |
| 27 | + t2 = @constinferred BraidingTensor{ComplexF64, spacetype(V[2]), JLVector{ComplexF64}}(W) |
| 28 | + @test scalartype(t2) == ComplexF64 |
| 29 | + @test storagetype(t2) == JLVector{ComplexF64} |
| 30 | + t3 = @testinferred adapt(storagetype(t2), t1) |
| 31 | + @test storagetype(t3) == storagetype(t2) |
| 32 | + t4 = @testinferred adapt(scalartype(t2), t1) |
| 33 | + @test storagetype(t3) == storagetype(t2) |
| 34 | + @test t3 ≈ t2 |
| 35 | + |
| 36 | + W2 = reverse(codomain(W)) ← domain(W) |
| 37 | + @test_throws SpaceMismatch BraidingTensor(W2) |
| 38 | + |
| 39 | + @test adjoint(t1) isa BraidingTensor |
| 40 | + @test complex(t1) isa BraidingTensor |
| 41 | + @test scalartype(complex(t1)) <: Complex |
| 42 | + |
| 43 | + t3 = @inferred TensorMap(t2) |
| 44 | + @test storagetype(t3) == JLVector{ComplexF64} |
| 45 | + t4 = braid(adapt(JLArray, id(scalartype(t2), domain(t2))), ((2, 1), (3, 4)), (1, 2, 3, 4)) |
| 46 | + @test t1 ≈ t4 |
| 47 | + for (c, b) in blocks(t1) |
| 48 | + @test block(t1, c) ≈ b ≈ block(t3, c) |
| 49 | + end |
| 50 | + |
| 51 | + JLArrays.@allowscalar begin |
| 52 | + for (f1, f2) in fusiontrees(t1) |
| 53 | + @test t1[f1, f2] ≈ t3[f1, f2] |
| 54 | + end |
| 55 | + end |
| 56 | + |
| 57 | + t5 = @inferred TensorMap(t2') |
| 58 | + @test storagetype(t5) == JLVector{ComplexF64} |
| 59 | + t6 = braid(adapt(JLArray, id(scalartype(t2), domain(t2'))), ((2, 1), (3, 4)), (4, 3, 2, 1)) |
| 60 | + JLArrays.@allowscalar begin |
| 61 | + @test t5 ≈ t6 |
| 62 | + for (c, b) in blocks(t1') |
| 63 | + @test block(t1', c) ≈ b ≈ block(t5, c) |
| 64 | + end |
| 65 | + for (f1, f2) in fusiontrees(t1') |
| 66 | + # needed here for broadcasting the - in isapprox |
| 67 | + @test t1'[f1, f2] ≈ t5[f1, f2] |
| 68 | + end |
| 69 | + end |
| 70 | + end |
| 71 | +end |
| 72 | + |
| 73 | +for V in spacelist |
| 74 | + I = sectortype(first(V)) |
| 75 | + Istr = type_repr(I) |
| 76 | + @timedtestset "planartrace + JLArrays with symmetry: $Istr" begin |
| 77 | + # (space, p, q) for a trace of neighbouring legs and a nested trace |
| 78 | + cases = ( |
| 79 | + (V[1] ⊗ V[2] ⊗ V[3] ← V[4] ⊗ V[3], ((1, 2), (4,)), ((3,), (5,))), |
| 80 | + (V[1] ⊗ V[2] ⊗ V[3] ⊗ V[4] ← V[3] ⊗ V[4], ((1,), (2,)), ((3, 4), (5, 6))), |
| 81 | + ) |
| 82 | + for (W, p, q) in cases, (α, β) in ((0.7, 0.3), (1.0, 0.0)) |
| 83 | + A = randn(ComplexF64, W) |
| 84 | + C = randn(ComplexF64, TensorKit.select(W, p)) |
| 85 | + Cgpu = planartrace!(adapt(JLArray, C), adapt(JLArray, A), p, q, α, β) |
| 86 | + @test adapt(Vector{ComplexF64}, Cgpu) ≈ planartrace!(C, A, p, q, α, β) |
| 87 | + end |
| 88 | + end |
| 89 | +end |
| 90 | + |
| 91 | +@testset "planar methods" verbose = true begin |
| 92 | + @testset "planaradd" begin |
| 93 | + A = adapt(JLArray, randn(ℂ^2 ⊗ ℂ^3 ← ℂ^6 ⊗ ℂ^5 ⊗ ℂ^4)) |
| 94 | + C = adapt(JLArray, randn((ℂ^5)' ⊗ (ℂ^6)' ← ℂ^4 ⊗ (ℂ^3)' ⊗ (ℂ^2)')) |
| 95 | + A′ = force_planar(A) |
| 96 | + C′ = force_planar(C) |
| 97 | + p = ((4, 3), (5, 2, 1)) |
| 98 | + |
| 99 | + @test force_planar(tensoradd!(C, A, p, false, true, true)) ≈ |
| 100 | + planaradd!(C′, A′, p, true, true) |
| 101 | + end |
| 102 | + |
| 103 | + @testset "planartrace" begin |
| 104 | + A = adapt(JLArray, randn(ℂ^2 ⊗ ℂ^3 ← ℂ^2 ⊗ ℂ^5 ⊗ ℂ^4)) |
| 105 | + C = adapt(JLArray, randn((ℂ^5)' ⊗ ℂ^3 ← ℂ^4)) |
| 106 | + A′ = force_planar(A) |
| 107 | + C′ = force_planar(C) |
| 108 | + p = ((4, 2), (5,)) |
| 109 | + q = ((1,), (3,)) |
| 110 | + |
| 111 | + @test force_planar(tensortrace!(C, A, p, q, false, true, true)) ≈ |
| 112 | + planartrace!(C′, A′, p, q, true, true) |
| 113 | + end |
| 114 | + |
| 115 | + @testset "planartrace with large traces: $I" for (I, V, Vr) in ( |
| 116 | + (FermionParity, Vect[FermionParity](0 => 40, 1 => 40), Vect[FermionParity](0 => 2, 1 => 3)), |
| 117 | + (FibonacciAnyon, Vect[FibonacciAnyon](:I => 30, :τ => 30), Vect[FibonacciAnyon](:I => 2, :τ => 1)), |
| 118 | + ) |
| 119 | + # (space, p, q) for a full reduction and for a trace with few remaining entries, which are |
| 120 | + # both split into several chunks of traced entries |
| 121 | + cases = ( |
| 122 | + (V ⊗ V ← V ⊗ V, ((), ()), ((1, 2), (3, 4))), |
| 123 | + (Vr ⊗ V ⊗ V ← V ⊗ V, ((1,), ()), ((2, 3), (4, 5))), |
| 124 | + ) |
| 125 | + for (W, p, q) in cases, (α, β) in ((0.7, 0.3), (1.0, 0.0)) |
| 126 | + A = randn(ComplexF64, W) |
| 127 | + C = randn(ComplexF64, TensorKit.select(W, p)) |
| 128 | + Cgpu = planartrace!(adapt(JLArray, C), adapt(JLArray, A), p, q, α, β) |
| 129 | + @test adapt(Vector{ComplexF64}, Cgpu) ≈ planartrace!(C, A, p, q, α, β) |
| 130 | + end |
| 131 | + end |
| 132 | + |
| 133 | + @testset "planarcontract" begin |
| 134 | + A = adapt(JLArray, randn(ℂ^2 ⊗ ℂ^3 ← ℂ^2 ⊗ ℂ^5 ⊗ ℂ^4)) |
| 135 | + B = adapt(JLArray, randn(ℂ^2 ⊗ ℂ^4 ← ℂ^4 ⊗ ℂ^3)) |
| 136 | + C = adapt(JLArray, randn((ℂ^5)' ⊗ (ℂ^2)' ⊗ ℂ^2 ← (ℂ^2)' ⊗ ℂ^4)) |
| 137 | + |
| 138 | + A′ = force_planar(A) |
| 139 | + B′ = force_planar(B) |
| 140 | + C′ = force_planar(C) |
| 141 | + |
| 142 | + pA = ((1, 3, 4), (5, 2)) |
| 143 | + pB = ((2, 4), (1, 3)) |
| 144 | + pAB = ((3, 2, 1), (4, 5)) |
| 145 | + |
| 146 | + @test force_planar(tensorcontract!(C, A, pA, false, B, pB, false, pAB, true, true)) ≈ |
| 147 | + planarcontract!(C′, A′, pA, B′, pB, pAB, true, true) |
| 148 | + end |
| 149 | +end |
| 150 | + |
| 151 | +@testset "@planar" verbose = true begin |
| 152 | + T = ComplexF64 |
| 153 | + |
| 154 | + @testset "contractcheck" begin |
| 155 | + V = ℂ^2 |
| 156 | + A = adapt(JLArray, rand(T, V ⊗ V ← V)) |
| 157 | + B = adapt(JLArray, rand(T, V ⊗ V ← V')) |
| 158 | + @tensor C1[i j; k l] := A[i j; m] * B[k l; m] |
| 159 | + @tensor contractcheck = true C2[i j; k l] := A[i j; m] * B[k l; m] |
| 160 | + @test C1 ≈ C2 |
| 161 | + B2 = adapt(JLArray, rand(T, V ⊗ V ← V)) # wrong duality for third space |
| 162 | + @test_throws SpaceMismatch("incompatible spaces for m: $V ≠ $(V')") begin |
| 163 | + @tensor contractcheck = true C3[i j; k l] := A[i j; m] * B2[k l; m] |
| 164 | + end |
| 165 | + |
| 166 | + A = adapt(JLArray, rand(T, V ← V ⊗ V)) |
| 167 | + B = adapt(JLArray, rand(T, V ⊗ V ← V)) |
| 168 | + @planar C1[i; j] := A[i; k l] * τ[k l; m n] * B[m n; j] |
| 169 | + @planar contractcheck = true C2[i; j] := A[i; k l] * τ[k l; m n] * B[m n; j] |
| 170 | + @test C1 ≈ C2 |
| 171 | + @test_throws SpaceMismatch("incompatible spaces for m: $V ≠ $(V')") begin |
| 172 | + @planar contractcheck = true C3[i; j] := A[i; k l] * τ[k l; m n] * B[n j; m] |
| 173 | + end |
| 174 | + end |
| 175 | + |
| 176 | + @testset "MPS networks" begin |
| 177 | + P = ℂ^2 |
| 178 | + Vmps = ℂ^12 |
| 179 | + Vmpo = ℂ^4 |
| 180 | + |
| 181 | + # ∂AC |
| 182 | + # ------- |
| 183 | + x = adapt(JLArray, randn(T, Vmps ⊗ P ← Vmps)) |
| 184 | + O = adapt(JLArray, randn(T, Vmpo ⊗ P ← P ⊗ Vmpo)) |
| 185 | + GL = adapt(JLArray, randn(T, Vmps ⊗ Vmpo' ← Vmps)) |
| 186 | + GR = adapt(JLArray, randn(T, Vmps ⊗ Vmpo ← Vmps)) |
| 187 | + |
| 188 | + x′ = force_planar(x) |
| 189 | + O′ = force_planar(O) |
| 190 | + GL′ = force_planar(GL) |
| 191 | + GR′ = force_planar(GR) |
| 192 | + |
| 193 | + for alloc in |
| 194 | + (TensorOperations.DefaultAllocator(),) |
| 195 | + @tensor allocator = alloc y[-1 -2; -3] := GL[-1 2; 1] * x[1 3; 4] * |
| 196 | + O[2 -2; 3 5] * GR[4 5; -3] |
| 197 | + @planar allocator = alloc y′[-1 -2; -3] := GL′[-1 2; 1] * x′[1 3; 4] * |
| 198 | + O′[2 -2; 3 5] * GR′[4 5; -3] |
| 199 | + @test force_planar(y) ≈ y′ |
| 200 | + end |
| 201 | + |
| 202 | + # ∂AC2 |
| 203 | + # ------- |
| 204 | + x2 = adapt(JLArray, randn(T, Vmps ⊗ P ← Vmps ⊗ P')) |
| 205 | + x2′ = force_planar(x2) |
| 206 | + @tensor contractcheck = true y2[-1 -2; -3 -4] := GL[-1 7; 6] * x2[6 5; 1 3] * |
| 207 | + O[7 -2; 5 4] * O[4 -4; 3 2] * |
| 208 | + GR[1 2; -3] |
| 209 | + @planar y2′[-1 -2; -3 -4] := GL′[-1 7; 6] * x2′[6 5; 1 3] * O′[7 -2; 5 4] * |
| 210 | + O′[4 -4; 3 2] * GR′[1 2; -3] |
| 211 | + @test force_planar(y2) ≈ y2′ |
| 212 | + |
| 213 | + # transfer matrix |
| 214 | + # ---------------- |
| 215 | + v = adapt(JLArray, randn(T, Vmps ← Vmps)) |
| 216 | + v′ = force_planar(v) |
| 217 | + @tensor ρ[-1; -2] := x[-1 2; 1] * conj(x[-2 2; 3]) * v[1; 3] |
| 218 | + @planar ρ′[-1; -2] := x′[-1 2; 1] * conj(x′[-2 2; 3]) * v′[1; 3] |
| 219 | + @test force_planar(ρ) ≈ ρ′ |
| 220 | + |
| 221 | + @tensor ρ2[-1 -2; -3] := GL[1 -2; 3] * x[3 2; -3] * conj(x[1 2; -1]) |
| 222 | + @plansor ρ3[-1 -2; -3] := GL[1 2; 4] * x[4 5; -3] * τ[2 3; 5 -2] * conj(x[1 3; -1]) |
| 223 | + @planar ρ2′[-1 -2; -3] := GL′[1 2; 4] * x′[4 5; -3] * τ[2 3; 5 -2] * |
| 224 | + conj(x′[1 3; -1]) |
| 225 | + @test force_planar(ρ2) ≈ ρ2′ |
| 226 | + @test ρ2 ≈ ρ3 |
| 227 | + |
| 228 | + # Periodic boundary conditions |
| 229 | + # ---------------------------- |
| 230 | + f1 = isomorphism(storagetype(O), fuse(Vmpo^3), Vmpo ⊗ Vmpo' ⊗ Vmpo) |
| 231 | + f2 = isomorphism(storagetype(O), fuse(Vmpo^3), Vmpo ⊗ Vmpo' ⊗ Vmpo) |
| 232 | + f1′ = force_planar(f1) |
| 233 | + f2′ = force_planar(f2) |
| 234 | + @tensor O_periodic1[-1 -2; -3 -4] := O[1 -2; -3 2] * f1[-1; 1 3 4] * |
| 235 | + conj(f2[-4; 2 3 4]) |
| 236 | + @plansor O_periodic2[-1 -2; -3 -4] := O[1 2; -3 6] * f1[-1; 1 3 5] * |
| 237 | + conj(f2[-4; 6 7 8]) * τ[2 3; 7 4] * |
| 238 | + τ[4 5; 8 -2] |
| 239 | + @planar O_periodic′[-1 -2; -3 -4] := O′[1 2; -3 6] * f1′[-1; 1 3 5] * |
| 240 | + conj(f2′[-4; 6 7 8]) * τ[2 3; 7 4] * |
| 241 | + τ[4 5; 8 -2] |
| 242 | + @test O_periodic1 ≈ O_periodic2 |
| 243 | + @test force_planar(O_periodic1) ≈ O_periodic′ |
| 244 | + end |
| 245 | + |
| 246 | + @testset "MERA networks" begin |
| 247 | + Vmera = ℂ^2 |
| 248 | + |
| 249 | + u = adapt(JLArray, randn(T, Vmera ⊗ Vmera ← Vmera ⊗ Vmera)) |
| 250 | + w = adapt(JLArray, randn(T, Vmera ⊗ Vmera ← Vmera)) |
| 251 | + ρ = adapt(JLArray, randn(T, Vmera ⊗ Vmera ⊗ Vmera ← Vmera ⊗ Vmera ⊗ Vmera)) |
| 252 | + h = adapt(JLArray, randn(T, Vmera ⊗ Vmera ⊗ Vmera ← Vmera ⊗ Vmera ⊗ Vmera)) |
| 253 | + |
| 254 | + u′ = force_planar(u) |
| 255 | + w′ = force_planar(w) |
| 256 | + ρ′ = force_planar(ρ) |
| 257 | + h′ = force_planar(h) |
| 258 | + |
| 259 | + for alloc in |
| 260 | + (TensorOperations.DefaultAllocator(),) |
| 261 | + @tensor allocator = alloc begin |
| 262 | + C = ( |
| 263 | + ( |
| 264 | + ( |
| 265 | + ( |
| 266 | + ( |
| 267 | + ((h[9 3 4; 5 1 2] * u[1 2; 7 12]) * conj(u[3 4; 11 13])) * |
| 268 | + (u[8 5; 15 6] * w[6 7; 19]) |
| 269 | + ) * |
| 270 | + (conj(u[8 9; 17 10]) * conj(w[10 11; 22])) |
| 271 | + ) * |
| 272 | + ((w[12 14; 20] * conj(w[13 14; 23])) * ρ[18 19 20; 21 22 23]) |
| 273 | + ) * |
| 274 | + w[16 15; 18] |
| 275 | + ) * conj(w[16 17; 21]) |
| 276 | + ) |
| 277 | + end |
| 278 | + @planar allocator = alloc begin |
| 279 | + C′ = ( |
| 280 | + ( |
| 281 | + ( |
| 282 | + ( |
| 283 | + ( |
| 284 | + ((h′[9 3 4; 5 1 2] * u′[1 2; 7 12]) * conj(u′[3 4; 11 13])) * |
| 285 | + (u′[8 5; 15 6] * w′[6 7; 19]) |
| 286 | + ) * |
| 287 | + (conj(u′[8 9; 17 10]) * conj(w′[10 11; 22])) |
| 288 | + ) * |
| 289 | + ((w′[12 14; 20] * conj(w′[13 14; 23])) * ρ′[18 19 20; 21 22 23]) |
| 290 | + ) * |
| 291 | + w′[16 15; 18] |
| 292 | + ) * conj(w′[16 17; 21]) |
| 293 | + ) |
| 294 | + end |
| 295 | + @test C ≈ C′ |
| 296 | + end |
| 297 | + end |
| 298 | + |
| 299 | + @testset "Issue 93" begin |
| 300 | + T = Float64 |
| 301 | + V1 = ℂ^2 |
| 302 | + V2 = ℂ^3 |
| 303 | + t1 = adapt(JLArray, randn(T, V1 ← V2)) |
| 304 | + t2 = adapt(JLArray, randn(T, V2 ← V1)) |
| 305 | + |
| 306 | + tr1 = @planar opt = true t1[a; b] * t2[b; a] / 2 |
| 307 | + tr2 = @planar opt = true t1[d; a] * t2[b; c] * 1 / 2 * τ[c b; a d] |
| 308 | + tr3 = @planar opt = true t1[d; a] * t2[b; c] * τ[a c; d b] / 2 |
| 309 | + tr4 = @planar opt = true t1[f; a] * 1 / 2 * t2[c; d] * τ[d b; c e] * τ[e b; a f] |
| 310 | + tr5 = @planar opt = true t1[f; a] * t2[c; d] / 2 * τ[d b; c e] * τ[a e; f b] |
| 311 | + tr6 = @planar opt = true t1[f; a] * t2[c; d] * τ[c d; e b] / 2 * τ[e b; a f] |
| 312 | + tr7 = @planar opt = true t1[f; a] * t2[c; d] * (τ[c d; e b] * τ[a e; f b] / 2) |
| 313 | + |
| 314 | + @test tr1 ≈ tr2 ≈ tr3 ≈ tr4 ≈ tr5 ≈ tr6 ≈ tr7 |
| 315 | + |
| 316 | + tr1 = @plansor opt = true t1[a; b] * t2[b; a] / 2 |
| 317 | + tr2 = @plansor opt = true t1[d; a] * t2[b; c] * 1 / 2 * τ[c b; a d] |
| 318 | + tr3 = @plansor opt = true t1[d; a] * t2[b; c] * τ[a c; d b] / 2 |
| 319 | + tr4 = @plansor opt = true t1[f; a] * 1 / 2 * t2[c; d] * τ[d b; c e] * τ[e b; a f] |
| 320 | + tr5 = @plansor opt = true t1[f; a] * t2[c; d] / 2 * τ[d b; c e] * τ[a e; f b] |
| 321 | + tr6 = @plansor opt = true t1[f; a] * t2[c; d] * τ[c d; e b] / 2 * τ[e b; a f] |
| 322 | + tr7 = @plansor opt = true t1[f; a] * t2[c; d] * (τ[c d; e b] * τ[a e; f b] / 2) |
| 323 | + |
| 324 | + @test tr1 ≈ tr2 ≈ tr3 ≈ tr4 ≈ tr5 ≈ tr6 ≈ tr7 |
| 325 | + end |
| 326 | + @testset "Issue 262" begin |
| 327 | + V = ℂ^2 |
| 328 | + A = adapt(JLArray, randn(T, V ← V)) |
| 329 | + B = adapt(JLArray, randn(T, V ← V')) |
| 330 | + C = adapt(JLArray, randn(T, V' ← V)) |
| 331 | + @planar D1[i; j] := A[i; j] + B[i; k] * C[k; j] |
| 332 | + @planar D2[i; j] := B[i; k] * C[k; j] + A[i; j] |
| 333 | + @test D1 ≈ D2 |
| 334 | + end |
| 335 | +end |
0 commit comments