catalyst.measure

measure(wires, reset: Optional[bool] = False, postselect: Optional[int] = None) jax._src.interpreters.partial_eval.DynamicJaxprTracer[source]

A qjit() compatible mid-circuit measurement on 1 qubit for PennyLane/Catalyst.

Important

The qml.measure() function is not QJIT compatible and catalyst.measure() from Catalyst should be used instead.

Parameters
  • wires (int) – The wire the projective measurement 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.

Returns

A JAX tracer for the mid-circuit measurement.

Raises

ValueError – Called outside the tape context.

Note

To configure the mid-circuit measurement simulation method and postselection mode, please see the relevant keyword arguments of the QNode().

For more details, check out our dynamic quantum circuit guide.

Example

dev = qml.device("lightning.qubit", wires=2)

@qjit
@qml.qnode(dev)
def circuit(x: float):
    qml.RX(x, wires=0)
    m1 = measure(wires=0)

    qml.RX(m1 * jnp.pi, wires=1)
    m2 = measure(wires=1)

    qml.RZ(m2 * jnp.pi / 2, wires=0)
    return qml.expval(qml.PauliZ(0)), m2
>>> circuit(0.43)
[Array(1., dtype=float64), Array(False, dtype=bool)]
>>> circuit(0.43)
[Array(-1., dtype=float64), Array(True, dtype=bool)]

Example with post-selection

dev = qml.device("lightning.qubit", wires=1)

@qjit
@qml.qnode(dev)
def circuit():
    qml.Hadamard(0)
    m = measure(0, postselect=1)
    return qml.expval(qml.PauliZ(0))
>>> circuit()
Array(-1., dtype=float64)

Example with reset

dev = qml.device("lightning.qubit", wires=1)

@qjit
@qml.qnode(dev)
def circuit():
    qml.Hadamard(0)
    m = measure(0, reset=True)
    return qml.expval(qml.PauliZ(0))
>>> circuit()
Array(1., dtype=float64)