How do I use CDR?#

Clifford data regression (CDR) is a learning-based quantum error mitigation technique in which an error mitigation model is trained with quantum circuits that resemble the circuit of interest, but which are easier to classically simulate [2, 11]. More information about this technique can be found in the section What is the theory behind CDR?.

Here we show how to use CDR by means of a simple example.

import numpy as np
import warnings
warnings.simplefilter("ignore", np.ComplexWarning)

import cirq
from mitiq import cdr, Observable, PauliString

Problem setup#

To use CDR, we call cdr.execute_with_cdr() with four “ingredients”:

  1. A quantum circuit to prepare a state \(\rho\).

  2. A quantum computer or noisy simulator to return a QuantumResult from \(\rho\).

  3. An observable \(O\) which specifies what we wish to compute via \(\text{Tr} [ \rho O ]\).

  4. A near-Clifford (classical) circuit simulator.

1. Define a quantum circuit#

The quantum circuit can be specified as any quantum circuit supported by Mitiq but must be compiled into a gateset in which the only non-Clifford gates are single-qubit rotations around the \(Z\) axis: \(R_Z(\theta)\). For example:

\[\{ \sqrt{X}, R_Z(\theta), \text{CNOT}\},\]
\[\{{R_X(\pi/2)}, R_Z(\theta), \text{CZ}\},\]
\[\{H, S, R_Z(\theta), \text{CNOT}\},\]
\[ \dots\]

In the next cell we define (as an example) a quantum circuit which contains some Clifford gates and some non-Clifford \(R_Z(\theta)\) rotations.

a, b = cirq.LineQubit.range(2)
circuit = cirq.Circuit(
    cirq.H.on(a), # Clifford
    cirq.H.on(b), # Clifford
    cirq.rz(1.75).on(a),
    cirq.rz(2.31).on(b),
    cirq.CNOT.on(a, b),  # Clifford
    cirq.rz(-1.17).on(b),
    cirq.rz(3.23).on(a),
    cirq.rx(np.pi / 2).on(a),  # Clifford
    cirq.rx(np.pi / 2).on(b),  # Clifford
)

# CDR works better if the circuit is not too short. So we increase its depth.
circuit = 5 * circuit

2. Define an executor#

We define an executor function which inputs a circuit and returns a QuantumResult. Here for sake of example we use a simulator that adds single-qubit depolarizing noise after each moment and returns the final density matrix.

from mitiq.interface.mitiq_cirq import compute_density_matrix

compute_density_matrix(circuit).round(3)
array([[ 0.834+0.j   , -0.027+0.075j, -0.004+0.061j, -0.022-0.052j],
       [-0.027-0.075j,  0.057-0.j   ,  0.007-0.006j, -0.003+0.002j],
       [-0.004-0.061j,  0.007+0.006j,  0.053+0.j   , -0.005+0.004j],
       [-0.022+0.052j, -0.003-0.002j, -0.005-0.004j,  0.056+0.j   ]],
      dtype=complex64)

3. Observable#

As an example, assume that we wish to compute the expectation value \(\text{Tr} [ \rho O ]\) of the following observable \(O\):

# Observable to measure.
obs = Observable(PauliString("ZZ"), PauliString("X", coeff=-1.75))
print(obs)
Z(q(0))*Z(q(1)) + (-1.75+0j)*X(q(0))

Note: To apply CDR the Observable should be Hermitian, i.e., it should always produce real expectation values.

You can read more about the Observable class in the documentation.

4. (Near-Clifford) Simulator#

The CDR method creates a set of “training circuits” which are related to the input circuit and are efficiently simulable. These circuits are simulated on a classical (noiseless) simulator to collect data for regression. The simulator should also return a QuantumResult.

To use CDR at scale, an efficient near-Clifford circuit simulator must be specified. In this example, the circuit is small enough to use any classical simulator, and we use the same density matrix simulator as above but without noise.

def simulate(circuit: cirq.Circuit) -> np.ndarray:
    return compute_density_matrix(circuit, noise_level=(0.0,))

simulate(circuit).round(3)
array([[ 0.982-0.j   , -0.016+0.081j, -0.004+0.049j, -0.049-0.075j],
       [-0.016-0.081j,  0.007-0.j   ,  0.004-0.j   , -0.005+0.005j],
       [-0.004-0.049j,  0.004+0.j   ,  0.002+0.j   , -0.004+0.003j],
       [-0.049+0.075j, -0.005-0.005j, -0.004-0.003j,  0.008+0.j   ]],
      dtype=complex64)

Run CDR#

Now we can run CDR. We first compute the noiseless result then the noisy result to compare to the mitigated result from CDR.

The noiseless result#

ideal_measurement = obs.expectation(circuit, simulate).real
print("ideal_measurement = ",ideal_measurement)
before: [array([[ 0.98240304-1.09896064e-07j, -0.01608144+8.09558332e-02j,
        -0.00435474+4.88507263e-02j, -0.04904465-7.52595514e-02j],
       [-0.01608145-8.09559822e-02j,  0.00693452-2.42143869e-08j,
         0.0040969 -4.40920703e-04j, -0.00539904+5.27343294e-03j],
       [-0.0043548 -4.88507152e-02j,  0.00409681+4.40857140e-04j,
         0.00244841+2.87545845e-08j, -0.00352496+2.77238758e-03j],
       [-0.04904461+7.52596110e-02j, -0.00539897-5.27355075e-03j,
        -0.00352498-2.77240947e-03j,  0.00821393+1.39698386e-08j]],
      dtype=complex64)]
after:  [array([[ 0.98240304-1.09896064e-07j, -0.01608144+8.09558332e-02j,
        -0.00435474+4.88507263e-02j, -0.04904465-7.52595514e-02j],
       [-0.01608145-8.09559822e-02j,  0.00693452-2.42143869e-08j,
         0.0040969 -4.40920703e-04j, -0.00539904+5.27343294e-03j],
       [-0.0043548 -4.88507152e-02j,  0.00409681+4.40857140e-04j,
         0.00244841+2.87545845e-08j, -0.00352496+2.77238758e-03j],
       [-0.04904461+7.52596110e-02j, -0.00539897-5.27355075e-03j,
        -0.00352498-2.77240947e-03j,  0.00821393+1.39698386e-08j]],
      dtype=complex64)]
ideal_measurement =  1.0153722348622978

The noisy result#

We now generate the noisy result. Note that compute_density_matrix() function by default runs a simulation with noise.

unmitigated_measurement = obs.expectation(circuit, compute_density_matrix).real
print("unmitigated_measurement = ", unmitigated_measurement)
before: [array([[ 0.834364  +1.0323221e-08j, -0.02658513+7.5414695e-02j,
        -0.00364555+6.0732979e-02j, -0.02219004-5.2193210e-02j],
       [-0.02658516-7.5414769e-02j,  0.05705505-2.1024118e-08j,
         0.00668314-6.3313493e-03j, -0.00279339+1.8779658e-03j],
       [-0.00364545-6.0733065e-02j,  0.00668317+6.3313707e-03j,
         0.05266568+2.8994888e-08j, -0.00493391+3.6680887e-03j],
       [-0.02219001+5.2193295e-02j, -0.00279342-1.8778936e-03j,
        -0.00493392-3.6681308e-03j,  0.05591451+1.8027587e-08j]],
      dtype=complex64)]
after:  [array([[ 0.834364  +1.0323221e-08j, -0.02658513+7.5414695e-02j,
        -0.00364555+6.0732979e-02j, -0.02219004-5.2193210e-02j],
       [-0.02658516-7.5414769e-02j,  0.05705505-2.1024118e-08j,
         0.00668314-6.3313493e-03j, -0.00279339+1.8779658e-03j],
       [-0.00364545-6.0733065e-02j,  0.00668317+6.3313707e-03j,
         0.05266568+2.8994888e-08j, -0.00493391+3.6680887e-03j],
       [-0.02219001+5.2193295e-02j, -0.00279342-1.8778936e-03j,
        -0.00493392-3.6681308e-03j,  0.05591451+1.8027587e-08j]],
      dtype=complex64)]
unmitigated_measurement =  0.8030939371092245

The mitigated result#

mitigated_measurement = cdr.execute_with_cdr(
    circuit,
    compute_density_matrix,
    observable=obs,
    simulator=simulate,
    seed=0,
).real
print("mitigated_measurement = ", mitigated_measurement)
before: [array([[ 0.834364  +1.0323221e-08j, -0.02658513+7.5414695e-02j,
        -0.00364555+6.0732979e-02j, -0.02219004-5.2193210e-02j],
       [-0.02658516-7.5414769e-02j,  0.05705505-2.1024118e-08j,
         0.00668314-6.3313493e-03j, -0.00279339+1.8779658e-03j],
       [-0.00364545-6.0733065e-02j,  0.00668317+6.3313707e-03j,
         0.05266568+2.8994888e-08j, -0.00493391+3.6680887e-03j],
       [-0.02219001+5.2193295e-02j, -0.00279342-1.8778936e-03j,
        -0.00493392-3.6681308e-03j,  0.05591451+1.8027587e-08j]],
      dtype=complex64), array([[ 0.8334872 -5.1037405e-09j, -0.00897043+1.2670519e-02j,
        -0.00132367-1.8638603e-02j,  0.00607865+4.4669058e-02j],
       [-0.00897041-1.2670521e-02j,  0.05386258-3.3515040e-09j,
        -0.00158951+3.0340680e-03j,  0.00090282-4.8634685e-03j],
       [-0.00132369+1.8638641e-02j, -0.00158951-3.0340799e-03j,
         0.06073165+5.0618314e-09j, -0.00353539+9.4206911e-04j],
       [ 0.00607856-4.4669110e-02j,  0.00090276+4.8634759e-03j,
        -0.00353537-9.4208925e-04j,  0.05191734-1.2322655e-08j]],
      dtype=complex64), array([[ 0.8311167 -2.19690790e-08j, -0.00493569+1.43773705e-02j,
        -0.00119027+1.38771962e-02j,  0.03417455+6.83205426e-02j],
       [-0.00493567-1.43774534e-02j,  0.05281535-2.62715028e-09j,
        -0.00147936+1.54268381e-03j,  0.00103362-4.64766240e-03j],
       [-0.0011903 -1.38771702e-02j, -0.00147934-1.54274504e-03j,
         0.0585065 -5.68130609e-09j, -0.00151571+1.04473217e-03j],
       [ 0.03417464-6.83205053e-02j,  0.00103361+4.64768475e-03j,
        -0.00151565-1.04467163e-03j,  0.05756009-9.07084274e-09j]],
      dtype=complex64), array([[ 8.3505905e-01-4.9913766e-09j, -1.7926365e-02-1.4269145e-02j,
        -1.5036911e-03+1.4314003e-02j,  7.5554536e-03+3.9096479e-03j],
       [-1.7926274e-02+1.4269208e-02j,  5.4866023e-02-1.2742168e-08j,
        -2.3788204e-03+1.9687857e-03j,  7.7484286e-04-3.7145910e-03j],
       [-1.5036875e-03-1.4314011e-02j, -2.3788472e-03-1.9688471e-03j,
         5.9528224e-02-2.0233495e-08j, -2.0415462e-03+1.5261882e-03j],
       [ 7.5554335e-03-3.9096316e-03j,  7.7487575e-04+3.7145498e-03j,
        -2.0414847e-03-1.5261992e-03j,  5.0545525e-02+1.6430205e-08j]],
      dtype=complex64), array([[ 7.0682812e-01+2.2145921e-08j, -1.3774879e-01+3.2782918e-03j,
        -9.6453645e-05-2.2562949e-01j,  8.7599149e-03+5.5322308e-02j],
       [-1.3774873e-01-3.2783141e-03j,  7.7812500e-02+2.2036023e-08j,
        -1.6975759e-03+4.7770541e-02j, -6.5300200e-04-1.9524697e-02j],
       [-9.6475756e-05+2.2562948e-01j, -1.6975962e-03-4.7770541e-02j,
         1.5326312e-01-4.2504627e-08j, -2.6437085e-02+5.5612335e-03j],
       [ 8.7599093e-03-5.5322323e-02j, -6.5298920e-04+1.9524770e-02j,
        -2.6437106e-02-5.5612596e-03j,  6.2094945e-02-9.1278922e-09j]],
      dtype=complex64), array([[ 8.0541533e-01-1.5990530e-08j, -1.8256154e-02+1.5455819e-02j,
        -6.8140045e-02+1.3591241e-02j, -1.3899501e-01+9.9401874e-03j],
       [-1.8256051e-02-1.5455839e-02j,  5.3543143e-02-1.5646497e-08j,
         4.6998943e-04+3.6150371e-03j,  2.5851817e-03-2.9918721e-03j],
       [-6.8140037e-02-1.3591251e-02j,  4.7001825e-04-3.6150585e-03j,
         6.6728115e-02+2.7660280e-11j,  8.8452660e-03+2.7562655e-03j],
       [-1.3899501e-01-9.9402983e-03j,  2.5851885e-03+2.9918363e-03j,
         8.8452054e-03-2.7562547e-03j,  7.4312232e-02+2.7383675e-09j]],
      dtype=complex64), array([[ 0.8011876 +2.96502609e-08j, -0.15794283+1.32115865e-02j,
        -0.03310011+1.69303343e-02j,  0.01295599+6.13177055e-03j],
       [-0.1579428 -1.32115539e-02j,  0.08720163-7.14557302e-11j,
         0.00596864-1.27087173e-03j, -0.00180657-6.33083330e-03j],
       [-0.03310017-1.69303194e-02j,  0.00596858+1.27086823e-03j,
         0.05988427-1.50541073e-08j, -0.00391752+6.82831858e-04j],
       [ 0.01295596-6.13173004e-03j, -0.00180648+6.33088313e-03j,
        -0.00391755-6.82874059e-04j,  0.05172533-7.07411640e-09j]],
      dtype=complex64), array([[ 0.78603995+3.3356063e-08j, -0.00436158+1.7191069e-02j,
        -0.00158394+1.3208469e-02j, -0.12887354+1.6205221e-01j],
       [-0.00436156-1.7191075e-02j,  0.05148041-1.8827432e-08j,
        -0.00088851+3.2436578e-03j,  0.00126167-2.6253278e-03j],
       [-0.00158406-1.3208531e-02j, -0.00088846-3.2436764e-03j,
         0.05831346+1.4567748e-09j, -0.00195425+4.8596229e-04j],
       [-0.12887357-1.6205220e-01j,  0.00126165+2.6253876e-03j,
        -0.00195431-4.8598435e-04j,  0.10416475-3.8337145e-08j]],
      dtype=complex64), array([[ 8.0162221e-01-7.6752276e-09j, -1.5803681e-01-1.9351071e-02j,
        -4.6951105e-04+1.6234804e-02j,  6.6591715e-03+6.2465994e-03j],
       [-1.5803675e-01+1.9351108e-02j,  8.8302836e-02-1.4748692e-08j,
        -1.2895665e-03-7.9333480e-04j, -2.5931135e-04-5.6354390e-03j],
       [-4.6953675e-04-1.6234808e-02j, -1.2895522e-03+7.9334120e-04j,
         5.8437109e-02-7.4878681e-09j, -3.3923942e-03+7.2707015e-04j],
       [ 6.6590882e-03-6.2466161e-03j, -2.5929345e-04+5.6354436e-03j,
        -3.3923511e-03-7.2703714e-04j,  5.1636584e-02+3.4229594e-10j]],
      dtype=complex64), array([[ 0.708969  -2.0525977e-08j,  0.04701609+1.2493659e-02j,
        -0.00099883+2.6755899e-01j,  0.0046451 +1.3918953e-01j],
       [ 0.04701601-1.2493660e-02j,  0.0563927 +1.1100992e-08j,
         0.00274669+2.0817427e-02j,  0.00183519+5.2773873e-03j],
       [-0.00099884-2.6755902e-01j,  0.00274669-2.0817466e-02j,
         0.15418473-1.4715269e-08j,  0.05194992-4.7423290e-03j],
       [ 0.00464507-1.3918956e-01j,  0.00183513-5.2774390e-03j,
         0.05194991+4.7422890e-03j,  0.08045215+3.6511569e-09j]],
      dtype=complex64), array([[ 8.0670410e-01-5.3475611e-09j, -1.2708660e-02+1.5613300e-02j,
        -1.3636015e-03+4.8187021e-02j,  5.4213172e-03+1.6214412e-01j],
       [-1.2708641e-02-1.5613317e-02j,  5.2392010e-02-2.0689891e-08j,
        -6.4703618e-04+2.5427132e-03j,  1.2091032e-03-5.6675831e-03j],
       [-1.3635962e-03-4.8187066e-02j, -6.4706430e-04-2.5427367e-03j,
         5.8427170e-02-1.5581958e-09j,  6.6996180e-03-1.7740536e-03j],
       [ 5.4213284e-03-1.6214415e-01j,  1.2091243e-03+5.6675663e-03j,
         6.6997241e-03+1.7740884e-03j,  8.2475320e-02+5.4767351e-09j]],
      dtype=complex64)]
after:  [array([[ 0.834364  +1.0323221e-08j, -0.02658513+7.5414695e-02j,
        -0.00364555+6.0732979e-02j, -0.02219004-5.2193210e-02j],
       [-0.02658516-7.5414769e-02j,  0.05705505-2.1024118e-08j,
         0.00668314-6.3313493e-03j, -0.00279339+1.8779658e-03j],
       [-0.00364545-6.0733065e-02j,  0.00668317+6.3313707e-03j,
         0.05266568+2.8994888e-08j, -0.00493391+3.6680887e-03j],
       [-0.02219001+5.2193295e-02j, -0.00279342-1.8778936e-03j,
        -0.00493392-3.6681308e-03j,  0.05591451+1.8027587e-08j]],
      dtype=complex64), array([[ 0.8334872 -5.1037405e-09j, -0.00897043+1.2670519e-02j,
        -0.00132367-1.8638603e-02j,  0.00607865+4.4669058e-02j],
       [-0.00897041-1.2670521e-02j,  0.05386258-3.3515040e-09j,
        -0.00158951+3.0340680e-03j,  0.00090282-4.8634685e-03j],
       [-0.00132369+1.8638641e-02j, -0.00158951-3.0340799e-03j,
         0.06073165+5.0618314e-09j, -0.00353539+9.4206911e-04j],
       [ 0.00607856-4.4669110e-02j,  0.00090276+4.8634759e-03j,
        -0.00353537-9.4208925e-04j,  0.05191734-1.2322655e-08j]],
      dtype=complex64), array([[ 0.8311167 -2.19690790e-08j, -0.00493569+1.43773705e-02j,
        -0.00119027+1.38771962e-02j,  0.03417455+6.83205426e-02j],
       [-0.00493567-1.43774534e-02j,  0.05281535-2.62715028e-09j,
        -0.00147936+1.54268381e-03j,  0.00103362-4.64766240e-03j],
       [-0.0011903 -1.38771702e-02j, -0.00147934-1.54274504e-03j,
         0.0585065 -5.68130609e-09j, -0.00151571+1.04473217e-03j],
       [ 0.03417464-6.83205053e-02j,  0.00103361+4.64768475e-03j,
        -0.00151565-1.04467163e-03j,  0.05756009-9.07084274e-09j]],
      dtype=complex64), array([[ 8.3505905e-01-4.9913766e-09j, -1.7926365e-02-1.4269145e-02j,
        -1.5036911e-03+1.4314003e-02j,  7.5554536e-03+3.9096479e-03j],
       [-1.7926274e-02+1.4269208e-02j,  5.4866023e-02-1.2742168e-08j,
        -2.3788204e-03+1.9687857e-03j,  7.7484286e-04-3.7145910e-03j],
       [-1.5036875e-03-1.4314011e-02j, -2.3788472e-03-1.9688471e-03j,
         5.9528224e-02-2.0233495e-08j, -2.0415462e-03+1.5261882e-03j],
       [ 7.5554335e-03-3.9096316e-03j,  7.7487575e-04+3.7145498e-03j,
        -2.0414847e-03-1.5261992e-03j,  5.0545525e-02+1.6430205e-08j]],
      dtype=complex64), array([[ 7.0682812e-01+2.2145921e-08j, -1.3774879e-01+3.2782918e-03j,
        -9.6453645e-05-2.2562949e-01j,  8.7599149e-03+5.5322308e-02j],
       [-1.3774873e-01-3.2783141e-03j,  7.7812500e-02+2.2036023e-08j,
        -1.6975759e-03+4.7770541e-02j, -6.5300200e-04-1.9524697e-02j],
       [-9.6475756e-05+2.2562948e-01j, -1.6975962e-03-4.7770541e-02j,
         1.5326312e-01-4.2504627e-08j, -2.6437085e-02+5.5612335e-03j],
       [ 8.7599093e-03-5.5322323e-02j, -6.5298920e-04+1.9524770e-02j,
        -2.6437106e-02-5.5612596e-03j,  6.2094945e-02-9.1278922e-09j]],
      dtype=complex64), array([[ 8.0541533e-01-1.5990530e-08j, -1.8256154e-02+1.5455819e-02j,
        -6.8140045e-02+1.3591241e-02j, -1.3899501e-01+9.9401874e-03j],
       [-1.8256051e-02-1.5455839e-02j,  5.3543143e-02-1.5646497e-08j,
         4.6998943e-04+3.6150371e-03j,  2.5851817e-03-2.9918721e-03j],
       [-6.8140037e-02-1.3591251e-02j,  4.7001825e-04-3.6150585e-03j,
         6.6728115e-02+2.7660280e-11j,  8.8452660e-03+2.7562655e-03j],
       [-1.3899501e-01-9.9402983e-03j,  2.5851885e-03+2.9918363e-03j,
         8.8452054e-03-2.7562547e-03j,  7.4312232e-02+2.7383675e-09j]],
      dtype=complex64), array([[ 0.8011876 +2.96502609e-08j, -0.15794283+1.32115865e-02j,
        -0.03310011+1.69303343e-02j,  0.01295599+6.13177055e-03j],
       [-0.1579428 -1.32115539e-02j,  0.08720163-7.14557302e-11j,
         0.00596864-1.27087173e-03j, -0.00180657-6.33083330e-03j],
       [-0.03310017-1.69303194e-02j,  0.00596858+1.27086823e-03j,
         0.05988427-1.50541073e-08j, -0.00391752+6.82831858e-04j],
       [ 0.01295596-6.13173004e-03j, -0.00180648+6.33088313e-03j,
        -0.00391755-6.82874059e-04j,  0.05172533-7.07411640e-09j]],
      dtype=complex64), array([[ 0.78603995+3.3356063e-08j, -0.00436158+1.7191069e-02j,
        -0.00158394+1.3208469e-02j, -0.12887354+1.6205221e-01j],
       [-0.00436156-1.7191075e-02j,  0.05148041-1.8827432e-08j,
        -0.00088851+3.2436578e-03j,  0.00126167-2.6253278e-03j],
       [-0.00158406-1.3208531e-02j, -0.00088846-3.2436764e-03j,
         0.05831346+1.4567748e-09j, -0.00195425+4.8596229e-04j],
       [-0.12887357-1.6205220e-01j,  0.00126165+2.6253876e-03j,
        -0.00195431-4.8598435e-04j,  0.10416475-3.8337145e-08j]],
      dtype=complex64), array([[ 8.0162221e-01-7.6752276e-09j, -1.5803681e-01-1.9351071e-02j,
        -4.6951105e-04+1.6234804e-02j,  6.6591715e-03+6.2465994e-03j],
       [-1.5803675e-01+1.9351108e-02j,  8.8302836e-02-1.4748692e-08j,
        -1.2895665e-03-7.9333480e-04j, -2.5931135e-04-5.6354390e-03j],
       [-4.6953675e-04-1.6234808e-02j, -1.2895522e-03+7.9334120e-04j,
         5.8437109e-02-7.4878681e-09j, -3.3923942e-03+7.2707015e-04j],
       [ 6.6590882e-03-6.2466161e-03j, -2.5929345e-04+5.6354436e-03j,
        -3.3923511e-03-7.2703714e-04j,  5.1636584e-02+3.4229594e-10j]],
      dtype=complex64), array([[ 0.708969  -2.0525977e-08j,  0.04701609+1.2493659e-02j,
        -0.00099883+2.6755899e-01j,  0.0046451 +1.3918953e-01j],
       [ 0.04701601-1.2493660e-02j,  0.0563927 +1.1100992e-08j,
         0.00274669+2.0817427e-02j,  0.00183519+5.2773873e-03j],
       [-0.00099884-2.6755902e-01j,  0.00274669-2.0817466e-02j,
         0.15418473-1.4715269e-08j,  0.05194992-4.7423290e-03j],
       [ 0.00464507-1.3918956e-01j,  0.00183513-5.2774390e-03j,
         0.05194991+4.7422890e-03j,  0.08045215+3.6511569e-09j]],
      dtype=complex64), array([[ 8.0670410e-01-5.3475611e-09j, -1.2708660e-02+1.5613300e-02j,
        -1.3636015e-03+4.8187021e-02j,  5.4213172e-03+1.6214412e-01j],
       [-1.2708641e-02-1.5613317e-02j,  5.2392010e-02-2.0689891e-08j,
        -6.4703618e-04+2.5427132e-03j,  1.2091032e-03-5.6675831e-03j],
       [-1.3635962e-03-4.8187066e-02j, -6.4706430e-04-2.5427367e-03j,
         5.8427170e-02-1.5581958e-09j,  6.6996180e-03-1.7740536e-03j],
       [ 5.4213284e-03-1.6214415e-01j,  1.2091243e-03+5.6675663e-03j,
         6.6997241e-03+1.7740884e-03j,  8.2475320e-02+5.4767351e-09j]],
      dtype=complex64)]
before: [array([[ 9.9609792e-01+9.8370947e-09j, -1.9488607e-03-0.0000000e+00j,
         5.9604645e-08-4.4059888e-02j,  0.0000000e+00+4.4059854e-02j],
       [-1.9488645e-03-0.0000000e+00j,  3.8353633e-06-6.0535967e-09j,
         0.0000000e+00+8.6219938e-05j,  1.9754225e-09-8.6173182e-05j],
       [ 4.6755304e-08+4.4059917e-02j,  3.7252903e-09-8.6167252e-05j,
         1.9488953e-03+6.1118044e-09j, -1.9489019e-03-5.1222742e-09j],
       [-4.6129571e-09-4.4059910e-02j,  1.6763806e-08+8.6217304e-05j,
        -1.9489131e-03+5.1222742e-09j,  1.9488544e-03-9.7206794e-09j]],
      dtype=complex64), array([[ 9.9199200e-01+1.3969839e-08j, -9.3132213e-10-0.0000000e+00j,
        -3.7252903e-09-0.0000000e+00j,  3.4771472e-02+8.2060024e-02j],
       [-8.3819023e-09-2.1073424e-08j,  2.1073424e-08+8.3819023e-09j,
         2.8975958e-08+0.0000000e+00j,  0.0000000e+00+0.0000000e+00j],
       [-1.3170890e-09-6.5854433e-10j,  2.1731967e-08+1.3170890e-09j,
         3.2927261e-10+9.3132257e-09j,  9.3132257e-09+2.0744151e-08j],
       [ 3.4771465e-02-8.2059987e-02j,  7.4505806e-09+0.0000000e+00j,
         1.8626451e-09-2.0489097e-08j,  8.0070579e-03+1.3038515e-08j]],
      dtype=complex64), array([[ 9.9804676e-01+9.3132257e-09j, -1.7223688e-02-4.0647537e-02j,
         1.8626449e-09-1.4901160e-08j,  1.4901160e-08+9.3132249e-09j],
       [-1.7223677e-02+4.0647537e-02j,  1.9526854e-03+1.8626451e-09j,
        -1.4901160e-08-1.8626449e-09j,  9.3132249e-09-1.4901160e-08j],
       [ 9.3745660e-09+2.6341777e-08j, -1.5805067e-08+1.8013040e-09j,
        -5.2683555e-09-1.8013040e-09j,  9.3745660e-09+5.2683555e-09j],
       [ 2.6341777e-08-9.3745660e-09j,  1.8013040e-09+1.5805067e-08j,
        -1.8013040e-09+5.2683555e-09j,  5.2683555e-09-9.3745660e-09j]],
      dtype=complex64), array([[ 8.3504856e-01-7.4505806e-09j, -1.6961890e-01-2.9802322e-08j,
         0.0000000e+00-3.2350206e-01j, -1.4901161e-08+6.5711237e-02j],
       [-1.6961890e-01-2.0489097e-08j,  3.4453765e-02-7.4505806e-09j,
        -1.5832484e-08+6.5711237e-02j,  0.0000000e+00-1.3347564e-02j],
       [ 1.1175871e-08+3.2350206e-01j,  1.4901161e-08-6.5711230e-02j,
         1.2532637e-01-6.5192580e-09j, -2.5456864e-02-7.4505806e-09j],
       [ 1.5832484e-08-6.5711208e-02j,  7.4505806e-09+1.3347554e-02j,
        -2.5456866e-02-1.5832484e-08j,  5.1708915e-03+1.8626451e-09j]],
      dtype=complex64), array([[ 9.5268506e-01+1.58324838e-08j, -1.7385725e-02-0.00000000e+00j,
        -8.5591502e-02+2.23517418e-08j, -1.9351372e-01-0.00000000e+00j],
       [-1.7385717e-02-0.00000000e+00j,  3.1728146e-04+8.38190317e-09j,
         1.5619569e-03-1.31549314e-08j,  3.5314527e-03-1.57742761e-08j],
       [-8.5591510e-02-0.00000000e+00j,  1.5620149e-03+1.86264515e-08j,
         7.6897573e-03-1.21071935e-08j,  1.7385736e-02+5.35510480e-09j],
       [-1.9351368e-01-1.17579475e-08j,  3.5314593e-03+0.00000000e+00j,
         1.7385729e-02+5.58793545e-09j,  3.9307371e-02-2.70083547e-08j]],
      dtype=complex64), array([[ 9.5849967e-01-2.9802322e-08j, -1.9469485e-01-0.0000000e+00j,
        -4.2396825e-02-7.4505806e-09j,  8.6118355e-03+1.1175871e-08j],
       [-1.9469480e-01-0.0000000e+00j,  3.9547313e-02-1.4901161e-08j,
         8.6118355e-03+5.9371814e-09j, -1.7492753e-03+7.4505806e-09j],
       [-4.2396821e-02+4.6566129e-10j,  8.6118542e-03-9.3132257e-09j,
         1.8753237e-03+3.2450771e-09j, -3.8092351e-04-1.0652002e-08j],
       [ 8.6118439e-03-1.3853423e-08j, -1.7492819e-03-1.1175871e-08j,
        -3.8092534e-04-8.0035534e-11j,  7.7371158e-05+4.9476512e-10j]],
      dtype=complex64), array([[ 9.2389011e-01+2.2351742e-08j,  3.3527613e-08+0.0000000e+00j,
         2.2351742e-08-3.7252903e-08j, -1.7961612e-01+1.9507560e-01j],
       [ 1.1175871e-08+0.0000000e+00j, -4.2146848e-08+2.9802322e-08j,
        -5.2683555e-09-4.2146848e-08j, -1.0536713e-08-2.6341780e-08j],
       [ 0.0000000e+00-1.5805067e-08j, -1.5805067e-08+2.1073424e-08j,
         2.2390513e-08-3.7252901e-09j,  7.4505797e-09+3.0293048e-08j],
       [-1.7961609e-01-1.9507569e-01j,  2.2351742e-08-2.2351742e-08j,
         4.0978193e-08+1.4901161e-08j,  7.6109059e-02+2.2351742e-08j]],
      dtype=complex64), array([[ 9.5857680e-01+2.2351742e-08j, -1.9431385e-01-4.4146031e-02j,
         7.4505788e-09+0.0000000e+00j,  0.0000000e+00-2.2351738e-08j],
       [-1.9431387e-01+4.4146061e-02j,  4.1422658e-02+0.0000000e+00j,
         0.0000000e+00+8.1956387e-08j,  7.4505770e-09+0.0000000e+00j],
       [ 5.2683555e-09-5.2683555e-09j, -5.2683555e-09-5.2683555e-09j,
        -5.2683555e-09-5.2683555e-09j, -5.2683555e-09+5.2683555e-09j],
       [-5.2683555e-09-5.2683555e-09j, -5.2683555e-09+5.2683555e-09j,
        -5.2683555e-09+5.2683555e-09j,  5.2683555e-09+5.2683555e-09j]],
      dtype=complex64), array([[ 8.3504808e-01+0.0000000e+00j,  6.5711185e-02+2.9802322e-08j,
         0.0000000e+00+3.2350200e-01j, -1.4901161e-08+1.6961882e-01j],
       [ 6.5711148e-02+2.0954758e-08j,  5.1709334e-03+0.0000000e+00j,
         1.0710210e-08+2.5456863e-02j,  1.8626451e-08+1.3347590e-02j],
       [ 2.7939677e-09-3.2350200e-01j, -8.9406967e-08-2.5456868e-02j,
         1.2532634e-01+1.1175871e-08j,  6.5711215e-02-7.4505806e-09j],
       [-5.1222742e-09-1.6961879e-01j,  7.4505806e-09-1.3347607e-02j,
         6.5711141e-02-1.8626451e-09j,  3.4453794e-02-1.1175871e-08j]],
      dtype=complex64), array([[ 9.5849931e-01+2.6077032e-08j, -8.6118244e-03-0.0000000e+00j,
        -1.1175871e-07+4.2396754e-02j,  2.9802322e-08+1.9469473e-01j],
       [-8.6118542e-03+4.2145984e-08j,  7.7339471e-05-1.8626451e-08j,
        -1.5133992e-08-3.8094955e-04j,  3.6874553e-08-1.7492318e-03j],
       [-4.2142347e-08-4.2396791e-02j, -3.1664968e-08+3.8093375e-04j,
         1.8753232e-03+3.1664968e-08j,  8.6118486e-03-2.0430889e-08j],
       [-3.1664968e-08-1.9469476e-01j,  0.0000000e+00+1.7492931e-03j,
         8.6117778e-03-2.0489097e-08j,  3.9547294e-02-4.2840838e-08j]],
      dtype=complex64)]
after:  [array([[ 9.9609792e-01+9.8370947e-09j, -1.9488607e-03-0.0000000e+00j,
         5.9604645e-08-4.4059888e-02j,  0.0000000e+00+4.4059854e-02j],
       [-1.9488645e-03-0.0000000e+00j,  3.8353633e-06-6.0535967e-09j,
         0.0000000e+00+8.6219938e-05j,  1.9754225e-09-8.6173182e-05j],
       [ 4.6755304e-08+4.4059917e-02j,  3.7252903e-09-8.6167252e-05j,
         1.9488953e-03+6.1118044e-09j, -1.9489019e-03-5.1222742e-09j],
       [-4.6129571e-09-4.4059910e-02j,  1.6763806e-08+8.6217304e-05j,
        -1.9489131e-03+5.1222742e-09j,  1.9488544e-03-9.7206794e-09j]],
      dtype=complex64), array([[ 9.9199200e-01+1.3969839e-08j, -9.3132213e-10-0.0000000e+00j,
        -3.7252903e-09-0.0000000e+00j,  3.4771472e-02+8.2060024e-02j],
       [-8.3819023e-09-2.1073424e-08j,  2.1073424e-08+8.3819023e-09j,
         2.8975958e-08+0.0000000e+00j,  0.0000000e+00+0.0000000e+00j],
       [-1.3170890e-09-6.5854433e-10j,  2.1731967e-08+1.3170890e-09j,
         3.2927261e-10+9.3132257e-09j,  9.3132257e-09+2.0744151e-08j],
       [ 3.4771465e-02-8.2059987e-02j,  7.4505806e-09+0.0000000e+00j,
         1.8626451e-09-2.0489097e-08j,  8.0070579e-03+1.3038515e-08j]],
      dtype=complex64), array([[ 9.9804676e-01+9.3132257e-09j, -1.7223688e-02-4.0647537e-02j,
         1.8626449e-09-1.4901160e-08j,  1.4901160e-08+9.3132249e-09j],
       [-1.7223677e-02+4.0647537e-02j,  1.9526854e-03+1.8626451e-09j,
        -1.4901160e-08-1.8626449e-09j,  9.3132249e-09-1.4901160e-08j],
       [ 9.3745660e-09+2.6341777e-08j, -1.5805067e-08+1.8013040e-09j,
        -5.2683555e-09-1.8013040e-09j,  9.3745660e-09+5.2683555e-09j],
       [ 2.6341777e-08-9.3745660e-09j,  1.8013040e-09+1.5805067e-08j,
        -1.8013040e-09+5.2683555e-09j,  5.2683555e-09-9.3745660e-09j]],
      dtype=complex64), array([[ 8.3504856e-01-7.4505806e-09j, -1.6961890e-01-2.9802322e-08j,
         0.0000000e+00-3.2350206e-01j, -1.4901161e-08+6.5711237e-02j],
       [-1.6961890e-01-2.0489097e-08j,  3.4453765e-02-7.4505806e-09j,
        -1.5832484e-08+6.5711237e-02j,  0.0000000e+00-1.3347564e-02j],
       [ 1.1175871e-08+3.2350206e-01j,  1.4901161e-08-6.5711230e-02j,
         1.2532637e-01-6.5192580e-09j, -2.5456864e-02-7.4505806e-09j],
       [ 1.5832484e-08-6.5711208e-02j,  7.4505806e-09+1.3347554e-02j,
        -2.5456866e-02-1.5832484e-08j,  5.1708915e-03+1.8626451e-09j]],
      dtype=complex64), array([[ 9.5268506e-01+1.58324838e-08j, -1.7385725e-02-0.00000000e+00j,
        -8.5591502e-02+2.23517418e-08j, -1.9351372e-01-0.00000000e+00j],
       [-1.7385717e-02-0.00000000e+00j,  3.1728146e-04+8.38190317e-09j,
         1.5619569e-03-1.31549314e-08j,  3.5314527e-03-1.57742761e-08j],
       [-8.5591510e-02-0.00000000e+00j,  1.5620149e-03+1.86264515e-08j,
         7.6897573e-03-1.21071935e-08j,  1.7385736e-02+5.35510480e-09j],
       [-1.9351368e-01-1.17579475e-08j,  3.5314593e-03+0.00000000e+00j,
         1.7385729e-02+5.58793545e-09j,  3.9307371e-02-2.70083547e-08j]],
      dtype=complex64), array([[ 9.5849967e-01-2.9802322e-08j, -1.9469485e-01-0.0000000e+00j,
        -4.2396825e-02-7.4505806e-09j,  8.6118355e-03+1.1175871e-08j],
       [-1.9469480e-01-0.0000000e+00j,  3.9547313e-02-1.4901161e-08j,
         8.6118355e-03+5.9371814e-09j, -1.7492753e-03+7.4505806e-09j],
       [-4.2396821e-02+4.6566129e-10j,  8.6118542e-03-9.3132257e-09j,
         1.8753237e-03+3.2450771e-09j, -3.8092351e-04-1.0652002e-08j],
       [ 8.6118439e-03-1.3853423e-08j, -1.7492819e-03-1.1175871e-08j,
        -3.8092534e-04-8.0035534e-11j,  7.7371158e-05+4.9476512e-10j]],
      dtype=complex64), array([[ 9.2389011e-01+2.2351742e-08j,  3.3527613e-08+0.0000000e+00j,
         2.2351742e-08-3.7252903e-08j, -1.7961612e-01+1.9507560e-01j],
       [ 1.1175871e-08+0.0000000e+00j, -4.2146848e-08+2.9802322e-08j,
        -5.2683555e-09-4.2146848e-08j, -1.0536713e-08-2.6341780e-08j],
       [ 0.0000000e+00-1.5805067e-08j, -1.5805067e-08+2.1073424e-08j,
         2.2390513e-08-3.7252901e-09j,  7.4505797e-09+3.0293048e-08j],
       [-1.7961609e-01-1.9507569e-01j,  2.2351742e-08-2.2351742e-08j,
         4.0978193e-08+1.4901161e-08j,  7.6109059e-02+2.2351742e-08j]],
      dtype=complex64), array([[ 9.5857680e-01+2.2351742e-08j, -1.9431385e-01-4.4146031e-02j,
         7.4505788e-09+0.0000000e+00j,  0.0000000e+00-2.2351738e-08j],
       [-1.9431387e-01+4.4146061e-02j,  4.1422658e-02+0.0000000e+00j,
         0.0000000e+00+8.1956387e-08j,  7.4505770e-09+0.0000000e+00j],
       [ 5.2683555e-09-5.2683555e-09j, -5.2683555e-09-5.2683555e-09j,
        -5.2683555e-09-5.2683555e-09j, -5.2683555e-09+5.2683555e-09j],
       [-5.2683555e-09-5.2683555e-09j, -5.2683555e-09+5.2683555e-09j,
        -5.2683555e-09+5.2683555e-09j,  5.2683555e-09+5.2683555e-09j]],
      dtype=complex64), array([[ 8.3504808e-01+0.0000000e+00j,  6.5711185e-02+2.9802322e-08j,
         0.0000000e+00+3.2350200e-01j, -1.4901161e-08+1.6961882e-01j],
       [ 6.5711148e-02+2.0954758e-08j,  5.1709334e-03+0.0000000e+00j,
         1.0710210e-08+2.5456863e-02j,  1.8626451e-08+1.3347590e-02j],
       [ 2.7939677e-09-3.2350200e-01j, -8.9406967e-08-2.5456868e-02j,
         1.2532634e-01+1.1175871e-08j,  6.5711215e-02-7.4505806e-09j],
       [-5.1222742e-09-1.6961879e-01j,  7.4505806e-09-1.3347607e-02j,
         6.5711141e-02-1.8626451e-09j,  3.4453794e-02-1.1175871e-08j]],
      dtype=complex64), array([[ 9.5849931e-01+2.6077032e-08j, -8.6118244e-03-0.0000000e+00j,
        -1.1175871e-07+4.2396754e-02j,  2.9802322e-08+1.9469473e-01j],
       [-8.6118542e-03+4.2145984e-08j,  7.7339471e-05-1.8626451e-08j,
        -1.5133992e-08-3.8094955e-04j,  3.6874553e-08-1.7492318e-03j],
       [-4.2142347e-08-4.2396791e-02j, -3.1664968e-08+3.8093375e-04j,
         1.8753232e-03+3.1664968e-08j,  8.6118486e-03-2.0430889e-08j],
       [-3.1664968e-08-1.9469476e-01j,  0.0000000e+00+1.7492931e-03j,
         8.6117778e-03-2.0489097e-08j,  3.9547294e-02-4.2840838e-08j]],
      dtype=complex64)]
mitigated_measurement =  1.0327889695283674
error_unmitigated = abs(unmitigated_measurement-ideal_measurement)
error_mitigated = abs(mitigated_measurement-ideal_measurement)

print("Error (unmitigated):", error_unmitigated)
print("Error (mitigated with CDR):", error_mitigated)

print("Relative error (unmitigated):", (error_unmitigated/ideal_measurement))
print("Relative error (mitigated with CDR):", error_mitigated/ideal_measurement)

print(f"Error reduction with CDR: {(error_unmitigated-error_mitigated)/error_unmitigated :.1%}.")
Error (unmitigated): 0.21227829775307328
Error (mitigated with CDR): 0.017416734666069633
Relative error (unmitigated): 0.2090645090190613
Relative error (mitigated with CDR): 0.01715305389302047
Error reduction with CDR: 91.8%.