qml.transforms.core.transform_program.TransformProgram¶
- class TransformProgram(initial_program=None, cotransform_cache=None)[source]¶
Bases:
object
Class that contains a transform program 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[TransformContainer]]) – A sequence of transforms with which to initialize the program.
cotransform_cache (Optional[CotransformCache]) – A named tuple containing the
qnode
,args
, andkwargs
required to compute classical cotransforms.
The main case where one would have to interact directly with a transform program is when developing a
Device
. In this case, the pre-processing method of a device returns a transform program. 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
Implemented Dunder methods
Programs have several implemented dunder methods for easy manipulation.
>>> from pennylane.transforms.core.transform_program import TransformProgram >>> from copy import copy >>> program = TransformProgram() >>> 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] TransformProgram(cancel_inverses, compile) >>> len(program) 2 >>> True if program else False True >>> True if TransformProgram() else False False >>> program2 = copy(program) >>> program2 == program True >>> qml.compile in program True >>> qml.transforms.split_non_commuting in program False >>> program + program TransformProgram(compile, cancel_inverses, compile, cancel_inverses)
Attributes
True
if the transform program has a terminal transform.True
if the transform program is informative.- has_final_transform¶
True
if the transform program has a terminal transform.
- is_informative¶
True
if the transform program is informative.- Returns
Boolean
- Return type
bool
Methods
add_transform
(transform, *targs, **tkwargs)Add a transform (dispatcher) to the end of the program.
get_last
()Get the last transform container.
Check if the transform program 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 transform program is empty or not.
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.
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 aTransformContainer
.- Parameters
transform (TransformDispatcher) – The transform to add to the transform program.
*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
- Raises
TransformError – It raises an error if the program is empty.
- has_classical_cotransform()[source]¶
Check if the transform program 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 (TransformContainer) – 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 transform program.
*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 transform program 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
- 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 (TransformContainer) – A transform represented by its container.