From d4936d32a8799eed715881babbcd38f9c611a748 Mon Sep 17 00:00:00 2001 From: Stefan Krastanov Date: Sat, 2 Nov 2024 09:54:01 -0400 Subject: [PATCH] minor cleanup --- ext/QuantumCliffordGPUExt/apply_noise.jl | 2 +- src/QuantumClifford.jl | 8 ++------ src/pauli_frames.jl | 24 ++++++++++++------------ src/pauli_operator.jl | 2 +- 4 files changed, 16 insertions(+), 20 deletions(-) diff --git a/ext/QuantumCliffordGPUExt/apply_noise.jl b/ext/QuantumCliffordGPUExt/apply_noise.jl index 277ead68a..201eb7c60 100644 --- a/ext/QuantumCliffordGPUExt/apply_noise.jl +++ b/ext/QuantumCliffordGPUExt/apply_noise.jl @@ -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) rows = size(stab, 1) diff --git a/src/QuantumClifford.jl b/src/QuantumClifford.jl index 8a3555107..6b4945b6b 100644 --- a/src/QuantumClifford.jl +++ b/src/QuantumClifford.jl @@ -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 @@ -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 diff --git a/src/pauli_frames.jl b/src/pauli_frames.jl index d5f037da5..af84bd63b 100644 --- a/src/pauli_frames.jl +++ b/src/pauli_frames.jl @@ -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) @@ -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 @@ -114,7 +114,7 @@ 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 @@ -122,31 +122,31 @@ function applynoise!(frame::PauliFrame,noise::UnbiasedUncorrelatedNoise,i::Int) @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 lowbit, ibig, ismall, ismallm = get_bitmask_idxs(xzs,i) @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 elseif r < noise.px+noise.pz # Z error - frame.frame.tab.xzs[end÷2+ibig,f] ⊻= ismallm + xzs[end÷2+ibig,f] ⊻= ismallm 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 end end return frame diff --git a/src/pauli_operator.jl b/src/pauli_operator.jl index ed0e89ec8..2246d4d5f 100644 --- a/src/pauli_operator.jl +++ b/src/pauli_operator.jl @@ -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