qp.defer_measurements¶
- defer_measurements(tape, reduce_postselected=True, allow_postselect=True, num_wires=None)[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
Devices that inherit from
QubitDevicemust be initialized with an additional wire for each mid-circuit measurement after which the measured wire is reused or reset fordefer_measurementsto transform the quantum tape correctly.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 contains the
Snapshotinstruction, state information corresponding to simulating the transformed circuit will be obtained. No post-measurement states are considered.Warning
state()is not supported with thedefer_measurementstransform. Additionally,probs(),sample()andcounts()can only be used withdefer_measurementsif wires or an observable are explicitly specified.- Parameters:
tape (QNode or QuantumTape or Callable) – a quantum circuit.
reduce_postselected (bool) – Whether to use postselection information to reduce the number of operations and control wires in the output tape. Active by default. This is currently ignored if program capture is enabled.
allow_postselect (bool) – Whether postselection is allowed. In order to perform postselection with
defer_measurements, the device must support theProjectoroperation. Defaults toTrue. This is currently ignored if program capture is enabled.num_wires (int) – Optional argument to specify the total number of circuit wires. This is only used if program capture is enabled.
- Returns:
- The
transformed circuit as described in
qp.transform.
- Return type:
qnode (QNode) or quantum function (Callable) or tuple[List[QuantumTape], function]
- Raises:
ValueError – If any measurements with no wires or observable are present
ValueError – If using the transform with any device other than
default.qubitand postselection is used
Example
Suppose we have a quantum function with mid-circuit measurements and conditional operations:
def qfunc(par): qp.RY(0.123, wires=0) qp.Hadamard(wires=1) m_0 = qp.measure(1) qp.cond(m_0, qp.RY)(par, wires=0) return qp.expval(qp.Z(0))
The
defer_measurementstransform allows executing such quantum functions without having to perform mid-circuit measurements:>>> dev = qp.device('default.qubit', wires=2) >>> transformed_qfunc = qp.defer_measurements(qfunc) >>> qnode = qp.QNode(transformed_qfunc, dev) >>> par = pnp.array(np.pi/2, requires_grad=True) >>> qnode(par) tensor(0.434..., requires_grad=True)
We can also differentiate parameters passed to conditional operations:
>>> qp.grad(qnode)(par) tensor(-0.496... requires_grad=True)
Reusing and resetting measured wires will work as expected with the
defer_measurementstransform:dev = qp.device("default.qubit", wires=3) @qp.qnode(dev) def func(x, y): qp.RY(x, wires=0) qp.CNOT(wires=[0, 1]) m_0 = qp.measure(1, reset=True) qp.cond(m_0, qp.RY)(y, wires=0) qp.RX(np.pi/4, wires=1) return qp.probs(wires=[0, 1])
Executing this QNode:
>>> pars = pnp.array([0.643, 0.246], requires_grad=True) >>> func(*pars) tensor([0.769..., 0.132..., 0.0839..., 0.014...], requires_grad=True)
Usage Details
By default,
defer_measurementsmakes use of postselection information of mid-circuit measurements in the circuit in order to reduce the number of controlled operations and control wires. We can explicitly switch this feature off and compare the created circuits with and without this optimization. Consider the following circuit:@qp.qnode(qp.device("default.qubit")) def node(x): qp.RX(x, 0) qp.RX(x, 1) qp.RX(x, 2) mcm0 = qp.measure(0, postselect=0, reset=False) mcm1 = qp.measure(1, postselect=None, reset=True) mcm2 = qp.measure(2, postselect=1, reset=False) qp.cond(mcm0+mcm1+mcm2==1, qp.RX)(0.5, 3) return qp.expval(qp.Z(0) @ qp.Z(3))
Without the optimization, we find three gates controlled on the three measured qubits. They correspond to the combinations of controls that satisfy the condition
mcm0+mcm1+mcm2==1.>>> print(qp.draw(qp.defer_measurements(node, reduce_postselected=False))(0.6)) 0: ──RX(0.60)──|0⟩⟨0|─╭●─────────────────────────────────────────────┤ ╭<Z@Z> 1: ──RX(0.60)─────────│──╭●─╭X───────────────────────────────────────┤ │ 2: ──RX(0.60)─────────│──│──│───|1⟩⟨1|─╭○────────╭●────────╭○────────┤ │ 3: ───────────────────│──│──│──────────├RX(0.50)─├RX(0.50)─├RX(0.50)─┤ ╰<Z@Z> 4: ───────────────────╰X─│──│──────────├○────────├○────────├●────────┤ 5: ──────────────────────╰X─╰●─────────╰●────────╰○────────╰○────────┤
If we do not explicitly deactivate the optimization, we obtain a much simpler circuit:
>>> print(qp.draw(qp.defer_measurements(node))(0.6)) 0: ──RX(0.60)──|0⟩⟨0|─╭●─────────────────┤ ╭<Z@Z> 1: ──RX(0.60)─────────│──╭●─╭X───────────┤ │ 2: ──RX(0.60)─────────│──│──│───|1⟩⟨1|───┤ │ 3: ───────────────────│──│──│──╭RX(0.50)─┤ ╰<Z@Z> 4: ───────────────────╰X─│──│──│─────────┤ 5: ──────────────────────╰X─╰●─╰○────────┤
There is only one controlled gate with only one control wire.