qml.CompilePipeline¶
- class CompilePipeline(*transforms, cotransform_cache=None)[source]¶
Bases:
objectA sequence of transforms to be applied to a quantum function or a
QNode.- Parameters:
*transforms (Optional[Sequence[Transform | BoundTransform]]) – A sequence of transforms with which to initialize the program.
cotransform_cache (Optional[CotransformCache]) – A named tuple containing the
qnode,args, andkwargsrequired to compute classical cotransforms.
Example:
The
CompilePipelineclass allows you to chain together multiple quantum function transforms to create custom circuit optimization pipelines.For example, consider if you wanted to apply the following optimizations to a quantum circuit:
pushing all commuting single-qubit gates as far right as possible (
commute_controlled())cancellation of adjacent inverse gates (
cancel_inverses())merging adjacent rotations of the same type (
merge_rotations())
You can specify a transform program (
pipeline) by passing these transforms to theCompilePipelineclass. By applying the createdpipelinedirectly on a quantum function as a decorator, the circuit will be transformed with each pass within the pipeline sequentially:pipeline = qml.CompilePipeline( qml.transforms.commute_controlled, qml.transforms.cancel_inverses(recursive=True), qml.transforms.merge_rotations, ) @pipeline @qml.qnode(qml.device("default.qubit")) def circuit(x, y): qml.CNOT([1, 0]) qml.X(0) qml.CNOT([1, 0]) qml.H(0) qml.H(0) qml.X(0) qml.RX(x, wires=0) qml.RX(y, wires=0) return qml.expval(qml.Z(1))
>>> print(qml.draw(circuit)(0.1, 0.2)) 0: ──RX(0.30)─┤ 1: ───────────┤ <Z>
Alternatively, the transform program can be constructed intuitively by combining multiple transforms. For example, the transforms can be added together with
+:>>> pipeline = qml.transforms.merge_rotations + qml.transforms.cancel_inverses(recursive=True) >>> pipeline CompilePipeline(merge_rotations, cancel_inverses)
Or multiplied by a scalar via
*: >>> pipeline += 2 * qml.transforms.commute_controlled >>> pipeline CompilePipeline(merge_rotations, cancel_inverses, commute_controlled, commute_controlled)A compilation pipeline can also be easily modified using operations similar to Python lists, including
insert,append,extendandpop:>>> pipeline.insert(0, qml.transforms.remove_barrier) >>> pipeline CompilePipeline(remove_barrier, merge_rotations, cancel_inverses, commute_controlled, commute_controlled)
Additionally, multiple compilation pipelines can be concatenated:
>>> another_pipeline = qml.transforms.decompose(gate_set={qml.RX, qml.RZ, qml.CNOT}) + qml.transforms.combine_global_phases >>> another_pipeline + pipeline CompilePipeline(decompose, combine_global_phases, remove_barrier, merge_rotations, cancel_inverses, commute_controlled, commute_controlled)
We can create a new pipeline that will do multiple passes of the original with multiplication:
>>> original = qml.transforms.merge_rotations + qml.transforms.cancel_inverses >>> 2 * original CompilePipeline(merge_rotations, cancel_inverses, merge_rotations, cancel_inverses)
Attributes
Trueif the compile pipeline has a terminal transform.Trueif the compile pipeline is informative.- has_final_transform¶
Trueif the compile pipeline has a terminal transform.
- is_informative¶
Trueif the compile pipeline is informative.- Returns:
Boolean
- Return type:
bool
Methods
add_transform(transform, *targs, **tkwargs)Add a transform to the end of the program.
append(transform)Add a transform to the end of the program.
extend(transforms)Extend the pipeline by appending transforms from an iterable.
Check if the compile pipeline has some classical cotransforms.
insert(index, transform)Insert a transform at a given index.
pop([index])Pop the transform container at a given index 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 to the end of the program.
Note that this should be a function decorated with/called by
qml.transform, and not aBoundTransform.- Parameters:
transform (Transform) – 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.
- append(transform)[source]¶
Add a transform to the end of the program.
- Parameters:
transform (Transform or BoundTransform) – A transform represented by its container.
- extend(transforms)[source]¶
Extend the pipeline by appending transforms from an iterable.
- Parameters:
transforms (CompilePipeline, or Sequence[BoundTransform | Transform]) – A CompilePipeline or an iterable of transforms to append.
- has_classical_cotransform()[source]¶
Check if the compile pipeline has some classical cotransforms.
- Returns:
Boolean
- Return type:
bool
- insert(index, transform)[source]¶
Insert a transform at a given index.
- Parameters:
index (int) – The index to insert the transform.
transform (transform or BoundTransform) – the transform to insert
- pop(index=-1)[source]¶
Pop the transform container at a given index of the program.
- Parameters:
index (int) – the index of the transform to remove.
- Returns:
The removed transform.
- Return type:
- remove(obj)[source]¶
In place remove the input containers, specifically, 1. if the input is a Transform, remove all containers matching the transform; 2. if the input is a BoundTransform, remove all containers exactly matching the input.
- Parameters:
obj (BoundTransform or Transform) – The object to remove from the program.