qml.dynamic_one_shot

dynamic_one_shot(tape, **kwargs)[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.

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. When this is the case, any user-applied dynamic_one_shot transforms will be ignored. 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", shots=100)
params = np.pi / 4 * np.ones(2)

@qml.qnode(dev, mcm_method="one-shot", postselect_mode="fill-shots")
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)

Contents

Using PennyLane

Release news

Development

API

Internals