Skip to content

Commit

Permalink
improve code quality and add tests for project! and projectrand!
Browse files Browse the repository at this point in the history
  • Loading branch information
Fe-r-oz committed Oct 1, 2024
1 parent d21f2a9 commit 991f853
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 25 deletions.
14 changes: 6 additions & 8 deletions src/nonclifford.jl
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,11 @@ with ϕᵢⱼ | Pᵢ | Pⱼ:
"""
function project!(sm::GeneralizedStabilizer, p::PauliOperator)
newstab, anticom_idx, res = project!(sm.stab, p)
newstab, anticom, res = project!(sm.stab, p)
χ′ = expect(p, sm)
n = nqubits(newstab)
newsm = GeneralizedStabilizer(newstab, DefaultDict(0.0im, (falses(n),falses(n))=>χ′))
return newsm, anticom_idx, res
return newsm, anticom, res
end

"""
Expand All @@ -225,21 +225,19 @@ function projectrand!(sm::GeneralizedStabilizer, p::PauliOperator)
end

function _proj₊(sm::GeneralizedStabilizer, p::PauliOperator)
newstab, anticom_idx, res = project!(sm.stab, p)
newstab, res = projectrand!(sm.stab, p)
χ′ = expect(p, sm)
sm.stab = newstab
n = nqubits(newstab)
newsm = GeneralizedStabilizer(newstab, DefaultDict(0.0im, (falses(n),falses(n))=>χ′))
return newsm, anticom_idx, res
return newsm, res
end

function _proj₋(sm::GeneralizedStabilizer, p::PauliOperator)
newstab, anticom_idx, res = project!(sm.stab, -p)
newstab, res = projectrand!(sm.stab, -p)
χ′ = expect(p, sm)
sm.stab = newstab
n = nqubits(newstab)
newsm = GeneralizedStabilizer(newstab, DefaultDict(0.0im, (falses(n),falses(n))=>χ′))
return newsm, anticom_idx, res
return newsm, res
end

Base.copy(sm::GeneralizedStabilizer) = GeneralizedStabilizer(copy(sm.stab),copy(sm.destabweights))
Expand Down
65 changes: 48 additions & 17 deletions test/test_nonclifford_quantumoptics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,53 @@ qo_tgate.data[2,2] = exp(im*pi/4)
end

@testset "project!" begin
checks = [(S"X",P"X"),(S"Y",P"Y"),(S"Z",P"Z"),(S"-X",P"-X"),(S"-Y",P"-Y"),(S"-Z",P"-Z")]
for (s, p) in checks
gs = GeneralizedStabilizer(s)
apply!(gs, p)
qo_state = Operator(gs)
project!(copy(gs), copy(p))[1]
qo_state_after_proj = Operator(gs)
qo_pauli = Operator(p)
qo_proj1 = (identityoperator(qo_pauli) - qo_pauli)/2
qo_proj2 = (identityoperator(qo_pauli) - qo_pauli)/2
qo_proj3 = (identityoperator(qo_pauli) + qo_pauli)/2
qo_proj4 = (identityoperator(qo_pauli) + qo_pauli)/2
result1 = qo_proj1*qo_state*qo_proj1'
result2 = qo_proj2*qo_state*qo_proj2'
result3 = qo_proj3*qo_state*qo_proj3'
result4 = qo_proj4*qo_state*qo_proj4'
@test qo_state_after_proj result4 || qo_state_after_proj result3 || qo_state_after_proj result2 || qo_state_after_proj result1
for n in 1:4
for repetition in 1:5
s = random_stabilizer(n)
p = random_pauli(n)
gs = GeneralizedStabilizer(s)
for i in 1:rand(1:3)
apply!(gs, embed(n, i, pcT))
end
qo_state = Operator(gs)
project!(copy(gs), copy(p))[1]
qo_state_after_proj = Operator(gs)
qo_pauli = Operator(gs)
qo_proj1 = (identityoperator(qo_pauli) - qo_pauli)/2
qo_proj2 = (identityoperator(qo_pauli) - qo_pauli)/2
qo_proj3 = (identityoperator(qo_pauli) + qo_pauli)/2
qo_proj4 = (identityoperator(qo_pauli) + qo_pauli)/2
result1 = qo_proj1*qo_state*qo_proj1'
result2 = qo_proj2*qo_state*qo_proj2'
result3 = qo_proj3*qo_state*qo_proj3'
result4 = qo_proj4*qo_state*qo_proj4'
@test qo_state_after_proj result4 || qo_state_after_proj result3 || qo_state_after_proj result2 || qo_state_after_proj result1
end
end
end

@testset "projectrand!" begin
for n in 1:4
for repetition in 1:5
s = random_stabilizer(n)
p = random_pauli(n)
gs = GeneralizedStabilizer(s)
for i in 1:rand(1:3)
apply!(gs, embed(n, i, pcT))
end
qo_state = Operator(gs)
projectrand!(copy(gs), copy(p))[1]
qo_state_after_proj = Operator(gs)
qo_pauli = Operator(gs)
qo_proj1 = (identityoperator(qo_pauli) - qo_pauli)/2
qo_proj2 = (identityoperator(qo_pauli) - qo_pauli)/2
qo_proj3 = (identityoperator(qo_pauli) + qo_pauli)/2
qo_proj4 = (identityoperator(qo_pauli) + qo_pauli)/2
result1 = qo_proj1*qo_state*qo_proj1'
result2 = qo_proj2*qo_state*qo_proj2'
result3 = qo_proj3*qo_state*qo_proj3'
result4 = qo_proj4*qo_state*qo_proj4'
@test qo_state_after_proj result4 || qo_state_after_proj result3 || qo_state_after_proj result2 || qo_state_after_proj result1
end
end
end

0 comments on commit 991f853

Please sign in to comment.