qp.transforms.decompositions.make_selectpaulirot_to_phase_gradient_decomp

make_selectpaulirot_to_phase_gradient_decomp(angle_wires, phase_grad_wires, work_wires, precision_stats=None)[source]

Create a custom decomposition rule for SelectPauliRot gates.

Here, the necessary auxiliary wires for the phase gradient decomposition of SelectPauliRot 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. Should have the same length as angle_wires.

  • work_wires (Wires) – additional work wires for SemiAdder and QROM decomposition. For the former, at least len(angle_wires)-1 work wires are required. For the latter, at least len(control_wires)-1 work wires are required for an efficient decomposition, where control_wires are the control wires of the (largest) SelectPauliRot to be decomposed with the produced decomposition rule. Overall, we thus require max(len(angle_wires), len(control_wires))-1 work wires.

  • precision_stats (dict[int, int] | None) – Optional statistics of the number of bits required by the SelectPauliRot operators, mapping a number of bits to the number of operators requiring that many bits. 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 SelectPauliRot gate 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_selectpaulirot_to_phase_gradient_decomp
import numpy as np

prec = 3
np.random.seed(35)
angles = np.random.rand(2**3)

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_selectpaulirot_to_phase_gradient_decomp(
    angle_wires, phase_grad_wires, work_wires
)

@qp.transforms._tape_decompose(
    gate_set={"QROM", "Adjoint(QROM)", "SemiAdder", "MultiControlledX", "GlobalPhase"},
    fixed_decomps={qp.SelectPauliRot: custom_decomp}
)
@qp.qnode(qp.device("null.qubit"))
def circuit(angles):
    qp.SelectPauliRot(angles, control_wires=range(3), target_wire=3)
    return qp.state()

The resulting circuit corresponds to the phase gradient decomposition of SelectPauliRot, containing two static CNOT fanouts that toggle between addition and subtraction based on the target wire state, two QROMs that load the binary representation of the rotation angles, and the SemiAdder:

>>> wire_order = [0, 1, 2, 3] + angle_wires + phase_grad_wires + work_wires
>>> print(qp.draw(circuit, wire_order=wire_order, show_matrices=False)(angles))
     0: ─╭◑────────────────────────────╭◑─────────────────┤ ╭State
     1: ─├◑────────────────────────────├◑─────────────────┤ ├State
     2: ─├◑────────────────────────────├◑─────────────────┤ ├State
     3: ─│─────────╭○─╭○─╭○────────────│─────────╭○─╭○─╭○─┤ ├State
 aux_0: ─├QROM(M0)─│──│──│──╭SemiAdder─├QROM(M0)─│──│──│──┤ ├State
 aux_1: ─├QROM(M0)─│──│──│──├SemiAdder─├QROM(M0)─│──│──│──┤ ├State
 aux_2: ─├QROM(M0)─│──│──│──├SemiAdder─├QROM(M0)─│──│──│──┤ ├State
 phg_0: ─│─────────╰X─│──│──├SemiAdder─│─────────╰X─│──│──┤ ├State
 phg_1: ─│────────────╰X─│──├SemiAdder─│────────────╰X─│──┤ ├State
 phg_2: ─│───────────────╰X─├SemiAdder─│───────────────╰X─┤ ├State
work_0: ─├work──────────────├SemiAdder─├work──────────────┤ ├State
work_1: ─╰work──────────────╰SemiAdder─╰work──────────────┤ ╰State

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 operator are the maximum over all of its angles, where the bits required by an angle \(\phi\) are computed from qp.math.binary_decimals(phi, len(angle_wires), unit=4 * np.pi). If provided, the data loading and the SemiAdder are truncated to the smallest number of bits in precision_stats that suffices for a given angle, using cond() branches that are hinted with the corresponding probabilities via hint(). If no number of bits in precision_stats suffices, the full precision is used. Keys must lie between 0 and len(angle_wires). Zero counts are ignored, and a dictionary without non-zero counts is treated like None. Use phase_gradient_precision_stats() to compute the statistics from the angles.

Example

from pennylane.transforms.decompositions import phase_gradient_precision_stats

angles = [
    np.array([0, 1/2, 1/4, 0, 0, 1/2, 0, 1/4]) * 4 * np.pi, # at most 2 bits
    np.array([1/8, 0, 0, 0, 1/2, 0, 0, 0]) * 4 * np.pi, # at most 3 bits
]
precision_stats = phase_gradient_precision_stats(angles, prec, qp.SelectPauliRot)

truncating_decomp = make_selectpaulirot_to_phase_gradient_decomp(
    angle_wires, phase_grad_wires, work_wires, precision_stats=precision_stats
)

@qp.transforms._tape_decompose(
    gate_set={"QROM", "Adjoint(QROM)", "SemiAdder", "MultiControlledX", "GlobalPhase"},
    fixed_decomps={qp.SelectPauliRot: truncating_decomp}
)
@qp.qnode(qp.device("null.qubit"))
def truncated_circuit(angles):
    qp.SelectPauliRot(angles, control_wires=range(3), target_wire=3)
    return qp.state()

The first operator only loads two bits per angle and uses two phase gradient wires:

>>> precision_stats
{2: 1, 3: 1}
>>> print(qp.draw(truncated_circuit, wire_order=wire_order, show_matrices=False)(angles[0]))
     0: ─╭◑─────────────────────────╭◑──────────────┤ ╭State
     1: ─├◑─────────────────────────├◑──────────────┤ ├State
     2: ─├◑─────────────────────────├◑──────────────┤ ├State
     3: ─│─────────╭○─╭○────────────│─────────╭○─╭○─┤ ├State
 aux_0: ─├QROM(M0)─│──│──╭SemiAdder─├QROM(M0)─│──│──┤ ├State
 aux_1: ─├QROM(M0)─│──│──├SemiAdder─├QROM(M0)─│──│──┤ ├State
 phg_0: ─│─────────╰X─│──├SemiAdder─│─────────╰X─│──┤ ├State
 phg_1: ─│────────────╰X─├SemiAdder─│────────────╰X─┤ ├State
work_0: ─├work───────────├SemiAdder─├work───────────┤ ├State
work_1: ─╰work───────────╰SemiAdder─╰work───────────┤ ╰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(). Use fixed_decomps to enforce this rule.

  • Angles that require more bits than the largest key in precision_stats fall back to the full precision. Unless len(angle_wires) is a key in precision_stats, this fallback branch is hinted with probability 0, so that resource estimates based on the hints do not account for these angles.

  • A SelectPauliRot without control wires is decomposed into a plain RX, RY or RZ rotation. If the resulting RZ rotations are decomposed by a rule from make_rz_to_phase_gradient_decomp() with precision_stats, include their angles in the statistics for op_type=qp.RZ, which uses the unit \(2\pi\) instead of \(4\pi\).