qp.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 qp.measure operation applies a probabilistic mid-circuit measurement to the circuit. This is in contrast with qp.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 qp.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 qp.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 qp.qnode decorator.

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

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

Contents

Using PennyLane

Release news

Development

API

Internals