Skip to content

Commit

Permalink
use less boilerplace for get bitmasks in some other places as well
Browse files Browse the repository at this point in the history
  • Loading branch information
Fe-r-oz committed Sep 28, 2024
1 parent e6dd17c commit 0232d62
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/QuantumClifford.jl
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,7 @@ Returns:
- `ismallm`: A bitmask isolating the specified bit.
"""
function get_bitmask_idxs(xzs::AbstractMatrix{UInt64}, i::Int)
function get_bitmask_idxs(xzs::AbstractArray{<:Unsigned}, i::Int)
T = eltype(xzs)
lowbit = T(1)
ibig = _div(T, i-1) + 1
Expand Down
14 changes: 9 additions & 5 deletions src/pauli_operator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,23 @@ macro P_str(a)
quote _P_str($a) end
end

Base.getindex(p::PauliOperator{Tₚ,Tᵥ}, i::Int) where {Tₚ, Tᵥₑ<:Unsigned, Tᵥ<:AbstractVector{Tᵥₑ}} = (p.xz[_div(Tᵥₑ, i-1)+1] & Tᵥₑ(0x1)<<_mod(Tᵥₑ,i-1))!=0x0, (p.xz[end÷2+_div(Tᵥₑ,i-1)+1] & Tᵥₑ(0x1)<<_mod(Tᵥₑ,i-1))!=0x0
Base.getindex(p::PauliOperator{Tₚ,Tᵥ}, i::Int) where {Tₚ, Tᵥₑ<:Unsigned, Tᵥ<:AbstractVector{Tᵥₑ}} = begin
_, ibig, _, ismallm = get_bitmask_idxs(p.xz,i)
(p.xz[ibig] & ismallm) != 0x0, (p.xz[end÷2+ibig] & ismallm) != 0x0
end
Base.getindex(p::PauliOperator{Tₚ,Tᵥ}, r) where {Tₚ, Tᵥₑ<:Unsigned, Tᵥ<:AbstractVector{Tᵥₑ}} = PauliOperator(p.phase[], xbit(p)[r], zbit(p)[r])

function Base.setindex!(p::PauliOperator{Tₚ,Tᵥ}, (x,z)::Tuple{Bool,Bool}, i) where {Tₚ, Tᵥₑ, Tᵥ<:AbstractVector{Tᵥₑ}}
_, ibig, _, ismallm = get_bitmask_idxs(p.xz,i)
if x
p.xz[_div(Tᵥₑ,i-1)+1] |= Tᵥₑ(0x1)<<_mod(Tᵥₑ,i-1)
p.xz[ibig] |= ismallm
else
p.xz[_div(Tᵥₑ,i-1)+1] &= ~(Tᵥₑ(0x1)<<_mod(Tᵥₑ,i-1))
p.xz[ibig] &= ~(ismallm)
end
if z
p.xz[end÷2+_div(Tᵥₑ,i-1)+1] |= Tᵥₑ(0x1)<<_mod(Tᵥₑ,i-1)
p.xz[end÷2+ibig] |= ismallm
else
p.xz[end÷2+_div(Tᵥₑ,i-1)+1] &= ~(Tᵥₑ(0x1)<<_mod(Tᵥₑ,i-1))
p.xz[end÷2+ibig] &= ~(ismallm)
end
p
end
Expand Down
15 changes: 6 additions & 9 deletions src/project_trace_reset.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,15 @@ function _generate!(pauli::PauliOperator{Tₚ,Tᵥ}, stabilizer::Stabilizer{Tabl
xzs = tab(stabilizer).xzs
xs = @view xzs[1:end÷2,:]
zs = @view xzs[end÷2+1:end,:]
lowbit = Tₘₑ(0x1)
zerobit = Tₘₑ(0x0)
px,pz = xview(pauli), zview(pauli)
used_indices = Int[]
used = 0
# remove Xs
while (i=unsafe_bitfindnext_(px,1); i !== nothing) # TODO awkward notation due to https://github.com/JuliaLang/julia/issues/45499
jbig = _div(Tₘₑ,i-1)+1
jsmall = lowbit<<_mod(Tₘₑ,i-1)
candidate = findfirst(e->e&jsmall!=zerobit, # TODO some form of reinterpret might be faster than equality check
xs[jbig,used+1:end])
_, ibig, _, ismallm = get_bitmask_idxs(xzs,i)
candidate = findfirst(e->e&ismallm!=zerobit, # TODO some form of reinterpret might be faster than equality check
xs[ibig,used+1:end])
if isnothing(candidate)
return nothing
else
Expand All @@ -63,10 +61,9 @@ function _generate!(pauli::PauliOperator{Tₚ,Tᵥ}, stabilizer::Stabilizer{Tabl
end
# remove Zs
while (i=unsafe_bitfindnext_(pz,1); i !== nothing) # TODO awkward notation due to https://github.com/JuliaLang/julia/issues/45499
jbig = _div(Tₘₑ,i-1)+1
jsmall = lowbit<<_mod(Tₘₑ,i-1)
candidate = findfirst(e->e&jsmall!=zerobit, # TODO some form of reinterpret might be faster than equality check
zs[jbig,used+1:end])
_, ibig, _, ismallm = get_bitmask_idxs(xzs,i)
candidate = findfirst(e->e&ismallm!=zerobit, # TODO some form of reinterpret might be faster than equality check
zs[ibig,used+1:end])
if isnothing(candidate)
return nothing
else
Expand Down
21 changes: 6 additions & 15 deletions src/symbolic_cliffords.jl
Original file line number Diff line number Diff line change
Expand Up @@ -394,12 +394,9 @@ LinearAlgebra.inv(op::sInvZCrY) = sZCrY(op.q1, op.q2)
"""Apply a Pauli Z to the `i`-th qubit of state `s`. You should use `apply!(stab,sZ(i))` instead of this."""
function apply_single_z!(stab::AbstractStabilizer, i)
s = tab(stab)
Tₘₑ = eltype(s.xzs)
bigi = _div(Tₘₑ,i-1)+1
smalli = _mod(Tₘₑ,i-1)
mask = Tₘₑ(0x1)<<smalli
_, ibig, _, ismallm = get_bitmask_idxs(s.xzs,i)
@inbounds @simd for row in 1:size(s.xzs,2)
if !iszero(s.xzs[bigi,row] & mask)
if !iszero(s.xzs[ibig,row] & ismallm)
s.phases[row] = (s.phases[row]+0x2)&0x3
end
end
Expand All @@ -409,12 +406,9 @@ end
"""Apply a Pauli X to the `i`-th qubit of state `s`. You should use `apply!(stab,sX(i))` instead of this."""
function apply_single_x!(stab::AbstractStabilizer, i)
s = tab(stab)
Tₘₑ = eltype(s.xzs)
bigi = _div(Tₘₑ,i-1)+1
smalli = _mod(Tₘₑ,i-1)
mask = Tₘₑ(0x1)<<smalli
_, ibig, _, ismallm = get_bitmask_idxs(s.xzs,i)
@inbounds @simd for row in 1:size(s.xzs,2)
if !iszero(s.xzs[end÷2+bigi,row] & mask)
if !iszero(s.xzs[end÷2+ibig,row] & ismallm)
s.phases[row] = (s.phases[row]+0x2)&0x3
end
end
Expand All @@ -424,12 +418,9 @@ end
"""Apply a Pauli Y to the `i`-th qubit of state `s`. You should use `apply!(stab,sY(i))` instead of this."""
function apply_single_y!(stab::AbstractStabilizer, i)
s = tab(stab)
Tₘₑ = eltype(s.xzs)
bigi = _div(Tₘₑ,i-1)+1
smalli = _mod(Tₘₑ,i-1)
mask = Tₘₑ(0x1)<<smalli
_, ibig, _, ismallm = get_bitmask_idxs(s.xzs,i)
@inbounds @simd for row in 1:size(s.xzs,2)
if !iszero((s.xzs[bigi,row] & mask) (s.xzs[end÷2+bigi,row] & mask))
if !iszero((s.xzs[ibig,row] & ismallm) (s.xzs[end÷2+ibig,row] & ismallm))
s.phases[row] = (s.phases[row]+0x2)&0x3
end
end
Expand Down

0 comments on commit 0232d62

Please sign in to comment.