qml.tape.interfaces.autograd.AutogradInterface

class AutogradInterface[source]

Bases: pennylane.tape.queuing.AnnotatedQueue

Mixin class for applying an autograd interface to a JacobianTape.

Autograd-compatible quantum tape classes can be created via subclassing:

class MyAutogradQuantumTape(AutogradInterface, JacobianTape):

Alternatively, the autograd interface can be dynamically applied to existing quantum tapes via the apply() class method. This modifies the tape in place.

Once created, the autograd interface can be used to perform quantum-classical differentiable programming.

Note

If using a device that supports native autograd computation and backpropagation, such as DefaultQubitAutograd, the Autograd interface does not need to be applied. It is only applied to tapes executed on non-Autograd compatible devices.

Example

Once an autograd quantum tape has been created, it can be differentiated using autograd:

tape = AutogradInterface.apply(JacobianTape())

with tape:
    qml.Rot(0, 0, 0, wires=0)
    expval(qml.PauliX(0))

def cost_fn(x, y, z, device):
    tape.set_parameters([x, y ** 2, y * np.sin(z)], trainable_only=False)
    return tape.execute(device=device)
>>> x = np.array(0.1, requires_grad=False)
>>> y = np.array(0.2, requires_grad=True)
>>> z = np.array(0.3, requires_grad=True)
>>> dev = qml.device("default.qubit", wires=2)
>>> cost_fn(x, y, z, device=dev)
[0.03991951]
>>> jac_fn = qml.jacobian(cost_fn)
>>> jac_fn(x, y, z, device=dev)
[[ 0.39828408, -0.00045133]]

interface

queue

Returns a list of objects in the annotated queue

interface
queue

Returns a list of objects in the annotated queue

active_context()

Returns the currently active queuing context.

append(obj, **kwargs)

Append an object to the queue(s).

apply(tape)

Apply the autograd interface to an existing tape in-place.

get_info(obj)

Retrieves information of an object in the active queue.

get_parameters([trainable_only])

recording()

Whether a queuing context is active and recording operations

remove(obj)

Remove an object from the queue(s) if it is in the queue(s).

update_info(obj, **kwargs)

Updates information of an object in the active queue.

vjp(ans, self, params, device)

Returns the vector-Jacobian product operator for the quantum tape.

classmethod active_context()

Returns the currently active queuing context.

classmethod append(obj, **kwargs)

Append an object to the queue(s).

Parameters

obj – the object to be appended

classmethod apply(tape)[source]

Apply the autograd interface to an existing tape in-place.

Parameters

tape (JacobianTape) – a quantum tape to apply the Autograd interface to

Example

>>> with JacobianTape() as tape:
...     qml.RX(0.5, wires=0)
...     expval(qml.PauliZ(0))
>>> AutogradInterface.apply(tape)
>>> tape
<AutogradQuantumTape: wires=<Wires = [0]>, params=1>
classmethod get_info(obj)

Retrieves information of an object in the active queue.

Parameters

obj – the object with metadata to be retrieved

Returns

object metadata

get_parameters(trainable_only=True)[source]
classmethod recording()

Whether a queuing context is active and recording operations

classmethod remove(obj)

Remove an object from the queue(s) if it is in the queue(s).

Parameters

obj – the object to be removed

classmethod update_info(obj, **kwargs)

Updates information of an object in the active queue.

Parameters

obj – the object with metadata to be updated

static vjp(ans, self, params, device)[source]

Returns the vector-Jacobian product operator for the quantum tape. The returned function takes the arguments as execute().

Parameters
  • ans (array) – the result of the tape execution

  • self (AutogradQuantumTape) – the tape instance

  • params (list[Any]) – the quantum tape operation parameters

  • device (Device) – a PennyLane device that can execute quantum operations and return measurement statistics

Returns

this function accepts the backpropagation gradient output vector, and computes the vector-Jacobian product

Return type

function