Skip to content

Commit

Permalink
Add cal time and backend name to saved data
Browse files Browse the repository at this point in the history
  • Loading branch information
nonhermitian committed Jan 14, 2022
1 parent c6881b3 commit 4169126
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ include README.md
include requirements.txt
recursive-include mthree *.pyx
recursive-include mthree *.pxd
recursive-include mthree/test/data *.json
30 changes: 25 additions & 5 deletions mthree/mitigation.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from time import perf_counter

import psutil
import datetime
import numpy as np
import scipy.linalg as la
import scipy.sparse.linalg as spla
Expand Down Expand Up @@ -129,6 +130,7 @@ def __init__(self, system=None, iter_threshold=4096):
self.iter_threshold = iter_threshold
self.cal_shots = None
self.cal_method = 'balanced'
self.cal_timestamp = None # The time at which the cals result was generated
self.rep_delay = None
# attributes for handling threaded job
self._thread = None
Expand Down Expand Up @@ -226,6 +228,7 @@ def cals_from_system(self, qubits=None, shots=None, method='balanced',
qubits = range(self.num_qubits)
self.cal_method = method
self.rep_delay = rep_delay
self.cal_timestamp = None
self._grab_additional_cals(qubits, shots=shots, method=method,
rep_delay=rep_delay, initial_reset=initial_reset)
if cals_file:
Expand All @@ -242,8 +245,16 @@ def cals_from_file(self, cals_file):
if self._thread:
raise M3Error('Calibration currently in progress.')
with open(cals_file, 'r', encoding='utf-8') as fd:
self.single_qubit_cals = [np.asarray(cal) if cal else None
for cal in orjson.loads(fd.read())]
loaded_data = orjson.loads(fd.read())
if isinstance(loaded_data, dict):
self.single_qubit_cals = [np.asarray(cal) if cal else None
for cal in loaded_data['cals']]
self.cal_timestamp = loaded_data['timestamp']
else:
warnings.warn('Loading from old M3 file format. Save again to update.')
self.cal_timestamp = None
self.single_qubit_cals = [np.asarray(cal) if cal else None
for cal in loaded_data]

def cals_to_file(self, cals_file=None):
"""Save calibration data to JSON file.
Expand All @@ -259,8 +270,11 @@ def cals_to_file(self, cals_file=None):
raise M3Error('cals_file must be explicitly set.')
if not self.single_qubit_cals:
raise M3Error('Mitigator is not calibrated.')
save_dict = {'timestamp': self.cal_timestamp,
'backend': self.system.name(),
'cals': self.single_qubit_cals}
with open(cals_file, 'wb') as fd:
fd.write(orjson.dumps(self.single_qubit_cals,
fd.write(orjson.dumps(save_dict,
option=orjson.OPT_SERIALIZE_NUMPY))

def tensored_cals_from_file(self, cals_file):
Expand Down Expand Up @@ -675,11 +689,18 @@ def _job_thread(job, mit, method, qubits, num_cal_qubits, cal_strings):
cal_strings (list): List of cal strings for balanced cals
"""
try:
counts = job.result().get_counts()
res = job.result()
# pylint: disable=broad-except
except Exception as error:
mit._job_error = error
return
counts = res.get_counts()
# attach timestamp
timestamp = res.date
# Needed since Aer result date is str but IBMQ job is datetime
if isinstance(timestamp, datetime.datetime):
timestamp = timestamp.isoformat()
mit.cal_timestamp = timestamp
# A list of qubits with bad meas cals
bad_list = []
if method == 'independent':
Expand Down Expand Up @@ -719,7 +740,6 @@ def _job_thread(job, mit, method, qubits, num_cal_qubits, cal_strings):
mit.single_qubit_cals[qubit][:, 1] = [P01, P11]
if P01 >= P00:
bad_list.append(qubit)

# balanced calibration
else:
cals = [np.zeros((2, 2), dtype=float) for kk in range(num_cal_qubits)]
Expand Down
10 changes: 10 additions & 0 deletions mthree/test/test_file_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ def test_load_cals_from_file():

mit2_counts = mit.apply_correction(raw_counts, qubits=range(5))
assert mit2_counts is not None
# Check that timestamps got set
assert mit2.cal_timestamp == mit.cal_timestamp


def test_load_cals_from_file2():
Expand Down Expand Up @@ -81,3 +83,11 @@ def test_load_cals_from_file2():

mit2_counts = mit.apply_correction(raw_counts, qubits=range(5))
assert mit2_counts is not None

def test_load_old_cals():
"""Check old cals can be loaded"""

mit = mthree.M3Mitigation()
mit.cals_from_file('data/8Qcal_Hanoi.json')

assert len(mit.single_qubit_cals) == 27

0 comments on commit 4169126

Please sign in to comment.