Qiskit / qiskit-tutorials

A collection of Jupyter notebooks showing how to use the Qiskit SDK
Apache License 2.0
2.3k stars 1.29k forks source link

Update Gradient Framework tutorial to use primitives #1390

Closed ElePT closed 1 year ago

ElePT commented 1 year ago

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:

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 commented 1 year ago

1. Moving the Gradient Tutorial to Algorithms

2. Updating the Tutorial to Show Use of Primitives

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

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.

maxwell04-wq commented 1 year ago

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 commented 1 year ago

Hi @maxwell04-wq, #https://github.com/Qiskit/qiskit-terra/pull/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 commented 1 year ago

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 commented 1 year ago

@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 commented 1 year ago

@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 commented 1 year ago

@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())

BramDo commented 1 year ago

@maxwell04-wq Great tutorial.