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

Update Gradient Framework tutorial to use primitives #1390

Closed
3 tasks
ElePT opened this issue Jan 23, 2023 · 8 comments · Fixed by maxwell04-wq/qiskit-tutorials-unitaryhack2023#1 or #1463
Closed
3 tasks
Assignees
Labels
documentation Something is not clear or error in description

Comments

@ElePT
Copy link
Collaborator

ElePT commented Jan 23, 2023

The current gradient framework tutorial has not been updated to show the implementation using primitives. Aside from the refactoring, new gradients (classically efficient) have been added, and the whole framework has been moved from qiskit.opflow to qiskit.algorithms. I believe that the TO-DO list here is:

  • Move gradient tutorial location from qiskit-tutorials/tutorials/operators to qiskit-tutorials/tutorials/algorithms
  • Update tutorial to show new gradient framework structure (similar to the existing diagram in the outdated tutorial), add an explanation of the different gradient methods implemented (here, some information from the former tutorial could be re-purposed), and show examples of how to use the new gradient classes.
  • Optionally, we could also add a new tutorial for classical efficient gradients.

You can explore the new gradient classes by navigating the API reference in:

As well as getting an introduction to the module in the gradients section of the opflow migration guide: https://qiskit.org/documentation/migration_guides/opflow_migration.html#gradients


Style guidelines:

Here are some references of the style of tutorial we are looking for

Make sure to follow the Qiskit documentation tutorial guidelines: https://qiskit.github.io/qiskit_sphinx_theme/tutorials/tutorials_guidelines.html

More examples for further reference: https://qiskit.github.io/qiskit_sphinx_theme/tutorials/tutorials_examples.html

@BramDo
Copy link

BramDo commented May 26, 2023

1. Moving the Gradient Tutorial to Algorithms

  • Begin with a brief introduction explaining the purpose of the tutorial.
  • Describe the old location of the gradient framework (qiskit.opflow) and the reasons for moving it to qiskit.algorithms.
  • Show how to import the gradient framework from its new location.

2. Updating the Tutorial to Show Use of Primitives

This tutorial would focus on explaining how primitives can be used within the gradient framework.

  • Start with a brief explanation of what primitives are and why they are useful in the context of the gradient framework.
  • Show how to use primitives in the gradient framework with a simple example. Explain each step in detail.
  • Build on the simple example by showing how to use more complex primitives or combinations of primitives.
  • End with a discussion of any potential pitfalls or common mistakes when using primitives in the gradient framework, and how to avoid them.

3. Adding a New Tutorial for Classically Efficient Gradients

This tutorial would introduce the concept of classically efficient gradients and show how to use them.

  • Begin with an explanation of what classically efficient gradients are, why they're important, and how they fit into the gradient framework.
  • Provide a simple example showing how to use classically efficient gradients. Explain each step in detail.
  • Expand on the simple example by introducing more complex usage of classically efficient gradients. As before, provide detailed explanations.

@maxwell04-wq
Copy link
Contributor

maxwell04-wq commented May 26, 2023

I am trying to start with qiskit.algorithms.gradients using this tutorial: https://medium.com/qiskit/introducing-qiskit-algorithms-with-qiskit-runtime-primitives-d89703ecfca3
However, the LinCombQFI cannot be imported anymore. While I have found the workaround for it, are there any updated tutorials with which one can get started with the gradients module?

@ElePT
Copy link
Collaborator Author

ElePT commented May 30, 2023

Hi @maxwell04-wq, #Qiskit/qiskit#9085 restructured the way the QFI class is instantiated. You can see an updated example in the gradients section of the opflow migration guide.

@ElePT
Copy link
Collaborator Author

ElePT commented May 30, 2023

1. Moving the Gradient Tutorial to Algorithms

* Begin with a brief introduction explaining the purpose of the tutorial.

* Describe the old location of the gradient framework (`qiskit.opflow`) and the reasons for moving it to `qiskit.algorithms`.

* Show how to import the gradient framework from its new location.

2. Updating the Tutorial to Show Use of Primitives

This tutorial would focus on explaining how primitives can be used within the gradient framework.

* Start with a brief explanation of what primitives are and why they are useful in the context of the gradient framework.

* Show how to use primitives in the gradient framework with a simple example. Explain each step in detail.

* Build on the simple example by showing how to use more complex primitives or combinations of primitives.

* End with a discussion of any potential pitfalls or common mistakes when using primitives in the gradient framework, and how to avoid them.

3. Adding a New Tutorial for Classically Efficient Gradients

This tutorial would introduce the concept of classically efficient gradients and show how to use them.

* Begin with an explanation of what classically efficient gradients are, why they're important, and how they fit into the gradient framework.

* Provide a simple example showing how to use classically efficient gradients. Explain each step in detail.

* Expand on the simple example by introducing more complex usage of classically efficient gradients. As before, provide detailed explanations.

Hi, thanks for your comment, but please note that this is not considered a contribution towards the Unitary Hack bounty. That being said, it also does not accurately reflect the content we want in this tutorial, but I agree that the explanation in the description was too short to properly convey this. I have noted this and added further explanation to the issue description, but I will also clarify here the 2 first points:

  1. Moving the Gradient Tutorial to Algorithms -> this referred to "physically" moving the file. The explanation of the migration of gredients can be found already in the opflow migration guide.
  2. Updating the Tutorial to Show Use of Primitives -> the idea is to show how to instantiate the new gradient classes that use primitives, the explanation of what primitives are is left to other introductory primitives material that can however be linked at the beginning of the tutorial.

@maxwell04-wq
Copy link
Contributor

maxwell04-wq commented May 30, 2023

@ElePT thanks for getting back and for explaining the issue.
Just to be sure, is this entire issue #1390 not a part of the UnitaryHack bounty or just a specific part of the tutorial?

@ElePT
Copy link
Collaborator Author

ElePT commented May 30, 2023

@maxwell04-wq this issue is a part of the UnitaryHack bounty, but the third item in the TO-DO list (adding a tutorial for classical gradients) is optional.

@BramDo
Copy link

BramDo commented May 31, 2023

@ElePT and @maxwell04-wq.
I made a start with the first part to get familiar with the gradient and estimator.

#General imports
import numpy as np

#Operator Imports
from qiskit.quantum_info import Pauli, SparsePauliOp
from qiskit.algorithms.gradients import ParamShiftEstimatorGradient
from qiskit.primitives import Estimator

#Circuit imports
from qiskit.circuit import QuantumCircuit, QuantumRegister, Parameter, ParameterVector, ParameterExpression
from qiskit.primitives import Estimator

# initialise the Estimator
estimator = Estimator()

# Instantiate the quantum state
a = Parameter('a')
q = QuantumRegister(1)
qc = QuantumCircuit(q)
qc.rx(a, q[0])

# Instantiate the Hamiltonian observable
H = SparsePauliOp(["Z"], coeffs = [1])

#Run gradient with target params
params = [[np.pi/4]]
param_grad = ParamShiftEstimatorGradient(estimator=estimator)
gradient = param_grad
gradient.run([qc], [H], params).result()

The result :
EstimatorGradientResult(gradients=[array([-0.70710678])], metadata=[{'parameters': ParameterView([Parameter(a)])}], options=Options())

maxwell04-wq added a commit to maxwell04-wq/qiskit-tutorials-unitaryhack2023 that referenced this issue May 31, 2023
**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
@BramDo
Copy link

BramDo commented May 31, 2023

@maxwell04-wq Great tutorial.

maxwell04-wq added a commit to maxwell04-wq/qiskit-tutorials-unitaryhack2023 that referenced this issue Jun 15, 2023
@ElePT ElePT removed their assignment Jun 16, 2023
ElePT added a commit that referenced this issue Jun 19, 2023
<!--
⚠️ 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 #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>
ElePT added a commit to ElePT/qiskit-algorithms that referenced this issue 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
documentation Something is not clear or error in description
Projects
None yet
3 participants