measure(wires, reset=False, postselect=None)¶
Perform a mid-circuit measurement in the computational basis on the supplied qubit.
Measurement outcomes can be obtained and used to conditionally apply operations.
If a device doesn’t support mid-circuit measurements natively, then the QNode will apply the
dev = qml.device("default.qubit", wires=3) @qml.qnode(dev) def func(x, y): qml.RY(x, wires=0) qml.CNOT(wires=[0, 1]) m_0 = qml.measure(1) qml.cond(m_0, qml.RY)(y, wires=0) return qml.probs(wires=)
Executing this QNode:
>>> pars = np.array([0.643, 0.246], requires_grad=True) >>> func(*pars) tensor([0.90165331, 0.09834669], requires_grad=True)
Wires can be reused after measurement. Moreover, measured wires can be reset to the \(|0 \rangle\) by setting
dev = qml.device("default.qubit", wires=3) @qml.qnode(dev) def func(): qml.PauliX(1) m_0 = qml.measure(1, reset=True) return qml.probs(wires=)
Executing this QNode:
>>> func() tensor([1., 0.], requires_grad=True)
Mid circuit measurements can be manipulated using the following arithmetic operators:
>with other mid-circuit measurements or scalars.
or, do not work since these do not have dunder methods. Instead use
wires (Wires) – The wire of the qubit the measurement process applies to.
reset (Optional[bool]) – Whether to reset the wire to the \(|0 \rangle\) state after measurement.
postselect (Optional[int]) – Which basis state to postselect after a mid-circuit measurement. None by default. If postselection is requested, only the post-measurement state that is used for postselection will be considered in the remaining circuit.
measurement process instance
- Return type
QuantumFunctionError – if multiple wires were specified
Postselection discards outcomes that do not meet the criteria provided by the
postselectargument. For example, specifying
postselect=1on wire 0 would be equivalent to projecting the state vector onto the \(|1\rangle\) state on wire 0:
dev = qml.device("default.qubit") @qml.qnode(dev) def func(x): qml.RX(x, wires=0) m0 = qml.measure(0, postselect=1) qml.cond(m0, qml.PauliX)(wires=1) return qml.sample(wires=1)
By postselecting on
1, we only consider the
1measurement outcome on wire 0. So, the probability of measuring
1on wire 1 after postselection should also be 1. Executing this QNode with 10 shots:
>>> func(np.pi / 2, shots=10) array([1, 1, 1, 1, 1, 1, 1])
Note that only 7 samples are returned. This is because samples that do not meet the postselection criteria are thrown away.
If postselection is requested on a state with zero probability of being measured, the result may contain
dev = qml.device("default.qubit") @qml.qnode(dev) def func(x): qml.RX(x, wires=0) m0 = qml.measure(0, postselect=1) qml.cond(m0, qml.PauliX)(wires=1) return qml.probs(wires=1)
>>> func(0.0) tensor([nan, nan], requires_grad=True)
In the case of
qml.sample, an empty array will be returned:
dev = qml.device("default.qubit") @qml.qnode(dev) def func(x): qml.RX(x, wires=0) m0 = qml.measure(0, postselect=1) qml.cond(m0, qml.PauliX)(wires=1) return qml.sample()
>>> func(0.0, shots=[10, 10]) (array(, dtype=float64), array(, dtype=float64))
Currently, postselection support is only available on
default.qubit. Using postselection on other devices will raise an error.
All measurements are supported when using postselection. However, postselection on a zero probability state can cause some measurements to break.
With finite shots, one must be careful when measuring
qml.counts, as these measurements will raise errors if there are no valid samples after postselection. This will occur with postselection states that have zero or close to zero probability.
With analytic execution,
qml.mutual_infowill raise errors when using any interfaces except
qml.vn_entropywill raise an error with the
tensorflowinterface when the postselection state has zero probability.
- What is PennyLane?
- Quantum circuits
- Gradients and training
- Quantum operators
- Inspecting circuits
- Compiling circuits
- Quantum Chemistry
- Quantum Datasets
- QNode returns