qml.dynamic_one_shot

dynamic_one_shot(tape, postselect_mode=None, **_)[source]

Transform a QNode to into several one-shot tapes to support dynamic circuit execution.

This transform enables the "one-shot" mid-circuit measurement method. The "one-shot" method prompts the device to perform a series of one-shot executions, where in each execution, the qml.measure operation applies a probabilistic mid-circuit measurement to the circuit. This is in contrast with qml.defer_measurement, which instead introduces an extra wire for each mid-circuit measurement. The "one-shot" method is favourable in the few-shots and several-mid-circuit-measurements limit, whereas qml.defer_measurements is favourable in the opposite limit.

Warning

This transform should not be directly applied on a QNode. It is automatically added to the compile pipeline when a QNode is constructed with mcm_method=’one-shot’.

Parameters:

tape (QNode or QuantumScript or Callable) – a quantum circuit.

Returns:

The transformed circuit as described in qml.transform. This circuit will provide the results of a dynamic execution.

Return type:

qnode (QNode) or quantum function (Callable) or tuple[List[QuantumScript], function]

Example

Most devices that support mid-circuit measurements will include this transform in its preprocessing automatically when applicable. The recommended way to use dynamic one shot is to specify mcm_method="one-shot" in the qml.qnode decorator.

dev = qml.device("default.qubit")
params = np.pi / 4 * np.ones(2)

@qml.set_shots(100)
@qml.qnode(dev, mcm_method="one-shot")
def func(x, y):
    qml.RX(x, wires=0)
    m0 = qml.measure(0)
    qml.cond(m0, qml.RY)(y, wires=1)
    return qml.expval(op=m0)
..details::
title:

Usage with Catalyst (qjit)

This transform is compatible with qjit, where it will be applied as an MLIR pass rather than a tape-level transform. That being said, there are a few differences to be aware of when using the MLIR pass:

  • Shot vectors or broadcasting are not supported

  • Workflows involving gradients are not supported

  • qml.var() on observables (non-MCM) are unsupported

To apply the MLIR pass version simply decorate your QNode with @qml.qjit:

@qml.qjit
@qml.set_shots(10)
@qml.qnode(qml.device("lightning.qubit", wires=2), mcm_method="one-shot")
def circ():
    return qml.expval(qml.X(0)+2*qml.Y(1))
>>> circ() 
Array(0.4, dtype=float64)

Contents

Using PennyLane

Release news

Development

API

Internals