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 RZ gates.

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 used as alt_decomps or fixed_decomp within decompose().

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 SemiAdder decomposition

  • precision_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 RZ 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_rz_to_phase_gradient_decomp
import numpy as np

qp.decomposition.enable_graph()

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 a GlobalPhase.

>>> 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

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 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([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 passed via alt_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.