qml.QueuingManager¶
- class QueuingManager[source]¶
Bases:
objectSingleton 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
OperatorandMeasurementProcess, should useQueuingManageras 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 metadataupdate_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()andremove_active_queue()methods.Methods
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.
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).
Ends recording on the currently active recording queue.
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 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 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 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_recordingcontext 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>