Skip to content

Commit

Permalink
adding doctests and some basic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Fe-r-oz committed Aug 11, 2024
1 parent 7d39200 commit 8827be4
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 18 deletions.
71 changes: 55 additions & 16 deletions src/nonclifford.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ DataStructures.DefaultDict{Tuple{BitVector, BitVector}, ComplexF64, ComplexF64}
([1], [0]) => 0.0-0.353553im
([0], [0]) => 0.853553+0.0im
([1], [1]) => 0.146447+0.0im
```
See also: [`PauliChannel`](@ref)
Expand Down Expand Up @@ -157,8 +156,8 @@ function (⊗)(state₁::GeneralizedStabilizer, state₂::GeneralizedStabilizer)
end
end
newsm.destabweights = newdict
newsm
end
return newsm
end

"""
$(TYPEDSIGNATURES)
Expand Down Expand Up @@ -189,13 +188,50 @@ function (*)(Op::AbstractCliffordOperator, state::GeneralizedStabilizer)
dtype = valtype(dict)
tzero = zero(dtype)
newdict = typeof(dict)(tzero)
newstab = Op * state.stab
newstab = Op * state.stab
n = nqubits(newstab)
newsm = GeneralizedStabilizer(newstab, DefaultDict(0.0im, (falses(n),falses(n))=>1.0+0.0im))
newsm.destabweights = dict
return newsm
end

"""
$(TYPEDSIGNATURES)
```jldoctest
julia> sm = GeneralizedStabilizer(S"-X")
A mixture ∑ ϕᵢⱼ Pᵢ ρ Pⱼ† where ρ is
𝒟ℯ𝓈𝓉𝒶𝒷
+ Z
𝒮𝓉𝒶𝒷
- X
with ϕᵢⱼ | Pᵢ | Pⱼ:
1.0+0.0im | + _ | + _
julia> P"-Y" * sm
A mixture ∑ ϕᵢⱼ Pᵢ ρ Pⱼ† where ρ is
𝒟ℯ𝓈𝓉𝒶𝒷
- Z
𝒮𝓉𝒶𝒷
+ X
with ϕᵢⱼ | Pᵢ | Pⱼ:
1.0+0.0im | + _ | + _
```
"""
function (*)(Op::PauliOperator, state::GeneralizedStabilizer)
dict = state.destabweights
dtype = valtype(dict)
tzero = zero(dtype)
newdict = typeof(dict)(tzero)
newstab = Op * state.stab
n = nqubits(newstab)
newsm = GeneralizedStabilizer(newstab, DefaultDict(0.0im, (falses(n),falses(n))=>1.0+0.0im))
newsm.destabweights = dict
newsm
return newsm
end


"""$(TYPEDSIGNATURES)
Expectation value for the [PauliOperator](@ref) observable given the [`GeneralizedStabilizer`](@ref) state `s`."""
Expand Down Expand Up @@ -410,15 +446,6 @@ nqubits(pc::UnitaryPauliChannel) = nqubits(pc.paulis[1])

apply!(state::GeneralizedStabilizer, gate::UnitaryPauliChannel; prune_threshold::Union{Nothing, Float64}=nothing) = prune_threshold === nothing ? apply!(state, gate.paulichannel) : apply!(state, gate.paulichannel, prune_threshold)

##
# Predefined Pauli Channels
##

const pcT = UnitaryPauliChannel(
(I, Z),
((1+exp(im*π/4))/2, (1-exp(im*π/4))/2)
)

"""
$(TYPEDSIGNATURES)
Expand All @@ -443,7 +470,7 @@ function (⊗)(gate::AbstractPauliChannel, Op::PauliOperator)
new_paulis = (p₁, p₂)
weights = gate.weights
return UnitaryPauliChannel(new_paulis, weights)
end
end

"""
$(TYPEDSIGNATURES)
Expand Down Expand Up @@ -473,7 +500,6 @@ function (*)(Op::AbstractCliffordOperator, gate::AbstractPauliChannel)
return UnitaryPauliChannel(new_paulis, weights)
end


"""
$(TYPEDSIGNATURES)
Expand All @@ -495,7 +521,20 @@ A mixture ∑ ϕᵢⱼ Pᵢ ρ Pⱼ† where ρ is
- X
with ϕᵢⱼ | Pᵢ | Pⱼ:
1.0+0.0im | + _ | + _
julia> sm == copy(sm)
true
```
"""
Base.copy(sm::GeneralizedStabilizer) = GeneralizedStabilizer(copy(sm.stab),copy(sm.destabweights))
Base.:(==)(sm₁::GeneralizedStabilizer, sm₂::GeneralizedStabilizer) = sm₁.stab==sm₂.stab && sm₁.destabweights==sm₂.destabweights

##
# Predefined Pauli Channels
##

const pcT = UnitaryPauliChannel(
(I, Z),
((1+exp(im*π/4))/2, (1-exp(im*π/4))/2)
)
30 changes: 28 additions & 2 deletions test/test_paulis.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@testitem "Pauli Operators" begin
using QuantumClifford: apply_single_x!, apply_single_y!, apply_single_z!
test_sizes = [1,2,10,63,64,65,127,128,129] # Including sizes that would test off-by-one errors in the bit encoding.
using QuantumClifford: apply_single_x!, apply_single_y!, apply_single_z!
test_sizes = [1,2,10,63,64,65,127,128,129] # Including sizes that would test off-by-one errors in the bit encoding.

@testset "Parsing, constructors, and properties" begin
@test P"-iXYZ" == PauliOperator(0x3, 3, vcat(BitArray([1,1,0]).chunks, BitArray([0,1,1]).chunks))
Expand Down Expand Up @@ -90,5 +90,31 @@
end
end
end

@testset "Single qubit Paulis and their action on GeneralizedStabilizer" begin
for i in 1:3
for n in test_sizes
x, y, z = rand(1:n), rand(1:n), rand(1:n)
px = single_x(n,x)
py = single_y(n,y)
pz = single_z(n,z)
rstab = random_stabilizer(n)
s1 = GeneralizedStabilizer(rstab)
s2 = copy(s1)
s3 = copy(s1)
apply!(s1,px)
apply_single_x!(s2.stab,x)
apply!(s3.stab,P"X",[x])
@test s1==s2==s3
apply!(s1,py)
apply_single_y!(s2.stab,y)
apply!(s3.stab,P"Y",[y])
@test s1==s2==s3
apply!(s1,pz)
apply_single_z!(s2.stab,z)
apply!(s3.stab,P"Z",[z])
@test s1==s2==s3
end
end
end
end
15 changes: 15 additions & 0 deletions test/test_stabs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,21 @@
end
end

@testset "Tensor products over generalized stabilizers" begin
for n in rand(1:10, 10)
l = random_stabilizer(n)
r = random_stabilizer(n)
sm = GeneralizedStabilizer(l)
sm1 = GeneralizedStabilizer(r)
s = lr
sm2 = smsm1
@test mixed_destab_looks_good(sm2.stab)
canonicalize!(s)
dss = canonicalize!(copy(stabilizerview(sm2.stab)))
@test s == dss
end
end

@testset "Stabilizer indexing" begin
s = random_stabilizer(9,10)
@test s[1,1] == s[[1,3,4],[1,3,5]][1,1]
Expand Down

0 comments on commit 8827be4

Please sign in to comment.