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.

Note

This function is currently not compatible with qjit(), or with pennylane.capture.enabled().

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 condition is True. The callable must create a single mid-circuit measurement.

  • false_fn (callable) – The quantum function or PennyLane operation to apply if condition is False. 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_fn and false_fn.

Return type

function

Example

import pennylane as qml
from pennylane.ftqc import cond_measure, diagonalize_mcms, measure_x, measure_y

dev = qml.device("default.qubit", wires=3, shots=1000)

@diagonalize_mcms
@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))


>>> qnode(np.pi/3, np.pi/2)
0.3806

Note

If the first argument of cond_measure is a measurement value (e.g., m_0 in qml.cond(m_0, measure_x, measure_y)), then m_0 == 1 is considered internally.

Warning

Expressions with boolean logic flow using operators like and, or and not are not supported as the condition argument.

While such statements may not result in errors, they may result in incorrect behaviour.