Fixtures¶
The qtest.fixtures subpackage provides ready-to-use pytest
fixtures for common quantum circuits, plus plain factory functions
that can be called outside of pytest.
To make the fixtures discoverable in your suite, list the relevant
plugin modules under pytest_plugins in your top-level
conftest.py:
pytest_plugins = [
"qtest.fixtures.common_states",
"qtest.fixtures.common_gates",
]
State-preparation fixtures¶
Yield a fresh Bell-state preparation circuit per test. |
|
Yield a fresh \(|+\rangle\) preparation circuit per test. |
|
Yield a fresh \(|-\rangle\) preparation circuit per test. |
|
Factory fixture yielding |
|
Shortcut for a 3-qubit GHZ preparation circuit. |
|
Shortcut for a 4-qubit GHZ preparation circuit. |
|
Shortcut for a 5-qubit GHZ preparation circuit. |
|
Factory fixture yielding |
Pytest fixtures (and plain factory functions) for common quantum states.
Each fixture / factory returns a qiskit.QuantumCircuit that prepares
the named state starting from \(|0\rangle^{\otimes n}\). Circuits are
unitary-only (no measurement / reset); add qc.measure_all() in your test
when sampling.
The same builders are exposed twice:
As pytest fixtures (
bell_state,ghz_state,plus_state,ghz_3…) — request them in test signatures.As plain functions (
bell_circuit,ghz_circuit(n),plus_circuit…) — call them directly outside of pytest tests.
For pytest discovery, list this module in the consumer’s
pytest_plugins or import individual fixtures into your conftest.py:
# tests/conftest.py
pytest_plugins = ["qtest.fixtures.common_states"]
Examples
>>> # In a pytest test:
>>> def test_bell(bell_state, qiskit_backend):
... assert_state_close(bell_state, "bell")
>>> # As a plain function:
>>> from qtest.fixtures.common_states import bell_circuit
>>> qc = bell_circuit()
- qtest.fixtures.common_states.bell_circuit()[source]¶
Return a circuit preparing the Bell state \((|00\rangle + |11\rangle)/\sqrt{2}\).
- Return type:
- qtest.fixtures.common_states.plus_circuit()[source]¶
Return a 1-qubit circuit preparing \(|+\rangle = (|0\rangle + |1\rangle)/\sqrt{2}\).
- Return type:
- qtest.fixtures.common_states.minus_circuit()[source]¶
Return a 1-qubit circuit preparing \(|-\rangle = (|0\rangle - |1\rangle)/\sqrt{2}\).
- Return type:
- qtest.fixtures.common_states.ghz_circuit(n)[source]¶
Return an n-qubit GHZ-state preparation circuit.
Builds \((|0\rangle^{\otimes n} + |1\rangle^{\otimes n}) / \sqrt{2}\) via the standard
H + (n-1) × CXrecipe.
- qtest.fixtures.common_states.w_circuit(n)[source]¶
Return an n-qubit W-state preparation circuit.
For
n == 1returnsX|0> = |1>; forn == 2uses the closed-formH + CX + Xrecipe; forn >= 3it appends a unitaryqiskit.circuit.library.StatePreparationblock with the target amplitudes \((|10\dots0\rangle + |01\dots0\rangle + \dots) / \sqrt{n}\).
- qtest.fixtures.common_states.bell_state()[source]¶
Yield a fresh Bell-state preparation circuit per test.
Example:
def test_bell(bell_state): assert_state_close(bell_state, "bell")
- Return type:
- qtest.fixtures.common_states.plus_state()[source]¶
Yield a fresh \(|+\rangle\) preparation circuit per test.
- Return type:
- qtest.fixtures.common_states.minus_state()[source]¶
Yield a fresh \(|-\rangle\) preparation circuit per test.
- Return type:
- qtest.fixtures.common_states.ghz_state()[source]¶
Factory fixture yielding
n -> QuantumCircuitfor GHZ-n states.Example:
def test_ghz_5(ghz_state): assert_state_close(ghz_state(5), "ghz_5")
- qtest.fixtures.common_states.w_state()[source]¶
Factory fixture yielding
n -> QuantumCircuitfor W-n states.Example:
def test_w_4(w_state): assert_state_close(w_state(4), "w_4")
- qtest.fixtures.common_states.ghz_3()[source]¶
Shortcut for a 3-qubit GHZ preparation circuit.
- Return type:
Gate-layer fixtures¶
Factory fixture for n-qubit Hadamard-layer circuits. |
|
Factory fixture for random Clifford circuits. |
Pytest fixtures (and plain factory functions) for common gate-sequence circuits.
Provides:
hadamard_circuit(n)— n-qubit Hadamard layer (oneHper qubit).random_clifford_circuit(n, depth, seed)— depth-controlled random Clifford built from the gate set{H, S, S†, X, Y, Z}plus occasionalCXforn >= 2.
Examples
>>> # In a pytest test:
>>> def test_uniform(hadamard_circuit):
... assert_distribution_close(
... hadamard_circuit(3), expected={"000": 1/8, ..., "111": 1/8}
... )
>>> # As plain functions:
>>> from qtest.fixtures.common_gates import random_clifford_circuit
>>> qc = random_clifford_circuit(n=2, depth=20, seed=42)
- qtest.fixtures.common_gates.hadamards(n=1)[source]¶
Return an n-qubit circuit that applies \(H\) to every qubit.
The resulting state from \(|0\rangle^{\otimes n}\) is the uniform superposition \(H^{\otimes n}|0\rangle^{\otimes n}\).
- qtest.fixtures.common_gates.random_clifford_circuit(n=1, depth=10, seed=None)[source]¶
Return a random Clifford circuit on n qubits with depth gates.
Each “layer” is a single gate: with probability ~0.3 (when
n >= 2) a randomCX, otherwise a random gate drawn from{H, S, S†, X, Y, Z}on a random qubit. Reproducible whenseedis set.
Plain factories (no pytest required)¶
The state and gate builders are also exposed as plain factory
functions via the qtest.fixtures package — usable in scripts,
notebooks, or any non-pytest context:
from qtest.fixtures import bell_circuit, ghz_circuit
qc = bell_circuit()
ghz5 = ghz_circuit(5)
Built-in pytest fixtures (and plain factories) for common quantum circuits.
To use the fixtures in your tests, add the relevant module(s) to
pytest_plugins in your top-level conftest.py:
# tests/conftest.py
pytest_plugins = [
"qtest.fixtures.common_states",
"qtest.fixtures.common_gates",
]
The same builders are also exposed as plain functions here, usable outside of pytest:
from qtest.fixtures import bell_circuit, ghz_circuit
qc = bell_circuit()
ghz5 = ghz_circuit(5)
Imports are lazy (PEP 562 __getattr__): nothing is loaded until the
attribute is actually requested. This lets the pytest plugin loader register
common_states and common_gates as plugins independently, without one
being imported as a side effect of the other (which would suppress assert
rewriting in the second).