qml.queuing.apply¶
- apply(op, context=<class 'pennylane.queuing.QueuingManager'>)[source]¶
Apply an instantiated operator or measurement to a queuing context.
- Parameters:
op (.Operator or .MeasurementProcess) – the operator or measurement to apply/queue
context (type[.QueuingManager] | AnnotatedQueue) – The queuing context to queue the operator to. Note that if no context is specified, the operator is applied to the currently active queuing context.
- Returns:
the input operator is returned for convenience
- Return type:
.Operator or .MeasurementProcess
Example
In PennyLane, operations and measurements are ‘queued’ or added to a circuit when they are instantiated.
The
applyfunction can be used to add operations that might have already been instantiated elsewhere to the QNode:op = qml.RX(0.4, wires=0) dev = qml.device("default.qubit", wires=2) @qml.qnode(dev) def circuit(x): qml.RY(x, wires=0) # applied during instantiation qml.apply(op) # manually applied return qml.expval(qml.Z(0))
>>> print(qml.draw(circuit)(0.6)) 0: ──RY(0.60)──RX(0.40)─┤ <Z>
It can also be used to apply functions repeatedly:
@qml.qnode(dev) def circuit(x): qml.apply(op) qml.RY(x, wires=0) qml.apply(op) return qml.expval(qml.Z(0))
>>> print(qml.draw(circuit)(0.6)) 0: ──RX(0.40)──RY(0.60)──RX(0.40)─┤ <Z>
Warning
If you use
applyon an operator that has already been queued, it will be queued for a second time. For example:@qml.qnode(dev) def circuit(): op = qml.Hadamard(0) qml.apply(op) return qml.expval(qml.Z(0))
>>> print(qml.draw(circuit)()) 0: ──H──H─┤ <Z>
Usage Details
Instantiated measurements can also be applied to queuing contexts using
apply:meas = qml.expval(qml.Z(0) @ qml.Y(1)) dev = qml.device("default.qubit", wires=2) @qml.qnode(dev) def circuit(x): qml.RY(x, wires=0) qml.CNOT(wires=[0, 1]) return qml.apply(meas)
>>> print(qml.draw(circuit)(0.6)) 0: ──RY(0.60)─╭●─┤ ╭<Z@Y> 1: ───────────╰X─┤ ╰<Z@Y>
By default,
applywill queue operators to the currently active queuing context.