Skip to content

Commit c97ab94

Browse files
Fe-r-ozKrastanov
andauthored
Fixes #335: Sanity checks on SparseGate operations (#341)
Co-authored-by: Stefan Krastanov <[email protected]>
1 parent 3616933 commit c97ab94

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

src/misc_ops.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,21 @@ end
2929
struct SparseGate{T<:Tableau} <: AbstractCliffordOperator # TODO simplify type parameters (remove nesting)
3030
cliff::CliffordOperator{T}
3131
indices::Vector{Int}
32+
function SparseGate(cliff::CliffordOperator{T}, indices::Vector{Int}) where T<:Tableau
33+
if length(indices) != nqubits(cliff)
34+
throw(ArgumentError("The number of target qubits (indices) must match the qubit count in the CliffordOperator."))
35+
end
36+
new{T}(cliff, indices)
37+
end
3238
end
3339

3440
SparseGate(c,t::Tuple) = SparseGate(c,collect(t))
3541

3642
function apply!(state::AbstractStabilizer, g::SparseGate; kwargs...)
43+
m = maximum(g.indices)
44+
if m > nqubits(state)
45+
throw(ArgumentError(lazy"SparseGate was attempted on invalid qubit index $(m) when the state contains only $(nqubits(state)) qubits."))
46+
end
3747
apply!(state, g.cliff, g.indices; kwargs...)
3848
end
3949

test/test_throws.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@
4545

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

48+
@test_throws ArgumentError SparseGate(random_clifford(2), [1, 2, 3])
49+
@test_throws ArgumentError apply!(random_stabilizer(2), SparseGate(random_clifford(3), [1, 2, 3]))
50+
4851
for gt in subtypes(QuantumClifford.AbstractSingleQubitOperator)
4952
gt == SingleQubitOperator && continue
5053
@test_throws ArgumentError gt(0)

0 commit comments

Comments
 (0)