qp.adjoint

adjoint(fn)[source]

Create the adjoint of an Operator or a function that applies the adjoint of the provided function. qjit() compatible.

Parameters:

fn (function or Operator) – A single operator or a quantum function that applies quantum operations.

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.

Return type:

(function or Operator)

Note

The adjoint and inverse are identical for unitary gates, but not in general. For example, quantum channels and observables may have different adjoint and inverse operators.

Note

When used with qjit(), this function only supports the Catalyst compiler. See catalyst.adjoint() 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.

Note

This function supports a batched operator:

>>> op = qp.adjoint(qp.RX([1, 2, 3], wires=0))
>>> qp.matrix(op).shape
(3, 2, 2)

But it doesn’t support batching of operators:

>>> op = qp.adjoint([qp.RX(1, wires=0), qp.RX(2, wires=0)])
Traceback (most recent call last):
    ...
ValueError: The object [RX(1, wires=[0]), RX(2, wires=[0])] of type <class 'list'> is not callable.
This error might occur if you apply adjoint to a list of operations instead of a function or template.

Example

The adjoint transform can accept a single operator.

>>> @qp.qnode(qp.device('default.qubit', wires=1))
... def circuit2(y):
...     qp.adjoint(qp.RY(y, wires=0))
...     return qp.expval(qp.Z(0))
>>> print(qp.draw(circuit2)(0.1))
0: ──RY(0.10)†─┤  <Z>
>>> print(qp.draw(circuit2, level="device")(0.1))
0: ──RY(0.10)†─┤  <Z>

The adjoint transforms can also be used to apply the adjoint of any quantum function. In this case, adjoint accepts a single function and returns a function with the same call signature.

We can create a QNode that applies the my_ops function followed by its adjoint:

def my_ops(a, wire):
    qp.RX(a, wires=wire)
    qp.SX(wire)

dev = qp.device('default.qubit', wires=1)

@qp.qnode(dev)
def circuit(a):
    my_ops(a, wire=0)
    qp.adjoint(my_ops)(a, wire=0)
    return qp.expval(qp.Z(0))

Printing this out, we can see that the inverse quantum function has indeed been applied:

>>> print(qp.draw(circuit)(0.2))
0: ──RX(0.20)──SX──SX†──RX(0.20)†─┤  <Z>

Example with compiler

The adjoint used in a compilation context can be applied on control flow.

dev = qp.device("lightning.qubit", wires=1)

@qp.qjit
@qp.qnode(dev)
def workflow(theta, n, wires):
    def func():
        @qp.for_loop(0, n, 1)
        def loop_fn(i):
            qp.RX(theta, wires=wires)

        loop_fn()
    qp.adjoint(func)()
    return qp.probs()
>>> import jax.numpy as jnp
>>> workflow(jnp.pi/2, 3, 0)
Array([0.5, 0.5], dtype=float64)

Warning

The Catalyst adjoint function does not support performing the adjoint of quantum functions that contain mid-circuit measurements.