microquantum.core.gradient¶
Analytical gradients via the Parameter-Shift Rule (MQ-13).
The parameter-shift rule computes the exact derivative of an expectation value \(\langle O\rangle(\theta)\) for circuits built from single-qubit rotation gates. For \(U(\theta)=e^{-i\theta P/2}\) with \(P\in\{X,Y,Z\}\) the derivative is
for any shift \(s\) that is not an integer multiple of \(\pi\) (the default \(s=\pi/2\)).
Policies (MQ-13):
Identity: parameters are matched by name (consistent with the MQ-12 parameter model), so a
Parameterworks transparently even when another instance with the same name appears in the circuit.Chain rule: when a gate angle is a
ParameterExpressiona*theta + c, each occurrence contributesatimes the parameter-shift difference; a parameter used in several gates contributes the sum of its per-occurrence gradients (product rule).Observables:
Operator,PauliStringandPauliSumare all supported;PauliString/PauliSumterms are evaluated without building dense matrices, optionally over a subset of qubits viatargets.Execution: by default shifted circuits are simulated with the built-in state-vector engine. Pass
backend=to route evaluation through the MQ-11/12 execution core (backend.runwithseed/shotsfor reproducibility).Strictness:
param_valuesmust be aMappingand must provide values for every circuit parameter; observables must be one of the supported types; a shift with \(\sin(s)\approx 0\) is rejected.
Module Contents¶
- microquantum.core.gradient.parameter_shift_gradient(circuit, observable, param, param_values, shift=np.pi / 2, targets=None, *, backend=None, seed=None, shots=None)[source]¶
Compute
d<O>/dparamwith the Parameter-Shift Rule.For a rotation \(U(\theta)=e^{-i\theta G/2}\) with Pauli generator \(G\), the derivative of an expectation value is
\[\frac{d\langle O\rangle}{d\theta} = \frac{\langle O\rangle(\theta+s) - \langle O\rangle(\theta-s)}{2\,\sin(s)}\;.\]When the parameter appears in several gates (or inside a
ParameterExpressionwith a coefficient), each occurrence is shifted independently, scaled by the chain-rule factoraof its angle expression w.r.t.param, and the results are summed (product rule).- Parameters:
circuit (microquantum.core.circuit.QuantumCircuit) – Parameterized quantum circuit.
observable (Union[microquantum.core.operators.Operator, microquantum.core.pauli.PauliString, microquantum.core.pauli.PauliSum]) –
Operator,PauliStringorPauliSum.param (microquantum.core.parameter.Parameter) – The parameter to differentiate with respect to (matched by name).
param_values (collections.abc.Mapping[Union[str, microquantum.core.parameter.Parameter], float]) – Mapping of parameter names/objects to numeric values for the differentiation point. Must cover every parameter in the circuit.
shift (float) – Shift angle for the rule (default
pi/2). Must not be an integer multiple ofpi.targets (Optional[list[int]]) – Optional qubit indices a Pauli/operator observable acts on. Needed when an
Operator/PauliString/PauliSumcovers a strict subset of the circuit’s qubits.backend (object) – Optional execution backend used to evaluate the shifted circuits. Defaults to the built-in state-vector engine.
seed (Optional[int]) – Optional seed passed to
backend.runfor reproducible evaluation.shots (Optional[int]) – Optional shot count passed to
backend.run. Expectation values use the exact statevector, soshotsonly affects the (ignored) sampled counts.
- Returns:
The analytical partial derivative
d<O>/d(param).- Raises:
ValueError – If
shiftis an integer multiple of pi, an observable is malformed, or the backend returns no statevector.TypeError – If
param_valuesis not aMappingor the observable is not anOperator/PauliString/PauliSum.KeyError – If
param(or any other circuit parameter) has no value inparam_values, orparamdoes not occur in any gate.
- Return type:
- microquantum.core.gradient.gradient(circuit, observable, param_values, shift=np.pi / 2, targets=None, *, backend=None, seed=None, shots=None)[source]¶
Compute the full analytic gradient vector for all parameters.
Evaluates
d<O>/d(theta_i)for everyParameterofcircuit(in the deterministicQuantumCircuit.parametersorder), delegating each entry toparameter_shift_gradient(). The result uses the circuit’sParameterobjects as keys, so it plugs directly into the gradient-aware optimizers (gradient_fn=).- Parameters:
circuit (microquantum.core.circuit.QuantumCircuit) – Parameterized quantum circuit.
observable (Union[microquantum.core.operators.Operator, microquantum.core.pauli.PauliString, microquantum.core.pauli.PauliSum]) –
Operator,PauliStringorPauliSum.param_values (collections.abc.Mapping[Union[str, microquantum.core.parameter.Parameter], float]) – Mapping of parameter names/objects to numeric values for the differentiation point. Must cover every parameter in the circuit.
shift (float) – Shift angle for the rule (default
pi/2).targets (Optional[list[int]]) – Optional qubit indices the observable acts on.
backend (object) – Optional execution backend (see
parameter_shift_gradient()).seed (Optional[int]) – Optional seed for backend evaluation.
shots (Optional[int]) – Optional shot count for backend evaluation (expectations use the statevector, so
shotsdoes not change the result).
- Returns:
A
{Parameter: float}mapping with one analytic gradient entry per circuit parameter.- Raises:
- Return type: