catalyst.adjoint¶
- adjoint(f: Union[Callable, pennylane.operation.Operator], lazy=True) Union[Callable, pennylane.operation.Operator] [source]¶
A
qjit()
compatible adjoint transformer for PennyLane/Catalyst.Returns a quantum function or operator that applies the adjoint of the provided function or operator.
Warning
This function does not support performing the adjoint of quantum functions that contain mid-circuit measurements.
- Parameters
f (Callable or Operator) – A PennyLane operation or a Python function containing PennyLane quantum operations.
lazy (bool) – Whether to delay the computation of the Hermitian conjugate until a later time (typically during decomposition or compilation). The default is
True
, whereasFalse
will immediately produce a new operator implementing the adjoint. Note thatFalse
is only supported when the adjoint is applied to a single Operator, rather than a quantum function.
- Returns
If an Operator is provided, returns an Operator that is the adjoint. If a function is provided, returns a function with the same call signature that returns the Adjoint of the provided function.
- Raises
ValueError – invalid parameter values
Example 1 (basic usage)
@qjit @qml.qnode(qml.device("lightning.qubit", wires=1)) def workflow(theta, wires): catalyst.adjoint(qml.RZ)(theta, wires=wires) catalyst.adjoint(qml.RZ(theta, wires=wires)) def func(): qml.RX(theta, wires=wires) qml.RY(theta, wires=wires) catalyst.adjoint(func)() return qml.probs()
>>> workflow(jnp.pi/2, wires=0) Array([0.5, 0.5], dtype=float64)
Example 2 (with Catalyst control flow)
@qjit @qml.qnode(qml.device("lightning.qubit", wires=1)) def workflow(theta, n, wires): def func(): @catalyst.for_loop(0, n, 1) def loop_fn(i): qml.RX(theta, wires=wires) loop_fn() catalyst.adjoint(func)() return qml.probs()
>>> workflow(jnp.pi/2, 3, 0) [1.00000000e+00 7.39557099e-32]