measure

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

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

Important

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

Parameters
  • wires (Wires) – The wire of the qubit the measurement process applies to

  • reset (Optional[bool]) – Whether to reset the wire to the |0⟩ 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.

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.), array(False)]
>>> circuit(0.43)
[array(-1.), array(True)]

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()
-1.0

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()
1.0