Shortcuts

Utility functions

There are a couple of functionalities that are needed for easy mitigation, but are not part of the Qiskit SDK and thus are included in M3. Each is detailed below.

Final-measurement mapping

We have already seen the mthree.utils.final_measurement_mapping() routine in the section on detailing with Transpiled circuits, where the addition of SWAP gates made it difficult to determine exactly which physical qubits are being used, and which classical bits they correspond to. Here we show another example of this usage. First we start with a 7-qubit GHZ state:

from qiskit import *
from qiskit.providers.fake_provider import FakeCasablanca
import mthree

qc = QuantumCircuit(7)
qc.h(0)
qc.cx(0, range(1,7))
qc.measure_all()
qc.draw('mpl')
_images/utils_0_0.png

and then transpile it:

backend = FakeCasablanca()
trans_qc = transpile(qc, backend, optimization_level=3, seed_transpiler=54321)
trans_qc.draw('mpl')
_images/utils_1_0.png

Once again we see that the physical qubits used and to which classical bits they map to is non-trivial to find. Yet this information is critical for successfully mitigating the results. This is where the mthree.utils.final_measurement_mapping() plays a critical role:

mapping = mthree.utils.final_measurement_mapping(trans_qc)
mapping
{0: 5, 1: 2, 2: 0, 3: 3, 4: 1, 5: 4, 6: 6}

The keys of this mapping index the classical bits, whereas the values tell you which qubits were measured to obtain the bit values. The mapping is ordered in terms of the classical bit indices. You can just pass the generated mapping into M3 functions.

Evaluating raw counts data

When mitigating results one often wants to know how much better the results are with mitigation compared to without. However, Qiskit does not have great support for computing things like expectation values. So M3 includes the generic functions mthree.utils.expval(), mthree.utils.stddev(), and mthree.utils.expval_and_stddev() that operate on the native Counts objects in Qiskit.

For example let us compare raw data verse the mitigated results in a simple case.

from qiskit.providers.fake_provider import FakeAthens
backend = FakeAthens()
qc = QuantumCircuit(4)
qc.h(2)
qc.cx(2, 1)
qc.cx(2, 3)
qc.cx(1, 0)
qc.measure_all()

raw_counts = execute(qc, backend).result().get_counts()
mit = mthree.M3Mitigation(backend)
mit.cals_from_system()
mit_counts = mit.apply_correction(raw_counts, qubits=range(4),
                                  return_mitigation_overhead=True)

print('Raw counts expval', mthree.utils.expval(raw_counts))
print('Mitigated expval', mit_counts.expval())
Raw counts expval 0.845703125
Mitigated expval 0.960046073456911

We can also compare things like upper-bounds on the standard deviation:

print('Raw counts uncertainty', mthree.utils.stddev(raw_counts))
print('Mitigated uncertainty', mit_counts.stddev())
Raw counts uncertainty 0.03125
Mitigated uncertainty 0.038158634538763725

where the uncertainty for the raw Counts data is just \(1/\sqrt{\rm{shots}}\).

These convenence functions work in the same manner as the methods for the distribution classes mthree.classes.QuasiDistribution and mthree.classes.ProbDistribution and collections mthree.classes.QuasiCollection and mthree.classes.ProbCollection. That is to say that, for example, I can pass operators to expval function:

print('These should be equal:', mthree.utils.expval(raw_counts, 'IIII'),
      mit_counts.expval('IIII'))
These should be equal: 1.0 0.9999999999999999

The routines also allow you to pass the native M3 distributions and collections. E.g.

print(mthree.utils.expval(mit_counts), mit_counts.expval())
0.960046073456911 0.960046073456911

Finally we note that you can pass multiple values at the same time. Here we run and mitigate 5 circuits:

raw_counts = execute([qc]*5, backend).result().get_counts()
mit = mthree.M3Mitigation(backend)
mit.cals_from_system()
mit_counts = mit.apply_correction(raw_counts, qubits=range(4),
                                  return_mitigation_overhead=True)


print('Raw counts expval', mthree.utils.expval(raw_counts))
print('Mitigated expval', mit_counts.expval())
Raw counts expval [0.8125     0.859375   0.83007812 0.84765625 0.83203125]
Mitigated expval [0.91528793 0.96697135 0.93218704 0.95195466 0.93228934]