Skip to content

Commit 3297023

Browse files
jlapeyreKrastanov
andauthored
Improve performance of random_pauli! by 100x (#454)
This commit improves the performance of the unbiased branch of `random_pauli!` by a factor of about 100. Each chunk in the data array is replaced with a random `UInt64`. --------- Co-authored-by: Stefan Krastanov <[email protected]>
1 parent 6065fab commit 3297023

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55

66
# News
77

8-
## v0.9.16 - dev
8+
## v0.9.16 - 2024-12-29
99

10+
- 100× faster unbiased `random_pauli`.
1011
- Enhancements to `GF(2)` Linear Algebra: unexported, experimental `gf2_row_echelon_with_pivots!`, `gf2_nullspace`, `gf2_rowspace_basis`.
1112

1213
## v0.9.15 - 2024-12-22

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "QuantumClifford"
22
uuid = "0525e862-1e90-11e9-3e4d-1b39d7109de1"
33
authors = ["Stefan Krastanov <[email protected]> and QuantumSavory community members"]
4-
version = "0.9.15"
4+
version = "0.9.16"
55

66
[deps]
77
Combinatorics = "861a8166-3701-5b0c-9a16-15d98fcdc6aa"

src/randoms.jl

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Random: randperm, AbstractRNG, GLOBAL_RNG
1+
using Random: randperm, AbstractRNG, GLOBAL_RNG, rand!
22
using ILog2
33
import Nemo
44

@@ -21,11 +21,24 @@ function random_pauli end
2121
"""An in-place version of [`random_pauli`](@ref)"""
2222
function random_pauli! end
2323

24+
25+
# Modified from `Base` for `BitArray`
26+
@inline _msk_end(::Type{T}, l::Int) where {T<:Unsigned} = ~T(0) >>> _mod(T, -l)
27+
28+
# A mask for the non-coding bits in the last chunk.
29+
@inline _msk_end(P::PauliOperator) = _msk_end(eltype(P.xz), length(P))
30+
31+
# Unset the leftover bits in the data array that don't code the Pauli operator.
32+
@inline function _unset_noncoding_bits!(P::PauliOperator)
33+
msk = _msk_end(P)
34+
P.xz[end] &= msk
35+
P.xz[end÷2] &= msk
36+
nothing
37+
end
38+
2439
function random_pauli!(rng::AbstractRNG, P::PauliOperator; nophase=true, realphase=true)
25-
n = nqubits(P)
26-
for i in 1:n
27-
P[i] = rand(rng, (true, false)), rand(rng, (true,false))
28-
end
40+
rand!(rng, P.xz)
41+
_unset_noncoding_bits!(P)
2942
P.phase[] = nophase ? 0x0 : (realphase ? rand(rng,(0x0,0x2)) : rand(rng,0x0:0x3))
3043
P
3144
end

0 commit comments

Comments
 (0)