Add SPSA optimization method
See original GitHub issueFeature 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:
- Described in James C. Spall, “An Overview of the Simultaneous Perturbation Method for Efficient Optimization”, 1998;
 - Showcased in the Optimization using SPSA PennyLane tutorial.
 
Furthermore, it is implemented:
- In the 
noisyoptpackage, specifically see the source code here; - In Qiskit, see the SPSA optimizer documentation.
 
Implementation and acceptance criteria
1. Implementation
There exists a qml.SPSAOptimizer optimizer that:
- Implements the 
compute_gradand theapply_gradmethods (seeGradientDescentOptimizer.apply_graddoc andGradientDescentOptimizer.compute_graddoc); - Implements a 
stepmethod (seeGradientDescentOptimizer.stepdoc); - Implements a 
step_and_costmethod (seeGradientDescentOptimizer.step_and_costdoc); - The 
SPSAOptimizerallows passing values to the quantities that greatly influence the optimization, including:a: scaling parameter for step size;A: stability constant;c: scaling parameter for evaluation step size;alpha: scaling exponent for step size;gamma: scaling exponent for evaluation step size; but also defines defaults suggested in the paper. Such defaults may be of the most practical use in many use cases. E.g.,noisyoptalso uses such defaults, except forAthat is “fixed” based on the number of iteration steps.
 
2. Testing
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:
- Introduce the concept of SPSA and the task it solves;
 - Include and explain equations (5) and (6) from “An Overview of the Simultaneous Perturbation Method for Efficient Optimization” (page 9 of the PDF that is page 490 of the volume);
 - Mention the number of quantum device executions required with SPSA;
 - Mention the use cases for SPSA as the optimization method of a hybrid quantum-classical workflow;
 - Add examples for showcasing the use of the optimizer as for 
QNGOptimizer. 
How important would you say this feature is?
2: Somewhat important. Needed this quarter.
Additional information
Issue Analytics
- State:
 - Created a year ago
 - Reactions:2
 - Comments:8 (7 by maintainers)
 

Top Related StackOverflow Question
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. 👍
What I like about PennyLane optimisers is the ability to have granular control over the iterations. This is usually missing from other libraries like
qiskitandscipy. 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.