-
Notifications
You must be signed in to change notification settings - Fork 60
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
Userguide and tutorial JAX transformation of Pulse Scheduler #194
Userguide and tutorial JAX transformation of Pulse Scheduler #194
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've given direct comments on the user guide. I think it is good, but just needs to be a bit simplified.
For the tutorial, I think it can be much more simplified. I don't think it is necessary to have separate 2-A, ..., 6-A and 2-B, ..., 6-B sections. I was thinking the original 2-6 steps could be kept as they are, and a single additional section numbered 7 (with subsections) could be added for doing the ScalableSymbolicPulse
optimization, using the same model and fidelity function as sections 2-6. I realize that you are wanting to use DRAG, the relevance of which requires using a transmon model, but for the simplicity of the presentation I think it makes sense to just use the qubit model of section 2.
.. warning:: | ||
This is an advanced topic --- Some extend of knowledge about ``Qiskit-pulse`` is required. | ||
See `Qiskit pulse API Reference <https://qiskit.org/documentation/apidoc/pulse.html>`__ for the detailed API information. | ||
Also, see `Qiskit tutorial <https://qiskit.org/documentation/tutorials/circuits_advanced/06_building_pulse_schedules.html>`__ | ||
for a tutorial about a pulse scheduler. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this warning is necessary. I think linking to documentation for using pulse
is good, but I think after that we can assume that the user knows what pulse
is.
.. note:: | ||
At present, only the ``ScalableSymbolicPulse`` class is supported by JAX. | ||
We cannot use any subclasses such as ``Gaussian`` and ``GaussianSquare`` | ||
for JAX. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a comment here about why you can only use ScalableSymbolicPulse
? Something like:
.. note:: | |
At present, only the ``ScalableSymbolicPulse`` class is supported by JAX. | |
We cannot use any subclasses such as ``Gaussian`` and ``GaussianSquare`` | |
for JAX. | |
.. note:: | |
At present, only the ``ScalableSymbolicPulse`` class is supported by JAX, as the validation | |
present in other pulse types, such as ``Gaussian``, is not JAX-compatible. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It makes sense. Modified at b7ef506.
Array.set_default_backend('jax') | ||
|
||
|
||
2. How to generate a variety of pulses by ``ScalableSymbolicPulse`` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think we need 3 examples here, or would one be sufficient? My initial thought is that only one is necessary, to show the user how to set it up.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe if you want to use the Guassian
pulse later in the next section, you can keep only that one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Certainly, it may be too much to users to show 3 patterns specially. Modified at b7ef506 .
4. How to execute jit compilation using ``ScalableSymbolicPulse`` | ||
----------------------------------------------------------------- | ||
|
||
Using a Gaussian pulse as an example, jit compilation is executed as follows. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this section should be more specifically phrased to be about how the pulse -> signal converter is JAX-compatible using ScalableSymbolicPulse
s. E.g. something like
4. How to execute jit compilation using ``ScalableSymbolicPulse`` | |
----------------------------------------------------------------- | |
Using a Gaussian pulse as an example, jit compilation is executed as follows. | |
4. JAX transforming Pulse to Signal conversion involving ``ScalableSymbolicPulse`` | |
------------------------------------------------------------------------------------ | |
Using a Gaussian pulse as an example, we show that code involving ``ScalableSymbolicPulse`` | |
and the pulse to signal converter can be JAX-compiled (or more generally, JAX-transformed). |
You could also add a reference to the optimization tutorial showing this process being performed with a function that involves a simulation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for adding the sentence. Modified at b7ef506.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This current iteration is looking good. I've suggestions for edits to the text.
Aside from these suggestions, one thing you should do is add direct sphinx references to classes/functions in Terra. E.g. in the DynamicsBackend
docs, there are references to objects in terra, such as:
:class:`~qiskit.providers.backend.BackendV2`
(As an aside, in case you haven't seen this before, this line in docs/conf.py sets up the intersphinx
package for linking documentation to other packages. In this case it is only qiskit
, but qiskit_experiments
is also being added in #193 .)
So, e.g., all of the references to ScalableSymbolicPulse
can be turned into links to the API documentation for the class. I think you should even be able to link to the qiskit pulse module using
:mod:`~qiskit.pulse`
(not sure if this is correct syntax, but something like this should work).
on :ref:`How-to use JAX with qiskit-dynamics <how-to use jax>` to | ||
work with JAX. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
on :ref:`How-to use JAX with qiskit-dynamics <how-to use jax>` to | |
work with JAX. | |
on :ref:`How-to use JAX with qiskit-dynamics <how-to use jax>`. |
7. Optimization of X-gate using pulse schedules. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
7. Optimization of X-gate using pulse schedules. | |
7. Repeat the :math:`X`-gate optimization, alternatively using pulse schedules to specify the control sequence. | |
@@ -254,3 +257,129 @@ approximation analysis. | |||
.. jupyter-execute:: | |||
|
|||
opt_signal.samples.sum() | |||
|
|||
|
|||
7. Optimization of X-gate using pulse schedules |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
7. Optimization of X-gate using pulse schedules | |
7. Repeat the :math:`X`-gate optimization, alternatively using pulse schedules to specify the control sequence |
7. Optimization of X-gate using pulse schedules | ||
----------------------------------------------- | ||
|
||
Here, we introduce the example of the optimization of a gate generated by pulse schedules. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here, we introduce the example of the optimization of a gate generated by pulse schedules. | |
Here, we perform the optimization again, however now we specify the parameterized control sequence to optimize as a pulse schedule. |
We construct the GaussianSquare pulse which sigma and width are parametrized directly from ``ScalableSymbolicPulse``. | ||
Although qiskit provides a ``GaussianSquare`` class that generates a GaussianSquare pulse, which is a subclass of ``ScalableSymbolicPulse``, | ||
this class is currently not JAX-compatible. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We construct the GaussianSquare pulse which sigma and width are parametrized directly from ``ScalableSymbolicPulse``. | |
Although qiskit provides a ``GaussianSquare`` class that generates a GaussianSquare pulse, which is a subclass of ``ScalableSymbolicPulse``, | |
this class is currently not JAX-compatible. | |
We construct a Gaussian square pulse as a ``ScalableSymbolicPulse`` instance, parameterized by ``sigma`` and ``width``. | |
Although qiskit pulse provides a ``GaussianSquare``, this class is not JAX compatible. | |
Add a reference here to the user guide as a source of more information about how ScalableSymbolicPulse
is JAX compatible but other pulses are not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also - I've changed your initial "GaussianSquare
" pulse to just the words Guassian square, to try to differentiate it from the GaussianSquare
class that exists in pulse.
2. How to generate a gaussion pulse by ``ScalableSymbolicPulse`` | ||
------------------------------------------------------------------- |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2. How to generate a gaussion pulse by ``ScalableSymbolicPulse`` | |
------------------------------------------------------------------- | |
2. How to define a Gaussian pulse using ``ScalableSymbolicPulse`` | |
----------------------------------------------------------------- | |
As the standard ``Gaussian`` pulse is not JAX-compatible, to define a Gaussian pulse to use in optimization, | |
we need to instantiate a ``ScalableSymbolicPulse`` with a Gaussian parameterization. | |
First, define the symbolic representation in `sympy`. |
Here is an example of a gaussian pulse generated using ``ScalableSymbolicPulse``. | ||
|
||
|
||
A Gaussian pulse is generated by the following codes. | ||
It notes that values of amplifier, angle, sigma and duration are typical ones. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here is an example of a gaussian pulse generated using ``ScalableSymbolicPulse``. | |
A Gaussian pulse is generated by the following codes. | |
It notes that values of amplifier, angle, sigma and duration are typical ones. | |
Next, define the ``ScalableSymbolicPulse`` using the above expression. |
gaussian_pulse.draw() | ||
|
||
|
||
4. JAX transforming Pulse to Signal conversion involving ``ScalableSymbolicPulse`` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
4. JAX transforming Pulse to Signal conversion involving ``ScalableSymbolicPulse`` | |
3. JAX transforming Pulse to Signal conversion involving ``ScalableSymbolicPulse`` |
4. JAX transforming Pulse to Signal conversion involving ``ScalableSymbolicPulse`` | ||
------------------------------------------------------------------------------------ | ||
|
||
Using a Gaussian pulse as an example, we show that code involving ``ScalableSymbolicPulse`` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using a Gaussian pulse as an example, we show that code involving ``ScalableSymbolicPulse`` | |
Using a Gaussian pulse as an example, we show that a function involving ``ScalableSymbolicPulse`` |
|
||
.. jupyter-execute:: | ||
|
||
# use Amplifier as a variable |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# use Amplifier as a variable | |
# use amplitude as the function argument |
I modified, improved English phrases and also add sphinx refs at 55ff5bc . |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some final small changes. After these I will approve.
return 1. - fidelity(Array(result[0].y[-1])).data | ||
|
||
|
||
We set the initial values of `sigma` and `width` for the optimization as :math:`initial_params = np.array([10, 10,])`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We set the initial values of `sigma` and `width` for the optimization as :math:`initial_params = np.array([10, 10,])`. | |
We set the initial values of ``sigma`` and ``width`` for the optimization as ``initial_params = np.array([10, 10])``. |
Sorry, I somehow missed the extra comma last time. Also - all mark up here should just be double ticks, as these are code references.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix at 83684a2.
|
||
.. jupyter-execute:: | ||
|
||
initial_params = np.array([10, 10,]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
initial_params = np.array([10, 10,]) | |
initial_params = np.array([10, 10]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix at 83684a2.
Here, we perform the optimization again, however now we specify the parameterized control sequence to optimize as a pulse schedule. | ||
|
||
We construct a Gaussian square pulse as a :class:`~qiskit.pulse.library.ScalableSymbolicPulse` instance, parameterized by ``sigma`` and ``width``. | ||
Although qiskit pulse provides a :class:`~qiskit.pulse.library.GaussianSquare`, this class is not JAX compatible. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a reference here to the how_to_use_pulse_schedule_for_jax_jit.rst
user guide. E.g.
See the user guide entry on :ref:`JAX-compatible pulse schedules <pulse jax>`.
(where pulse jax
is the reference name to add to the start of the user guide).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix at 83684a2.
|
||
|
||
|
||
We can draw the optimized pulse, whose parameters are retrieved by :math:`opt_results.x`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can draw the optimized pulse, whose parameters are retrieved by :math:`opt_results.x`. | |
We can draw the optimized pulse, whose parameters are retrieved by ``opt_results.x``. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix at 83684a2.
Summary
This tutorial and user guide are related to #149.
In the user guide,
In the tutorial,
The tutorial is much overlapping with the configuration of https://qiskit.org/documentation/dynamics/tutorials/optimizing_pulse_sequence.html .
Whether or not merging this PR depends on the discussion.
Details and comments
Currently, a new class ScalableSymbolicPulse which is a subclass of SymbolicPuse and assumed to hava a scalable form was introduced. So I use this class in this PR. However, I use the SymbolicPulse in #149. I would like to discuss which we should use.