From bf649a66a83caf832de6f97ce71783488617c19e Mon Sep 17 00:00:00 2001 From: Feroz Ahmad Date: Sat, 14 Sep 2024 10:14:52 +0500 Subject: [PATCH] fixes expect (#348) --- src/nonclifford.jl | 6 +-- test/test_nonclifford_quantumoptics.jl | 64 +++++++++++--------------- 2 files changed, 31 insertions(+), 39 deletions(-) diff --git a/src/nonclifford.jl b/src/nonclifford.jl index eba12bd39..a9a5ecf62 100644 --- a/src/nonclifford.jl +++ b/src/nonclifford.jl @@ -98,13 +98,13 @@ end Expectation value for the [PauliOperator](@ref) observable given the [`GeneralizedStabilizer`](@ref) state `s`.""" function expect(p::PauliOperator, s::GeneralizedStabilizer) # TODO optimize - e = zero(_dictvaltype(s.destabweights)) + χ′ = zero(_dictvaltype(s.destabweights)) phase, b, c = rowdecompose(p, s.stab) for ((dᵢ,dⱼ), χ) in s.destabweights _allthreesumtozero(dᵢ,dⱼ,b) || continue - e += χ * (-1)^(dᵢ'*c) + χ′ += χ * (-1)^(dᵢ'*c) end - return (-1)^(phase÷2) * e + return (im)^(phase) * χ′ end """Same as `all(==(0), (a.+b.+c) .% 2)`""" diff --git a/test/test_nonclifford_quantumoptics.jl b/test/test_nonclifford_quantumoptics.jl index 48f51881c..48cd1b321 100644 --- a/test/test_nonclifford_quantumoptics.jl +++ b/test/test_nonclifford_quantumoptics.jl @@ -13,48 +13,40 @@ qo_tgate.data[2,2] = exp(im*pi/4) ## -for s in [S"X", S"Y", S"Z", S"-X", S"-Y", S"-Z"] - for p in [P"X", P"Y", P"Z", P"-X", P"-Y", P"-Z"] - gs = GeneralizedStabilizer(s) - apply!(gs, pcT) - ρ = dm(qo_tgate*Ket(s)) - @test Operator(gs) ≈ ρ - if isapprox(expect(p, gs), expect(Operator(p),ρ); atol=1e-5) - else - println("failure of expectation calculation for state = T*(", s, ") and observable = ", p) +@testset "expect" begin + for s in [S"X", S"Y", S"Z", S"-X", S"-Y", S"-Z"] + for p in [P"X", P"Y", P"Z", P"-X", P"-Y", P"-Z"] + gs = GeneralizedStabilizer(s) + apply!(gs, pcT) + ρ = dm(qo_tgate*Ket(s)) + @test Operator(gs) ≈ ρ + @test isapprox(expect(p, gs), expect(Operator(p),ρ); atol=1e-5) end end -end -## - -for _ in 1:10 - for n in 1:1 - i = rand(1:n) - stab = random_stabilizer(n) - genstab = GeneralizedStabilizer(stab) - ket = Ket(stab) - @test dm(ket) ≈ Operator(stab) - @test dm(ket) ≈ Operator(genstab) + for _ in 1:10 + for n in 1:1 + i = rand(1:n) + stab = random_stabilizer(n) + genstab = GeneralizedStabilizer(stab) + ket = Ket(stab) + @test dm(ket) ≈ Operator(stab) + @test dm(ket) ≈ Operator(genstab) - pauli = random_pauli(n; nophase=false, realphase=true) - qo_pauli = Operator(pauli) + pauli = random_pauli(n; nophase=false, realphase=true) + qo_pauli = Operator(pauli) - qo_bigtgate = n==1 ? qo_tgate : embed(qo_basis^n, i, qo_tgate) - bigtgate = embed(n,i, pcT) - @test qo_bigtgate ≈ Operator(bigtgate) + qo_bigtgate = n==1 ? qo_tgate : embed(qo_basis^n, i, qo_tgate) + bigtgate = embed(n,i, pcT) + @test qo_bigtgate ≈ Operator(bigtgate) - for step in 1:10 + for step in 1:10 # apply!(ket, qo_bigtgate) TODO implement this API - ket = qo_bigtgate*ket - apply!(genstab, bigtgate) - @test dm(ket) ≈ Operator(genstab) - if isapprox(expect(qo_pauli, ket), expect(pauli, genstab); atol=1e-5) - else - @show step - ref[] = (stab,pauli) - break - end + ket = qo_bigtgate*ket + apply!(genstab, bigtgate) + @test dm(ket) ≈ Operator(genstab) + @test isapprox(expect(qo_pauli, ket), expect(pauli, genstab); atol=1e-5) + end end end -end +end \ No newline at end of file