qp.transforms.decompositions.make_rz_to_phase_gradient_decomp¶
- make_rz_to_phase_gradient_decomp(angle_wires, phase_grad_wires, work_wires, precision_stats=None)[source]¶
Create a custom decomposition rule for
RZgates.Here, the necessary auxiliary wires for the phase gradient decomposition of RZ are taken as inputs and bound into the created decomposition rule. This way, this function can be used in a workflow context that explicitly uses those wires to generate this decomposition rule, which can then be registered with
add_decomps().- Parameters:
angle_wires (Wires) – wires that encode the binary representation of the rotation angle
phase_grad_wires (Wires) – wires that carry a phase gradient state
work_wires (Wires) – additional work wires for
SemiAdderdecompositionprecision_stats (dict[int, int] | None) – Optional statistics of the number of bits required by the rotation angles, mapping a number of bits to the number of rotations requiring that many bits. Defaults to
None.
- Returns:
decomposition rule to be used within
decompose().- Return type:
qp.decomposition.DecompositionRule
Example
In this example we decompose a circuit containing only a single
RZgate using the custom decomposition rule that we generate from within the context of the example, where all auxiliary wires exist.import pennylane as qp from pennylane.transforms.decompositions import make_rz_to_phase_gradient_decomp import numpy as np prec = 3 phi = (1/2 + 1/4 + 1/8) * 2 * np.pi # binary rep is (111) angle_wires = qp.wires.Wires([f"aux_{i}" for i in range(prec)]) phase_grad_wires = qp.wires.Wires([f"phg_{i}" for i in range(prec)]) work_wires = qp.wires.Wires([f"work_{i}" for i in range(prec - 1)]) custom_decomp = make_rz_to_phase_gradient_decomp( angle_wires, phase_grad_wires, work_wires ) gate_set = {"CNOT", "SemiAdder", "GlobalPhase", "PauliX"} @qp.transforms._tape_decompose(gate_set=gate_set, fixed_decomps={qp.RZ: custom_decomp}) @qp.qnode(qp.device("null.qubit")) def circuit(): qp.RZ(phi, 0) return qp.state()
The resulting circuit corresponds to the phase gradient decomposition of RZ, containing two CNOT fanouts corresponding to the binary representation of the angle (111 in this case), the
SemiAdder, and aGlobalPhase.>>> print(qp.draw(circuit)()) 0: ─╭GlobalPhase(2.75)─╭●─╭●─╭●────────────╭●─╭●─╭●─┤ ╭State aux_0: ─├GlobalPhase(2.75)─╰X─│──│──╭SemiAdder─╰X─│──│──┤ ├State aux_1: ─├GlobalPhase(2.75)────╰X─│──├SemiAdder────╰X─│──┤ ├State aux_2: ─├GlobalPhase(2.75)───────╰X─├SemiAdder───────╰X─┤ ├State phg_0: ─├GlobalPhase(2.75)──────────├SemiAdder──────────┤ ├State phg_1: ─├GlobalPhase(2.75)──────────├SemiAdder──────────┤ ├State phg_2: ─├GlobalPhase(2.75)──────────├SemiAdder──────────┤ ├State work_0: ─├GlobalPhase(2.75)──────────├SemiAdder──────────┤ ├State work_1: ─╰GlobalPhase(2.75)──────────╰SemiAdder──────────┤ ╰State
Usage details
Adaptive precision via ``precision_stats``
For angles that do not require the full precision determined by the angle encoding and phase gradient wires, we may use a smaller encoding and adder, reducing the cost of the phase gradient rotation. However, as rotation angles are not known statically in general, this adaptive precision is only supported if additional information about the distribution of rotation angles is provided via
precision_stats, a dictionary mapping a number of bits to the number of rotations requiring that many bits.The bits required by an angle \(\phi\) are the position of the last set bit in
qp.math.binary_decimals(phi, len(angle_wires), unit=2 * np.pi), i.e., the precision without trailing zeros. If provided, the data loading and theSemiAdderare truncated to the smallest number of bits inprecision_statsthat suffices for a given angle, usingcond()branches that are hinted with the corresponding probabilities viahint(). If no number of bits inprecision_statssuffices, the full precision is used. Keys must lie between0andlen(angle_wires). Zero counts are ignored, and a dictionary without non-zero counts is treated likeNone. Usephase_gradient_precision_stats()to compute the statistics from the angles.Example
from pennylane.transforms.decompositions import phase_gradient_precision_stats angles = np.array([1/2, 1/4 + 1/8, 1/2 + 1/4 + 1/8]) * 2 * np.pi # (100), (011), (111) precision_stats = phase_gradient_precision_stats(angles, prec, qp.RZ) truncating_decomp = make_rz_to_phase_gradient_decomp( angle_wires, phase_grad_wires, work_wires, precision_stats=precision_stats ) @qp.transforms._tape_decompose( gate_set=gate_set, fixed_decomps={qp.RZ: truncating_decomp} ) @qp.qnode(qp.device("null.qubit")) def truncated_circuit(phi): qp.RZ(phi, 0) return qp.state()
One rotation requires a single bit and two rotations require all three bits. The branches of the rule are hinted with these frequencies, and the rotation by the first angle only uses a single angle wire and phase gradient wire:
>>> precision_stats {1: 1, 3: 2} >>> wire_order = [0] + angle_wires + phase_grad_wires + work_wires >>> print(qp.draw(truncated_circuit, wire_order=wire_order)(angles[0])) 0: ─╭GlobalPhase(1.57)─╭●────────────╭●─┤ ╭State aux_0: ─├GlobalPhase(1.57)─╰X─╭SemiAdder─╰X─┤ ├State phg_0: ─├GlobalPhase(1.57)────├SemiAdder────┤ ├State work_0: ─├GlobalPhase(1.57)────├SemiAdder────┤ ├State work_1: ─╰GlobalPhase(1.57)────╰SemiAdder────┤ ╰State
>>> print(qp.draw(truncated_circuit, wire_order=wire_order)(angles[1])) 0: ─╭GlobalPhase(1.18)─╭●─╭●────────────╭●─╭●─┤ ╭State aux_0: ─├GlobalPhase(1.18)─│──│──╭SemiAdder─│──│──┤ ├State aux_1: ─├GlobalPhase(1.18)─╰X─│──├SemiAdder─╰X─│──┤ ├State aux_2: ─├GlobalPhase(1.18)────╰X─├SemiAdder────╰X─┤ ├State phg_0: ─├GlobalPhase(1.18)───────├SemiAdder───────┤ ├State phg_1: ─├GlobalPhase(1.18)───────├SemiAdder───────┤ ├State phg_2: ─├GlobalPhase(1.18)───────├SemiAdder───────┤ ├State work_0: ─├GlobalPhase(1.18)───────├SemiAdder───────┤ ├State work_1: ─╰GlobalPhase(1.18)───────╰SemiAdder───────┤ ╰State
Finally, keep the following in mind when using
precision_stats:The resource function of the rule adds up the cost of all supported precisions, so that the graph-based decomposition system accounts for the operators in all conditional branches. This over-reports the cost of the rule, so that the decomposition graph may prefer another rule over this one when it is registered with
add_decomps(). Usefixed_decompsto enforce this rule.Angles that require more bits than the largest key in
precision_statsfall back to the full precision. Unlesslen(angle_wires)is a key inprecision_stats, this fallback branch is hinted with probability0, so that resource estimates based on the hints do not account for these angles.