Skip to content

perf: vectorize generalized Pauli-Z construction#1893

Merged
vprusso merged 1 commit into
vprusso:masterfrom
happykawayigt:perf/vectorize-gen-pauli-z
Jul 25, 2026
Merged

perf: vectorize generalized Pauli-Z construction#1893
vprusso merged 1 commit into
vprusso:masterfrom
happykawayigt:perf/vectorize-gen-pauli-z

Conversation

@happykawayigt

Copy link
Copy Markdown
Contributor

Description

Vectorizes the roots-of-unity construction in gen_pauli_z with NumPy operations while preserving the function's signature and bit-for-bit output. Addresses #1887.

Changes

  • Replace the Python generator loop with np.exp(np.arange(dim) * c_var).
  • Preserve existing behavior and exact output.

Benchmark

Measured on Windows, Python 3.12, NumPy 2.5.1:

Dimension Iterations Before After Speedup
64 10,000 23.26 us 13.34 us 1.74x
256 3,000 311.78 us 243.97 us 1.28x
1024 300 2391.03 us 2233.95 us 1.07x

Validation

  • python -m pytest toqito/matrices/tests/test_gen_pauli_z.py -q (19 passed)
  • python -m ruff check toqito/matrices/gen_pauli_z.py
  • python -m ruff format --check toqito/matrices/gen_pauli_z.py
  • exact equality against the previous implementation for dimensions 1, 2, 3, 4, 5, 8, 16, 31, 64, and 256

Checklist

  • Use ruff for errors related to code style and formatting.
  • Verify all relevant previous unit tests pass in pytest.
  • Check the documentation build does not lead to failures (not run; this code-only change does not affect documentation).

@codecov

codecov Bot commented Jul 25, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@happykawayigt
happykawayigt marked this pull request as ready for review July 25, 2026 17:03
@vprusso

vprusso commented Jul 25, 2026

Copy link
Copy Markdown
Owner

Thanks for this, and welcome to toqito! I verified the vectorized output is bit-for-bit identical to the previous implementation across a wide range of dimensions, and the existing tests pass. Clean change, and good call dropping the now-unused cmath import. Merging.

@vprusso
vprusso merged commit 3c100bd into vprusso:master Jul 25, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants