qml.tape.interfaces.tf.TFInterface

class TFInterface[source]

Bases: pennylane.tape.queuing.AnnotatedQueue

Mixin class for applying an TensorFlow interface to a JacobianTape.

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

class MyTFQuantumTape(TFInterface, JacobianTape):

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

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

Note

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

Example

Once a TensorFlow quantum tape has been created, it can be differentiated using the gradient tape:

dev = qml.device("default.qubit", wires=1)
p = tf.Variable([0.1, 0.2, 0.3], dtype=tf.float64)

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

    result = qtape.execute(dev)
>>> print(result)
tf.Tensor([0.06982072], shape=(1,), dtype=float64)
>>> grad = tape.gradient(result, p)
>>> print(grad)
tf.Tensor([0.29874274 0.39710271 0.09958091], shape=(3,), dtype=float64)

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

>>> p = tf.Variable([0.1, 0.2, 0.3], dtype=tf.float32)
>>> with tf.GradientTape() as tape:
...     TFInterface.apply(qtape, dtype=tf.float32)  # reusing the previous qtape
...     result = qtape.execute(dev)
>>> print(result)
tf.Tensor([0.06982072], shape=(1,), dtype=float32)
>>> grad = tape.gradient(result, p)
>>> print(grad)
tf.Tensor([0.2895088  0.38464668 0.09645163], shape=(3,), dtype=float32)

dtype

interface

queue

Returns a list of objects in the annotated queue

dtype = tf.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 TensorFlow interface to an existing tape in-place.

convert_to_numpy(tensors)

Converts any TensorFlow tensors in a sequence to NumPy arrays.

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=tf.float64)[source]

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

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

  • dtype (tf.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))
>>> TFInterface.apply(tape)
>>> tape
<TFQuantumTape: wires=<Wires = [0]>, params=1>
static convert_to_numpy(tensors)[source]

Converts any TensorFlow tensors in a sequence to NumPy arrays.

Parameters

tensors (Sequence[Any, tf.Variable, tf.Tensor]) – input sequence

Returns

list with all tensors converted to NumPy arrays

Return type

list[Any, array]

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