Skip to content

angle

Angle encoding as a product of single-qubit Y rotations.

AngleEncoding

Bases: StateEncoder

Prepare \(\bigotimes_i R_y(\theta_i)\lvert 0\rangle\) from one angle per qubit.

Each angle is a rotation in radians. On a single qubit,

\[ R_y(\theta)\lvert 0\rangle = \cos(\theta/2)\lvert 0\rangle + \sin(\theta/2)\lvert 1\rangle. \]

Angles are not amplitudes: they are not normalized. The first angle is the first factor of tensor, so it is the most significant qubit. The number of qubits is the length of data.

Examples:

A \(\pi/2\) rotation prepares the equal superposition:

import numpy as np
from shadowsim.encoding import AngleEncoding

state = AngleEncoding().encode([np.pi / 2])
print(np.round(state.to_numpy(), decimals=8))
[0.70710678+0.j 0.70710678+0.j]
Source code in shadowsim/encoding/angle.py
class AngleEncoding(StateEncoder):
    r"""Prepare \(\bigotimes_i R_y(\theta_i)\lvert 0\rangle\) from one angle per qubit.

    Each angle is a rotation in radians. On a single qubit,

    \[
        R_y(\theta)\lvert 0\rangle = \cos(\theta/2)\lvert 0\rangle + \sin(\theta/2)\lvert 1\rangle.
    \]

    Angles are not amplitudes: they are not normalized. The first angle is the
    first factor of ``tensor``, so it is the most
    significant qubit. The number of qubits is the length of ``data``.

    Examples:
        A \(\pi/2\) rotation prepares the equal superposition:

        ```python exec="1" source="above" result="text"
        import numpy as np
        from shadowsim.encoding import AngleEncoding

        state = AngleEncoding().encode([np.pi / 2])
        print(np.round(state.to_numpy(), decimals=8))
        ```

    """

    def encode(self, data: npt.ArrayLike) -> State:
        """Return the product state for one Y rotation per qubit.

        Args:
            data: One finite real angle, in radians, per qubit.

        Returns:
            Normalized product state on ``len(data)`` qubits.

        Raises:
            ValueError: If ``data`` is not a non-empty 1D array of finite real numbers.
        """
        angles = _real_angles(data)
        kets = [np.array([np.cos(0.5 * theta), np.sin(0.5 * theta)], dtype=np.complex128) for theta in angles]
        vector = tensor(kets)
        vector = vector / np.linalg.norm(vector)
        return State(vector, int(angles.shape[0]))

encode

encode(data: ArrayLike) -> State

Return the product state for one Y rotation per qubit.

Parameters:

  • data (ArrayLike) –

    One finite real angle, in radians, per qubit.

Returns:

  • State –

    Normalized product state on len(data) qubits.

Raises:

  • ValueError –

    If data is not a non-empty 1D array of finite real numbers.

Source code in shadowsim/encoding/angle.py
def encode(self, data: npt.ArrayLike) -> State:
    """Return the product state for one Y rotation per qubit.

    Args:
        data: One finite real angle, in radians, per qubit.

    Returns:
        Normalized product state on ``len(data)`` qubits.

    Raises:
        ValueError: If ``data`` is not a non-empty 1D array of finite real numbers.
    """
    angles = _real_angles(data)
    kets = [np.array([np.cos(0.5 * theta), np.sin(0.5 * theta)], dtype=np.complex128) for theta in angles]
    vector = tensor(kets)
    vector = vector / np.linalg.norm(vector)
    return State(vector, int(angles.shape[0]))