qml.tape.interfaces.torch.TorchInterface

class TorchInterface[source]

Bases: pennylane.tape.queuing.AnnotatedQueue

Mixin class for applying an Torch interface to a JacobianTape.

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

class MyTorchQuantumTape(TorchInterface, JacobianTape):

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

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

Example

Once a Torch quantum tape has been created, it can be evaluated and differentiated:

dev = qml.device("default.qubit", wires=1)
p = torch.tensor([0.1, 0.2, 0.3], requires_grad=True)

with TorchInterface.apply(JacobianTape()) as qtape:
    qml.Rot(p[0], p[1] ** 2 + p[0] * p[2], p[1] * torch.sin(p[2]), wires=0)
    expval(qml.PauliX(0))

result = qtape.execute(dev)
>>> print(result)
tensor([0.0698], dtype=torch.float64, grad_fn=<_TorchInterfaceBackward>)
>>> result.backward()
>>> print(p.grad)
tensor([0.2987, 0.3971, 0.0988])

The Torch interface defaults to torch.float64 output. This can be modified by providing the dtype argument when applying the interface:

>>> p = torch.tensor([0.1, 0.2, 0.3], requires_grad=True)
>>> with TorchInterface.apply(JacobianTape()) as qtape:
...     qml.Rot(p[0], p[1] ** 2 + p[0] * p[2], p[1] * torch.sin(p[2]), wires=0)
...     expval(qml.PauliX(0))
>>> result = qtape.execute(dev)
>>> print(result)
tensor([0.0698], grad_fn=<_TorchInterfaceBackward>)
>>> print(result.dtype)
torch.float32
>>> result.backward()
>>> print(p.grad)
tensor([0.2987, 0.3971, 0.0988])
>>> print(p.grad.dtype)
torch.float32

dtype

interface

queue

Returns a list of objects in the annotated queue

dtype = torch.float64
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[, dtype])

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

get_info(obj)

Retrieves information of an object in the active queue.

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.

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, dtype=torch.float64)[source]

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

Parameters
  • tape (JacobianTape) – a quantum tape to apply the Torch interface to

  • dtype (torch.dtype) – the dtype that the returned quantum tape should output

Example

>>> with JacobianTape() as tape:
...     qml.RX(0.5, wires=0)
...     expval(qml.PauliZ(0))
>>> TorchInterface.apply(tape)
>>> tape
<TorchQuantumTape: 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

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