Skip to content

Commit

Permalink
improve the code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
Fe-r-oz committed Nov 16, 2024
1 parent 79af118 commit da67b55
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
11 changes: 4 additions & 7 deletions src/nonclifford.jl
Original file line number Diff line number Diff line change
Expand Up @@ -233,15 +233,12 @@ function projectrand!(sm::GeneralizedStabilizer, p::PauliOperator)
end

function _proj(sm::GeneralizedStabilizer, p::PauliOperator)
# Returns the updated `GeneralizedStabilizer` state sm′ = (χ′, B(S′, D′)),
# where (S′, D′) is derived from (S, D) through the traditional stabilizer update,
# and χ′ is the updated density matrix after measurement. Note: Λ(χ′) ≤ Λ(χ).
n = nqubits(sm)
state, res = projectrand!(sm.stab, p)
# A projective measurement on a single qubit always collapses the GeneralizedStabilizer state (sm)
# to a pure state, regardless of the evolution of its χ matrix by the channel, even after applying apply!(sm, pcT).
if n == 1
sm = GeneralizedStabilizer(state, DefaultDict(0.0im, (falses(n),falses(n))=>1.0+0.0im))
return sm, res
end
sm.stab = state # in-place
sm = GeneralizedStabilizer(state, DefaultDict(0.0im, (falses(n),falses(n))=>expect(p, sm)))
return sm, res
end

Expand Down
28 changes: 16 additions & 12 deletions test/test_nonclifford_quantumoptics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,20 @@ function multiqubit_projrand(τ,p)
return qo_state_after_proj, result1, result2
end

@testset "Single-qubit projections using for stabilizer states wrt to evolution of χ by channel" begin
@testset "Single-qubit projections using for stabilizer states wrt to evolution of χ by unitary channel" begin
n = 1
for repetition in 1:5
s = random_stabilizer(n)
p = random_pauli(n)
gs = GeneralizedStabilizer(s)
# A projective measurement on a single qubit always collapses the GeneralizedStabilizer state (sm)
# to a pure state, regardless of the evolution of its χ matrix by the channel, even after applying apply!(sm, pcT).
@test projectrand!(apply!(gs, pcT), p)[1].destabweights == GeneralizedStabilizer(s).destabweights
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)
qo_state_after_proj, result1, result2 = multiqubit_projrand(gs,p)
# Normalize to ensure consistent comparison of the projected state, independent of scaling factors
norm_qo_state_after_proj = qo_state_after_proj/tr(qo_state_after_proj)
norm_result1 = result1/tr(result1)
norm_result2 = result2/tr(result2)
@test projectrand!(gs, p)[1] |> invsparsity <= gs |> invsparsity # Note: Λ(χ′) ≤ Λ(χ).
@test norm_qo_state_after_proj norm_result2 || norm_qo_state_after_proj norm_result1
end
end
end

Expand Down Expand Up @@ -139,7 +144,7 @@ function non_stabilizer_simulator(num_trials,n)
p = random_pauli(n)
gs = GeneralizedStabilizer(s)
i = rand(1:n)
nc = embed(n, i, pcT) # multi-qubit random non-Clifford gate
nc = embed(n, i, pcT)
apply!(gs, nc)
qo_state_after_proj, result1, result2 = multiqubit_projrand(gs,p)
# Normalize to ensure consistent comparison of the projected state, independent of scaling factors
Expand Down Expand Up @@ -168,8 +173,7 @@ end
# Therefore, non-Clifford simulators generally require probabilistic sampling
# techniques.

num_qubits = 10
num_trials = 10
num_qubits = 5
num_trials = 5
prob = non_stabilizer_simulator(num_trials, num_qubits)
@test prob > 0.5
end

0 comments on commit da67b55

Please sign in to comment.