Skip to content

Commit

Permalink
fix QuantumSavory#113 - better bit-wrangling abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
Fe-r-oz committed Sep 23, 2024
1 parent 05491d9 commit 7d0db71
Showing 1 changed file with 17 additions and 28 deletions.
45 changes: 17 additions & 28 deletions src/pauli_frames.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,22 @@ function apply!(frame::PauliFrame, op::sMRX) # TODO implement a faster direct ve
apply!(frame, sMRZ(op.qubit, op.bit))
end

function _get_bitmask_idxs(frame::PauliFrame, i::Int)
T = eltype(frame.frame.tab.xzs)
lowbit = T(1)
ibig = _div(T, i-1) + 1
ismall = _mod(T, i-1)
ismallm = lowbit << ismall
return ibig, ismall, ismallm
end

function apply!(frame::PauliFrame, op::sMZ) # TODO sMY, and faster sMX
op.bit == 0 && return frame
i = op.qubit
xzs = frame.frame.tab.xzs
T = eltype(xzs)
lowbit = T(1)
ibig = _div(T,i-1)+1
ismall = _mod(T,i-1)
ismallm = lowbit<<(ismall)
ibig, ismall, ismallm = _get_bitmask_idxs(frame,i)

@inbounds @simd for f in eachindex(frame)
should_flip = !iszero(xzs[ibig,f] & ismallm)
should_flip = !iszero(frame.frame.tab.xzs[ibig,f] & ismallm)
frame.measurements[f,op.bit] = should_flip
end

Expand All @@ -99,35 +103,25 @@ end

function apply!(frame::PauliFrame, op::sMRZ) # TODO sMRY, and faster sMRX
i = op.qubit
xzs = frame.frame.tab.xzs
T = eltype(xzs)
lowbit = T(1)
ibig = _div(T,i-1)+1
ismall = _mod(T,i-1)
ismallm = lowbit<<(ismall)
ibig, ismall, ismallm = _get_bitmask_idxs(frame,i)

if op.bit != 0
@inbounds @simd for f in eachindex(frame)
should_flip = !iszero(xzs[ibig,f] & ismallm)
should_flip = !iszero(frame.frame.tab.xzs[ibig,f] & ismallm)
frame.measurements[f,op.bit] = should_flip
end
end
@inbounds @simd for f in eachindex(frame)
xzs[ibig,f] &= ~ismallm
rand(Bool) && (xzs[end÷2+ibig,f] ⊻= ismallm)
frame.frame.tab.xzs[ibig,f] &= ~ismallm
rand(Bool) && (frame.frame.tab.xzs[end÷2+ibig,f] ⊻= ismallm)
end

return frame
end

function applynoise!(frame::PauliFrame,noise::UnbiasedUncorrelatedNoise,i::Int)
p = noise.p
T = eltype(frame.frame.tab.xzs)

lowbit = T(1)
ibig = _div(T,i-1)+1
ismall = _mod(T,i-1)
ismallm = lowbit<<(ismall)
ibig, ismall, ismallm = _get_bitmask_idxs(frame,i)
p = p/3

@inbounds @simd for f in eachindex(frame)
Expand All @@ -145,12 +139,7 @@ function applynoise!(frame::PauliFrame,noise::UnbiasedUncorrelatedNoise,i::Int)
end

function applynoise!(frame::PauliFrame,noise::PauliNoise,i::Int)
T = eltype(frame.frame.tab.xzs)

lowbit = T(1)
ibig = _div(T,i-1)+1
ismall = _mod(T,i-1)
ismallm = lowbit<<(ismall)
ibig, ismall, ismallm = _get_bitmask_idxs(frame,i)

@inbounds @simd for f in eachindex(frame)
r = rand()
Expand Down

0 comments on commit 7d0db71

Please sign in to comment.