Error Mitigation

Data-driven error mitigation learns how noise biases an expectation value from training circuits close to the target, whose noiseless values HSMPO computes. Clifford data regression (CDR) learns a map from noisy expectation values to noiseless ones on those circuits, then applies it to the target’s noisy value. The training circuits are low-magic variants of the target, in which each non-Clifford gate is replaced by a Clifford one with some probability. HSMPO computes their noiseless values cheaply, even with the leftover magic that keeps them close to the target.

  • cdr() fits exact = a * noisy + b;

  • vncdr() (variable-noise CDR) fits the exact value as a linear combination of the noisy values at several noise levels.

Both return a MitigationNoiseMap, which holds the fitted parameters and the training data, and is called on the target’s noisy value(s) to mitigate them.

The noisy values come from noise: a NoiseModel, simulated with noisy_expectation(), or any callable returning the noisy value of a circuit, such as one running it on hardware. For vnCDR a noise model is amplified either by scaling every error’s strength (amplification="scale") or by global unitary folding (amplification="fold", fold_circuit()).

Example

import numpy as np
from qibo import gates

from mpstab import (
    HSMPO,
    DepolarizingError,
    NoiseModel,
    PauliError,
    cdr,
    noisy_value,
    vncdr,
)
from mpstab.models.ansatze import HardwareEfficient

ansatz = HardwareEfficient(nqubits=4, nlayers=2)
ansatz.circuit.set_parameters(
    np.random.default_rng(1).uniform(-np.pi, np.pi, ansatz.nparams)
)
noise = (
    NoiseModel()
    .add(DepolarizingError(0.01))
    .add(PauliError({"ZZ": 0.01}), gates.CZ)
)

target = HSMPO(ansatz)
noisy = noisy_value(target, "ZZII", noise, seed=0)

noise_map = cdr(ansatz, "ZZII", noise, n_training_circuits=30,
                replacement_probability=0.7, seed=0)
print(target.expectation("ZZII"), noisy, noise_map(noisy))

levels = (1, 3, 5)
noise_map = vncdr(ansatz, "ZZII", noise, noise_levels=levels,
                  n_training_circuits=30, replacement_probability=0.7, seed=0)
print(noise_map([noisy_value(target, "ZZII", noise, l, seed=0) for l in levels]))

On hardware, pass the executor instead of the noise model, and mitigate the value measured on the target circuit (run_on_qpu stands for your own execution code):

noise_map = cdr(ansatz, "ZZII", lambda circuit: run_on_qpu(circuit, "ZZII"))
mitigated = noise_map(run_on_qpu(ansatz.circuit, "ZZII"))

Mitigating an HQSMPO measurement plan

On hardware, HQSMPO measures a head circuit under many measurement settings. VnCDRMitigation plans the extra circuits that mitigate all of them at once: the head folded to several noise levels, low-magic training variants of it measured in their most informative settings, and six circuits calibrating the measurement layer. One vnCDR map, fitted on every training measurement, corrects every setting (ansatz, hamiltonian and noise stand for your own):

from mpstab import (
    HQSMPO,
    DensityMatrixNoiseSimulator,
    VnCDRMitigation,
    run_plan,
)

model = HQSMPO(ansatz, head_synthesis="extraction")
mitigation = VnCDRMitigation(n_training_circuits=8, training_settings=3)
plan = mitigation.build_plan(model, hamiltonian, cut_index, n_shots=50_000, seed=0)

counts = run_plan(plan, DensityMatrixNoiseSimulator(noise))  # or run plan.circuits on a device
result = plan.estimate(counts)
print(result.unmitigated, result.measurement_mitigated, result.value)

API

Data-driven methods

Clifford data regression (CDR).

Fits exact = a * noisy + b on low-magic training circuits whose noiseless values HSMPO computes, then applies it to the target’s noisy value.

Reference:

P. Czarnik, A. Arrasmith et al., Error mitigation with Clifford quantum-circuit data, arXiv:2005.10189.

mpstab.models.error_mitigation.data_driven_methods.cdr.cdr(ansatz: Ansatz | Circuit, observable: str | list | dict | SymbolicHamiltonian, noise: NoiseModel | Callable[[...], float], n_training_circuits: int = 50, replacement_probability: float = 0.9, replacement_method: str = 'closest', model: Callable | None = None, initial_state: Circuit | None = None, max_bond_dimension: int | None = None, n_samples: int = 1000, seed: int | None = None, n_workers: int = 1) → MitigationNoiseMap[source]

Learn the CDR noise map of observable on ansatz.

Example:

from mpstab import HSMPO, DepolarizingError, NoiseModel, cdr
from mpstab.models.ansatze import HardwareEfficient

ansatz = HardwareEfficient(nqubits=4, nlayers=2)
noise = NoiseModel().add(DepolarizingError(0.01))

noise_map = cdr(ansatz, "ZZII", noise, n_training_circuits=20, seed=0)
noisy = HSMPO(ansatz).noisy_expectation("ZZII", noise, seed=0).mean
mitigated = noise_map(noisy)
Parameters:
  • ansatz – the circuit to mitigate.

  • observable – a Pauli string or any other format pauli_terms() accepts.

  • noise – a NoiseModel, simulated with HSMPO, or a callable f(circuit) -> float giving the noisy value of a circuit, for instance by running it on hardware. The circuit it receives starts with initial_state, if any.

  • n_training_circuits – training circuits to sample.

  • replacement_probability – chance of replacing each non-Clifford gate in a training circuit (training_circuits()).

  • replacement_method – "closest" or "random" Clifford angle.

  • model – model(x, *params) to fit, with x the noisy value. The default a * x + b is fitted by least squares; any other is fitted with scipy.optimize.curve_fit, starting from ones.

  • initial_state – one-qubit-gates-only circuit preparing the initial state, |0...0> if None.

  • max_bond_dimension – MPS truncation cap for HSMPO.

  • n_samples – fault configurations HSMPO samples per noisy value, when noise is a noise model.

  • seed – seed of the training circuits and of the noise sampling.

  • n_workers – processes evaluating the training circuits in parallel, when noise is a noise model; a callable always runs in this process. The result does not depend on it.

Returns:

MitigationNoiseMap

Variable-noise Clifford data regression (vnCDR).

Fits the noiseless value as a combination of the noisy values at several noise levels, on low-magic training circuits whose noiseless values HSMPO computes.

Reference:

A. Lowe, M. H. Gordon et al., Unified approach to data-driven quantum error mitigation, arXiv:2011.01157.

mpstab.models.error_mitigation.data_driven_methods.vncdr.vncdr(ansatz: Ansatz | Circuit, observable: str | list | dict | SymbolicHamiltonian, noise: NoiseModel | Callable[[...], float], noise_levels: Sequence[float] = (1, 3, 5), amplification: str = 'scale', n_training_circuits: int = 50, replacement_probability: float = 0.9, replacement_method: str = 'closest', model: Callable | None = None, initial_state: Circuit | None = None, max_bond_dimension: int | None = None, n_samples: int = 1000, seed: int | None = None, n_workers: int = 1) → MitigationNoiseMap[source]

Learn the variable-noise CDR noise map of observable on ansatz.

The map takes the target’s noisy values at every level of noise_levels, so evaluate the target the same way. For a noise model, noisy_value() does it:

levels = (1, 3, 5)
noise_map = vncdr(ansatz, "ZZII", noise, noise_levels=levels, seed=0)
target = HSMPO(ansatz)
mitigated = noise_map(
    [noisy_value(target, "ZZII", noise, level) for level in levels]
)
Parameters:
  • ansatz – the circuit to mitigate.

  • observable – a Pauli string or any other format pauli_terms() accepts.

  • noise – a NoiseModel, simulated with HSMPO, or a callable f(circuit, level) -> float giving the noisy value of a circuit at a noise level, for instance by running fold_circuit() of it on hardware. The circuit it receives starts with initial_state, if any.

  • noise_levels – the noise amplification factors, 1 being the unamplified noise.

  • amplification – how a noise model is amplified: "scale" multiplies every error’s strength by the level (scaled()), "fold" runs the folded circuit (fold_circuit()), which needs odd integer levels. Ignored for a callable.

  • n_training_circuits – training circuits to sample.

  • replacement_probability – chance of replacing each non-Clifford gate in a training circuit (training_circuits()).

  • replacement_method – "closest" or "random" Clifford angle.

  • model – model(x, *params) to fit, with x an array whose last axis runs over the noise levels. The default x @ params, a linear combination of the levels as in [2], is fitted by least squares; any other with scipy.optimize.curve_fit, starting from ones.

  • initial_state – one-qubit-gates-only circuit preparing the initial state, |0...0> if None.

  • max_bond_dimension – MPS truncation cap for HSMPO.

  • n_samples – fault configurations HSMPO samples per noisy value, when noise is a noise model.

  • seed – seed of the training circuits and of the noise sampling.

  • n_workers – processes evaluating the training circuits in parallel, when noise is a noise model; a callable always runs in this process. The result does not depend on it.

Returns:

MitigationNoiseMap, with noise_levels set.

The machinery shared by Clifford data regression and its variable-noise extension: training data, noisy values and the fitted noise map.

Both methods learn a map from noisy expectation values to noiseless ones on training circuits close to the target, then apply it to the target’s noisy value:

  • the training circuits are low-magic variants of the target (training_circuits());

  • their noiseless values come from HSMPO, cheap because they carry little magic;

  • their noisy values come either from a NoiseModel, simulated by noisy_expectation(), or from any callable, such as one running the circuit on hardware.

The methods themselves are cdr() and vncdr().

class mpstab.models.error_mitigation.data_driven_methods.regression.MitigationNoiseMap(params: ndarray, exact: ndarray, noisy: ndarray, model: Callable, noise_levels: tuple[float, ...] | None=None, noisy_stderr: ndarray | None = None, circuits: list[Circuit] = <factory>)[source]

A learned map from noisy expectation values to noiseless ones.

Call it on the target circuit’s noisy value (CDR) or on its values at each of noise_levels (vnCDR) to get the mitigated value. Several values at once go along the leading axes.

Variables:
  • params (numpy.ndarray) – the fitted parameters of model.

  • exact (numpy.ndarray) – noiseless value of each training circuit, shape (n,).

  • noisy (numpy.ndarray) – their noisy values, shape (n,) for CDR and (n, len(noise_levels)) for vnCDR.

  • noise_levels (tuple[float, ...] | None) – the noise levels of vnCDR, None for CDR.

  • noisy_stderr (numpy.ndarray | None) – standard error of each noisy value, same shape as noisy, when a noise model was sampled; None for a callable.

  • circuits (list[qibo.models.circuit.Circuit]) – the training circuits.

  • model (Callable) – model(x, *params), with x a noisy value (CDR) or an array whose last axis runs over the noise levels (vnCDR).

property residuals: ndarray

Mitigated minus noiseless value of each training circuit.

mpstab.models.error_mitigation.data_driven_methods.regression.noisy_value(hsmpo: HSMPO, observable: str | list | dict | SymbolicHamiltonian, noise: NoiseModel, level: float = 1, amplification: str = 'scale', n_samples: int = 1000, seed: int | None = None) → float[source]

Noisy expectation value of observable on the circuit of hsmpo, with noise amplified to level, as the training circuits get it.

Parameters:
  • hsmpo – the surrogate of the circuit.

  • observable – a Pauli string or any other format pauli_terms() accepts.

  • noise – the noise model.

  • level – the noise amplification factor.

  • amplification – "scale" or "fold", as in vncdr().

  • n_samples – fault configurations sampled by noisy_expectation().

  • seed – seed of the sampler.

Error mitigation of an HQSMPO measurement plan with variable-noise CDR.

An HQSMPO plan runs one head circuit under many measurement settings, each measuring a group of Pauli strings of the folded observable O = sum_P c_P P. Its noise splits in two:

  • the gates of the head, the same for every circuit of the plan, mitigated with variable-noise Clifford data regression (vnCDR);

  • the measurement layer, basis rotations and readout, which changes with the setting, calibrated once for every Pauli string at a low overhead.

One map, fitted and applied per setting. The training circuits are low-magic variants of the head. HSMPO gives the noiseless value of every setting of a training circuit, so each training measurement, one circuit in one setting at every noise level, is a data point of its own: one vnCDR map is fitted on all of them and applied to every setting of the target. Fitting the map on whole-observable values instead would fail whenever terms of opposite sign partly cancel the noise in the sum.

Choosing the data. Training is cheap when the data are informative:

  • circuits: a pool of candidates is sampled and the training set picked to spread its noiseless values evenly, since clustered values carry little information on the map (Czarnik et al., arXiv:2204.07109);

  • settings: each training circuit can measure only a few settings, drawn by the size of their noiseless contribution to it. A setting worth nothing on a training circuit says nothing about the map.

The measurement layer is calibrated by preparing both eigenstates of the X, Y and Z bases on every qubit and measuring them in the same basis, six circuits in all. A noisy measurement of qubit q in basis B reads z' = c z + a on average, with an attenuation c and, for an asymmetric readout, an offset a, both read off the two eigenstates. On estimation each factor z'_q of a shot’s parity becomes (z'_q - a_q) / c_q, which undoes the measurement noise exactly when it acts on each qubit independently.

class mpstab.models.error_mitigation.data_driven_methods.plan.MitigatedResult(value: float, unmitigated: float, measurement_mitigated: float | None, cdr: float, stderr: float, settings: ndarray, settings_noisy: ndarray, params: ndarray, training_exact: ndarray, training_noisy: ndarray, n_shots: int)[source]

The result of MitigationPlan.estimate().

Variables:
  • value (float) – the vnCDR-mitigated expectation value.

  • unmitigated (float) – the plain estimate of the target at noise level 1, as the plan alone would give it.

  • measurement_mitigated (float | None) – the same, with the measurement layer calibrated out; None without calibration.

  • cdr (float) – the CDR-mitigated value, from level 1 only, for comparison: one factor rescaling every setting.

  • stderr (float) – standard error of value from the target’s shots alone; the uncertainty of the fitted map is not included.

  • settings (numpy.ndarray) – the mitigated value of each setting of the target, whose sum plus the observable’s constant is value.

  • settings_noisy (numpy.ndarray) – each setting’s measured value at every noise level, measurement calibrated when available, shape (n_settings, len(noise_levels)).

  • params (numpy.ndarray) – the vnCDR coefficients, one per noise level.

  • training_exact (numpy.ndarray) – noiseless value of each training measurement, one per training circuit and measured setting.

  • training_noisy (numpy.ndarray) – their measured values, shape (n_points, len(noise_levels)).

  • n_shots (int) – shots spent by the whole plan.

class mpstab.models.error_mitigation.data_driven_methods.plan.MitigationPlan(target: object, noise_levels: tuple, circuits: tuple, shots: tuple, measurements: tuple, training_exact: ndarray, training_group_exact: ndarray, training_circuits: tuple = ())[source]

Every circuit to run for a mitigated measurement, with its shots.

It runs anywhere a MeasurementPlan does, e.g. run_plan() or save_circuits(); estimate() turns the counts into a MitigatedResult.

Variables:
  • target (object) – the plan of the circuit of interest, at noise level 1.

  • noise_levels (tuple) – the folding levels, odd integers starting at 1.

  • circuits (tuple) – every circuit to run.

  • shots (tuple) – their shot counts.

  • measurements (tuple) – what each circuit is for, in the same order.

  • training_exact (numpy.ndarray) – noiseless value of the whole observable on each training circuit.

  • training_group_exact (numpy.ndarray) – noiseless value of each setting on each training circuit, shape (n_training, n_settings).

  • training_circuits (tuple) – the training circuits, without measurement.

estimate(frequencies) → MitigatedResult[source]

Mitigate from one {bitstring: count} dict per circuit, in circuits order.

class mpstab.models.error_mitigation.data_driven_methods.plan.VnCDRMitigation(noise_levels: tuple = (1, 3, 5), n_training_circuits: int = 16, replacement_probability: float = 0.7, replacement_method: str = 'closest', candidate_factor: int = 4, training_settings: int | None = 8, training_shots: int = 2000, calibrate_measurement: bool = True, calibration_shots: int = 4000)[source]

Variable-noise CDR on the head of an HQSMPO plan, plus a calibration of the measurement layer.

Parameters:
  • noise_levels – folding levels of the head, odd integers starting at 1: level 2k + 1 runs U (U^dag U)^k. Only the head is folded, not the measurement layer.

  • n_training_circuits – training circuits in the regression.

  • replacement_probability – chance of replacing each non-Clifford rotation of the head by a Clifford one in a training circuit.

  • replacement_method – "closest" or "random" Clifford angle.

  • candidate_factor – candidates sampled per training circuit; the training set is the subset whose noiseless values spread most evenly. 1 keeps the first candidates.

  • training_settings – settings each training circuit measures, drawn without replacement by the size of their noiseless contribution to it; None measures all of them with a nonzero contribution, or every setting of a circuit where all contributions vanish.

  • training_shots – shots per training circuit and noise level, split over its settings.

  • calibrate_measurement – calibrate the measurement layer, attenuation and offset of every qubit in every basis, and undo it on every shot.

  • calibration_shots – shots per calibration circuit, six in all.

build_plan(model, observable, cut_index: int, n_shots: int | None = None, epsilon: float | None = None, method: PauliSampling | None = None, max_bond_dimension=-1, seed: int | None = None) → MitigationPlan[source]

The target plan of model at cut_index, folded to every noise level, plus the training and calibration circuits.

Parameters:
  • model – the HQSMPO.

  • observable – as in plan().

  • cut_index – the split point.

  • n_shots – shots of the target plan at each noise level; exactly one of this and epsilon.

  • epsilon – target standard error sizing those shots instead.

  • method – the "pauli" measurement method, with its options.

  • max_bond_dimension – bond cap of the tail fold, as in the plan.

  • seed – seed of the plan, the training circuits and the settings.

mpstab.models.error_mitigation.data_driven_methods.plan.mitigated_expectation_at_cut(model, observable, cut_index: int, mitigation: VnCDRMitigation | None = None, n_shots: int | None = None, epsilon: float | None = None, backend=None, method: PauliSampling | None = None, max_bond_dimension=-1, seed: int | None = None) → MitigatedResult[source]

Plan, run and mitigate in one call, like expectation_at_cut(). On a device, call VnCDRMitigation.build_plan(), run its circuits, then MitigationPlan.estimate().

Parameters:
  • model – the HQSMPO.

  • observable – as in plan().

  • cut_index – the split point.

  • mitigation – the options, VnCDRMitigation defaults if None.

  • n_shots – shots of the target plan at each noise level.

  • epsilon – target standard error sizing them instead.

  • backend – anything with execute_circuits(circuits, nshots), e.g. DensityMatrixNoiseSimulator.

  • method – the "pauli" measurement method, with its options.

  • max_bond_dimension – bond cap of the tail fold.

  • seed – seed of the plan.

Training circuits for Clifford data regression.

mpstab.models.error_mitigation.data_driven_methods.training.training_circuits(ansatz: Ansatz | Circuit, n_circuits: int, replacement_probability: float = 0.9, replacement_method: str = 'closest', seed: int | None = None) → list[Circuit][source]

Low-magic variants of ansatz to train a CDR noise map on.

Each non-Clifford gate is replaced by a Clifford one with probability replacement_probability and kept otherwise (partitionate_circuit()). Unlike the fully Clifford circuits of the original CDR, the gates that are kept leave some magic in each circuit, which HSMPO still simulates cheaply, so the training set stays closer to the target circuit. Every circuit keeps the target’s gate sequence, only with other angles, so noise placed by gate type or position acts on it as on the target.

Parameters:
  • ansatz – the circuit to mitigate.

  • n_circuits – training circuits to sample.

  • replacement_probability – chance of replacing each non-Clifford gate. Lower values keep more magic: closer to the target, costlier to simulate.

  • replacement_method – "closest" or "random" Clifford angle.

  • seed – seed of the replacement draws.

Returns:

The sampled circuits, as qibo Circuit objects.

Noise amplification

Noise amplification by global unitary folding.

mpstab.models.error_mitigation.folding.fold_circuit(circuit: Circuit, level: int) → Circuit[source]

Amplify the noise of circuit by global unitary folding.

Returns \(U (U^\dagger U)^k\) with level = 2k + 1: the same unitary, with level times as many gates, hence about level times the noise. Measurement gates are dropped.

Parameters:
  • circuit – the circuit \(U\) to fold.

  • level – the noise level, an odd positive integer. 1 returns a copy.

Raises:

ValueError – if level is not an odd positive integer.