Search

Search papers and lessons

Simultaneous Perturbation Stochastic Approximation

Level 3 · 12 min read

SPSA estimates a gradient of a noisy loss with only two measurements, by nudging every parameter at once in a random direction, so its cost does not grow with the number of parameters.

On this page

  • Colour = topic mix
  • Calculus
  • Discrete math
  • Probability
  • Optimizers
  • Linear algebra
  • Losses & regularization
  • ReadReady to readNot yet

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, L(θ)=θ12+2θ22L(\theta) = \theta_1^2 + 2\theta_2^2. Its minimum is at (0,0)(0, 0), and its Gradient at θ=(1,1)\theta = (1, 1) is (2,4)(2, 4). Pretend we can see only the loss values, and we want a gradient estimate from just two of them.

  1. Pick a random direction. Draw Δ=(+1,−1)\Delta = (+1, -1), each entry ±1\pm 1 with equal chance. Check: every parameter is moved by the same size, cc, but with a random sign.
  2. Probe both sides at once. With c=0.1c = 0.1, evaluate the loss at θ+cΔ=(1.1,0.9)\theta + c\Delta = (1.1, 0.9) and at θ−cΔ=(0.9,1.1)\theta - c\Delta = (0.9, 1.1). We get L(1.1,0.9)=1.21+1.62=2.83L(1.1, 0.9) = 1.21 + 1.62 = 2.83 and L(0.9,1.1)=0.81+2.42=3.23L(0.9, 1.1) = 0.81 + 2.42 = 3.23. Check: the loss is lower on the side where the move goes downhill.
  3. Measure the slope along the direction. The difference is 2.83−3.23=−0.402.83 - 3.23 = -0.40. Divide by 2c=0.22c = 0.2 to get −2.0-2.0. This is the slope of the loss along Δ\Delta. Check: the true slope along Δ\Delta is (2,4)⋅(1,−1)=−2(2, 4)\cdot(1, -1) = -2, which matches.
  4. Turn the slope into a gradient estimate. Multiply by each Δi\Delta_i, since Δi=±1\Delta_i = \pm 1 and dividing by Δi\Delta_i is the same as multiplying: g^=(−2.0)(1,−1)=(−2,2)\hat{g} = (-2.0)(1, -1) = (-2, 2). Check: the estimate is (−2,2)(-2, 2), while the true gradient is (2,4)(2, 4). One direction gives a noisy estimate, and averaging over many random directions makes it correct on average.
  5. Take a step. With step size a=0.1a = 0.1, move to θ′=(1,1)−0.1(−2,2)=(1.2,0.8)\theta' = (1, 1) - 0.1(-2, 2) = (1.2, 0.8). The loss drops from 33 to 1.44+1.28=2.721.44 + 1.28 = 2.72. 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 L(θ)L(\theta) be the loss, which we can measure only with noise, and θ∈Rp\theta \in \mathbb{R}^p. SPSA draws a random vector Δk\Delta_k whose pp entries are independent ±1\pm 1 values with equal probability, a Rademacher vector. It computes

g^k(θk)=L(θk+ckΔk)−L(θk−ckΔk)2ck Δk−1\hat{g}_k(\theta_k) = \frac{L(\theta_k + c_k \Delta_k) - L(\theta_k - c_k \Delta_k)}{2 c_k}\, \Delta_k^{-1}

where Δk−1\Delta_k^{-1} is the vector with entries 1/Δk,i1/\Delta_{k,i}, which equal Δk,i\Delta_{k,i} because each entry is ±1\pm 1. Then it updates θk+1=θk−akg^k(θk)\theta_{k+1} = \theta_k - a_k \hat{g}_k(\theta_k). The sequences aka_k and ckc_k 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 Δk\Delta_k with P(Δk,i=1)=P(Δk,i=−1)=12\mathbb{P}(\Delta_{k,i} = 1) = \mathbb{P}(\Delta_{k,i} = -1) = \tfrac12, independently across ii. This gives an expected value of E[Δk,i]=0\mathbb{E}[\Delta_{k,i}] = 0 and Δk,i2=1\Delta_{k,i}^2 = 1.

Step 2 in symbols: the two probes

Write L±=L(θk±ckΔk)L_\pm = L(\theta_k \pm c_k \Delta_k). 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 L±=L(θk)±ck∇L(θk)⊤Δk+O(ck2)L_\pm = L(\theta_k) \pm c_k \nabla L(\theta_k)^\top \Delta_k + O(c_k^2), so the difference is L+−L−=2ck ∇L(θk)⊤Δk+O(ck2)L_+ - L_- = 2 c_k\, \nabla L(\theta_k)^\top \Delta_k + O(c_k^2). This is why the difference divided by 2ck2 c_k is the slope of LL along the direction Δk\Delta_k, up to a small error that shrinks with ckc_k.

Step 3 in symbols: the expectation

Multiplying the slope by Δk,i\Delta_{k,i} gives g^k,i=(∇L⊤Δk)Δk,i=∑j∂jL Δk,jΔk,i\hat{g}_{k,i} = \bigl(\nabla L^\top \Delta_k\bigr)\Delta_{k,i} = \sum_j \partial_j L\, \Delta_{k,j} \Delta_{k,i}. The terms with j≠ij \neq i have expectation zero, because E[Δk,jΔk,i]=0\mathbb{E}[\Delta_{k,j}\Delta_{k,i}] = 0 for independent signs, and the term with j=ij = i has Δk,i2=1\Delta_{k,i}^2 = 1. So E[g^k,i]=∂iL(θk)\mathbb{E}[\hat{g}_{k,i}] = \partial_i L(\theta_k). The estimate is unbiased, up to the O(ck2)O(c_k^2) 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 pp is.

Step 4 in symbols: the cost comparison

A finite-difference estimate perturbs one coordinate at a time and needs 2p2p evaluations per gradient. SPSA needs 2. Over KK steps, the costs are 2K2K and 2pK2pK.

Worked examples

Example
1. Two parameters, one probe pair (tiny)

Let L(θ)=θ12+2θ22L(\theta) = \theta_1^2 + 2\theta_2^2 at θ=(1,1)\theta = (1, 1), with c=0.1c = 0.1 and Δ=(−1,+1)\Delta = (-1, +1).

  1. The probes are θ+cΔ=(0.9,1.1)\theta + c\Delta = (0.9, 1.1) and θ−cΔ=(1.1,0.9)\theta - c\Delta = (1.1, 0.9).
  2. The loss at (0.9,1.1)(0.9, 1.1) is 0.81+2(1.21)=3.230.81 + 2(1.21) = 3.23. The loss at (1.1,0.9)(1.1, 0.9) is 1.21+2(0.81)=2.831.21 + 2(0.81) = 2.83.
  3. The difference is 3.23−2.83=0.403.23 - 2.83 = 0.40, and divided by 2c=0.22c = 0.2 it is 2.02.0.
  4. The estimate is g^=2.0 (Δ1,Δ2)=2.0 (−1,1)=(−2,2)\hat{g} = 2.0\,(\Delta_1, \Delta_2) = 2.0\,(-1, 1) = (-2, 2).
  5. This is the same estimate as in the lesson's worked instance, because flipping the sign of Δ\Delta flips both the difference and the entries, leaving their product unchanged.

Interpretation: flipping the sign of Δ\Delta flips both the probe difference and the direction, so the estimate does not change. Only the choice of random direction matters.

Example
2. Four parameters, one noisy estimate (standard)

Let L(θ)=θ12+θ22+θ32+θ42L(\theta) = \theta_1^2 + \theta_2^2 + \theta_3^2 + \theta_4^2 at θ=(1,−1,2,0)\theta = (1, -1, 2, 0). The true gradient is (2,−2,4,0)(2, -2, 4, 0). Use c=0.1c = 0.1 and Δ=(1,1,−1,1)\Delta = (1, 1, -1, 1).

  1. The probe θ+cΔ=(1.1,−0.9,1.9,0.1)\theta + c\Delta = (1.1, -0.9, 1.9, 0.1) has loss 1.21+0.81+3.61+0.01=5.641.21 + 0.81 + 3.61 + 0.01 = 5.64.
  2. The probe θ−cΔ=(0.9,−1.1,2.1,−0.1)\theta - c\Delta = (0.9, -1.1, 2.1, -0.1) has loss 0.81+1.21+4.41+0.01=6.440.81 + 1.21 + 4.41 + 0.01 = 6.44.
  3. The difference is 5.64−6.44=−0.805.64 - 6.44 = -0.80, and divided by 2c=0.22c = 0.2 it is −4.0-4.0.
  4. The estimate is g^=−4.0 (Δ1,Δ2,Δ3,Δ4)=(−4,−4,4,−4)\hat{g} = -4.0\,(\Delta_1, \Delta_2, \Delta_3, \Delta_4) = (-4, -4, 4, -4).
  5. Compare with the truth (2,−2,4,0)(2, -2, 4, 0). The third entry is exactly right and the second has the right sign, but the first has the wrong sign (−4-4 where the truth is +2+2), and the fourth is −4-4 where the truth is 0. A single draw is noisy. Averaging over several random directions reduces this error.
  6. 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.

Example
3. Tuning a quantum circuit with 50 angles (realistic)

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.

  1. Finite differences need 2×50=1002 \times 50 = 100 energy estimates per step. Over 200 steps that is 100×200=20,000100 \times 200 = 20{,}000 estimates.
  2. SPSA needs 2 energy estimates per step. Over 200 steps that is 2×200=4002 \times 200 = 400 estimates.
  3. The ratio is 20,000/400=5020{,}000 / 400 = 50. The savings equal the number of angles, because each SPSA step uses two runs regardless of the count.
  4. 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.
  5. 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: 2×200=4002 \times 200 = 400. Finite differences would need 2×1000×200=400,0002 \times 1000 \times 200 = 400{,}000. 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 ckc_k must shrink slowly, so that the bias from the O(ck2)O(c_k^2) 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 Δ\Delta must be independent and have mean zero, and a standard choice is ±1\pm 1 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 2c2c and multiplies by the sign vector, and it is unbiased up to a small term.
  • Each step costs two evaluations, while finite differences cost 2p2p 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.

Check yourself

QuestionWhat is the difference between a Rademacher vector and a standard normal vector, and which one does SPSA use?Show answer
A Rademacher vector has entries that are independently +1+1 or −1-1, each with probability one half. A standard normal vector has entries drawn from a bell curve. SPSA uses the Rademacher vector, so that each entry's inverse is simply the entry itself.
QuestionLet L(θ)=θ12+2θ22L(\theta) = \theta_1^2 + 2\theta_2^2 at θ=(1,1)\theta = (1, 1), with c=0.1c = 0.1 and Δ=(1,1)\Delta = (1, 1). What is the SPSA estimate g^\hat{g}?Show answer
The probes are (1.1,1.1)(1.1, 1.1) and (0.9,0.9)(0.9, 0.9). Their losses are 1.21+2.42=3.631.21 + 2.42 = 3.63 and 0.81+1.62=2.430.81 + 1.62 = 2.43. The difference is 1.201.20, divided by 2c=0.22c = 0.2 it is 6.06.0. The estimate is 6.0 (1,1)=(6,6)6.0\,(1, 1) = (6, 6). The true gradient is (2,4)(2, 4), so one estimate is far from it, and averaging over directions is needed.
QuestionWhy does the cost of SPSA not grow with the number of parameters, while the cost of finite differences does?Show answer
SPSA perturbs every parameter at once, so one pair of loss values gives information about all coordinates together. Finite differences perturb one coordinate at a time, so each coordinate needs its own pair of loss values, which makes the cost 2p2p per gradient.
QuestionAn engineer uses SPSA with a fixed perturbation size c=0.5c = 0.5 on a loss whose curvature changes quickly. The estimates are poor. Explain one likely cause and one fix.Show answer
A large fixed cc makes the probes far from the local region, so the O(c2)O(c^2) error term in the estimate dominates. The fix is to shrink ckc_k over time, following a gain sequence, so the probes stay close to the current point while the loss is still being learned.

Sources