uniqc.pytorch.gradient module#
Parameter-shift rule gradient computation for quantum circuits.
The parameter-shift rule allows exact gradient computation for parametric quantum gates without finite differences. For gates with generator G:
∂⟨G(θ)⟩/∂θ = (⟨G(θ + π/2)⟩ - ⟨G(θ - π/2)⟩) / 2
This module provides functions to compute gradients for all parameters in a parametric circuit.
- uniqc.pytorch.gradient.compute_all_gradients(circuit, expectation_fn, shift=1.5707963267948966)[source]#
Compute gradients for all parameters in a circuit.
Uses the parameter-shift rule for each parameter independently.
- Parameters:
- Returns:
Dictionary mapping parameter names to gradient values
- Return type:
Example
>>> grads = compute_all_gradients(circuit, expectation) >>> print(grads) # {'theta': 0.5, 'phi': -0.3}
- uniqc.pytorch.gradient.parameter_shift_gradient(circuit, param_name, expectation_fn, shift=1.5707963267948966)[source]#
Compute gradient using the parameter-shift rule.
For a parametric gate G(θ), the gradient of an expectation value is:
∂⟨G(θ)⟩/∂θ = 0.5 * (⟨G(θ + π/2)⟩ - ⟨G(θ - π/2)⟩)
- Parameters:
- Returns:
Gradient value
- Return type:
Example
>>> def expectation(c): ... # Simulate and compute <Z0> ... sim.simulate(c.originir) ... return sim.expectation([("Z", [0])]) >>> grad = parameter_shift_gradient(circuit, "theta", expectation)