qp.add_decomps¶
- add_decomps(op_type, *decomps)[source]¶
Globally registers new decomposition rules with an operator class.
Multiple decomposition rules can be registered for the same operator class. The specified decomposition rules in
add_decompsserve as alternative decomposition rules that may be chosen if they lead to a more resource-efficient decomposition.- Parameters:
op_type (type or str) – the operator type for which new decomposition rules are specified. For symbolic operators, use strings such as
"Adjoint(RY)","Pow(H)","C(RX)", etc.decomps (DecompositionRule) – new decomposition rules to add to the given
op_type. A decomposition is a quantum function registered with a resource estimate usingqp.register_resources.
See also
Example
This example demonstrates adding two new decomposition rules to the
qp.Hadamardoperator.import pennylane as qp import numpy as np @qp.register_resources({qp.RZ: 2, qp.RX: 1, qp.GlobalPhase: 1}, name="my_hadamard1") def my_hadamard1(wires): qp.RZ(np.pi / 2, wires=wires) qp.RX(np.pi / 2, wires=wires) qp.RZ(np.pi / 2, wires=wires) qp.GlobalPhase(-np.pi / 2) @qp.register_resources({qp.RZ: 1, qp.RY: 1, qp.GlobalPhase: 1}, name="my_hadamard2") def my_hadamard2(wires): qp.RZ(np.pi, wires=wires) qp.RY(np.pi / 2, wires=wires) qp.GlobalPhase(-np.pi / 2) qp.add_decomps(qp.Hadamard, my_hadamard1, my_hadamard2)
These two new decomposition rules for
qp.Hadamardwill 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 keep alternative decompositions local to a workflow, register the rules and compile the circuit within alocal_decomps()context. Usefixed_decompsorexclude_decompswithdecompose()to select or exclude rules for an individual compilation pass.Custom decomposition rules can also be specified for symbolic operators. In this case, the operator type can be specified as a string. For example,
@register_resources({qp.RY: 1}, name="adjoint_ry") def adjoint_ry(phi, wires, **_): qp.RY(-phi, wires=wires) qp.add_decomps("Adjoint(RY)", adjoint_ry)
See also