pytest-quantum¶
The only pytest plugin built specifically for quantum software. 80+ assertions. 10 frameworks. Zero boilerplate. Ship quantum code you trust.
Who is this for?¶
Quantum software engineers writing gate-model circuits in Qiskit, Cirq, PennyLane, Pytket, or Braket who want rigorous, reproducible unit tests.
Researchers running VQE or QAOA who need statistically sound assertions on variational algorithm convergence, expressibility, and barren plateaus.
Teams CI-testing quantum algorithms across multiple frameworks and backends, including noisy simulation and real hardware targets.
Hardware validation engineers characterising real devices with quantum volume, randomised benchmarking, T1/T2/T2* coherence, and XEB fidelity.
The problem with quantum testing¶
# ❌ The old way: brittle, flaky, wrong
def test_bell_state():
counts = run_bell_circuit(shots=1024)
assert counts["00"] == 512 # fails 50% of the time due to shot noise
assert counts["11"] == 512 # still wrong — global phase?
# ✅ The pytest-quantum way
from pytest_quantum import assert_measurement_distribution, assert_unitary
def test_bell_distribution(aer_simulator):
counts = run_bell_circuit(aer_simulator, shots=2000)
assert_measurement_distribution(counts, {"00": 0.5, "11": 0.5})
# Chi-square tested. Won't flake. ✓
def test_hadamard():
import numpy as np
assert_unitary(h_circuit, np.array([[1,1],[1,-1]])/np.sqrt(2))
# Global-phase safe. Works on Qiskit, Cirq, Braket, Pytket. ✓
One line install. Fixtures auto-injected. Tests just work.
Install in 10 seconds¶
pip install pytest-quantum # core
pip install "pytest-quantum[qiskit]" # + Qiskit + Aer ← most common
pip install "pytest-quantum[all]" # everything
Then just write tests. No conftest.py. No imports. Fixtures appear by magic.
What’s in the box¶
Category |
Count |
Examples |
|---|---|---|
Unitary & equivalence |
6 |
|
States & fidelity |
3 |
|
Measurement distributions |
2 |
|
Density matrices |
4 |
|
Quantum channels |
6 |
|
Noise models |
6 |
|
Entanglement |
3 |
|
Observables & VQE |
4 |
|
Circuit structure |
7 |
|
Benchmarking |
7 |
|
Quantum ML |
4 |
|
Error mitigation |
7 |
|
Hardware |
5 |
|
QEC / Stim |
3 |
|
Sweeps |
3 |
|
QASM round-trips |
2 |
|
Snapshots |
2 |
|
Information theory |
3 |
|
Total |
80+ |
Framework support¶
Works everywhere quantum code runs:
Framework |
Install |
Fixtures |
|---|---|---|
Qiskit + Aer |
|
|
Cirq |
|
|
Amazon Braket |
|
|
PennyLane |
|
|
Pytket |
|
|
Stim (QEC) |
|
|
Mitiq |
|
works with any backend |
IBM Quantum (real HW) |
|
|
IonQ (real HW) |
|
|
Quantinuum (real HW) |
|
|
Documentation¶
Start here
- Getting Started
- Concepts
- Why shot noise is a first-class problem
- Why global phase is not a bug
- How fidelity is measured
- Statistical distance between distributions
- Quantum Volume
- Randomised Benchmarking
- Cross-Entropy Benchmarking (XEB)
- T1, T2, and T2* Coherence Times
- Expressibility and Barren Plateaus
- Diamond Norm
- Why fixtures instead of setup code
Reference
- Assertions Reference
- Quick decision guide
- Unitary & Circuit Equivalence
- State Assertions
- Measurement Distributions
- Density Matrix Assertions
- Quantum Channel Assertions
- Noise Channel Assertions
- Observable & Expectation Value
- Circuit Structure
- Transpilation & Compilation
- Sweeps / Parametrised Circuits
- Entanglement
- Information Theory
- Snapshots / Golden Files
- QASM Round-Trips
- QEC / Stim
- Benchmarking (v1.0.0)
- Quantum ML (v1.0.0)
- Hardware Assertions (v1.0.0)
- Error Mitigation (Mitiq) (v1.0.0)
- Primitives (Qiskit 1.0+)
- Fixtures Reference
- Statistics Utilities Reference
- API Reference
Tutorials
- Tutorial: Testing a VQE Ansatz End-to-End
- 0. Installation
- 1. The Problem: Testing a VQE Circuit for H2
- Step 1: Test the Ansatz Circuit Structure
- Step 2: Test the Ansatz Unitary for Specific Parameter Values
- Step 3: Parameter Sweep — Verify Unitary Across All Theta Values
- Step 4: Test Expectation Value with the Ideal Simulator
- Step 5: Test VQE Convergence
- Step 6: Error Mitigation — Apply ZNE and Assert It Improves
- Step 7: Real Hardware Check (Skip Unless –quantum-real)
- Full Working Test File
- Common Issues and Troubleshooting
- Tutorial: Noise-Aware Testing with Qiskit Aer and Mitiq
- 1. Why Noise Testing Matters
- 2. The
aer_noise_simulatorFixture - 3. Testing That Your Circuit Degrades Gracefully with Noise
- 4.
assert_counts_closevsassert_real_counts_close - 5. Testing ZNE Actually Helps:
assert_zne_reduces_error - 6. Testing CDR:
assert_cdr_reduces_error - 7. Snapshot Testing Noisy Distributions
- 8. Complete Working Test File
- Common Issues and Troubleshooting
- Tutorial: Running Tests on Real IBM Quantum Hardware
- 1. Setup: Get an IBM Quantum Account and API Key
- 2. Setting Environment Variables
- 3. The
ibm_backendFixture - 4.
assert_backend_calibration: Gate Your Tests on Device Quality - 5.
assert_circuit_fits_backend: Check Your Circuit Compiles - 6.
assert_real_counts_close: Verify Quantum Behavior on Hardware - 7.
assert_mirror_fidelity: Measure Actual Gate Fidelity - 8. Using the
--quantum-realFlag and CI Setup - 9. Dealing with Queue Times
- 10. Cost and Time Management with the Open Plan
- 11. Complete Working Test File
- Common Issues and Troubleshooting
Project
Quick links¶
🚀 Getting Started: Zero to passing tests in 5 minutes
📖 Assertions Reference: All 80+ assertions with examples
🔧 Fixtures Reference: Every fixture documented
🧪 Cookbook: Copy-paste recipes for common patterns
💡 Concepts: Why shot noise matters, global phase, and more
🏆 Tutorials: End-to-end: VQE, noise, real hardware
Built with ❤️ for the quantum software community. MIT License · GitHub · PyPI