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.
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()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.
The runtime maximizes score. For a minimization (energy, cost), pass score=-cost. Keep the score consistent across rounds.