Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes #208: Constructing only valid Stabilizer(phases, xs, zs) #350

Merged
merged 3 commits into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions src/QuantumClifford.jl
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,20 @@ function Tableau(paulis::AbstractVector{PauliOperator{Tₚ,Tᵥ}}) where {Tₚ<:
tab
end

Tableau(phases::AbstractVector{UInt8}, xs::AbstractMatrix{Bool}, zs::AbstractMatrix{Bool}) = Tableau(
phases, size(xs,2),
vcat(hcat((BitArray(xs[i,:]).chunks for i in 1:size(xs,1))...)::Matrix{UInt},
hcat((BitArray(zs[i,:]).chunks for i in 1:size(zs,1))...)::Matrix{UInt}) # type assertions to help Julia infer types
)
function Tableau(phases::AbstractVector{UInt8}, xs::AbstractMatrix{Bool}, zs::AbstractMatrix{Bool})
r_xs = size(xs, 1)
r_zs = size(zs, 1)
if length(phases) != r_xs || r_xs != r_zs
throw(DimensionMismatch(lazy"The length of phases ($(length(phases))), rows of xs ($r_xs), rows of zs ($r_zs) must all be equal."))
end
Tableau(
phases,size(xs, 2),
vcat(
hcat((BitArray(xs[i, :]).chunks for i in 1:r_xs)...)::Matrix{UInt},
hcat((BitArray(zs[i, :]).chunks for i in 1:r_zs)...)::Matrix{UInt} # type assertions to help Julia infer types
)
)
end

Tableau(phases::AbstractVector{UInt8}, xzs::AbstractMatrix{Bool}) = Tableau(phases, xzs[:,1:end÷2], xzs[:,end÷2+1:end])

Expand Down
98 changes: 51 additions & 47 deletions test/test_throws.jl
Original file line number Diff line number Diff line change
@@ -1,69 +1,73 @@
@testitem "throws" begin
using QuantumClifford: rank, mul_left!, mul_right!
using InteractiveUtils: subtypes
using QuantumClifford: rank, mul_left!, mul_right!
using InteractiveUtils: subtypes

@test_throws DimensionMismatch CliffordOperator(T"XXX ZZ_")
@test_throws DimensionMismatch CliffordOperator(T"XXX ZZ_")

@test_throws DimensionMismatch tCNOT*S"X"
@test_throws DimensionMismatch tCNOT*S"X"

#@test_throws DomainError bigram(random_stabilizer(50), clip=false)
#@test_throws DomainError bigram(random_stabilizer(50), clip=false)

@test_throws DomainError logdot(S"XX", S"XX ZZ")
@test_throws DimensionMismatch logdot(S"X", S"XX ZZ")
@test_throws DomainError logdot(S"XX", S"XX ZZ")
@test_throws DimensionMismatch logdot(S"X", S"XX ZZ")

@test_throws BadDataStructure rank(S"X")
@test_throws BadDataStructure rank(Destabilizer(S"X"))
@test_throws BadDataStructure rank(S"X")
@test_throws BadDataStructure rank(Destabilizer(S"X"))

@test_throws DimensionMismatch mul_left!(P"X", P"XX")
@test_throws DimensionMismatch mul_right!(P"X", P"XX")
@test_throws DimensionMismatch mul_left!(P"X", P"XX")
@test_throws DimensionMismatch mul_right!(P"X", P"XX")

@test_throws ArgumentError StabMixture(S"XX")
@test_throws ArgumentError StabMixture(S"XX")

@test_throws ArgumentError UnitaryPauliChannel([P"X"], [1,2])
@test_throws ArgumentError UnitaryPauliChannel([P"X",P"XX"], [1,2])
@test_throws ArgumentError UnitaryPauliChannel([P"X"], [1,2])
@test_throws ArgumentError UnitaryPauliChannel([P"X",P"XX"], [1,2])

@test_throws ArgumentError embed(10,2,P"XX")
@test_throws ArgumentError embed(10,[2,3],P"X")
@test_throws ArgumentError embed(10,2,P"XX")
@test_throws ArgumentError embed(10,[2,3],P"X")

struct A <: QuantumClifford.AbstractOperation end
@test_throws ArgumentError applybranches(S"X",A())
struct A <: QuantumClifford.AbstractOperation end
@test_throws ArgumentError applybranches(S"X",A())

@test_throws BadDataStructure project!(Destabilizer(S"XX"), P"ZZ")
@test_throws BadDataStructure project!(Destabilizer(S"XX"), P"ZZ")

@test_throws DimensionMismatch reset_qubits!(ghz(4), ghz(3), [1,2])
@test_throws DimensionMismatch reset_qubits!(ghz(3), ghz(4), [1,2,3,4])
@test_throws DimensionMismatch reset_qubits!(MixedStabilizer(ghz(4)), MixedStabilizer(ghz(3)), [1,2])
@test_throws DimensionMismatch reset_qubits!(MixedStabilizer(ghz(3)), MixedStabilizer(ghz(4)), [1,2,3,4])
@test_throws DimensionMismatch reset_qubits!(MixedDestabilizer(ghz(4)), MixedDestabilizer(ghz(3)), [1,2])
@test_throws DimensionMismatch reset_qubits!(MixedDestabilizer(ghz(3)), MixedDestabilizer(ghz(4)), [1,2,3,4])
@test_throws DimensionMismatch reset_qubits!(ghz(4), ghz(3), [1,2])
@test_throws DimensionMismatch reset_qubits!(ghz(3), ghz(4), [1,2,3,4])
@test_throws DimensionMismatch reset_qubits!(MixedStabilizer(ghz(4)), MixedStabilizer(ghz(3)), [1,2])
@test_throws DimensionMismatch reset_qubits!(MixedStabilizer(ghz(3)), MixedStabilizer(ghz(4)), [1,2,3,4])
@test_throws DimensionMismatch reset_qubits!(MixedDestabilizer(ghz(4)), MixedDestabilizer(ghz(3)), [1,2])
@test_throws DimensionMismatch reset_qubits!(MixedDestabilizer(ghz(3)), MixedDestabilizer(ghz(4)), [1,2,3,4])

#TODO broken in other ways @test_throws DomainError MixedDestabilizer(Destabilizer(S"XX"))
#TODO broken in other ways @test_throws DomainError MixedDestabilizer(Destabilizer(S"XX"))

@test_throws DomainError 2*P"X"
@test_throws DomainError 2*P"X"

@test_throws DimensionMismatch P"X" * S"XX"
@test_throws DimensionMismatch P"X" * S"XX"

@test_throws ArgumentError one(typeof(T"X"), 1, basis=:U)
@test_throws ArgumentError one(typeof(T"X"), 1, basis=:U)

@test_throws ArgumentError SparseGate(random_clifford(2), [1, 2, 3])
@test_throws ArgumentError apply!(random_stabilizer(2), SparseGate(random_clifford(3), [1, 2, 3]))
@test_throws ArgumentError SparseGate(random_clifford(2), [1, 2, 3])
@test_throws ArgumentError apply!(random_stabilizer(2), SparseGate(random_clifford(3), [1, 2, 3]))

for gt in subtypes(QuantumClifford.AbstractSingleQubitOperator)
gt == SingleQubitOperator && continue
@test_throws ArgumentError gt(0)
@test_throws ArgumentError gt(-1)
end
for gt in subtypes(QuantumClifford.AbstractSingleQubitOperator)
gt == SingleQubitOperator && continue
@test_throws ArgumentError gt(0)
@test_throws ArgumentError gt(-1)
end

for gt in subtypes(QuantumClifford.AbstractTwoQubitOperator)
@test_throws ArgumentError gt(0,1)
@test_throws ArgumentError gt(-1,1)
@test_throws ArgumentError gt(2,2)
end
for gt in subtypes(QuantumClifford.AbstractTwoQubitOperator)
@test_throws ArgumentError gt(0,1)
@test_throws ArgumentError gt(-1,1)
@test_throws ArgumentError gt(2,2)
end

for m in [sMX,sMZ,sMY,sMRX,sMRZ,sMRY]
@test_throws ArgumentError m(0)
@test_throws ArgumentError m(-1)
@test_throws ArgumentError m(0,1)
@test_throws ArgumentError m(-1,0)
end

@test_throws DimensionMismatch Stabilizer(fill(0x0, 2), Matrix{Bool}([1 0 1;1 1 1; 1 0 1]), Matrix{Bool}([1 0 0;1 1 1;1 0 1]))
@test_throws DimensionMismatch Stabilizer(fill(0x0, 2), Matrix{Bool}([1 0 1 1 0 0; 1 1 1 1 1 1; 1 0 1 1 0 1]))

for m in [sMX,sMZ,sMY,sMRX,sMRZ,sMRY]
@test_throws ArgumentError m(0)
@test_throws ArgumentError m(-1)
@test_throws ArgumentError m(0,1)
@test_throws ArgumentError m(-1,0)
end
end
Loading