|
| 1 | +using .TestSetup |
| 2 | +using Test, TestExtras |
| 3 | +using MPSKit |
| 4 | +using MPSKit: GeometryStyle, FiniteChainStyle, InfiniteChainStyle, OperatorStyle, MPOStyle |
| 5 | +using TensorKit |
| 6 | +using TensorKit: ℙ, tensormaptype, TensorMapWithStorage |
| 7 | +using Adapt, CUDA, cuTENSOR |
| 8 | + |
| 9 | +@testset "CuFiniteMPO" for V in (ℂ^2, U1Space(0 => 1, 1 => 1)) |
| 10 | + # start from random operators |
| 11 | + L = 4 |
| 12 | + T = ComplexF64 |
| 13 | + |
| 14 | + O₁ = rand(T, V^L, V^L) |
| 15 | + O₂ = rand(T, space(O₁)) |
| 16 | + O₃ = rand(real(T), space(O₁)) |
| 17 | + |
| 18 | + mpo₁ = adapt(CuArray, FiniteMPO(O₁)) |
| 19 | + mpo₂ = adapt(CuArray, FiniteMPO(O₂)) |
| 20 | + mpo₃ = adapt(CuArray, FiniteMPO(O₃)) |
| 21 | + |
| 22 | + @test isfinite(mpo₁) |
| 23 | + @test isfinite(typeof(mpo₁)) |
| 24 | + @test GeometryStyle(typeof(mpo₁)) == FiniteChainStyle() |
| 25 | + @test GeometryStyle(mpo₁) == FiniteChainStyle() |
| 26 | + @test OperatorStyle(typeof(mpo₁)) == MPOStyle() |
| 27 | + |
| 28 | + @test @constinferred physicalspace(mpo₁) == fill(V, L) |
| 29 | + Vleft = @constinferred left_virtualspace(mpo₁) |
| 30 | + Vright = @constinferred right_virtualspace(mpo₂) |
| 31 | + for i in 1:L |
| 32 | + @test Vleft[i] == left_virtualspace(mpo₁, i) |
| 33 | + @test Vright[i] == right_virtualspace(mpo₁, i) |
| 34 | + end |
| 35 | + |
| 36 | + TM = TensorMapWithStorage{T, CuVector{T, CUDA.DeviceMemory}} |
| 37 | + #@test convert(TM, mpo₁) ≈ O₁ |
| 38 | + #@test convert(TM, -mpo₂) ≈ -O₂ |
| 39 | + #@test convert(TM, @constinferred complex(mpo₃)) ≈ complex(O₃) |
| 40 | + |
| 41 | + |
| 42 | + # test scalar multiplication |
| 43 | + α = rand(T) |
| 44 | + #@test convert(TM, α * mpo₁) ≈ α * O₁ |
| 45 | + #@test convert(TM, mpo₁ * α) ≈ O₁ * α |
| 46 | + @test α * mpo₃ ≈ α * complex(mpo₃) atol = 1.0e-6 |
| 47 | + |
| 48 | + # test addition and multiplication |
| 49 | + #@test convert(TM, mpo₁ + mpo₂) ≈ O₁ + O₂ |
| 50 | + #@test convert(TM, mpo₁ + mpo₃) ≈ O₁ + O₃ |
| 51 | + #@test convert(TM, mpo₁ * mpo₂) ≈ O₁ * O₂ |
| 52 | + #@test convert(TM, mpo₁ * mpo₃) ≈ O₁ * O₃ |
| 53 | + |
| 54 | + # test application to a state |
| 55 | + ψ₁ = adapt(CuArray, rand(T, domain(O₁))) |
| 56 | + #ψ₂ = adapt(CuArray, rand(real(T), domain(O₂))) # not allowed due to cuTENSOR |
| 57 | + mps₁ = adapt(CuArray, FiniteMPS(ψ₁)) |
| 58 | + #mps₂ = adapt(CuArray, FiniteMPS(ψ₂)) |
| 59 | + |
| 60 | + @test @constinferred GeometryStyle(mps₁, mpo₁, mps₁) == GeometryStyle(mps₁) |
| 61 | + |
| 62 | + #@test convert(TM, mpo₁ * mps₁) ≈ O₁ * ψ₁ |
| 63 | + @test mpo₁ * ψ₁ ≈ O₁ * ψ₁ |
| 64 | + #@test convert(TM, mpo₃ * mps₁) ≈ O₃ * ψ₁ |
| 65 | + @test mpo₃ * ψ₁ ≈ O₃ * ψ₁ |
| 66 | + #@test convert(TM, mpo₁ * mps₂) ≈ O₁ * ψ₂ |
| 67 | + #@test mpo₁ * ψ₂ ≈ O₁ * ψ₂ |
| 68 | + |
| 69 | + @test dot(mps₁, mpo₁, mps₁) ≈ dot(ψ₁, O₁, ψ₁) |
| 70 | + @test dot(mps₁, mpo₁, mps₁) ≈ dot(mps₁, mpo₁ * mps₁) |
| 71 | + # test conversion to and from mps |
| 72 | + mpomps₁ = convert(FiniteMPS, mpo₁) |
| 73 | + mpompsmpo₁ = convert(FiniteMPO, mpomps₁) |
| 74 | + |
| 75 | + @test convert(FiniteMPO, mpomps₁) ≈ mpo₁ rtol = 1.0e-6 |
| 76 | + |
| 77 | + @test dot(mpomps₁, mpomps₁) ≈ dot(mpo₁, mpo₁) |
| 78 | +end |
0 commit comments