qml.ftqc.cond_measure¶
- cond_measure(condition, true_fn, false_fn)[source]¶
Perform a mid-circuit measurement where the basis of the measurement is conditional on the supplied expression. This conditional expression may involve the results of other mid-circuit qubit measurements.
- Parameters:
condition (Union[.MeasurementValue, bool]) – a conditional expression that may involve a mid-circuit measurement value (see
pennylane.measure()).true_fn (callable) – The quantum function or PennyLane operation to apply if
conditionisTrue. The callable must create a single mid-circuit measurement.false_fn (callable) – The quantum function or PennyLane operation to apply if
conditionisFalse. The callable must create a single mid-circuit measurement.
Note
The mid-circuit measurements applied on the two branches must both be applied to the same wire, and they must have the same settings for reset and postselection. The two branches can differ only in regard to the measurement basis of the applied measurement.
- Returns:
A new function that applies the conditional measurements. The returned function takes the same input arguments as
true_fnandfalse_fn.- Return type:
function
Example
from pennylane.ftqc import cond_measure, diagonalize_mcms, measure_x, measure_y from functools import partial dev = qml.device("default.qubit", wires=3) @diagonalize_mcms @qml.set_shots(shots=1_000) @qml.qnode(dev, mcm_method="one-shot") def qnode(x, y): qml.RY(x, 0) qml.Hadamard(1) m0 = qml.measure(0) m2 = cond_measure(m0, measure_x, measure_y)(1) qml.Hadamard(2) qml.cond(m2 == 0, qml.RY)(y, wires=2) return qml.expval(qml.X(2))
>>> print(qnode(np.pi/3, np.pi/2)) 0.3914
Note
If the first argument of
cond_measureis a measurement value (e.g.,m_0inqml.cond(m_0, measure_x, measure_y)), thenm_0 == 1is considered internally.Warning
Expressions with boolean logic flow using operators like
and,orandnotare not supported as theconditionargument.While such statements may not result in errors, they may result in incorrect behaviour.