Skip to content
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

Merged

Conversation

to24toro
Copy link
Contributor

@to24toro to24toro commented Feb 27, 2023

Summary

This tutorial and user guide are related to #149.
In the user guide,

  • show some pulses using ScalableSymbolicPulse.
  • jax-jit these pulses.

In the tutorial,

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.

@to24toro to24toro changed the title Feature/add docs for jax jit Userguide and tutorial JAX transformation of Pulse Scheduler Feb 27, 2023
Copy link
Collaborator

@DanPuzzuoli DanPuzzuoli left a 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.

Comment on lines 14 to 18
.. 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.
Copy link
Collaborator

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.

Comment on lines 20 to 23
.. note::
At present, only the ``ScalableSymbolicPulse`` class is supported by JAX.
We cannot use any subclasses such as ``Gaussian`` and ``GaussianSquare``
for JAX.
Copy link
Collaborator

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:

Suggested change
.. 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.

Copy link
Contributor Author

@to24toro to24toro Mar 1, 2023

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``
Copy link
Collaborator

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.

Copy link
Collaborator

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?

Copy link
Contributor Author

@to24toro to24toro Mar 1, 2023

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 .

Comment on lines 201 to 204
4. How to execute jit compilation using ``ScalableSymbolicPulse``
-----------------------------------------------------------------

Using a Gaussian pulse as an example, jit compilation is executed as follows.
Copy link
Collaborator

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 ScalableSymbolicPulses. E.g. something like

Suggested change
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.

Copy link
Contributor Author

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.

@to24toro to24toro marked this pull request as ready for review March 2, 2023 03:09
Copy link
Collaborator

@DanPuzzuoli DanPuzzuoli left a 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).

Comment on lines 6 to 7
on :ref:`How-to use JAX with qiskit-dynamics <how-to use jax>` to
work with JAX.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
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>`.

Comment on lines 18 to 19
7. Optimization of X-gate using pulse schedules.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
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
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
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.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
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.

Comment on lines 267 to 270
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.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
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.

Copy link
Collaborator

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.

Comment on lines 46 to 47
2. How to generate a gaussion pulse by ``ScalableSymbolicPulse``
-------------------------------------------------------------------
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
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`.

Comment on lines 74 to 78
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.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
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``
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
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``
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
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
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
# use Amplifier as a variable
# use amplitude as the function argument

@to24toro
Copy link
Contributor Author

to24toro commented Mar 6, 2023

I modified, improved English phrases and also add sphinx refs at 55ff5bc .

Copy link
Collaborator

@DanPuzzuoli DanPuzzuoli left a 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,])`.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
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.

Copy link
Contributor Author

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,])
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
initial_params = np.array([10, 10,])
initial_params = np.array([10, 10])

Copy link
Contributor Author

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.
Copy link
Collaborator

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).

Copy link
Contributor Author

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`.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
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``.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fix at 83684a2.

@DanPuzzuoli DanPuzzuoli merged commit 96c1a14 into qiskit-community:main Mar 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants