qml.add_decomps¶
- add_decomps(op_type, *decomps)[source]¶
Globally registers new decomposition rules with an operator class.
Note
This function is only relevant when the new experimental graph-based decomposition system (introduced in v0.41) is enabled via
enable_graph()
. This new way of doing decompositions is generally more resource efficient and accommodates multiple alternative decomposition rules for an operator. In this new system, custom decomposition rules are defined as quantum functions, and it is currently required that every decomposition rule declares its required resources usingregister_resources()
In the new system of decompositions, multiple decomposition rules can be registered for the same operator class. The specified decomposition rules in
add_decomps
serve as alternative decomposition rules that may be chosen if they lead to a more resource-efficient decomposition.- Parameters
op_type – the operator type for which new decomposition rules are specified.
decomps (DecompositionRule) – new decomposition rules to add to the given
op_type
. A decomposition is a quantum function registered with a resource estimate usingqml.register_resources
.
See also
Example
This example demonstrates adding two new decomposition rules to the
qml.Hadamard
operator.import pennylane as qml import numpy as np @qml.register_resources({qml.RZ: 2, qml.RX: 1, qml.GlobalPhase: 1}) def my_hadamard1(wires): qml.RZ(np.pi / 2, wires=wires) qml.RX(np.pi / 2, wires=wires) qml.RZ(np.pi / 2, wires=wires) qml.GlobalPhase(-np.pi / 2, wires=wires) @qml.register_resources({qml.RZ: 1, qml.RY: 1, qml.GlobalPhase: 1}) def my_hadamard2(wires): qml.RZ(np.pi, wires=wires) qml.RY(np.pi / 2, wires=wires) qml.GlobalPhase(-np.pi / 2) qml.add_decomps(qml.Hadamard, my_hadamard1, my_hadamard2)
These two new decomposition rules for
qml.Hadamard
will be subsequently stored within the scope of this program, and they will be taken into account for all circuit decompositions for the duration of the session. To add alternative decompositions for a particular circuit as opposed to globally, use thealt_decomps
argument of thedecompose()
transform.See also