liveBook · Manning

Test

As explained in Quantum Programming in Depth

A test case is a specified input scenario and expected result used to evaluate software behavior.

A test case specifies an input scenario and expected result for evaluating software behavior. In quantum programming, automated tests can run circuits on a simulator and compare measured or internal states with expectations. section 2.4

Effective test cases expose distinct properties of the problem rather than checking only an easy success case. State-preparation tests should include varied amplitudes, negative values, zero values, basis states, and cases that reveal qubit-ordering errors. section 2.4

A quantum test case can validate behavior by:

  • Preparing a chosen basis state and measuring it repeatedly; deterministic behavior should produce one result for every shot. section 4.1.2
  • Converting between integer basis-state values and bit strings while keeping little-endian and big-endian conventions consistent. section 4.1.2
  • Running the same validation over randomly selected qubit counts and basis states to broaden coverage. section 4.1.2
  • Comparing reconstructed state coefficients with the actual coefficients using a classical closeness check. section 4.5.2

Testing a basis-state readout

A Qiskit helper accepts a qubit count and integer basis state, applies X gates for set bits, measures 100 shots, asserts that the result map has one key, and checks that the decoded bit string equals the input. section 4.1.2

Listing 4.2 Qiskit code validating the information readout
from random import randint
from qiskit import QuantumCircuit, transpile
from qiskit_aer import AerSimulator
from .read_info import read_info

sim = AerSimulator(method='statevector')

def run_test_read_info(n, basis_state):
  circ = QuantumCircuit(n, n)
  for i in range(n):         #1
    if basis_state & (1 {\fontsize{9}{0}\selectfont<\hskip-1pt<} i) > 0:   #2
      circ.x(i)

  circ.append(read_info(n), range(n), range(n))  #3

  circ = transpile(circ, backend=simulator)  #4
  res_map = simulator.run(circ, shots=100).result().get_counts()

  assert len(res_map) == 1  #5

  res_bitstring = list(res_map.keys())[0]
  res = int(res_bitstring, 2)          #6
  assert res == basis_state

def test_read_info():
  for _ in range(1, 20):  #7
    n = randint(1, 5)
    num = randint(0, 2 ** n - 1)
    run_test_read_info(n, num)

Inspect the complete Qiskit test workflow, including basis-state preparation, simulation, shot aggregation, result decoding, assertions, and randomized repetition. section 4.1.2

How Quantum Programming in Depth covers it

How deep it goes
Derives test selection from state properties and corner cases, then implements repeated simulations, decoding, assertions, and coefficient checks.
Examples
Uses distinct-amplitude states, negative or zero amplitudes, basis states, randomized basis-state readouts, and reconstructed coefficient estimates.
What it assumes
Assumes basic quantum states, amplitudes, measurement, simulator use, and Qiskit or Q# implementation skills.
What you can do after
Diagnose/debug quantum code by selecting revealing inputs, running simulator shots, decoding results, and asserting expected states or coefficients.
How it connects
Connects unit testing with quantum simulation, state preparation, measurement, basis states, amplitudes, and endianness.

Also explained in

Compare how 53 books explain test →

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.