Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion qiskit_experiments/calibration_management/calibrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -1063,7 +1063,13 @@ def load_parameter_values(self, file_name: str = "parameter_values.csv"):
param_val = ParameterValue(
row["value"], row["date_time"], row["valid"], row["exp_id"], row["group"]
)
key = ParameterKey(row["parameter"], self._to_tuple(row["qubits"]), row["schedule"])

if row["schedule"] == "":
schedule_name = None
else:
schedule_name = row["schedule"]

key = ParameterKey(row["parameter"], self._to_tuple(row["qubits"]), schedule_name)
self.add_parameter_value(param_val, *key)

@classmethod
Expand Down
29 changes: 29 additions & 0 deletions test/calibration/test_calibrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@
from qiskit.pulse.transforms import inline_subroutines, block_to_schedule
import qiskit.pulse as pulse
from qiskit.test import QiskitTestCase
from qiskit.test.mock import FakeArmonk
from qiskit_experiments.calibration_management.calibrations import Calibrations, ParameterKey
from qiskit_experiments.calibration_management.parameter_value import ParameterValue
from qiskit_experiments.calibration_management.basis_gate_library import FixedFrequencyTransmon
from qiskit_experiments.calibration_management import BackendCalibrations
from qiskit_experiments.exceptions import CalibrationError


Expand Down Expand Up @@ -1352,3 +1355,29 @@ def test_alternate_date_formats(self):
self.addCleanup(self._remove_files, self._prefix)
self.cals._params = defaultdict(list)
self.cals.load_parameter_values(self._prefix + "parameter_values.csv")

def test_save_load_library(self):
"""Test that we can load and save a library.

These libraries contain both parameters with schedules and parameters without
any schedules (e.g. frequencies for qubits and readout).
"""

library = FixedFrequencyTransmon()
backend = FakeArmonk()
cals = BackendCalibrations(backend, library)

cals.parameters_table()

cals.save(file_type="csv", overwrite=True, file_prefix=self._prefix)

cals.load_parameter_values(self._prefix + "parameter_values.csv")

# Test the value of a few loaded params.
self.assertEqual(cals.get_parameter_value("amp", (0,), "x"), 0.5)
self.assertEqual(
cals.get_parameter_value("qubit_lo_freq", (0,)),
backend.defaults().qubit_freq_est[0],
)

self.addCleanup(self._remove_files, self._prefix)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be the first line after save() so that files are always cleaned up even if the test encounters an error.

More generally, since all the test methods in this class use this same addCleanup call, it should probably just go in a tearDown method (with _remove_files made robust to the case where the files were not created). Even better would be to use the tempfile module to make a temporary directory for the files rather than writing them the working directory of the test.