From 8827be40d95b52e0d16016023951b48844980f43 Mon Sep 17 00:00:00 2001 From: Fe-r-oz Date: Sun, 11 Aug 2024 17:58:11 +0500 Subject: [PATCH] adding doctests and some basic tests --- src/nonclifford.jl | 71 +++++++++++++++++++++++++++++++++++---------- test/test_paulis.jl | 30 +++++++++++++++++-- test/test_stabs.jl | 15 ++++++++++ 3 files changed, 98 insertions(+), 18 deletions(-) diff --git a/src/nonclifford.jl b/src/nonclifford.jl index 2ee28038f..65306489b 100644 --- a/src/nonclifford.jl +++ b/src/nonclifford.jl @@ -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) @@ -157,8 +156,8 @@ function (⊗)(state₁::GeneralizedStabilizer, state₂::GeneralizedStabilizer) end end newsm.destabweights = newdict - newsm -end + return newsm +end """ $(TYPEDSIGNATURES) @@ -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`.""" @@ -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) @@ -443,7 +470,7 @@ function (⊗)(gate::AbstractPauliChannel, Op::PauliOperator) new_paulis = (p₁, p₂) weights = gate.weights return UnitaryPauliChannel(new_paulis, weights) -end +end """ $(TYPEDSIGNATURES) @@ -473,7 +500,6 @@ function (*)(Op::AbstractCliffordOperator, gate::AbstractPauliChannel) return UnitaryPauliChannel(new_paulis, weights) end - """ $(TYPEDSIGNATURES) @@ -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) +) diff --git a/test/test_paulis.jl b/test/test_paulis.jl index c53c7d847..88bf473a5 100644 --- a/test/test_paulis.jl +++ b/test/test_paulis.jl @@ -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)) @@ -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 diff --git a/test/test_stabs.jl b/test/test_stabs.jl index dab3d20df..96349406c 100644 --- a/test/test_stabs.jl +++ b/test/test_stabs.jl @@ -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 = l⊗r + sm2 = sm⊗sm1 + @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]