cosmiq.gradients#
Gradients of a program’s loss, chosen in one place.
g = cq.gradients.estimate(program, loss_fn, method="spsa", shots=2000, samples=50, seed=1)
g.value, g.stderr, g.shots
estimate: the selector. “adjoint” and “checkpoint” are exact, through the emulator; “spsa” and “finite-diff” sample, as the hardware would have to; “auto” picks the adjoint, or SPSA in samples-only mode.spsaandfinite_diff: the two sampled estimators, for any function of a flat parameter vector (the PennyLane device’s shot mode callsspsa). No parameter-shift rule: an analog block has none.
What |
|
What |
|
\(\partial\,\mathrm{loss}/\partial\theta\), where loss = |
|
Finite differences of |
|
SPSA's estimate of \(\partial\,\mathrm{fn}/\partial\,\mathrm{theta}\), from |
|
Classes
- class cosmiq.gradients.GradientEstimate(value: Mapping[str, Tensor], stderr: Mapping[str, Tensor] | None, shots: int, evaluations: int, method: str)#
Bases:
objectWhat
estimatereturns.- value: Mapping[str, Tensor]#
\(\partial\,\mathrm{loss}/\partial\theta\) by name (
named_parameters, the ones that require grad), shaped like \(\theta\), float64.
- class cosmiq.gradients.Jacobian(value: Tensor, stderr: Tensor | None, evaluations: int)#
Bases:
objectWhat
spsaandfinite_diffreturn.
Functions
- cosmiq.gradients.estimate(program: AnalogProgram, loss_fn: Callable[[EmulationResult], Tensor | float], *, inputs: Sequence[Any] = (), method: Literal['auto', 'adjoint', 'checkpoint', 'spsa', 'finite-diff'] = 'auto', emulator: Emulator | RemoteSession | LocalSession | None = None, shots: int | None = None, samples: int | None = None, step: float | None = None, noise: Literal['device', 'hardware'] | NoiseModel | None = None, seed: int | None = None) GradientEstimate#
\(\partial\,\mathrm{loss}/\partial\theta\), where loss =
loss_fn(emulator.run(program(*inputs), ...))is a scalar and \(\theta\) are the program’s parameters that require grad.method:“adjoint” and “checkpoint”: exact, from one run of a copy of
emulatorwith thatgradient=(the caller’s emulator is never changed);loss_fnmust keep its graph.“spsa”:
samplesrandom directions (default 20), steps ofstep(default 0.1), two runs each. “finite-diff”: central differences along every value of \(\theta\), steps ofstep(default 0.05), two runs each, repeatedsamplestimes (default 1).“auto”: “spsa” when
emulator.samples_only. Otherwise the emulator’s own choice (gradient="auto"): the adjoint, falling back to checkpointing when its certificate fails;methodin the result says which ran. “adjoint” and “checkpoint” are strict.
The sampled methods step the raw parameters, so a sigmoid-mode
boundedvalue can’t leave its limits. A clamp-mode value whose step would cross a limit takes a second-order one-sided difference anchored at its value, pointing inside (bias \(-h^2/3 \cdot \partial^3 f\); two steps must fit inside), and SPSA holds it still in its random steps (spsa,finite_diff).shots: run in shot mode, for aloss_fnthat readsresult.shots(samples-only mode needs it). For the sampled methods only.noise: as inEmulator.run. With noise, the run keeps no state, soloss_fnreads the emulator’s observable or the shots.seed: the root of every seed: the directions and each run’s draws, which are all different, as the hardware’s would be. None draws one from torch’s generator.emulatormay be acq.remotesession: the runs go to its box with itsemulator’s options (see the module docstring);loss_fnthen reads the observable or the shots, since remote runs keep no state.
- cosmiq.gradients.finite_diff(fn: Callable[[Tensor], Tensor], theta: Tensor, *, step: float = FD_STEP, low: Tensor | None = None, high: Tensor | None = None) Jacobian#
Finite differences of
fnalong every axis oftheta, a step ofstep: central (2 calls a value), or, where a step would cross a limit inloworhigh([P], \(\pm\infty\) for none), second-order one-sided and anchored at theta (2 calls a value, plus one at theta).fntakes a detached float64 [P] tensor and returns a float64 [*out] one.
- cosmiq.gradients.spsa(fn: Callable[[Tensor], Tensor], theta: Tensor, *, samples: int = SPSA_SAMPLES, step: float = SPSA_STEP, seed: int | None = None, low: Tensor | None = None, high: Tensor | None = None) Jacobian#
SPSA’s estimate of \(\partial\,\mathrm{fn}/\partial\,\mathrm{theta}\), from
samplesrandom directions.fntakes a detached float64 [P] tensor and returns a float64 [*out] tensor, which may be a shot-noise estimate.stepis c, the size of each step along every axis.seeddraws the directions (None: from torch’s generator); fn’s own randomness is its business.lowandhighare [P] limits (\(\pm\infty\) for none): a value whose step would cross one takes a one-sided difference anchored at theta instead (see the module docstring).
Values