Docs

VQA & QAOA loops

Read this first — scope. This pattern works only when your cost is diagonal: a function of the measured bits (a QAOA / Ising cost, max-cut). It does not work for VQE-style energy — expectation values of non-diagonal Hamiltonians, where the objective lives in correlations the runtime cannot steer. We tested this thoroughly; for VQE energy, keep your classical optimizer. For diagonal cost, the cleanest path is QUBO & diagonal optimization — and pass the scored configuration as the measured statistic, not a separate reading.

A diagonal variational loop has the same shape as the runtime: you hold a parameter vector, you measure a cost under a shot budget, and you update. For a diagonal cost you may replace your classical optimizer (COBYLA, SPSA) with the runtime and pass the measured cost each round.

The pattern

copyfrom swc import SWCOptimizer

theta = init_angles(p)                 # your starting parameters
opt = SWCOptimizer(license_key="EVAL-...", n=len(theta))
theta = opt.start(theta)
for _ in range(rounds):
    exp = run_circuit(theta, shots=S)  # ONE batch of shots -> a measured statistic
    theta = opt.step(exp, score=-cost(exp))   # score rewards lower cost
opt.end()

Where it pays off

The advantage concentrates at tight shot budgets — the regime where parameter-shift cannot afford its gradient and SPSA is noisy. At very small per-round budgets on decision-style objectives, fewer shots per round can be enough; see choosing measurements per round. Before a long run, gate it with the envelope.

Score convention

The runtime maximizes score. For a minimization (energy, cost), pass score=-cost. Keep the score consistent across rounds.