qp.transforms.decompositions.phase_gradient_precision_stats¶
- phase_gradient_precision_stats(angles, precision, op_type)[source]¶
Compute the
precision_statsargument of the phase gradient decomposition rule factories from the rotation angles of the operators that a rule will decompose.A phase gradient decomposition with
precisionangle wires represents an angle \(\phi\) by theprecisionleading binary decimals of \(\phi/u\) (rounded to nearest), where the unit is \(u=2\pi\) forRZand \(u=4\pi\) forCRZandSelectPauliRot. If the trailing decimals are zero, the rotation can be realized with fewer bits, i.e., with a smallerSemiAdderand less data loading. This function counts how many operators require each number of bits. Passing the result asprecision_statstomake_rz_to_phase_gradient_decomp(),make_crz_to_phase_gradient_decomp()ormake_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,CRZorSelectPauliRot, 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]
See also
make_rz_to_phase_gradient_decomp(),make_crz_to_phase_gradient_decomp(),make_selectpaulirot_to_phase_gradient_decomp()Example
Consider four
RZrotations 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
SelectPauliRotwithout control wires is decomposed into a plainRX,RYorRZrotation bymake_selectpaulirot_to_phase_gradient_decomp(). If the resultingRZrotations are decomposed by a rule frommake_rz_to_phase_gradient_decomp(), include their angles in the statistics forop_type=qp.RZ, which uses the unit \(2\pi\) instead of \(4\pi\).