qp.workflow.construct_batch

construct_batch(qnode, level='user')[source]

Construct the batch of tapes and post processing for a designated stage in the transform program.

Parameters:
  • qnode (QNode) – the qnode we want to get the tapes and post-processing for.

  • level (str, int, slice) –

    An indication of what transforms to apply before constructing the batch. Defaults to "user".

    • "top": No transforms are applied.

    • "user": Only the transforms manually applied by the user.

    • "gradient": The user-applied transforms, followed by the transform that expands the trainable operations for the gradient method, if any.

    • "device": All the transforms, including the gradient and device preprocessing transforms, i.e. the tapes that the device executes.

    • str: The label of a marker(), to apply all transforms up to the location of that marker.

    • int: The number of transforms to apply from the start of the compile pipeline, e.g. level=0 applies no transforms.

    • slice: A specific range of transforms, using standard 0-based Pythonic indexing.

    Note

    These levels count each gradient and device preprocessing transform separately, and support "gradient". This differs from get_compile_pipeline(), analyze(), estimate() and track(), where the device preprocessing is a single level. Therefore, an int or slice level past the user-applied transforms can select different transforms in these functions.

Returns:

A function with the same call signature as the initial quantum function. This function returns a batch (tuple) of tapes and postprocessing function.

Return type:

Callable

Suppose we have a QNode with several user transforms.

from pennylane.workflow import construct_batch

dev = qp.device('default.qubit')

@qp.transforms.undo_swaps
@qp.transforms.merge_rotations
@qp.transforms.cancel_inverses
@qp.qnode(dev, diff_method="parameter-shift", gradient_kwargs = {"shifts": np.pi/4})
def circuit(x):
    qp.RandomLayers(qp.numpy.array([[1.0, 2.0]]), wires=(0,1))
    qp.RX(x, wires=0)
    qp.RX(-x, wires=0)
    qp.SWAP((0,1))
    qp.X(0)
    qp.X(0)
    return qp.expval(qp.X(0) + qp.Y(0))

We can inspect what the device will execute with:

>>> batch, fn = construct_batch(circuit, level="device")(1.23)
>>> batch[0].circuit
[RY(1.0, wires=[1]),
 RX(2.0, wires=[0]),
 expval(X(0) + Y(0))]

These tapes can be natively executed by the device. However, with non-backprop devices the parameters will need to be converted to NumPy with convert_to_numpy_parameters().

>>> fn(dev.execute(batch))
(np.float64(-0.9092974268256817),)

Or what the parameter shift gradient transform will be applied to:

>>> batch, fn = construct_batch(circuit, level="gradient")(1.23)
>>> batch[0].circuit
[RY(1.0, wires=[1]), RX(2.0, wires=[0]), expval(X(0) + Y(0))]

We can inspect what was directly captured from the qfunc with level=0.

>>> batch, fn = construct_batch(circuit, level=0)(1.23)
>>> batch[0].circuit
[RandomLayers(tensor([[1., 2.]], requires_grad=True), wires=[0, 1]),
 RX(1.23, wires=[0]),
 RX(-1.23, wires=[0]),
 SWAP(wires=[0, 1]),
 X(0),
 X(0),
 expval(X(0) + Y(0))]

And iterate though stages in the transform program with different integers. If we request level=1, the cancel_inverses transform has been applied.

>>> batch, fn = construct_batch(circuit, level=1)(1.23)
>>> batch[0].circuit
[RandomLayers(tensor([[1., 2.]], requires_grad=True), wires=[0, 1]),
 RX(1.23, wires=[0]),
 RX(-1.23, wires=[0]),
 SWAP(wires=[0, 1]),
 expval(X(0) + Y(0))]

We can also slice into a subset of the transform program. slice(1, None) would skip the first user transform cancel_inverses:

>>> batch, fn = construct_batch(circuit, level=slice(1,None))(1.23)
>>> batch[0].circuit
[RY(1.0, wires=[1]), RX(2.0, wires=[0]), X(0), X(0), expval(X(0) + Y(0))]