Skip to content

Commit

Permalink
Improve performance of random_pauli! by 100x (#454)
Browse files Browse the repository at this point in the history
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 <github.acc@krastanov.org>
  • Loading branch information
jlapeyre and Krastanov authored Dec 29, 2024
1 parent 6065fab commit 3297023
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@

# News

## v0.9.16 - dev
## v0.9.16 - 2024-12-29

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

## v0.9.15 - 2024-12-22
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "QuantumClifford"
uuid = "0525e862-1e90-11e9-3e4d-1b39d7109de1"
authors = ["Stefan Krastanov <stefan@krastanov.org> and QuantumSavory community members"]
version = "0.9.15"
version = "0.9.16"

[deps]
Combinatorics = "861a8166-3701-5b0c-9a16-15d98fcdc6aa"
Expand Down
23 changes: 18 additions & 5 deletions src/randoms.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Random: randperm, AbstractRNG, GLOBAL_RNG
using Random: randperm, AbstractRNG, GLOBAL_RNG, rand!
using ILog2
import Nemo

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


# Modified from `Base` for `BitArray`
@inline _msk_end(::Type{T}, l::Int) where {T<:Unsigned} = ~T(0) >>> _mod(T, -l)

# A mask for the non-coding bits in the last chunk.
@inline _msk_end(P::PauliOperator) = _msk_end(eltype(P.xz), length(P))

# Unset the leftover bits in the data array that don't code the Pauli operator.
@inline function _unset_noncoding_bits!(P::PauliOperator)
msk = _msk_end(P)
P.xz[end] &= msk
P.xz[end÷2] &= msk
nothing
end

function random_pauli!(rng::AbstractRNG, P::PauliOperator; nophase=true, realphase=true)
n = nqubits(P)
for i in 1:n
P[i] = rand(rng, (true, false)), rand(rng, (true,false))
end
rand!(rng, P.xz)
_unset_noncoding_bits!(P)
P.phase[] = nophase ? 0x0 : (realphase ? rand(rng,(0x0,0x2)) : rand(rng,0x0:0x3))
P
end
Expand Down

2 comments on commit 3297023

@Krastanov
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/122123

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.9.16 -m "<description of version>" 329702394c9cdf9d9a6bd79f40ed843700b39bd0
git push origin v0.9.16

Please sign in to comment.