Skip to content

Commit

Permalink
minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Krastanov committed Nov 2, 2024
1 parent 6c4c659 commit d4936d3
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 20 deletions.
2 changes: 1 addition & 1 deletion ext/QuantumCliffordGPUExt/apply_noise.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ using QuantumClifford: get_bitmask_idxs
#according to https://github.com/JuliaGPU/CUDA.jl/blob/ac1bc29a118e7be56d9edb084a4dea4224c1d707/test/core/device/random.jl#L33
#CUDA.jl supports calling rand() inside kernel
function applynoise!(frame::PauliFrameGPU{T},noise::UnbiasedUncorrelatedNoise,i::Int) where {T <: Unsigned}
xzs = frame.frame.tab.xzs
p = noise.p
xzs = tab(frame.frame).xzs
lowbit, ibig, ismall, ismallm = get_bitmask_idxs(xzs,i)

Check warning on line 8 in ext/QuantumCliffordGPUExt/apply_noise.jl

View check run for this annotation

Codecov / codecov/patch

ext/QuantumCliffordGPUExt/apply_noise.jl#L7-L8

Added lines #L7 - L8 were not covered by tests
rows = size(stab, 1)

Expand Down
8 changes: 2 additions & 6 deletions src/QuantumClifford.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export
nqubits,
stabilizerview, destabilizerview, logicalxview, logicalzview, phases,
fastcolumn, fastrow,
bitview, quantumstate, tab, rank, get_bitmask_idxs,
bitview, quantumstate, tab, rank,
BadDataStructure,
affectedqubits, #TODO move to QuantumInterface?
# GF2
Expand Down Expand Up @@ -926,12 +926,8 @@ the following values based on the index `i`:
- `ibig`, the index of the word containing the bit.
- `ismall`, the position of the bit within the word.
- `ismallm`, a bitmask isolating the specified bit.
Internal bitwise operations `_div`, `_mod`, and `_mask` are
used to compute the word and bit positions based on the bit
size of the type `T`.
"""
function get_bitmask_idxs(xzs::AbstractArray{<:Unsigned}, i::Int)
@inline function get_bitmask_idxs(xzs::AbstractArray{<:Unsigned}, i::Int)
T = eltype(xzs)
lowbit = T(1)
ibig = _div(T, i-1) + 1
Expand Down
24 changes: 12 additions & 12 deletions src/pauli_frames.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ 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
xzs = tab(frame.frame).xzs
lowbit, ibig, ismall, ismallm = get_bitmask_idxs(xzs,i)

@inbounds @simd for f in eachindex(frame)
Expand All @@ -95,7 +95,7 @@ end

function apply!(frame::PauliFrame, op::sMRZ) # TODO sMRY, and faster sMRX
i = op.qubit
xzs = frame.frame.tab.xzs
xzs = tab(frame.frame).xzs
lowbit, ibig, ismall, ismallm = get_bitmask_idxs(xzs,i)

if op.bit != 0
Expand All @@ -114,39 +114,39 @@ end

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

lowbit, ibig, ismall, ismallm = get_bitmask_idxs(xzs,i)
p = p/3

@inbounds @simd for f in eachindex(frame)
r = rand()
if r < p # X error
frame.frame.tab.xzs[ibig,f] ⊻= ismallm
xzs[ibig,f] ⊻= ismallm
elseif r < 2p # Z error
frame.frame.tab.xzs[end÷2+ibig,f] ⊻= ismallm
xzs[end÷2+ibig,f] ⊻= ismallm
elseif r < 3p # Y error
frame.frame.tab.xzs[ibig,f] ⊻= ismallm
frame.frame.tab.xzs[end÷2+ibig,f] ⊻= ismallm
xzs[ibig,f] ⊻= ismallm
xzs[end÷2+ibig,f] ⊻= ismallm
end
end
return frame
end

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

Check warning on line 137 in src/pauli_frames.jl

View check run for this annotation

Codecov / codecov/patch

src/pauli_frames.jl#L137

Added line #L137 was not covered by tests

lowbit, ibig, ismall, ismallm = get_bitmask_idxs(xzs,i)

Check warning on line 139 in src/pauli_frames.jl

View check run for this annotation

Codecov / codecov/patch

src/pauli_frames.jl#L139

Added line #L139 was not covered by tests

@inbounds @simd for f in eachindex(frame)
r = rand()
if r < noise.px # X error
frame.frame.tab.xzs[ibig,f] ⊻= ismallm
xzs[ibig,f] ⊻= ismallm

Check warning on line 144 in src/pauli_frames.jl

View check run for this annotation

Codecov / codecov/patch

src/pauli_frames.jl#L144

Added line #L144 was not covered by tests
elseif r < noise.px+noise.pz # Z error
frame.frame.tab.xzs[end÷2+ibig,f] ⊻= ismallm
xzs[end÷2+ibig,f] ⊻= ismallm

Check warning on line 146 in src/pauli_frames.jl

View check run for this annotation

Codecov / codecov/patch

src/pauli_frames.jl#L146

Added line #L146 was not covered by tests
elseif r < noise.px+noise.pz+noise.py # Y error
frame.frame.tab.xzs[ibig,f] ⊻= ismallm
frame.frame.tab.xzs[end÷2+ibig,f] ⊻= ismallm
xzs[ibig,f] ⊻= ismallm
xzs[end÷2+ibig,f] ⊻= ismallm

Check warning on line 149 in src/pauli_frames.jl

View check run for this annotation

Codecov / codecov/patch

src/pauli_frames.jl#L148-L149

Added lines #L148 - L149 were not covered by tests
end
end
return frame
Expand Down
2 changes: 1 addition & 1 deletion src/pauli_operator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ 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ᵥₑ}} = begin
function Base.getindex(p::PauliOperator{Tₚ,Tᵥ}, i::Int) where {Tₚ, Tᵥₑ<:Unsigned, Tᵥ<:AbstractVector{Tᵥₑ}}
_, ibig, _, ismallm = get_bitmask_idxs(p.xz,i)
((p.xz[ibig] & ismallm) != 0x0)::Bool, ((p.xz[end÷2+ibig] & ismallm) != 0x0)::Bool
end
Expand Down

0 comments on commit d4936d3

Please sign in to comment.