qml.CompilePipeline

class CompilePipeline(*transforms, cotransform_cache=None)[source]

Bases: object

Class that contains a compile pipeline and the methods to interact with it.

The order of execution is the order in the list containing the containers.

Parameters:
  • initial_program (Optional[Sequence[BoundTransform]]) – A sequence of transforms with which to initialize the program.

  • cotransform_cache (Optional[CotransformCache]) – A named tuple containing the qnode, args, and kwargs required to compute classical cotransforms.

The main case where one would have to interact directly with a compile pipeline is when developing a Device. In this case, the pre-processing method of a device returns a compile pipeline. You should directly refer to the device API documentation for more details.

Warning

This class is developer-facing and should not be used directly. Instead, use qml.transform if you would like to make a custom transform.

See also

transform()

Implemented Dunder methods

Programs have several implemented dunder methods for easy manipulation.

>>> from pennylane import CompilePipeline
>>> from copy import copy
>>> program = CompilePipeline()
>>> program.add_transform(qml.compile)
>>> program.add_transform(qml.transforms.cancel_inverses)
>>> [t for t in program]  # Iteration
[<compile((), {})>, <cancel_inverses((), {})>]
>>> program[0]
<compile((), {})>
>>> program[::-1]
CompilePipeline(cancel_inverses, compile)
>>> len(program)
2
>>> True if program else False
True
>>> True if CompilePipeline() else False
False
>>> program2 = copy(program)
>>> program2 == program
True
>>> qml.compile in program
True
>>> qml.transforms.split_non_commuting in program
False
>>> program + program
CompilePipeline(compile, cancel_inverses, compile, cancel_inverses)

has_final_transform

True if the compile pipeline has a terminal transform.

is_informative

True if the compile pipeline is informative.

has_final_transform

True if the compile pipeline has a terminal transform.

is_informative

True if the compile pipeline is informative.

Returns:

Boolean

Return type:

bool

add_transform(transform, *targs, **tkwargs)

Add a transform (dispatcher) to the end of the program.

get_last()

Get the last transform container.

has_classical_cotransform()

Check if the compile pipeline has some classical cotransforms.

insert_front(transform_container)

Insert the transform container at the beginning of the program.

insert_front_transform(transform, *targs, ...)

Add a transform (dispatcher) to the beginning of the program.

is_empty()

Check if the compile pipeline is empty or not.

pop_front()

Pop the transform container at the beginning of the program.

prune_dynamic_transform([type_to_keep])

Ensures that only one or none dynamic_one_shot is applied.

push_back(transform_container)

Add a transform (container) to the end of the program.

remove(obj)

In place remove the input containers, specifically, 1.

set_classical_component(qnode, args, kwargs)

Set the classical jacobians and argnums if the transform is hybrid with a classical cotransform.

add_transform(transform, *targs, **tkwargs)[source]

Add a transform (dispatcher) to the end of the program.

Note that this should be a function decorated with/called by qml.transforms.transform, and not a BoundTransform.

Parameters:
  • transform (TransformDispatcher) – The transform to add to the compile pipeline.

  • *targs – Any additional arguments that are passed to the transform.

Keyword Arguments:

**tkwargs – Any additional keyword arguments that are passed to the transform.

get_last()[source]

Get the last transform container.

Returns:

The last transform in the program.

Return type:

BoundTransform

Raises:

TransformError – It raises an error if the program is empty.

has_classical_cotransform()[source]

Check if the compile pipeline has some classical cotransforms.

Returns:

Boolean

Return type:

bool

insert_front(transform_container)[source]

Insert the transform container at the beginning of the program.

Parameters:

transform_container (BoundTransform) – A transform represented by its container.

insert_front_transform(transform, *targs, **tkwargs)[source]

Add a transform (dispatcher) to the beginning of the program.

Parameters:
  • transform (TransformDispatcher) – The transform to add to the front of the compile pipeline.

  • *targs – Any additional arguments that are passed to the transform.

Keyword Arguments:

**tkwargs – Any additional keyword arguments that are passed to the transform.

is_empty()[source]

Check if the compile pipeline is empty or not.

Returns:

Boolean, True if empty, False otherwise.

Return type:

bool

pop_front()[source]

Pop the transform container at the beginning of the program.

Returns:

The transform container at the beginning of the program.

Return type:

BoundTransform

prune_dynamic_transform(type_to_keep=1)[source]

Ensures that only one or none dynamic_one_shot is applied.

Parameters:

type_to_keep (int) – The type of the dynamic transform to keep. 0: keep none, 1: dynamic_one_shot or mid_circuit_measurements, 2: only mid_circuit_measurements.

Returns:

True if a dynamic transform was found, False otherwise.

Return type:

bool

push_back(transform_container)[source]

Add a transform (container) to the end of the program.

Parameters:

transform_container (BoundTransform) – A transform represented by its container.

remove(obj)[source]

In place remove the input containers, specifically, 1. if the input is a TransformDispatcher, remove all containers matching the dispatcher; 2. if the input is a BoundTransform, remove all containers exactly matching the input.

Parameters:

obj (BoundTransform or TransformDispatcher) – The object to remove from the program.

set_classical_component(qnode, args, kwargs)[source]

Set the classical jacobians and argnums if the transform is hybrid with a classical cotransform.