Skip to content
This repository has been archived by the owner on Aug 21, 2023. It is now read-only.

Tutorial on the gradients framework (fixes #1390) #1463

Merged
merged 15 commits into from
Jun 19, 2023

Conversation

maxwell04-wq
Copy link
Contributor

Summary

Fixes #1390

Details and comments

  • Updates the gradients tutorial from opflow.gradients to algorithms.gradients
  • Use of BaseEstimator and BaseSampler classes from Primitives for gradient evaluation
  • Demonstration of different methods for gradient evaluation
  • Application example: VQE - Solved using Estimator, Sampler, and classical optimizer (scipy.minimize)
  • More details on SPSA gradient and Qiskit Primitives can be added

**Summary**
Fixes Qiskit#1390

**Details**
Updates the gradients tutorial from `opflow.gradients` to `algorithms.gradients`
- Use of BaseEstimator and BaseSampler classes from Primitives for gradient evaluation
- Demonstration of different methods for gradient evaluation
- Application example: VQE - Solved using Estimator, Sampler, and classical optimizer (scipy.minimize)

*Comments*
- More details on SPSA gradient and Qiskit Primitives can be added
…thms

Tutorial on the gradients framework
@review-notebook-app
Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@CLAassistant
Copy link

CLAassistant commented May 31, 2023

CLA assistant check
All committers have signed the CLA.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@ElePT ElePT self-assigned this Jun 1, 2023
@review-notebook-app
Copy link

review-notebook-app bot commented Jun 1, 2023

View / edit / reply to this conversation on ReviewNB

ElePT commented on 2023-06-01T15:05:57Z
----------------------------------------------------------------

I think that the title "Qiskit Gradient Framework using Primitives" is a bit more fitting, "The Gradient Framework" sounds too generic for me.

Also, I like how concise the tutorial goal is, but I think it could even be made into a single sentence, and include a link to the API ref. for the primitives:

This tutorial demonstrates the use of the qiskit.algorithms.gradients module to evaluate quantum gradients using the Qiskit Primitives

Finally, if it is not longer useful, could you please remove the comment? It is fine to have some comments while drafting the tutorial, but we should avoid them in the final version :)


maxwell04-wq commented on 2023-06-01T19:32:19Z
----------------------------------------------------------------

Incorporated.

@review-notebook-app
Copy link

review-notebook-app bot commented Jun 1, 2023

View / edit / reply to this conversation on ReviewNB

ElePT commented on 2023-06-01T15:05:58Z
----------------------------------------------------------------

I would call this section something like "A quick refresher on Qiskit Primitives".

Qiskit Primitives is a new framework for executing quantum jobs. Instead of binding parameters to quantum circuits, the primitives module uses either a Sampler or an Estimator class to evaluate the quantum circuits. These Primitive classes take the circuits, the observable hamiltonians, and the circuit parameters and return the results of quantum simulations.

And I would suggest a few changes to the sentence above, I know that explaining the primitives is still a bit tricky, but to make it better it is important to be precise describing what the primitives do:

The Qiskit Primitives work as an abstraction level between algorithms and (real/simulated) quantum devices. Instead of having to manually deal with tasks such as parameter binding or circuit transpilation, the primitives module offers a Sampler and an Estimator class that take the circuits, the observable Hamiltonians, and the circuit parameters and return the sampling distribution and the computed expectation values respectively.

Now, for the following section:

Qiskit Primitives provides two classess for evaluating the circuit:
The Estimator class allows to evaluate the expectation values of the observables.
The Sampler class allows to evaluate the quasi-probability distribution of the observables.

I would say:

  1. use qiskit.primitives instead of "Qiskit Primitives"
  2. there is a small typo on classes (double s at the end)
  3. it is important to understand what each primitive does. The estimator calculates expectation values of circuit-observable pairs, while the sampler returns prob. distributions from only circuits (no observables here). I think the description could be changed to:
  4. The Estimator class allows to evaluate expectation values of observables with respect to states prepared by quantum circuits
  5. The Sampler class returns quasi-probability distributions as a result of sampling quantum circuits


maxwell04-wq commented on 2023-06-01T19:37:37Z
----------------------------------------------------------------

Incorporated.

@review-notebook-app
Copy link

review-notebook-app bot commented Jun 1, 2023

View / edit / reply to this conversation on ReviewNB

ElePT commented on 2023-06-01T15:05:59Z
----------------------------------------------------------------

Overall, I really like this section, but to push it to the next level I would restructure it a little bit, and explain the base classes first, include classical gradients and talk about the QGT. This would look like this:

The algorithms.gradients module contains both circuit gradients and circuit metrics. The gradients extend the BaseEstimatorGradient and BaseSamplerGradient base classes. These are abstract classes on top of which different gradient methods have been implemented. The methods currently available in this module are:
  • Parameter Shift Gradients
  • Finite Difference Gradients
  • Linear Combination of Unitaries Gradients
  • Simultaneous Perturbation Stochastic Approximation (SPSA) Gradients
Additionally, the module offers reverse gradients for efficient classical computations.

The metrics available are based on the notion of the Quantum Geometric Tensor (QGT). There is a BaseQGT class (Estimator-based) on top of which different QGT methods have been implemented:
  • Linear Comination of Unitaries QGT
  • Reverse QGT (classical)

As well as a Quantum Fisher Information class (QFI) that is initialized with a reference QGT implementation from the above list.

maxwell04-wq commented on 2023-06-01T19:40:37Z
----------------------------------------------------------------

Incorporated.

@review-notebook-app
Copy link

review-notebook-app bot commented Jun 1, 2023

View / edit / reply to this conversation on ReviewNB

ElePT commented on 2023-06-01T15:06:00Z
----------------------------------------------------------------

I love that you have made a diagram, this shows a lot of effort :) I think we can replace Qiskit Algorithms with Qiskit Primitives at the bottom (as the gradients are built on top of that), and add the Reverse Gradient to the list (I know that I mentioned that a reverse gradient tutorial was optional, we don't have to go into detail, but I think that we should at least name them and show where they fit in the overall structure).

I am not so sure about the "Circuit" in "Circuit Gradients" and "Circuit Metrics", we can maybe drop it.


maxwell04-wq commented on 2023-06-01T20:12:42Z
----------------------------------------------------------------

Incorporated.

@review-notebook-app
Copy link

review-notebook-app bot commented Jun 1, 2023

View / edit / reply to this conversation on ReviewNB

ElePT commented on 2023-06-01T15:06:01Z
----------------------------------------------------------------

We are trying to move away from having import blocks at the beginning of the tutorial, instead, we encourage to add the relevant imports at the top of the cell where they are used. You can see this, for example, in this tutorial https://qiskit.org/ecosystem/ibm-runtime/tutorials/grover_with_sampler.html


maxwell04-wq commented on 2023-06-01T20:28:57Z
----------------------------------------------------------------

Incorporated.

@review-notebook-app
Copy link

review-notebook-app bot commented Jun 1, 2023

View / edit / reply to this conversation on ReviewNB

ElePT commented on 2023-06-01T15:06:02Z
----------------------------------------------------------------

Given that we no longer talk about the difference between First and Second Order gradients, maybe just "Gradients" fits this section better.

Here, and in the following subsections, I think it's important to always be very clear about when we are showing estimator gradients and when we are using sampler gradients. You already do this in the Parameter Shift Gradients section, but in the other sections you don't mention that you are talking about estimator gradients.


maxwell04-wq commented on 2023-06-01T20:30:46Z
----------------------------------------------------------------

I'm adding a note in the Sampler example that all the following examples will be with estimators.

@review-notebook-app
Copy link

review-notebook-app bot commented Jun 1, 2023

View / edit / reply to this conversation on ReviewNB

ElePT commented on 2023-06-01T15:06:03Z
----------------------------------------------------------------

Citing the spsa paperwould be nice.


maxwell04-wq commented on 2023-06-01T20:35:20Z
----------------------------------------------------------------

Done.

@review-notebook-app
Copy link

review-notebook-app bot commented Jun 1, 2023

View / edit / reply to this conversation on ReviewNB

ElePT commented on 2023-06-01T15:06:04Z
----------------------------------------------------------------

similar comment to the one I left above, we should spread out the imports.


maxwell04-wq commented on 2023-06-01T20:37:15Z
----------------------------------------------------------------

Done.

@review-notebook-app
Copy link

review-notebook-app bot commented Jun 1, 2023

View / edit / reply to this conversation on ReviewNB

ElePT commented on 2023-06-01T15:06:06Z
----------------------------------------------------------------

Line #1.    #Make circuit copies for different VQEs


@review-notebook-app
Copy link

review-notebook-app bot commented Jun 1, 2023

View / edit / reply to this conversation on ReviewNB

ElePT commented on 2023-06-01T15:06:07Z
----------------------------------------------------------------

This problem can also be tackled with the Sampler-based SamplingVQE, as this algorithm requires the Hamiltonian to be diagonal (this is not the case for regular VQE). In this case, the algorithm will use a Sampler-based gradient.


@review-notebook-app
Copy link

review-notebook-app bot commented Jun 1, 2023

View / edit / reply to this conversation on ReviewNB

ElePT commented on 2023-06-01T15:06:08Z
----------------------------------------------------------------

Line #3.    vqe = SamplingVQE(sampler=sampler, ansatz=wavefunction_1, optimizer=optimizer)

I think that the point of this example was to show the use of a sampler gradient, right? but no custom gradient is set here


maxwell04-wq commented on 2023-06-01T20:41:21Z
----------------------------------------------------------------

SamplingVQE does not take a gradient as a parameter. Dropping this example.

Copy link
Contributor Author

Incorporated.


View entire conversation on ReviewNB

Copy link
Contributor Author

Incorporated.


View entire conversation on ReviewNB

Copy link
Contributor Author

Incorporated.


View entire conversation on ReviewNB

Copy link
Contributor Author

Incorporated.


View entire conversation on ReviewNB

Copy link
Contributor Author

Incorporated.


View entire conversation on ReviewNB

Copy link
Contributor Author

I'm adding a note in the Sampler example that all the following examples will be with estimators.


View entire conversation on ReviewNB

Copy link
Contributor Author

Done.


View entire conversation on ReviewNB

Copy link
Contributor Author

Done.


View entire conversation on ReviewNB

Copy link
Contributor Author

SamplingVQE does not take a gradient as a parameter. Dropping this example.


View entire conversation on ReviewNB

Copy link
Collaborator

@ElePT ElePT left a comment

Choose a reason for hiding this comment

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

Thanks a lot for the quick application of the feedback. It is looking very good! I am now going through some technical details in the content. I started reviewing on the raw notebook so that I could make suggestions on formulas, but because the image takes a lot of space in the raw file, I think I'll switch over to ReviewNB again for the remainder of this review.

tutorials/algorithms/11_gradients_framework.ipynb Outdated Show resolved Hide resolved
tutorials/algorithms/11_gradients_framework.ipynb Outdated Show resolved Hide resolved
tutorials/algorithms/11_gradients_framework.ipynb Outdated Show resolved Hide resolved
tutorials/algorithms/11_gradients_framework.ipynb Outdated Show resolved Hide resolved
tutorials/algorithms/11_gradients_framework.ipynb Outdated Show resolved Hide resolved
tutorials/algorithms/11_gradients_framework.ipynb Outdated Show resolved Hide resolved
tutorials/algorithms/11_gradients_framework.ipynb Outdated Show resolved Hide resolved
tutorials/algorithms/11_gradients_framework.ipynb Outdated Show resolved Hide resolved
tutorials/algorithms/11_gradients_framework.ipynb Outdated Show resolved Hide resolved
tutorials/algorithms/11_gradients_framework.ipynb Outdated Show resolved Hide resolved
tutorials/algorithms/11_gradients_framework.ipynb Outdated Show resolved Hide resolved
tutorials/algorithms/11_gradients_framework.ipynb Outdated Show resolved Hide resolved
Co-authored-by: Elena Peña Tapia <57907331+ElePT@users.noreply.github.com>
@maxwell04-wq
Copy link
Contributor Author

@ElePT I have incorporated the suggested changes. Please review it at your earliest convenience.

@ElePT
Copy link
Collaborator

ElePT commented Jun 6, 2023

Thanks Fatima, my response is going to be a bit slower this week, but I will get around to reviewing before the Unitary Hack deadline :)

@maxwell04-wq maxwell04-wq requested a review from ElePT June 12, 2023 14:12
@maxwell04-wq
Copy link
Contributor Author

@ElePT as UnitaryHack ends in about half a day, can you please review the PR so that I can claim the bounty?

@nathanshammah
Copy link

nathanshammah commented Jun 13, 2023

@maxwell04-wq open PRs reviewed also later in the week will be considered for unitaryHACK.

Copy link
Collaborator

@ElePT ElePT left a comment

Choose a reason for hiding this comment

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

Thank you very very much for your contribution. I have left some final comments, after which, I will approve your PR. I have also confirmed you as the main candidate to obtain the unitary hack bounty :)

tutorials/algorithms/11_gradients_framework.ipynb Outdated Show resolved Hide resolved
tutorials/algorithms/11_gradients_framework.ipynb Outdated Show resolved Hide resolved
tutorials/algorithms/11_gradients_framework.ipynb Outdated Show resolved Hide resolved
@maxwell04-wq maxwell04-wq requested a review from ElePT June 16, 2023 14:31
Copy link
Collaborator

@ElePT ElePT left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks for your hard work :)

@ElePT ElePT merged commit f6b5bc8 into Qiskit:master Jun 19, 2023
ElePT added a commit to ElePT/qiskit-algorithms that referenced this pull request Aug 17, 2023
…) (Qiskit/qiskit-tutorials#1463)

<!--
⚠️ If you do not respect this template, your pull request will be
closed.
⚠️ Your pull request title should be short detailed and understandable
for all.
⚠️ If your pull request fixes an open issue, please link to the issue.

✅ I have added the tests to cover my changes.
✅ I have updated the documentation accordingly.
✅ I have read the CONTRIBUTING document.
-->

### Summary
Fixes Qiskit/qiskit-tutorials#1390, Unitary Hack contribution.


### Details and comments
- Updates the gradients tutorial from `opflow.gradients` to
`algorithms.gradients`
- Use of `BaseEstimator` and `BaseSampler` classes from Primitives for
gradient evaluation
- Demonstration of different methods for gradient evaluation
- Application example: VQE - Solved using Estimator, Sampler, and
classical optimizer (scipy.minimize)
- More details on SPSA gradient and Qiskit Primitives can be added

---------

Co-authored-by: Elena Peña Tapia <57907331+ElePT@users.noreply.github.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Update Gradient Framework tutorial to use primitives
5 participants