qp.transforms.decompositions.phase_gradient_precision_stats

phase_gradient_precision_stats(angles, precision, op_type)[source]

Compute the precision_stats argument of the phase gradient decomposition rule factories from the rotation angles of the operators that a rule will decompose.

A phase gradient decomposition with precision angle wires represents an angle \(\phi\) by the precision leading binary decimals of \(\phi/u\) (rounded to nearest), where the unit is \(u=2\pi\) for RZ and \(u=4\pi\) for CRZ and SelectPauliRot. If the trailing decimals are zero, the rotation can be realized with fewer bits, i.e., with a smaller SemiAdder and less data loading. This function counts how many operators require each number of bits. Passing the result as precision_stats to make_rz_to_phase_gradient_decomp(), make_crz_to_phase_gradient_decomp() or make_selectpaulirot_to_phase_gradient_decomp() makes the produced rule apply each of these operators at exactly the number of bits it requires, and hints the conditional branches with the observed frequencies.

Parameters:
  • angles (Sequence[TensorLike]) – The angle(s) of each operator. For operators with multiple angles, such as SelectPauliRot, the bits required by the operator are the maximum over all of its angles. The angles must be concrete.

  • precision (int) – The full precision of the decomposition rule, i.e., the number of angle wires.

  • op_type (type | str) – The type of the operators, i.e., RZ, CRZ or SelectPauliRot, or its name. It determines the unit of the angles in the respective decomposition rule.

Returns:

The number of operators requiring each number of bits, sorted by the number of bits.

Return type:

dict[int, int]

Example

Consider four RZ rotations that we want to decompose with four angle wires. The first angle, \(\tfrac{1}{2}\cdot 2\pi\), has the binary representation \((0.1000)_2\) and thus only requires one bit, whereas \(0.1 \cdot 2\pi\) is rounded to \((0.0010)_2\), requiring three bits:

>>> from pennylane.transforms.decompositions import phase_gradient_precision_stats
>>> angles = np.array([1 / 2, 1 / 4, 3 / 8, 0.1]) * 2 * np.pi
>>> phase_gradient_precision_stats(angles, precision=4, op_type=qp.RZ)
{1: 1, 2: 1, 3: 2}

See make_rz_to_phase_gradient_decomp() for an example that uses these statistics in a decomposition rule.

For SelectPauliRot, we pass the angles of each operator. The operator type may also be specified by its name:

>>> ops = [
...     qp.SelectPauliRot(np.array([1 / 2, 1 / 4]) * 4 * np.pi, control_wires=[0], target_wire=1),
...     qp.SelectPauliRot(np.array([1 / 2, 0]) * 4 * np.pi, control_wires=[0], target_wire=1),
... ]
>>> angles = [op.data[0] for op in ops]
>>> phase_gradient_precision_stats(angles, precision=4, op_type="SelectPauliRot")
{1: 1, 2: 1}

Note

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