qml.vjp¶
- vjp(f, params, cotangents, method=None, h=None, argnums=None, *, argnum=None)[source]¶
A
qjit()compatible Vector-Jacobian product of PennyLane programs.This function allows the Vector-Jacobian Product of a hybrid quantum-classical function to be computed within the compiled program.
Warning
vjpis intended to be used withqjit()only.Note
When used with
qjit(), this function only supports the Catalyst compiler. Seecatalyst.vjp()for more details.Please see the Catalyst quickstart guide, as well as the sharp bits and debugging tips page for an overview of the differences between Catalyst and PennyLane.
Warning
The argument
argnumhas been renamed toargnumsto match Catalyst and JAX. The ability to useargnumwill be removed in v0.45.- Parameters:
f (Callable) – Function-like object to calculate VJP for
params (List[Array]) – List (or a tuple) of arguments for f specifying the point to calculate VJP at. A subset of these parameters are declared as differentiable by listing their indices in the
argnumsparameter.cotangents (List[Array]) – List (or a tuple) of tangent values to use in VJP. The list size and shapes must match the size and shape of
foutputs.method (str) – Differentiation method to use, same as in
grad().h (float) – the step-size value for the finite-difference (
"fd") methodargnums (Union[int, List[int]]) – the params’ indices to differentiate.
- Returns:
Return values of
fpaired with the VJP values.- Return type:
Tuple[Array]
- Raises:
TypeError – invalid parameter types
ValueError – invalid parameter values
See also
Example
@qml.qjit def vjp(params, cotangent): def f(x): y = [jnp.sin(x[0]), x[1] ** 2, x[0] * x[1]] return jnp.stack(y) return qml.vjp(f, [params], [cotangent])
>>> x = jnp.array([0.1, 0.2]) >>> dy = jnp.array([-0.5, 0.1, 0.3]) >>> vjp(x, dy) (Array([0.09983342, 0.04 , 0.02 ], dtype=float64), (Array([-0.43750208, 0.07 ], dtype=float64),))