Note

This is the documentation for the current state of the development branch of Qiskit Experiments. The documentation or APIs here can change prior to being released.

Readout Mitigation

Readout errors affect quantum computation during the measurement of the qubits in a quantum device. By characterizing the readout errors, it is possible to construct a readout error mitigator that is used both to obtain a more accurate distribution of the outputs, and more accurate measurements of expectation value for measurables.

The readout mitigator is generated from an assignment matrix: a \(2^n \times 2^n\) matrix \(A\) such that \(A_{y,x}\) is the probability to observe \(y\) given the true outcome should be \(x\). The assignment matrix is used to compute the mitigation matrix used in the readout error mitigation process itself.

A Local readout mitigator works under the assumption that readout errors are mostly local, meaning readout errors for different qubits are independent of each other. In this case, the assignment matrix is the tensor product of \(n\) \(2 \times 2\) matrices, one for each qubit, making it practical to store the assignment matrix in implicit form, by storing the individual \(2 \times 2\) assignment matrices. The corresponding class in Qiskit is the LocalReadoutMitigator.

A Correlated readout mitigator uses the full \(2^n \times 2^n\) assignment matrix, meaning it can only be used for small values of \(n\). The corresponding class in Qiskit is the CorrelatedReadoutMitigator.

This notebook demonstrates the usage of both the local and correlated experiments to generate the corresponding mitigators.

Note

This tutorial requires the qiskit-aer and qiskit-ibm-runtime packages to run simulations. You can install them with python -m pip install qiskit-aer qiskit-ibm-runtime.

import numpy as np
import matplotlib.pyplot as plt
from qiskit import QuantumCircuit
from qiskit.quantum_info import Operator
from qiskit.visualization import plot_distribution
from qiskit_experiments.data_processing import LocalReadoutMitigator
from qiskit_experiments.library import LocalReadoutError, CorrelatedReadoutError

from qiskit_aer import AerSimulator
from qiskit_ibm_runtime.fake_provider import FakePerth

backend = AerSimulator.from_backend(FakePerth())

Standard mitigation experiment

The default mitigation experiment is local, meaning error probability is measured individually for each qubit. The experiment generates two circuits, one for all “0” and one for all “1” results.

shots = 1024
qubits = [0,1,2,3]
num_qubits = len(qubits)

exp = LocalReadoutError(qubits)
for c in exp.circuits():
    print(c)

exp.analysis.set_options(plot=True)
result = exp.run(backend)
mitigator = result.analysis_results("Local Readout Mitigator").value
         ░ ┌─┐         
   q_0: ─░─┤M├─────────
         ░ └╥┘┌─┐      
   q_1: ─░──╫─┤M├──────
         ░  ║ └╥┘┌─┐   
   q_2: ─░──╫──╫─┤M├───
         ░  ║  ║ └╥┘┌─┐
   q_3: ─░──╫──╫──╫─┤M├
         ░  ║  ║  ║ └╥┘
meas: 4/════╩══╩══╩══╩═
            0  1  2  3 
        ┌───┐ ░ ┌─┐         
   q_0: ┤ X ├─░─┤M├─────────
        ├───┤ ░ └╥┘┌─┐      
   q_1: ┤ X ├─░──╫─┤M├──────
        ├───┤ ░  ║ └╥┘┌─┐   
   q_2: ┤ X ├─░──╫──╫─┤M├───
        ├───┤ ░  ║  ║ └╥┘┌─┐
   q_3: ┤ X ├─░──╫──╫──╫─┤M├
        └───┘ ░  ║  ║  ║ └╥┘
meas: 4/═════════╩══╩══╩══╩═
                 0  1  2  3 

The resulting measurement matrix can be illustrated by comparing it to the identity.

result.figure(0)
../../_images/readout_mitigation_3_0.png

Mitigation matrices

The individual mitigation matrices can be read off the mitigator.

for qubit in mitigator.qubits:
    print(f"Qubit: {qubit}")
    print(mitigator.mitigation_matrix(qubits=qubit))
Qubit: 0
[[ 1.03409091 -0.02376033]
 [-0.03409091  1.02376033]]
Qubit: 1
[[ 1.02356557 -0.02561475]
 [-0.02356557  1.02561475]]
Qubit: 2
[[ 1.02683179 -0.02992776]
 [-0.02683179  1.02992776]]
Qubit: 3
[[ 1.02916667 -0.0375    ]
 [-0.02916667  1.0375    ]]

Mitigation example

qc = QuantumCircuit(num_qubits)
qc.sx(0)
for i in range(1, num_qubits):
    qc.cx(i - 1, i)
qc.measure_all()

counts = backend.run(qc, shots=shots, seed_simulator=42, method="density_matrix").result().get_counts()
unmitigated_probs = {label: count / shots for label, count in counts.items()}

mitigated_quasi_probs = mitigator.quasi_probabilities(counts)
mitigated_stddev = mitigated_quasi_probs._stddev_upper_bound
mitigated_probs = (mitigated_quasi_probs.nearest_probability_distribution().binary_probabilities())

Probabilities

legend = ['Mitigated Probabilities', 'Unmitigated Probabilities']
plot_distribution([mitigated_probs, unmitigated_probs], legend=legend, sort="value_desc", bar_labels=False)
../../_images/readout_mitigation_6_0.png

Expectation value

diagonal_labels = ["ZZZZ", "ZIZI", "IZII", "1ZZ0"]
diagonals = [
    np.diag(np.real(Operator.from_label(d).to_matrix()))
    for d in diagonal_labels
]

# Create a mitigator with no mitigation so that we can use its
# expectation_values method to generate an unmitigated expectation value to
# compare to the mitigated one.
identity_mitigator = LocalReadoutMitigator([np.eye(2) for _ in range(4)])

qubit_index = {i: i for i in range(num_qubits)}
unmitigated_expectation = [identity_mitigator.expectation_value(counts, d) for d in diagonals]
mitigated_expectation = [mitigator.expectation_value(counts, d) for d in diagonals]

mitigated_expectation_values, mitigated_stddev = zip(*mitigated_expectation)
unmitigated_expectation_values, unmitigated_stddev = zip(*unmitigated_expectation)
legend = ['Mitigated Expectation', 'Unmitigated Expectation']
fig, ax = plt.subplots()
X = np.arange(4)
ax.bar(X + 0.00, mitigated_expectation_values, yerr=mitigated_stddev, color='b', width = 0.25, label="Mitigated Expectation")
ax.bar(X + 0.25, unmitigated_expectation_values, yerr=unmitigated_stddev, color='g', width = 0.25, label="Unmitigated Expectation")
ax.set_xticks([0.125 + i for i in range(len(diagonals))])
ax.set_xticklabels(diagonal_labels)
ax.legend()
<matplotlib.legend.Legend at 0x7ff17d3f59d0>
../../_images/readout_mitigation_7_1.png

Correlated readout mitigation

In correlated readout mitigation on \(n\) qubits, a circuit is generated for each of the possible \(2^n\) combinations of “0” and “1”. This results in more accurate mitigation in the case where the readout errors are correlated and not independent, but requires a large amount of circuits and storage space, and so is infeasible for more than a few qubits.

qubits = [0,3]
num_qubits = len(qubits)
exp = CorrelatedReadoutError(qubits)
for c in exp.circuits():
    print(c)
         ░ ┌─┐   
   q_0: ─░─┤M├───
         ░ └╥┘┌─┐
   q_1: ─░──╫─┤M├
         ░  ║ └╥┘
meas: 2/════╩══╩═
            0  1 
        ┌───┐ ░ ┌─┐   
   q_0: ┤ X ├─░─┤M├───
        └───┘ ░ └╥┘┌─┐
   q_1: ──────░──╫─┤M├
              ░  ║ └╥┘
meas: 2/═════════╩══╩═
                 0  1 
              ░ ┌─┐   
   q_0: ──────░─┤M├───
        ┌───┐ ░ └╥┘┌─┐
   q_1: ┤ X ├─░──╫─┤M├
        └───┘ ░  ║ └╥┘
meas: 2/═════════╩══╩═
                 0  1 
        ┌───┐ ░ ┌─┐   
   q_0: ┤ X ├─░─┤M├───
        ├───┤ ░ └╥┘┌─┐
   q_1: ┤ X ├─░──╫─┤M├
        └───┘ ░  ║ └╥┘
meas: 2/═════════╩══╩═
                 0  1 

See also