catalyst.measure¶
- measure(wires, reset: bool | None = False, postselect: int | None = None) DynamicJaxprTracer[source]¶
A
qjit()compatible mid-circuit measurement on 1 qubit for PennyLane/Catalyst.Important
The
qp.measure()function is not QJIT compatible andcatalyst.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
import pennylane as qp from catalyst import qjit, measure dev = qp.device("lightning.qubit", wires=2) @qjit @qp.qnode(dev) def circuit(x: float): qp.RX(x, wires=0) m1 = measure(wires=0) qp.RX(m1 * jnp.pi, wires=1) m2 = measure(wires=1) qp.RZ(m2 * jnp.pi / 2, wires=0) return qp.expval(qp.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 = qp.device("lightning.qubit", wires=1) @qjit @qp.qnode(dev) def circuit(): qp.Hadamard(0) m = measure(0, postselect=1) return qp.expval(qp.PauliZ(0))
>>> circuit() Array(-1., dtype=float64)
Example with reset
dev = qp.device("lightning.qubit", wires=1) @qjit @qp.qnode(dev) def circuit(): qp.Hadamard(0) m = measure(0, reset=True) return qp.expval(qp.PauliZ(0))
>>> circuit() Array(1., dtype=float64)