def superoperator_to_choi(superoperator: np.ndarray) -> np.ndarray:
    r"""Returns the Choi matrix of a quantum channel specified via the superoperator matrix.

    Quantum channel E: L(H1) -> L(H2) may be specified by its Choi matrix J(E) defined as

        $$
        J(E) = (E \otimes I)(|\phi\rangle\langle\phi|)
        $$

    where $|\phi\rangle = \sum_i|i\rangle|i\rangle$ is the unnormalized maximally entangled state
    and I: L(H1) -> L(H1) is the identity map. Choi matrix is unique for a given channel.
    Alternatively, E may be specified by its superoperator matrix K(E) defined so that

        $$
        K(E) vec(\rho) = vec(E(\rho))
        $$

    where the vectorization map $vec$ rearranges d-by-d matrices into d**2-dimensional vectors.
    Superoperator matrix is unique for a given channel. It is also called the natural
    representation of a quantum channel.

    A quantum channel can be viewed as a tensor with four indices. Different ways of grouping
    the indices into two pairs yield different matrix representations of the channel, including
    the superoperator and Choi representations. Hence, the conversion between the superoperator
    and Choi matrices is a permutation of matrix elements effected by reshaping the array and
    swapping its axes. Therefore, its cost is O(d**4) where d is the dimension of the input and
    output Hilbert space.

    Args:
        superoperator: Superoperator matrix specifying a quantum channel.

    Returns:
        Choi matrix of the channel specified by superoperator.

    Raises:
        ValueError: If superoperator has invalid shape.
    """
    d = int(np.round(np.sqrt(superoperator.shape[0])))
    if superoperator.shape != (d * d, d * d):
        raise ValueError(
            f"Invalid superoperator matrix shape, expected {(d * d, d * d)}, "
            f"got {superoperator.shape}"
        )

    s = np.reshape(superoperator, (d, d, d, d))
    c = np.swapaxes(s, 1, 2)
    return np.reshape(c, (d * d, d * d))
Returns the Choi matrix of a quantum channel specified via the superoperator matrix.

    Quantum channel E: L(H1) -> L(H2) may be specified by its Choi matrix J(E) defined as

        $$
        J(E) = (E \otimes I)(|\phi\rangle\langle\phi|)
        $$

    where $|\phi\rangle = \sum_i|i\rangle|i\rangle$ is the unnormalized maximally entangled state
    and I: L(H1) -> L(H1) is the identity map. Choi matrix is unique for a given channel.
    Alternatively, E may be specified by its superoperator matrix K(E) defined so that

        $$
        K(E) vec(\rho) = vec(E(\rho))
        $$

    where the vectorization map $vec$ rearranges d-by-d matrices into d**2-dimensional vectors.
    Superoperator matrix is unique for a given channel. It is also called the natural
    representation of a quantum channel.

    A quantum channel can be viewed as a tensor with four indices. Different ways of grouping
    the indices into two pairs yield different matrix representations of the channel, including
    the superoperator and Choi representations. Hence, the conversion between the superoperator
    and Choi matrices is a permutation of matrix elements effected by reshaping the array and
    swapping its axes. Therefore, its cost is O(d**4) where d is the dimension of the input and
    output Hilbert space.

    Args:
        superoperator: Superoperator matrix specifying a quantum channel.

    Returns:
        Choi matrix of the channel specified by superoperator.

    Raises:
        ValueError: If superoperator has invalid shape.
    
cirq-core/cirq/qis/channels.py