qml.defer_measurements¶
-
defer_measurements
(tape)[source]¶ Quantum function transform that substitutes operations conditioned on measurement outcomes to controlled operations.
This transform uses the deferred measurement principle and applies to qubit-based quantum functions.
Support for mid-circuit measurements is device-dependent. If a device doesn’t support mid-circuit measurements natively, then the QNode will apply this transform.
Note
The transform uses the
ctrl()
transform to implement operations controlled on mid-circuit measurement outcomes. The set of operations that can be controlled as such depends on the set of operations supported by the chosen device.Note
This transform does not change the list of terminal measurements returned by the quantum function.
Note
When applying the transform on a quantum function that returns
state()
as the terminal measurement or contains theSnapshot
instruction, state information corresponding to simulating the transformed circuit will be obtained. No post-measurement states are considered.- Parameters
qfunc (function) – a quantum function
Example
Suppose we have a quantum function with mid-circuit measurements and conditional operations:
def qfunc(par): qml.RY(0.123, wires=0) qml.Hadamard(wires=1) m_0 = qml.measure(1) qml.cond(m_0, qml.RY)(par, wires=0) return qml.expval(qml.PauliZ(0))
The
defer_measurements
transform allows executing such quantum functions without having to perform mid-circuit measurements:>>> dev = qml.device('default.qubit', wires=2) >>> transformed_qfunc = qml.defer_measurements(qfunc) >>> qnode = qml.QNode(transformed_qfunc, dev) >>> par = np.array(np.pi/2, requires_grad=True) >>> qnode(par) tensor(0.43487747, requires_grad=True)
We can also differentiate parameters passed to conditional operations:
>>> qml.grad(qnode)(par) tensor(-0.49622252, requires_grad=True)