qp.transforms.decompositions.make_crz_to_phase_gradient_decomp¶
- make_crz_to_phase_gradient_decomp(angle_wires, phase_grad_wires, work_wires, precision_stats=None)[source]¶
Create a custom decomposition rule for
CRZgates.Here, the necessary auxiliary wires for the phase gradient decomposition of CRZ 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 used as
alt_decompsorfixed_decompwithindecompose().- 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. Should have the same length as
angle_wires.work_wires (Wires) – additional work wires for
SemiAdderdecomposition. At leastlen(angle_wires)-1work wires are required.precision_stats (dict[int, int] | None) – Optional statistics of the number of bits required by the rotation angles. Defaults to
None.
- Returns:
decomposition rule to be used within
decompose().- Return type:
func
Example
In this example we decompose a circuit containing only a single
CRZgate 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_crz_to_phase_gradient_decomp import numpy as np qp.decomposition.enable_graph() prec = 3 phi = (1/2 + 1/4 + 1/8) * 4 * 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_crz_to_phase_gradient_decomp( angle_wires, phase_grad_wires, work_wires ) @qp.transforms._tape_decompose( gate_set={"CNOT", "SemiAdder", "PauliX"}, fixed_decomps={qp.CRZ: custom_decomp} ) @qp.qnode(qp.device("null.qubit")) def circuit(): qp.CRZ(phi, [0, 1]) return qp.state()
The resulting circuit corresponds to the phase gradient decomposition of
CRZ. The compute and uncompute fanouts load the binary representation of the angle (111 in this case) onto theangle_wireswith aMultiXcontrolled by the control wire (rendering asCNOT, since the control is always on |1>), and flip all of thephase_grad_wireswith a singleMultiXcontrolled by the target wire on |0> (rendering as aPauliXflip of that control wire,CNOT\ s to thephase_grad_wires, and aPauliXflip back), and enclose theSemiAdder.>>> wire_order = [0, 1] + angle_wires + phase_grad_wires + work_wires >>> print(qp.draw(circuit, wire_order=wire_order)()) 0: ─╭●─╭●─╭●────────────────────────╭●─╭●─╭●────────────────┤ ╭State 1: ─│──│──│───X─╭●─╭●─╭●──X─────────│──│──│───X─╭●─╭●─╭●──X─┤ ├State aux_0: ─╰X─│──│─────│──│──│──╭SemiAdder─╰X─│──│─────│──│──│─────┤ ├State aux_1: ────╰X─│─────│──│──│──├SemiAdder────╰X─│─────│──│──│─────┤ ├State aux_2: ───────╰X────│──│──│──├SemiAdder───────╰X────│──│──│─────┤ ├State phg_0: ─────────────╰X─│──│──├SemiAdder─────────────╰X─│──│─────┤ ├State phg_1: ────────────────╰X─│──├SemiAdder────────────────╰X─│─────┤ ├State phg_2: ───────────────────╰X─├SemiAdder───────────────────╰X────┤ ├State work_0: ──────────────────────├SemiAdder─────────────────────────┤ ├State work_1: ──────────────────────╰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 smaller fanouts 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=4 * np.pi), i.e., the precision without trailing zeros. If provided, the fanouts 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/4, 1/2 + 1/8]) * 4 * np.pi # (010) and (101) precision_stats = phase_gradient_precision_stats(angles, prec, qp.CRZ) truncating_decomp = make_crz_to_phase_gradient_decomp( angle_wires, phase_grad_wires, work_wires, precision_stats=precision_stats ) @qp.transforms._tape_decompose( gate_set={"CNOT", "SemiAdder", "PauliX"}, fixed_decomps={qp.CRZ: truncating_decomp} ) @qp.qnode(qp.device("null.qubit")) def truncated_circuit(phi): qp.CRZ(phi, [0, 1]) return qp.state()
The rotation by the first angle only uses two angle wires and phase gradient wires:
>>> precision_stats {2: 1, 3: 1} >>> print(qp.draw(truncated_circuit, wire_order=wire_order)(angles[0])) 0: ─╭●─────────────────────╭●─────────────┤ ╭State 1: ─│───X─╭●─╭●──X─────────│───X─╭●─╭●──X─┤ ├State aux_0: ─│─────│──│──╭SemiAdder─│─────│──│─────┤ ├State aux_1: ─╰X────│──│──├SemiAdder─╰X────│──│─────┤ ├State phg_0: ───────╰X─│──├SemiAdder───────╰X─│─────┤ ├State phg_1: ──────────╰X─├SemiAdder──────────╰X────┤ ├State work_0: ─────────────├SemiAdder────────────────┤ ├State work_1: ─────────────╰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 passed via
alt_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.