TL;DR
Training a model often means lowering a loss that you can only measure with noise, and you have no formula for its Gradient. The usual fix estimates the gradient one parameter at a time, which costs two loss evaluations per parameter. SPSA perturbs all parameters at once along a random sign pattern and uses just two loss evaluations to form a gradient estimate, however many parameters there are.
Builds on: Stochastic Gradient Descent, Stochastic Approximation, Rademacher Distribution · Leads to: practical optimisers for noisy black-box objectives, such as tuning quantum circuits and simulations
Where it's used
- Tuning variational quantum circuits. A quantum circuit with many rotation angles is run on hardware, and its energy is estimated from a finite number of measurements, so each evaluation is noisy and costly. SPSA is a common choice because it needs two energy estimates per step, however many angles the circuit has.
- Simulation optimisation. A model of a factory, a traffic network or a supply chain is run as a simulation, and its output is random. Engineers tune many settings against the simulation's average output, where computing a Derivative directly is not possible.
- Black-box models. When the model is only available as a black box, for example a system that can be queried but not differentiated, SPSA gives a gradient estimate from function values alone. It is used in settings where backpropagation is not available.
How it works
Take a tiny instance: a loss with two parameters, . Its minimum is at , and its Gradient at is . Pretend we can see only the loss values, and we want a gradient estimate from just two of them.
- Pick a random direction. Draw , each entry with equal chance. Check: every parameter is moved by the same size, , but with a random sign.
- Probe both sides at once. With , evaluate the loss at and at . We get and . Check: the loss is lower on the side where the move goes downhill.
- Measure the slope along the direction. The difference is . Divide by to get . This is the slope of the loss along . Check: the true slope along is , which matches.
- Turn the slope into a gradient estimate. Multiply by each , since and dividing by is the same as multiplying: . Check: the estimate is , while the true gradient is . One direction gives a noisy estimate, and averaging over many random directions makes it correct on average.
- Take a step. With step size , move to . The loss drops from to . Check: the loss went down, as a descent step should.
The estimate used only two loss values. A finite-difference estimate for the same problem would need two values per parameter, so four here. The cost difference grows with the number of parameters.
flowchart TD A["Current parameters theta"] --> B["Draw random sign vector Delta"] B --> C["Measure loss at theta plus c Delta"] B --> D["Measure loss at theta minus c Delta"] C --> E["Gradient estimate: difference over 2c, divided by each Delta entry"] D --> E E --> F["Step: theta minus a times estimate"] F --> A
The math
Let be the loss, which we can measure only with noise, and . SPSA draws a random vector whose entries are independent values with equal probability, a Rademacher vector. It computes
where is the vector with entries , which equal because each entry is . Then it updates . The sequences and are gain sequences that shrink over time, in the same way as the step sizes of stochastic approximation.
Step 1 in symbols: the random direction
Each step draws with , independently across . This gives an expected value of and .
Step 2 in symbols: the two probes
Write . These are two loss measurements, and their noise is what the rest of the analysis has to average out. For a smooth loss, a Taylor expansion of each probe gives , so the difference is . This is why the difference divided by is the slope of along the direction , up to a small error that shrinks with .
Step 3 in symbols: the expectation
Multiplying the slope by gives . The terms with have expectation zero, because for independent signs, and the term with has . So . The estimate is unbiased, up to the term in Step 2.
The price is variance. Each estimate has extra noise from the cross terms, which is why many steps are needed. The total number of loss evaluations per step is 2, whatever is.
Step 4 in symbols: the cost comparison
A finite-difference estimate perturbs one coordinate at a time and needs evaluations per gradient. SPSA needs 2. Over steps, the costs are and .
Worked examples
Let at , with and .
- The probes are and .
- The loss at is . The loss at is .
- The difference is , and divided by it is .
- The estimate is .
- This is the same estimate as in the lesson's worked instance, because flipping the sign of flips both the difference and the entries, leaving their product unchanged.
Interpretation: flipping the sign of flips both the probe difference and the direction, so the estimate does not change. Only the choice of random direction matters.
Let at . The true gradient is . Use and .
- The probe has loss .
- The probe has loss .
- The difference is , and divided by it is .
- The estimate is .
- Compare with the truth . The third entry is exactly right and the second has the right sign, but the first has the wrong sign ( where the truth is ), and the fourth is where the truth is 0. A single draw is noisy. Averaging over several random directions reduces this error.
- A finite-difference estimate would have used 8 loss values here, against 2 for SPSA.
Interpretation: one SPSA estimate is a rough guide, and its value is that it costs very little. Many cheap, noisy steps reach a good point, as with Stochastic Gradient Descent.
A variational quantum circuit has 50 rotation angles. Its energy is estimated from measurements, and each estimate costs one run of the circuit. We plan 200 optimisation steps.
- Finite differences need energy estimates per step. Over 200 steps that is estimates.
- SPSA needs 2 energy estimates per step. Over 200 steps that is estimates.
- The ratio is . The savings equal the number of angles, because each SPSA step uses two runs regardless of the count.
- In practice SPSA needs more steps than an exact gradient method would, because each estimate is noisy. The 400 estimates are still far fewer than 20,000.
- The step sizes must shrink, following the conditions in Stochastic Approximation, or the angles keep moving with the measurement noise.
Interpretation: when each evaluation is expensive and the number of parameters is large, the cost per step decides the method. SPSA trades a noisier direction for a fixed cost per step.
Try it: the same plan with 1,000 angles and 200 steps. How many energy estimates does SPSA need in total? Answer: . Finite differences would need . SPSA still needs 400 because its cost does not depend on the number of angles.
Common mistakes and tradeoffs
It is tempting to think SPSA is a cheap gradient, as accurate as a full one. In fact each estimate is noisy, and its accuracy comes from averaging many steps, as the law of large numbers suggests. A single estimate can have the wrong sign in some coordinates, as Example 2 shows.
It is tempting to think the perturbation must be small in every direction to get a good estimate. In fact the perturbation size must shrink slowly, so that the bias from the term falls. A perturbation that is too large makes the probe values far from the local slope.
It is tempting to think any random direction works. In fact the entries of must be independent and have mean zero, and a standard choice is with equal probability. Entries of zero, or a direction whose entries have very different sizes, make some coordinates look flat when they are not.
It is tempting to think SPSA is always better than finite differences because it uses fewer evaluations. In fact it needs more steps, and for a loss that is cheap to compute and has few parameters, finite differences cost little more and give a cleaner gradient.
When to use it: many parameters, noisy or expensive loss values, and no access to derivatives. When not to use it: a few parameters with exact cheap gradients, where backpropagation or Newton-type methods are far better. Alternatives include Evolution Strategies, which also use only loss values and perturb parameters randomly, and full finite differences when the parameter count is small.
History
Kiefer and Wolfowitz introduced finite-difference stochastic approximation in 1952, which needs two evaluations per parameter. In 1992 James Spall proposed the simultaneous perturbation idea, which perturbs all parameters at once with random signs. Its cost per step is independent of the number of parameters. The method was developed for control and simulation problems, where losses are noisy and gradients are unavailable. It gained wider use in the 2000s and 2010s for tuning systems whose only interface is a noisy score, including quantum circuits.
Recap
- SPSA estimates a gradient of a noisy loss from two loss values, by perturbing all parameters at once along a random sign vector.
- The estimate divides the difference of the two losses by and multiplies by the sign vector, and it is unbiased up to a small term.
- Each step costs two evaluations, while finite differences cost per gradient.
- The estimates are noisy, so many steps and shrinking gains are needed.
- The method suits high-dimensional noisy objectives, and is a poor choice when cheap exact gradients are available.
Read next: Evolution Strategies, which also optimises from loss values alone, and Stochastic Gradient Descent for the gradient-based methods that SPSA replaces when derivatives are unavailable.