PennyLaneAI / pennylane

PennyLane is a cross-platform Python library for quantum computing, quantum machine learning, and quantum chemistry. Train a quantum computer the same way as a neural network.
https://pennylane.ai
Apache License 2.0
2.28k stars 585 forks source link

Add SPSA optimization method #2451

Closed ankit27kh closed 2 years ago

ankit27kh commented 2 years ago

Feature details

The Simultaneous Perturbation Stochastic Approximation (SPSA) optimisation method is a faster optimisation method.

If the number of terms being optimized is p, then the finite-difference method takes 2p measurements of the objective function at each iteration (to form one gradient approximation), while SPSA takes only two measurements

It is also naturally suited for noisy measurements. Thus, it will be useful when simulating noisy systems.

The theory (and implementation) for SPSA is:

Furthermore, it is implemented:

Implementation and acceptance criteria

1. Implementation

There exists a qml.SPSAOptimizer optimizer that:

Test the SPSAOptimizer class similar to how GradientDescentOptimizer and QNGOptimizer are being tested.

3. Documentation

The documentation of the optimizer should be similar in fashion to the documentation of the QNGOptimizer.

The docstring of qml.SPSAOptimizer should:

How important would you say this feature is?

2: Somewhat important. Needed this quarter.

Additional information

https://www.jhuapl.edu/SPSA/

antalszava commented 2 years ago

Hi @ankit27kh, thank you! We have a demonstration showcasing how to use PennyLane with SPSA optimization. In the demo, the noisyopt package is used as it integrates well with PennyLane.

Would it be beneficial if PennyLane came with a dedicated SPSA optimizer?

ankit27kh commented 2 years ago

Hi @antalszava, thanks for the link. I wasn't aware of this. I would still suggest having SPSA built into PennyLane as it is advantageous for optimisations in the quantum world and should be available without needing to install additional libraries. After integration, it should work like other optimisers:

opt = qml.SPSA()
for _ in range(10):
  params = opt.step(cost, params)
ankit27kh commented 2 years ago

What I like about PennyLane optimisers is the ability to have granular control over the iterations. This is usually missing from other libraries like qiskit and scipy. Sometimes you can use callback functions, but it's not the same. I hope we can have SPSA in PennyLane so that we still enjoy a high degree of control over the optimisation process.

antalszava commented 2 years ago

Hi @ankit27kh, thank you so much for this feedback! In fact, we've been considering adding a dedicated SPSA optimizer to PennyLane. Good to hear that there would be value in such an addition. :+1:

ixime commented 2 years ago

I am not sure what is required for the use cases of hybrid quantum-classical workflow, because as far as I know, in those cases, the optimizers used are the ones of the classical platforms, i.e. tf.keras.optimizers.Adam

antalszava commented 2 years ago

Hi @ixime, the part on quantum classical workflows just means to mention in the docstring of the optimizer the fact that SPSA can be particularly beneficial when optimizing workflows that are using noisy quantum devices.

maxwell04-wq commented 2 years ago

Hi @ankit27kh,

I would like to contribute a tutorial for simulated annealing using PennyLane. Can be converted in a module too. Can I do it under UnitaryHack?

CatalinaAlbornoz commented 2 years ago

Hi @maxwell04-wq, you're welcome to contribute a tutorial to our qml repo! However this repo is not participating in UnitaryHACK.

I would encourage you to make a new issue on this repo (not the qml repo) if you want to contribute your module to PennyLane and participate in UnitaryHACK!