Skip to content

haar_random_unitary

qoptcraft.operators.haar_random_unitary

Random haar uniform unitary.

haar_random_unitary(dim, seed=None)

Create a random unitary matrix distributed with Haar measure.

Parameters:

Name Type Description Default
dim int

the dimension of the unitary matrix.

required

Returns:

Name Type Description
NDArray NDArray

the haar uniform random unitary.

References

The algorithm can be found in Francesco Mezzadri, "How to generate random matrices from the classical compact groups" arXiv, 2007. https://arxiv.org/abs/math-ph/0609050

Source code in qoptcraft/operators/haar_random_unitary.py
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
def haar_random_unitary(dim: int, seed: int = None) -> NDArray:
    """Create a random unitary matrix distributed with Haar measure.

    Args:
        dim (int): the dimension of the unitary matrix.

    Returns:
        NDArray: the haar uniform random unitary.

    References:
        The algorithm can be found in
        Francesco Mezzadri, "How to generate random matrices from the classical
        compact groups" arXiv, 2007. https://arxiv.org/abs/math-ph/0609050
    """
    rng = np.random.default_rng() if seed is None else np.random.default_rng(seed)
    Z = rng.normal(0, 1, dim * dim).reshape(dim, dim)
    Z = (Z + 1j * rng.normal(0, 1, dim * dim).reshape(dim, dim)) / np.sqrt(2.0)
    Q, R = sp.linalg.qr(Z)  # QR decomposition
    D = np.diag(R)  # diag() outputs a 1-D array
    Λ = np.diag(D / np.absolute(D))  # diag() outputs a 2-D array again
    return Q @ Λ @ Q