liveBook · Manning

Pytest

As explained in Quantum Programming in Depth

pytest is a Python testing framework for discovering, organizing, and running automated tests.

pytest is a Python testing framework for discovering, organizing, and running automated tests. It finds functions beginning with test_ in files named test_*.py or *_test.py, keeping tests separate from library logic. section 2.4.1

Quantum programs can expose simulator state as ordinary Python data, allowing pytest assertions to compare computed amplitudes with expected values. Q# tests divide the test harness between Python and Q# project files. section 2.4.1 section 2.4.2

A typical quantum-state test workflow:

  • Decorate a test with pytest.mark.parametrize to run it against several amplitude lists. section 2.4.1 section 2.4.2
  • Run the Qiskit circuit on an AerSimulator statevector backend, retrieve its state vector, and compare it with pytest.approx. section 2.4.1
  • For Q#, initialize the project with qsharp.init, evaluate the operation with qsharp.eval, retrieve the dense state, and assert its amplitudes. section 2.4.2
  • Validate deterministic measurement by asserting one result key and converting its bit string to an integer. section 4.1.2

Testing two-qubit state preparation

A parametrized test prepares several two-qubit states, simulates the circuit, extracts the state vector, and checks state_vector == pytest.approx(a). section 2.4.1

Listing 2.7 Qiskit code to test preparation of an arbitrary state
from math import sqrt
from qiskit import transpile
from qiskit_aer import AerSimulator
import pytest
from .prep_two_qubit import prep_two_qubit

simulator = AerSimulator(method='statevector')

@pytest.mark.parametrize("a",
    [ [1., 0., 0., 0.],     #1
      [0., 1., 0., 0.],
      [0.36, 0.48, 0.64, -0.48],
      [1. / sqrt(3.), -1. / sqrt(3.), 1. / sqrt(3.), 0.]
    ])
def test_prep_two_qubit(a):
  circ = prep_two_qubit(a)
  circ.save_statevector()

  circ = transpile(circ, backend=simulator)
  res = simulator.run(circ).result()
  state_vector = res.get_statevector().data

  assert state_vector == pytest.approx(a)    #2

Inspect a complete Qiskit pytest listing using AerSimulator, parametrization, state-vector extraction, and approximate comparison. section 2.4.1

Listing 2.8 Python code to test Q# preparation of a two-qubit state
from math import sqrt
import qsharp
import pytest

@pytest.mark.parametrize("a",
    [ [1., 0., 0., 0.],
      [0., 1., 0., 0.],   #1
      [0.36, 0.48, 0.64, -0.48],
      [1. / sqrt(3.), -1. / sqrt(3.), 1. / sqrt(3.), 0.]
    ])
def test_prep_two_qubit(a):
  qsharp.init(project_root='.')
  qsharp.eval(f"use qs = Qubit[2]; StatePreparation.PrepTwoQubits(qs, {a});")
  state_vector = qsharp.dump_machine().as_dense_state()

  assert state_vector == pytest.approx(a)   #2

Inspect the corresponding Q# test harness, which loads the project, evaluates Q# code, dumps the simulated state, and validates amplitudes. section 2.4.2

How Quantum Programming in Depth covers it

How deep it goes
Explains discovery, parametrization, simulator integration, state extraction, and assertions, but does not detail pytest internals.
Examples
Uses parametrized two-qubit state-preparation tests and randomized basis-state measurement tests.
What it assumes
Assumes Python testing familiarity and quantum-library code, while demonstrating project layout and simulator-based assertions.
What you can do after
Apply it via pytest to validate quantum amplitudes, Q# simulator states, and deterministic measurement results.
How it connects
Connects Python testing with Qiskit, Q#, AerSimulator, state vectors, parametrization, and deterministic measurement validation.

Also explained in

Compare how 11 books explain pytest →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Quantum Programming in Depth. The section links point to the parts of the book it draws on.