First, run h/w calibration then save it to a file.
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister, transpile
from qiskit_aer import AerSimulator, Aer
from qiskit.visualization import plot_histogram
from qiskit_ibm_runtime import QiskitRuntimeService
from qiskit_ibm_provider import IBMProvider, least_busy
from qiskit_ibm_runtime.fake_provider import FakeSherbrooke, FakeBrisbane
from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager
from qiskit_ibm_runtime import Session, SamplerV2 as Sampler
from mthree import M3Mitigation
import mthree.utils as mutils
import json
import time
#real_backend = provider.get_backend('ibm_brisbane')
service = QiskitRuntimeService()
fake_be = FakeBrisbane()
# Create a quantum circuit
qc = QuantumCircuit(2)
qc.h(0)
qc.cx(0, 1)
qc.measure_all()
transpiled_qc = transpile(qc, fake_be)
start = time.time()
# Get the final measurement mapping
meas_mapping = mutils.final_measurement_mapping(transpiled_qc)
end = time.time()
t_final_measurement_mapping = end - start
start = time.time()
# Initialize M3Mitigation object
mit = M3Mitigation(fake_be)
end = time.time()
t_initalize = end - start
start = time.time()
# Perform the calibration
mit.cals_from_system(meas_mapping)
end = time.time()
t_cals_from_system = end - start
start = time.time()
# Save the calibration data and measurement mapping to files
mit.cals_to_file('calibration_data.json')
end = time.time()
t_cals_to_file = end - start
start = time.time()
# Save measurement mapping to a file
with open('measurement_mapping.json', 'w') as f:
json.dump(meas_mapping, f)
end = time.time()
t_dump_meas_mapping = end - start
print("final_measurement_mapping : %d(sec)"%(t_final_measurement_mapping))
print("initialize : %d(sec)"%(t_initalize))
print("cals_from_system : %d(sec)"%(t_cals_from_system))
print("cals_to_file : %d(sec)"%(t_cals_to_file))
print("dump : %d(sec)"%(t_dump_meas_mapping))
Second, load the calibration data, run the circuit and apply correction.
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister
from qiskit_aer import AerSimulator, Aer
from qiskit.visualization import plot_histogram
from qiskit_ibm_runtime import QiskitRuntimeService
from qiskit_ibm_provider import IBMProvider, least_busy
from qiskit_ibm_runtime.fake_provider import FakeSherbrooke, FakeBrisbane
from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager
from qiskit_ibm_runtime import Session, SamplerV2 as Sampler
from mthree import M3Mitigation
import mthree.utils as mutils
import json
import time
hub = "ibm-q"
group = "open"
project = "main"
provider = IBMProvider(instance=f"{hub}/{group}/{project}")
service = QiskitRuntimeService()
#real_be = provider.get_backend('ibm_brisbane')
fake_be = FakeBrisbane()
# Create a quantum circuit
qc = QuantumCircuit(2)
qc.h(0)
qc.cx(0, 1)
qc.measure_all()
transpiled_qc = transpile(qc, fake_be)
job = fake_be.run(transpiled_qc, shots=1000)
result = job.result()
counts = result.get_counts()
# load calib from a file
start = time.time()
m3_mitigator_loaded = M3Mitigation()
end = time.time()
t_initalize = end - start
start = time.time()
m3_mitigator_loaded.cals_from_file('calibration_data.json')
end = time.time()
t_cals_from_file = end - start
# load measurement mapping from the file
start = time.time()
with open('measurement_mapping.json', 'r') as f:
meas_mapping_loaded = json.load(f)
end = time.time()
t_load_meas_mapping = end - start
start = time.time()
mitigated_counts = m3_mitigator_loaded.apply_correction(counts, meas_mapping_loaded)
end = time.time()
t_apply_correction = end - start
print("initialize : %d(sec)"%(t_initalize))
print("cals_from_file : %d(sec)"%(t_cals_from_file))
print("load meas mapping : %d(sec)"%(t_load_meas_mapping))
print("apply correction : %d(sec)"%(t_apply_correction))
# plot
plot_histogram([counts, mitigated_counts], legend=['raw', 'mitigated'])