RandomUnitary

RandomUnitary is a function that generates a random unitary or orthogonal matrix, uniformly according to Haar measure.

Syntax

 * U = RandomUnitary(DIM)
 * U = RandomUnitary(DIM,RE)

Argument descriptions

 * DIM: The number of rows (or equivalently, columns) that U will have.
 * RE (optional, default 0): A flag (either 0 or 1) indicating that U should only have real entries (RE = 1) or that it is allowed to have complex entries (RE = 0</tt>). That is, if you set RE = 1</tt> then U</tt> will be an orthogonal matrix, not just a unitary matrix.

A random qubit gate
To generate a random quantum gate that acts on qubits, you could use the following code:

A random orthogonal matrix
To generate a random orthogonal (rather than unitary) matrix, set RE = 1</tt>:

To verify that this matrix is indeed orthogonal, we multiply it by its transpose:

Moments of the trace of an orthogonal matrix
It was shown in this MathOverflow thread that if O is a random (according to Haar measure) 3-by-3 orthogonal matrix, then the expectated value of ${\mathrm Tr}(O)^{2k}$ for $k = 1, 2, 3, 4, \ldots$ is $1, 3, 15, 91, \ldots$ (sequence A099251 in the OEIS). We can use the RandomUnitary</tt> function to reproduce these values approximately as follows: