qml.tape.stop_recording¶
-
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 following quantum 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 within a QNode or a tape context, these operations will be recorded, even if this is not the intention:
>>> 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 only the last operation from the list ... return qml.expval(qml.PauliZ(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 within is not queued or recorded by the QNode:>>> @qml.qnode(dev) ... def circuit(params): ... with stop_recording(): ... ops = list_of_ops(params, wires=0) ... qml.apply(ops[-1]) ... return qml.expval(qml.PauliZ(0)) >>> print(qml.draw(circuit)([1, 2, 3])) 0: ──RZ(3.00)─┤ <Z>
stop_recording
can also be used as a decorator. Decorated functions, when executed, will inhibit any internal quantum operation processing from being recorded by the QNode:>>> @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.PauliZ(0)) >>> print(qml.draw(circuit)([1, 2, 3])) 0: ──RZ(3.00)─┤ <Z>
code/api/pennylane.tape.stop_recording
Download Python script
Download Notebook
View on GitHub