qml.QueuingManager

class QueuingManager[source]

Bases: object

Singleton global entry point for managing active recording contexts.

This class consists purely of class methods. It both maintains a list of recording queues and allows communication with the currently active object.

Queueable objects, like Operator and MeasurementProcess, should use QueuingManager as an entry point for accessing the active queue.

See also: AnnotatedQueue, QuantumTape, queue().

Recording queues, such as AnnotatedQueue, must define the following methods:

  • append: define an action to perform when an object append request is made.

  • remove: define an action to perform when an object removal request is made.

  • get_info: retrieve the object’s metadata

  • update_info: Update an object’s metadata if it is already queued.

To start and end recording, the recording queue can use the add_active_queue() and remove_active_queue() methods.

active_context()

Returns the currently active queuing context.

add_active_queue(queue)

Makes a queue the currently active recording context.

append(obj, **kwargs)

Append an object to the queue(s).

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).

remove_active_queue()

Ends recording on the currently active recording queue.

stop_recording()

A context manager and decorator to ensure that contained logic is non-recordable or non-queueable within a QNode or quantum tape context.

update_info(obj, **kwargs)

Updates information of an object in the active queue if it is already in the queue.

classmethod active_context()[source]

Returns the currently active queuing context.

classmethod add_active_queue(queue)[source]

Makes a queue the currently active recording context.

classmethod append(obj, **kwargs)[source]

Append an object to the queue(s).

Parameters

obj – the object to be appended

classmethod get_info(obj)[source]

Retrieves information of an object in the active queue.

Parameters

obj – the object with metadata to be retrieved

Returns

object metadata

classmethod recording()[source]

Whether a queuing context is active and recording operations

classmethod remove(obj)[source]

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

Parameters

obj – the object to be removed

classmethod remove_active_queue()[source]

Ends recording on the currently active recording queue.

classmethod stop_recording()[source]

A context manager and decorator to ensure that contained logic is non-recordable or non-queueable within a QNode or quantum tape context.

Example:

Consider the function:

>>> def list_of_ops(params, wires):
...     return [
...         qml.RX(params[0], wires=wires),
...         qml.RY(params[1], wires=wires),
...         qml.RZ(params[2], wires=wires)
...     ]

If executed in a recording context, the operations constructed in the function will be queued:

>>> dev = qml.device("default.qubit", wires=2)
>>> @qml.qnode(dev)
... def circuit(params):
...     ops = list_of_ops(params, wires=0)
...     qml.apply(ops[-1])  # apply the last operation from the list again
...     return qml.expval(qml.Z(0))
>>> print(qml.draw(circuit)([1, 2, 3]))
0: ──RX(1.00)──RY(2.00)──RZ(3.00)──RZ(3.00)─┤  <Z>

Using the stop_recording context manager, all logic contained inside is not queued or recorded.

>>> @qml.qnode(dev)
... def circuit(params):
...     with qml.QueuingManager.stop_recording():
...         ops = list_of_ops(params, wires=0)
...     qml.apply(ops[-1])
...     return qml.expval(qml.Z(0))
>>> print(qml.draw(circuit)([1, 2, 3]))
0: ──RZ(3.00)─┤  <Z>

The context manager can also be used as a decorator on a function:

>>> @qml.QueuingManager.stop_recording()
... def list_of_ops(params, wires):
...     return [
...         qml.RX(params[0], wires=wires),
...         qml.RY(params[1], wires=wires),
...         qml.RZ(params[2], wires=wires)
...     ]
>>> @qml.qnode(dev)
... def circuit(params):
...     ops = list_of_ops(params, wires=0)
...     qml.apply(ops[-1])
...     return qml.expval(qml.Z(0))
>>> print(qml.draw(circuit)([1, 2, 3]))
0: ──RZ(3.00)─┤  <Z>
classmethod update_info(obj, **kwargs)[source]

Updates information of an object in the active queue if it is already in the queue.

Parameters

obj – the object with metadata to be updated

Contents

Using PennyLane

Release news

Development

API

Internals