2
$\begingroup$

I am stuck with a problem while trying to run the code on quantum hardware. What I have learned through video lectures is that you can save the job id when you run job on hardware and from job id retreive the results because quantum HW sometimes takes a lot of time due to big queues. But in the below code, when I use m3_mitigator.cals_from_system(final_maps) , it calls the actual hardware, which may take up a lot of time. Not sure how to deal in this case? Any suggestions would be great help.

  m3_mitigator = mthree.M3Mitigation(backend)

  final_maps = final_measurement_mapping(pt_circs)
        
  m3_mitigator.cals_from_system(final_maps)  # Here, the CODE is sending job to the HW

  max_shots = 100000
  
  job = backend.run(pt_circs, shots = max_shots)
  print(job.job_id())         # I will save it to some csv file and later retrieve results

Edit 1

I just realized, I am doing one more mistake. I have to somehow apply the correction also. Should I apply corrections after retreiveing results from job_id? Thank you for great and kind help.

$\endgroup$

1 Answer 1

1
$\begingroup$

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))

enter image description here

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'])

enter image description here

$\endgroup$
6
  • $\begingroup$ Thank you for the response @taketoshikinoshita. So, I have to like first save the calib.json and meas_mapping.json files in my system (which needs qiskit hardware and will take time). After I get the result, I can run my remaining code. $\endgroup$
    – Manu
    Commented Jul 18 at 6:31
  • $\begingroup$ But during this time till the m3_mitigator.cals_from_system(final_maps) is working, system terminal is always busy. Any solution for this problem? $\endgroup$
    – Manu
    Commented Jul 18 at 6:37
  • $\begingroup$ I edited my answer. we don't have to call cals_from_system each time. Once we call it and save a calibration data to a file, then we can skip it for the later code. Please take a look at the edited code. $\endgroup$ Commented Jul 18 at 8:07
  • $\begingroup$ since cals_from_system throws a job to backend to grab a calib data I assume it took some time for some reason that made a system busy. here $\endgroup$ Commented Jul 18 at 9:38
  • $\begingroup$ A fake backend model with noise can also be used, I edited the code with that. I hope it can solve the system-busy problem. $\endgroup$ Commented Jul 18 at 12:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.