|
106 | 106 | @test all([isapprox(sol_cpu.expect[i], sol_gpu64.expect[i]) for i in 1:length(tlist)]) |
107 | 107 | @test all([isapprox(sol_cpu.expect[i], sol_gpu32.expect[i]; atol = 1e-6) for i in 1:length(tlist)]) |
108 | 108 | end |
| 109 | + |
| 110 | +@testset "CUDA ptrace" begin |
| 111 | + g = fock(2, 1) |
| 112 | + e = fock(2, 0) |
| 113 | + α = sqrt(0.7) |
| 114 | + β = sqrt(0.3) * 1im |
| 115 | + ψ = α * kron(g, e) + β * kron(e, g) |> cu |
| 116 | + |
| 117 | + ρ1 = ptrace(ψ, 1) |
| 118 | + ρ2 = ptrace(ψ, 2) |
| 119 | + @test ρ1.data isa CuArray |
| 120 | + @test ρ2.data isa CuArray |
| 121 | + @test Array(ρ1.data) ≈ [0.3 0.0; 0.0 0.7] atol = 1e-10 |
| 122 | + @test Array(ρ2.data) ≈ [0.7 0.0; 0.0 0.3] atol = 1e-10 |
| 123 | + |
| 124 | + ψ_d = ψ' |
| 125 | + |
| 126 | + ρ1 = ptrace(ψ_d, 1) |
| 127 | + ρ2 = ptrace(ψ_d, 2) |
| 128 | + @test ρ1.data isa CuArray |
| 129 | + @test ρ2.data isa CuArray |
| 130 | + @test Array(ρ1.data) ≈ [0.3 0.0; 0.0 0.7] atol = 1e-10 |
| 131 | + @test Array(ρ2.data) ≈ [0.7 0.0; 0.0 0.3] atol = 1e-10 |
| 132 | + |
| 133 | + ρ = ket2dm(ψ) |
| 134 | + ρ1 = ptrace(ρ, 1) |
| 135 | + ρ2 = ptrace(ρ, 2) |
| 136 | + @test ρ.data isa CuArray |
| 137 | + @test ρ1.data isa CuArray |
| 138 | + @test ρ2.data isa CuArray |
| 139 | + @test Array(ρ1.data) ≈ [0.3 0.0; 0.0 0.7] atol = 1e-10 |
| 140 | + @test Array(ρ2.data) ≈ [0.7 0.0; 0.0 0.3] atol = 1e-10 |
| 141 | + |
| 142 | + ψ1 = normalize(g + 1im * e) |
| 143 | + ψ2 = normalize(g + e) |
| 144 | + ρ1 = ket2dm(ψ1) |
| 145 | + ρ2 = ket2dm(ψ2) |
| 146 | + ρ = kron(ρ1, ρ2) |> cu |
| 147 | + ρ1_ptr = ptrace(ρ, 1) |
| 148 | + ρ2_ptr = ptrace(ρ, 2) |
| 149 | + @test ρ1_ptr.data isa CuArray |
| 150 | + @test ρ2_ptr.data isa CuArray |
| 151 | + @test ρ1.data ≈ Array(ρ1_ptr.data) atol = 1e-10 |
| 152 | + @test ρ2.data ≈ Array(ρ2_ptr.data) atol = 1e-10 |
| 153 | + |
| 154 | + ψlist = [rand_ket(2), rand_ket(3), rand_ket(4), rand_ket(5)] |
| 155 | + ρlist = [rand_dm(2), rand_dm(3), rand_dm(4), rand_dm(5)] |
| 156 | + ψtotal = tensor(ψlist...) |> cu |
| 157 | + ρtotal = tensor(ρlist...) |> cu |
| 158 | + sel_tests = [ |
| 159 | + SVector{0,Int}(), |
| 160 | + 1, |
| 161 | + 2, |
| 162 | + 3, |
| 163 | + 4, |
| 164 | + (1, 2), |
| 165 | + (1, 3), |
| 166 | + (1, 4), |
| 167 | + (2, 3), |
| 168 | + (2, 4), |
| 169 | + (3, 4), |
| 170 | + (1, 2, 3), |
| 171 | + (1, 2, 4), |
| 172 | + (1, 3, 4), |
| 173 | + (2, 3, 4), |
| 174 | + (1, 2, 3, 4), |
| 175 | + ] |
| 176 | + for sel in sel_tests |
| 177 | + if length(sel) == 0 |
| 178 | + @test ptrace(ψtotal, sel) ≈ 1.0 |
| 179 | + @test ptrace(ρtotal, sel) ≈ 1.0 |
| 180 | + else |
| 181 | + @test ptrace(ψtotal, sel) ≈ cu(tensor([ket2dm(ψlist[i]) for i in sel]...)) |
| 182 | + @test ptrace(ρtotal, sel) ≈ cu(tensor([ρlist[i] for i in sel]...)) |
| 183 | + end |
| 184 | + end |
| 185 | + @test ptrace(ψtotal, (1, 3, 4)) ≈ ptrace(ψtotal, (4, 3, 1)) # check sort of sel |
| 186 | + @test ptrace(ρtotal, (1, 3, 4)) ≈ ptrace(ρtotal, (3, 1, 4)) # check sort of sel |
| 187 | + |
| 188 | + @testset "Type Inference (ptrace)" begin |
| 189 | + @inferred ptrace(ρ, 1) |
| 190 | + @inferred ptrace(ρ, 2) |
| 191 | + @inferred ptrace(ψ_d, 1) |
| 192 | + @inferred ptrace(ψ_d, 2) |
| 193 | + @inferred ptrace(ψ, 1) |
| 194 | + @inferred ptrace(ψ, 2) |
| 195 | + end |
| 196 | +end |
0 commit comments