-
Notifications
You must be signed in to change notification settings - Fork 131
Fine drag cal refactor #519
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
eggerdj
merged 10 commits into
qiskit-community:main
from
eggerdj:fine_drag_cal_refactor
Nov 10, 2021
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0f92d0f
* First draft of framework to save and rebuild calibrations from meta…
eggerdj b2c0ab8
Merge branch 'main' of github.com:Qiskit/qiskit-experiments into main
eggerdj a4c1688
* Moved FineDrag to characterization and created a calibration versio…
eggerdj e35f89e
* Undo git merging issues.
eggerdj 5b576b5
* Fix merge issues.
eggerdj 49f13ff
* Lint
eggerdj fa7bb7c
* Docs.
eggerdj d091f91
* Lint.
eggerdj 612e735
* Docs
eggerdj 2b85dda
* Docs.
eggerdj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
221 changes: 221 additions & 0 deletions
221
qiskit_experiments/library/calibration/fine_drag_cal.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,221 @@ | ||
| # This code is part of Qiskit. | ||
| # | ||
| # (C) Copyright IBM 2021. | ||
| # | ||
| # This code is licensed under the Apache License, Version 2.0. You may | ||
| # obtain a copy of this license in the LICENSE.txt file in the root directory | ||
| # of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. | ||
| # | ||
| # Any modifications or derivative works of this code must retain this | ||
| # copyright notice, and modified files need to carry a notice indicating | ||
| # that they have been altered from the originals. | ||
|
|
||
| """Fine drag calibration experiment.""" | ||
|
|
||
| from typing import List, Optional | ||
| import numpy as np | ||
|
|
||
| from qiskit.circuit import Gate, QuantumCircuit | ||
| from qiskit.providers.backend import Backend | ||
| from qiskit.pulse import Play | ||
|
|
||
| from qiskit_experiments.exceptions import CalibrationError | ||
| from qiskit_experiments.framework import ExperimentData, fix_class_docs, Options | ||
| from qiskit_experiments.calibration_management import ( | ||
| BaseCalibrationExperiment, | ||
| BackendCalibrations, | ||
| ) | ||
| from qiskit_experiments.calibration_management.update_library import BaseUpdater | ||
| from qiskit_experiments.library.characterization.fine_drag import FineDrag | ||
|
|
||
|
|
||
| @fix_class_docs | ||
| class FineDragCal(BaseCalibrationExperiment, FineDrag): | ||
| """A calibration version of the fine drag experiment.""" | ||
|
|
||
| def __init__( | ||
| self, | ||
| qubit: int, | ||
| calibrations: BackendCalibrations, | ||
| schedule_name: str, | ||
| backend: Optional[Backend] = None, | ||
| cal_parameter_name: Optional[str] = "β", | ||
| auto_update: bool = True, | ||
| ): | ||
| r"""see class :class:`FineDrag` for details. | ||
|
|
||
| Note that this class implicitly assumes that the target angle of the gate | ||
| is :math:`\pi` as seen from the default experiment options. | ||
|
|
||
| Args: | ||
| qubit: The qubit for which to run the fine drag calibration. | ||
| calibrations: The calibrations instance with the schedules. | ||
| schedule_name: The name of the schedule to calibrate. | ||
| backend: Optional, the backend to run the experiment on. | ||
| cal_parameter_name: The name of the parameter in the schedule to update. | ||
| auto_update: Whether or not to automatically update the calibrations. By | ||
| default this variable is set to True. | ||
| """ | ||
| super().__init__( | ||
| calibrations, | ||
| qubit, | ||
| Gate(name=schedule_name, num_qubits=1, params=[]), | ||
| schedule_name=schedule_name, | ||
| backend=backend, | ||
| cal_parameter_name=cal_parameter_name, | ||
| auto_update=auto_update, | ||
| ) | ||
|
|
||
| self.set_transpile_options( | ||
| inst_map=calibrations.default_inst_map, | ||
| basis_gates=["sx", schedule_name, "rz"], | ||
| ) | ||
|
|
||
| @classmethod | ||
| def _default_experiment_options(cls) -> Options: | ||
| """Default experiment options. | ||
|
|
||
| Experiment Options: | ||
| target_angle (float): The target rotation angle of the gate being calibrated. | ||
| This value is needed for the update rule. | ||
| """ | ||
| options = super()._default_experiment_options() | ||
| options.target_angle = np.pi | ||
| return options | ||
|
|
||
| def _add_cal_metadata(self, circuits: List[QuantumCircuit]): | ||
| """Add metadata to the circuit to make the experiment data more self contained. | ||
|
|
||
| The following keys are added to each circuit's metadata: | ||
| cal_param_value: The value of the drag parameter. This value together with | ||
| the fit result will be used to find the new value of the drag parameter. | ||
| cal_param_name: The name of the parameter in the calibrations. | ||
| cal_schedule: The name of the schedule in the calibrations. | ||
| target_angle: The target angle of the gate. | ||
| cal_group: The calibration group to which the parameter belongs. | ||
| """ | ||
|
|
||
| param_val = self._cals.get_parameter_value( | ||
| self._param_name, | ||
| self.physical_qubits, | ||
| self._sched_name, | ||
| group=self.experiment_options.group, | ||
| ) | ||
|
|
||
| for circuit in circuits: | ||
| circuit.metadata["cal_param_value"] = param_val | ||
| circuit.metadata["cal_param_name"] = self._param_name | ||
| circuit.metadata["cal_schedule"] = self._sched_name | ||
| circuit.metadata["target_angle"] = self.experiment_options.target_angle | ||
| circuit.metadata["cal_group"] = self.experiment_options.group | ||
|
|
||
| def update_calibrations(self, experiment_data: ExperimentData): | ||
| """Update the drag parameter of the pulse in the calibrations.""" | ||
|
|
||
| data = experiment_data.data() | ||
|
|
||
| # No data -> no update | ||
| if len(data) > 0: | ||
|
|
||
| result_index = self.experiment_options.result_index | ||
| group = data[0]["metadata"]["cal_group"] | ||
| target_angle = data[0]["metadata"]["target_angle"] | ||
| qubits = experiment_data.metadata["physical_qubits"] | ||
|
|
||
| schedule = self._cals.get_schedule(self._sched_name, qubits) | ||
|
|
||
| # Obtain sigma as it is needed for the fine DRAG update rule. | ||
| sigmas = [] | ||
| for block in schedule.blocks: | ||
| if isinstance(block, Play) and hasattr(block.pulse, "sigma"): | ||
| sigmas.append(getattr(block.pulse, "sigma")) | ||
|
|
||
| if len(set(sigmas)) != 1: | ||
| raise CalibrationError( | ||
| "Cannot run fine Drag calibration on a schedule with multiple values of sigma." | ||
| ) | ||
|
|
||
| if len(sigmas) == 0: | ||
| raise CalibrationError(f"Could not infer sigma from {schedule}.") | ||
|
|
||
| d_theta = BaseUpdater.get_value(experiment_data, "d_theta", result_index) | ||
|
|
||
| # See the documentation in fine_drag.py for the derivation of this rule. | ||
| d_beta = -np.sqrt(np.pi) * d_theta * sigmas[0] / target_angle ** 2 | ||
| old_beta = data[0]["metadata"]["cal_param_value"] | ||
| new_beta = old_beta + d_beta | ||
|
|
||
| BaseUpdater.add_parameter_value( | ||
| self._cals, experiment_data, new_beta, self._param_name, schedule, group | ||
| ) | ||
|
|
||
|
|
||
| @fix_class_docs | ||
| class FineXDragCal(FineDragCal): | ||
| """Fine drag calibration of X gate.""" | ||
|
|
||
| def __init__( | ||
| self, | ||
| qubit: int, | ||
| calibrations: BackendCalibrations, | ||
| backend: Optional[Backend] = None, | ||
| cal_parameter_name: Optional[str] = "β", | ||
| auto_update: bool = True, | ||
| ): | ||
| r"""see class :class:`FineDrag` for details. | ||
|
|
||
| Args: | ||
| qubit: The qubit for which to run the fine drag calibration. | ||
| calibrations: The calibrations instance with the schedules. | ||
| backend: Optional, the backend to run the experiment on. | ||
| cal_parameter_name: The name of the parameter in the schedule to update. | ||
| auto_update: Whether or not to automatically update the calibrations. By | ||
| default this variable is set to True. | ||
| """ | ||
| super().__init__( | ||
| qubit, | ||
| calibrations, | ||
| schedule_name="x", | ||
| backend=backend, | ||
| cal_parameter_name=cal_parameter_name, | ||
| auto_update=auto_update, | ||
| ) | ||
|
|
||
|
|
||
| @fix_class_docs | ||
| class FineSXDragCal(FineDragCal): | ||
| """Fine drag calibration of X gate.""" | ||
|
|
||
| def __init__( | ||
| self, | ||
| qubit: int, | ||
| calibrations: BackendCalibrations, | ||
| backend: Optional[Backend] = None, | ||
| cal_parameter_name: Optional[str] = "β", | ||
| auto_update: bool = True, | ||
| ): | ||
| r"""see class :class:`FineDrag` for details. | ||
|
|
||
| Args: | ||
| qubit: The qubit for which to run the fine drag calibration. | ||
| calibrations: The calibrations instance with the schedules. | ||
| backend: Optional, the backend to run the experiment on. | ||
| cal_parameter_name: The name of the parameter in the schedule to update. | ||
| auto_update: Whether or not to automatically update the calibrations. By | ||
| default this variable is set to True. | ||
| """ | ||
| super().__init__( | ||
| qubit, | ||
| calibrations, | ||
| schedule_name="sx", | ||
| backend=backend, | ||
| cal_parameter_name=cal_parameter_name, | ||
| auto_update=auto_update, | ||
| ) | ||
|
|
||
| @classmethod | ||
| def _default_experiment_options(cls) -> Options: | ||
| """Default experiment options.""" | ||
| options = super()._default_experiment_options() | ||
| options.target_angle = np.pi / 2 | ||
| return options |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.