Readout Error Mitigation on a 4-Qubit GHZ State, Step by Step
In Measurement Error Mitigation Without a Physics Degree I explained the idea: measure how often the hardware misreads each qubit, build a matrix from those rates, and invert it. This post is the follow-up I wanted to read back then — one complete experiment, start to finish, with real numbers: a 4-qubit GHZ state, its raw counts, a calibration, and the corrected result.
Everything here runs on a laptop with Qiskit Runtime's local mode and FakeFez, a simulator that carries a snapshot of the ibm_fez noise model. The numbers are from that model, not from the live machine — but the code is the same one you would send to the real device.
The experiment
A GHZ state on four qubits is (|0000⟩ + |1111⟩)/√2. Ideally you only ever read 0000 or 1111, so the fraction of shots landing on those two outcomes is a handy quality score. Anything else is noise — from the gates, from decoherence, or from the readout itself. Readout mitigation can only fix the last part, which makes it a good test of how much of the error is really "just reading it wrong".
from qiskit import QuantumCircuit
from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager
from qiskit_ibm_runtime import SamplerV2 as Sampler
from qiskit_ibm_runtime.fake_provider import FakeFez
backend = FakeFez()
n, shots = 4, 20000
ghz = QuantumCircuit(n, n)
ghz.h(0)
for i in range(n - 1):
ghz.cx(i, i + 1)
ghz.measure(range(n), range(n))
pm = generate_preset_pass_manager(backend=backend, optimization_level=1, seed_transpiler=7)
isa_ghz = pm.run(ghz)
layout = isa_ghz.layout.final_index_layout() # which physical qubits were used
print("physical qubits:", layout)
Step 1 — Calibrate on the same qubits
Readout error is a property of each physical qubit, so the calibration circuits must run on exactly the qubits the experiment used. Passing initial_layout to the pass manager pins them. Two circuits are enough if we treat each qubit as independent: all zeros, and all ones.
cal0 = QuantumCircuit(n, n); cal0.measure(range(n), range(n))
cal1 = QuantumCircuit(n, n); cal1.x(range(n)); cal1.measure(range(n), range(n))
pm_fixed = generate_preset_pass_manager(backend=backend, optimization_level=1,
initial_layout=layout, seed_transpiler=7)
sampler = Sampler(mode=backend)
res = sampler.run([isa_ghz, pm_fixed.run(cal0), pm_fixed.run(cal1)], shots=shots).result()
raw, c0, c1 = (r.data.c.get_counts() for r in res)
What came back for the four qubits:
| Bit | e0 = P(read 1 | prepared 0) | e1 = P(read 0 | prepared 1) |
|---|---|---|
| 0 | 0.57% | 1.86% |
| 1 | 0.51% | 1.77% |
| 2 | 0.15% | 0.84% |
| 3 | 0.55% | 2.87% |
Notice the asymmetry: on every qubit, a |1⟩ is misread as 0 three to six times more often than the other way round. That is the signature of relaxation during the readout pulse — an excited qubit can decay to |0⟩ before the measurement finishes. It is also why using a single "readout error" number per qubit hides useful information.
Step 2 — Build the matrix (and get the bit order right)
For each qubit the 2×2 matrix is [[1-e0, e1], [e0, 1-e1]]: column 0 says what happens to a prepared 0, column 1 to a prepared 1. The 16×16 matrix for four qubits is their Kronecker product. One detail trips almost everyone up: Qiskit bitstrings are little-endian (bit 0 is the rightmost character), so when the bitstring is read as a binary number, the last qubit is the most significant. That is why the list of matrices is reversed before the product.
import numpy as np
from functools import reduce
def marginal(counts, k, bit):
"""Probability that classical bit k reads `bit` (bitstrings are little-endian)."""
return sum(v for s, v in counts.items() if s[::-1][k] == bit) / shots
mats = []
for k in range(n):
e0 = marginal(c0, k, '1')
e1 = marginal(c1, k, '0')
mats.append(np.array([[1 - e0, e1], [e0, 1 - e1]]))
A = reduce(np.kron, mats[::-1]) # reversed: qubit n-1 is the most significant bit
Step 3 — Invert and clean up
p = np.zeros(2 ** n)
for s, v in raw.items():
p[int(s, 2)] = v / shots
q = np.linalg.solve(A, p) # same as A^-1 · p, but numerically safer
q = np.clip(q, 0, None) # inversion can give tiny negative values
q /= q.sum()
good = [0, 2 ** n - 1] # indices of 0000 and 1111
print(f"raw: {p[good].sum():.4f}")
print(f"mitigated: {q[good].sum():.4f}")
The result
| P(0000) + P(1111) | |
|---|---|
| Raw counts | 93.9% |
| After readout mitigation | 98.2% |
| Ideal | 100% |
(A second run gave 94.2% → 98.5%; expect a few tenths of a percent of variation from shot noise.)
Readout correction removed about 70% of the error. The remaining 1.8% is not a readout problem: it comes from the three CNOTs and from decoherence while the circuit runs, which no amount of post-processing on the measurement can undo. The most common wrong outcomes in the raw data were 0111 and 1110 — exactly the "one qubit didn't flip" pattern you expect from an imperfect CNOT chain.
The inversion produced a negative "probability" mass of only 0.04%, so the simple clip-and-renormalise step was harmless here. With noisier qubits or fewer calibration shots that number grows, and a constrained least-squares fit (or a dedicated tool) becomes the better choice.
Lessons worth keeping
- Calibrate on the physical qubits you actually used. Pin them with
initial_layout; the transpiler is free to pick different ones otherwise. - Mind the bit order. A reversed Kronecker product gives a matrix that looks fine and corrects the wrong qubits.
- Separate e0 and e1. Readout errors are asymmetric, and a symmetric model under-corrects the 1→0 flips.
- Know what it can't fix. Readout mitigation only addresses the final measurement. For expectation values on real hardware, the Estimator's
resilience_level = 1(TREX) does a related correction for you; for gate errors you need other tools such as zero-noise extrapolation. - Scale. The full matrix grows as 2n×2n. Beyond a dozen or so qubits, use a matrix-free method such as M3 (the
mthreepackage), which only works with the bitstrings you actually observed.
References
- P. D. Nation, H. Kang, N. Sundaresan, J. M. Gambetta, "Scalable mitigation of measurement errors on quantum computers," PRX Quantum 2, 040326 (2021).
- E. van den Berg, Z. K. Minev, K. Temme, "Model-free readout-error mitigation for quantum expectation values," Phys. Rev. A 105, 032620 (2022).
- Qiskit documentation: SamplerV2, local testing mode and fake backends.
Comments
Post a Comment