Exemplo n.º 1
0
def random_orthogonal(
        dim: int,
        *,
        random_state: value.RANDOM_STATE_LIKE = None) -> np.ndarray:
    """Returns a random orthogonal matrix distributed with Haar measure.

    Args:
        dim: The width and height of the matrix.
        random_state: A seed (int) or `np.random.RandomState` class to use when
            generating random values. If not set, defaults to using the module
            methods in `np.random`.

    Returns:
        The sampled orthogonal matrix.

    References:
        'How to generate random matrices from the classical compact groups'
        http://arxiv.org/abs/math-ph/0609050
    """
    random_state = value.parse_random_state(random_state)

    m = random_state.randn(dim, dim)
    q, r = np.linalg.qr(m)
    d = np.diag(r)
    return q * (d / abs(d))
Exemplo n.º 2
0
def random_superposition(dim: int,
                         *,
                         random_state: value.RANDOM_STATE_LIKE = None
                        ) -> np.ndarray:
    """Returns a random unit-length vector from the uniform distribution.

    Args:
        dim: The dimension of the vector.

    Returns:
        The sampled unit-length vector.
    """
    random_state = value.parse_random_state(random_state)

    state_vector = random_state.randn(dim).astype(complex)
    state_vector += 1j * random_state.randn(dim)
    state_vector /= np.linalg.norm(state_vector)
    return state_vector
Exemplo n.º 3
0
def random_superposition(
        dim: int,
        *,
        random_state: value.RANDOM_STATE_LIKE = None) -> np.ndarray:
    """Returns a random unit-length vector from the uniform distribution.

    Args:
        dim: The dimension of the vector.
        random_state: A seed (int) or `np.random.RandomState` class to use when
            generating random values. If not set, defaults to using the module
            methods in `np.random`.

    Returns:
        The sampled unit-length vector.
    """
    random_state = value.parse_random_state(random_state)

    state_vector = random_state.randn(dim).astype(complex)
    state_vector += 1j * random_state.randn(dim)
    state_vector /= np.linalg.norm(state_vector)
    return state_vector
Exemplo n.º 4
0
def random_unitary(dim: int, *,
                   random_state: value.RANDOM_STATE_LIKE = None) -> np.ndarray:
    """Returns a random unitary matrix distributed with Haar measure.

    Args:
        dim: The width and height of the matrix.
        random_state: A seed to use for random number generation.

    Returns:
        The sampled unitary matrix.

    References:
        'How to generate random matrices from the classical compact groups'
        http://arxiv.org/abs/math-ph/0609050
    """
    random_state = value.parse_random_state(random_state)

    z = (random_state.randn(dim, dim) + 1j * random_state.randn(dim, dim))
    q, r = np.linalg.qr(z)
    d = np.diag(r)
    return q * (d / abs(d))
Exemplo n.º 5
0
def random_density_matrix(
        dim: int,
        *,
        random_state: value.RANDOM_STATE_LIKE = None) -> np.ndarray:
    """Returns a random density matrix distributed with Hilbert-Schmidt measure.

    Args:
        dim: The width and height of the matrix.
        random_state: A seed to use for random number generation.

    Returns:
        The sampled density matrix.

    Reference:
        'Random Bures mixed states and the distribution of their purity'
        https://arxiv.org/abs/0909.5094
    """
    random_state = value.parse_random_state(random_state)

    mat = random_state.randn(dim, dim) + 1j * random_state.randn(dim, dim)
    mat = mat @ mat.T.conj()
    return mat / np.trace(mat)
Exemplo n.º 6
0
def random_orthogonal(dim: int, *, random_state: value.RANDOM_STATE_LIKE = None
                     ) -> np.ndarray:
    """Returns a random orthogonal matrix distributed with Haar measure.

    Args:
      dim: The width and height of the matrix.

    Returns:
      The sampled orthogonal matrix.

    References:
        'How to generate random matrices from the classical compact groups'
        http://arxiv.org/abs/math-ph/0609050
    """
    random_state = value.parse_random_state(random_state)

    m = random_state.randn(dim, dim)
    q, r = np.linalg.qr(m)
    d = np.diag(r)
    return q * (d / abs(d))