-
Notifications
You must be signed in to change notification settings - Fork 135
Setup single-qubit calibrations #114
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
Merged
Changes from 23 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
9bb60ae
* Implemented method to setup single-qubit calibrations.
eggerdj 6b83656
Merge branch 'main' into calibrations_setup
eggerdj 9b16df9
Merge branch 'main' into calibrations_setup
eggerdj f973582
* Refactored cals setup to library style.
eggerdj 262f2a4
* Fix sigma in test.
eggerdj 319de19
Merge branch 'main' into calibrations_setup
eggerdj 894794a
Update qiskit_experiments/calibration_management/basis_gate_library.py
eggerdj 97524de
Merge branch 'main' into calibrations_setup
eggerdj d10d155
* Made library options kwargs.
eggerdj 59bde3e
* Added error raising.
eggerdj b33f73b
* Added basis gates property.
eggerdj 58acc43
* Renamed transmon library.
eggerdj c5962fe
* Docs.
eggerdj 4ae1763
* Added better Drag optionality.
eggerdj 654d017
* Added y and sy gates to library.
eggerdj 1dadebe
* Add check to make sure we do not add default values for gates that …
eggerdj 577394b
Update qiskit_experiments/calibration_management/basis_gate_library.py
eggerdj 84ee237
Merge branch 'main' into calibrations_setup
eggerdj 529d3b1
* Docs.
eggerdj 977122c
Merge branch 'calibrations_setup' of github.com:eggerdj/qiskit-experi…
eggerdj 824bb44
* Update the way the library is passed to BackendCalibrations.
eggerdj 332f8a4
* Lint.
eggerdj fd4b286
Merge branch 'main' into calibrations_setup
eggerdj 8282ee0
* Added option to link parameters.
eggerdj 86c3e9a
Merge branch 'calibrations_setup' of github.com:eggerdj/qiskit-experi…
eggerdj d3c0b69
* Partial generation of basis gates.
eggerdj 4ca85e4
* Added test.
eggerdj 58fad06
Merge branch 'main' into calibrations_setup
eggerdj a457f1a
* Fixed test issue and added in operator.
eggerdj 3ac1e08
Merge branch 'calibrations_setup' of github.com:eggerdj/qiskit-experi…
eggerdj 9e478b2
Merge branch 'main' into calibrations_setup
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
158 changes: 158 additions & 0 deletions
158
qiskit_experiments/calibration_management/basis_gate_library.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,158 @@ | ||
| # 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. | ||
|
|
||
| """ | ||
| A collections of libraries to setup Calibrations. | ||
|
|
||
| Note that the set of available libraries will be extended in future releases. | ||
| """ | ||
|
|
||
| from abc import ABC, abstractmethod | ||
| from typing import Dict, List, Optional, Tuple | ||
|
|
||
| from qiskit.circuit import Parameter | ||
| import qiskit.pulse as pulse | ||
| from qiskit.pulse import ScheduleBlock | ||
|
|
||
| from qiskit_experiments.calibration_management.calibration_key_types import ParameterValueType | ||
| from qiskit_experiments.exceptions import CalibrationError | ||
|
|
||
|
|
||
| class BasisGateLibrary(ABC): | ||
|
eggerdj marked this conversation as resolved.
|
||
| """A base class for libraries of basis gates to make it easier to setup Calibrations.""" | ||
|
eggerdj marked this conversation as resolved.
|
||
|
|
||
| # Location where default parameter values are stored. These may be updated at construction. | ||
| __default_values__ = dict() | ||
|
|
||
| def __init__(self, default_values: Optional[Dict] = None): | ||
| """Setup the library. | ||
|
|
||
| Args: | ||
| default_values: A dictionary to override library default parameter values. | ||
| """ | ||
| self._schedules = dict() | ||
|
|
||
| if default_values is not None: | ||
| self.__default_values__.update(default_values) | ||
|
|
||
| def __getitem__(self, name: str) -> ScheduleBlock: | ||
| """Return the schedule.""" | ||
| if name not in self._schedules: | ||
| raise CalibrationError(f"Gate {name} is not contained in {self.__class__.__name__}.") | ||
|
eggerdj marked this conversation as resolved.
|
||
|
|
||
| return self._schedules[name] | ||
|
eggerdj marked this conversation as resolved.
|
||
|
|
||
| @property | ||
| def basis_gates(self) -> List[str]: | ||
| """Return the basis gates supported by the library.""" | ||
| return list(name for name in self._schedules) | ||
|
eggerdj marked this conversation as resolved.
|
||
|
|
||
| @abstractmethod | ||
| def default_values(self) -> List[Tuple[ParameterValueType, Parameter, Tuple, ScheduleBlock]]: | ||
| """Return the default values for the parameters. | ||
|
|
||
| Returns | ||
| A list of tuples is returned. These tuples are structured so that instances of | ||
| :class:`Calibrations` can call :meth:`add_parameter_value` on the tuples. | ||
| """ | ||
|
|
||
|
|
||
| class FixedFrequencyTransmon(BasisGateLibrary): | ||
| """A library of gates for fixed-frequency superconducting qubit architectures. | ||
|
|
||
| Note that for now this library supports single-qubit gates and will be extended | ||
| in the future. | ||
| """ | ||
|
|
||
| __default_values__ = {"duration": 160, "amp": 0.5, "β": 0.0} | ||
|
|
||
| def __init__(self, default_values: Optional[Dict] = None, use_drag: bool = True): | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. perhaps you can explicitly write |
||
| """Setup the schedules. | ||
|
|
||
| Args: | ||
| default_values: Default values for the parameters this dictionary can contain | ||
| the following keys: "duration", "amp", "β", and "σ". If "σ" is not provided | ||
| this library will take one fourth of the pulse duration as default value. | ||
| use_drag: If set to False then Gaussian pulses will be used instead of DRAG | ||
| pulses. | ||
| """ | ||
| super().__init__(default_values) | ||
|
|
||
| dur = Parameter("duration") | ||
| sigma = Parameter("σ") | ||
|
|
||
| def _beta(use_drag): | ||
| return Parameter("β") if use_drag else None | ||
|
|
||
| # X gates | ||
| sched_x = self._single_qubit_schedule("x", dur, Parameter("amp"), sigma, _beta(use_drag)) | ||
|
|
||
| # Y gates | ||
| sched_y = self._single_qubit_schedule("y", dur, Parameter("amp"), sigma, _beta(use_drag)) | ||
|
|
||
| # square-root X gates | ||
| sched_sx = self._single_qubit_schedule("sx", dur, Parameter("amp"), sigma, _beta(use_drag)) | ||
|
|
||
| # square-root Y gates | ||
| sched_sy = self._single_qubit_schedule("sy", dur, Parameter("amp"), sigma, _beta(use_drag)) | ||
|
eggerdj marked this conversation as resolved.
Outdated
|
||
|
|
||
| for sched in [sched_x, sched_y, sched_sx, sched_sy]: | ||
| self._schedules[sched.name] = sched | ||
|
eggerdj marked this conversation as resolved.
Outdated
|
||
|
|
||
| @staticmethod | ||
| def _single_qubit_schedule( | ||
| name: str, | ||
| dur: Parameter, | ||
| amp: Parameter, | ||
| sigma: Parameter, | ||
| beta: Optional[Parameter] = None, | ||
| ) -> ScheduleBlock: | ||
| """Build a single qubit pulse.""" | ||
|
|
||
| chan = pulse.DriveChannel(Parameter("ch0")) | ||
|
|
||
| if beta is not None: | ||
|
eggerdj marked this conversation as resolved.
|
||
| with pulse.build(name=name) as sched: | ||
| pulse.play(pulse.Drag(duration=dur, amp=amp, sigma=sigma, beta=beta), chan) | ||
| else: | ||
| with pulse.build(name=name) as sched: | ||
| pulse.play(pulse.Gaussian(duration=dur, amp=amp, sigma=sigma), chan) | ||
|
|
||
| return sched | ||
|
|
||
| def default_values(self) -> List[Tuple[ParameterValueType, Parameter, Tuple, ScheduleBlock]]: | ||
| """Return the default values for the parameters. | ||
|
|
||
| Returns | ||
| A list of tuples is returned. These tuples are structured so that instances of | ||
| :class:`Calibrations` can call :meth:`add_parameter_value` on the tuples. | ||
| """ | ||
| defaults = [] | ||
| for name in self.basis_gates: | ||
| schedule = self._schedules[name] | ||
| for param in schedule.parameters: | ||
| if "ch" not in param.name: | ||
|
|
||
| if param.name == "σ" and "σ" not in self.__default_values__: | ||
| value = self.__default_values__["duration"] / 4 | ||
| else: | ||
| value = self.__default_values__[param.name] | ||
|
|
||
| if name in {"sx", "sy"} and param.name == "amp": | ||
| value /= 2.0 | ||
|
|
||
| if "y" in name and param.name == "amp": | ||
| value *= 1.0j | ||
|
|
||
| defaults.append((value, param.name, tuple(), name)) | ||
|
|
||
| return defaults | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| # 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. | ||
|
|
||
| """Class to test the calibrations setup methods.""" | ||
|
|
||
| import qiskit.pulse as pulse | ||
| from qiskit.test import QiskitTestCase | ||
|
|
||
| from qiskit_experiments.calibration_management.basis_gate_library import FixedFrequencyTransmon | ||
| from qiskit_experiments.exceptions import CalibrationError | ||
|
|
||
|
|
||
| class TestFixedFrequencyTransmon(QiskitTestCase): | ||
| """Test the various setup methods.""" | ||
|
|
||
| def test_standard_single_qubit_gates(self): | ||
| """Test the setup of single-qubit gates.""" | ||
|
|
||
| library = FixedFrequencyTransmon(default_values={"duration": 320}) | ||
|
|
||
| for gate in ["x", "sx"]: | ||
| sched = library[gate] | ||
| self.assertTrue(isinstance(sched, pulse.ScheduleBlock)) | ||
| self.assertEqual(len(sched.parameters), 5) | ||
|
|
||
| sched_x = library["x"] | ||
| sched_sx = library["sx"] | ||
|
|
||
| self.assertEqual(sched_x.blocks[0].pulse.duration, sched_sx.blocks[0].pulse.duration) | ||
| self.assertEqual(sched_x.blocks[0].pulse.sigma, sched_sx.blocks[0].pulse.sigma) | ||
|
|
||
| expected = [ | ||
| (0.5, "amp", (), "x"), | ||
| (0.0, "β", (), "x"), | ||
| (320, "duration", (), "x"), | ||
| (80, "σ", (), "x"), | ||
| (320, "duration", (), "sx"), | ||
| (0.0, "β", (), "sx"), | ||
| (0.25, "amp", (), "sx"), | ||
| (80, "σ", (), "sx"), | ||
| (0.5j, "amp", (), "y"), | ||
| (0.0, "β", (), "y"), | ||
| (320, "duration", (), "y"), | ||
| (80, "σ", (), "y"), | ||
| (320, "duration", (), "sy"), | ||
| (0.0, "β", (), "sy"), | ||
| (0.25j, "amp", (), "sy"), | ||
| (80, "σ", (), "sy"), | ||
| ] | ||
|
|
||
| for param_conf in library.default_values(): | ||
| self.assertTrue(param_conf in expected) | ||
|
|
||
| # Check that an error gets raise if the gate is not in the library. | ||
| with self.assertRaises(CalibrationError): | ||
| print(library["bswap"]) | ||
|
|
||
| # Test the basis gates of the library. | ||
| self.assertListEqual(library.basis_gates, ["x", "y", "sx", "sy"]) | ||
|
|
||
| def test_turn_off_drag(self): | ||
| """Test the use_drag parameter.""" | ||
|
|
||
| library = FixedFrequencyTransmon(use_drag=False) | ||
| self.assertTrue(isinstance(library["x"].blocks[0].pulse, pulse.Gaussian)) | ||
|
|
||
| library = FixedFrequencyTransmon() | ||
| self.assertTrue(isinstance(library["x"].blocks[0].pulse, pulse.Drag)) |
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.