# RandomUnitary

 Other toolboxes required RandomUnitary Generates a random unitary or orthogonal matrix none RandomDensityMatrixRandomPOVMRandomStateVectorRandomSuperoperator Random things

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 = 1). That is, if you set RE = 1 then U will be an orthogonal matrix, not just a unitary matrix.

## Examples

### A random qubit gate

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

>> RandomUnitary(2)

ans =

0.2280 + 0.6126i  -0.2894 - 0.6993i
-0.3147 + 0.6883i  -0.2501 + 0.6039i

### A random orthogonal matrix

To generate a random orthogonal (rather than unitary) matrix, set RE = 1:

>> U = RandomUnitary(3,1)

U =

0.9805   -0.1869   -0.0603
0.1678    0.6381    0.7515
-0.1020   -0.7470    0.6570

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

>> U'*U

ans =

1.0000    0.0000    0.0000
0.0000    1.0000    0.0000
0.0000    0.0000    1.0000

### 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 function to reproduce these values approximately as follows:

>> s = 10^5;
ct = zeros(1,4);
for j = 1:s
trO = trace(RandomUnitary(3,1))^2;
ct = ct + trO.^(1:4);
end
ct/s

ans =

0.9997    2.9917   14.9298   90.4464

## Notes

The random unitary matrix is generated by constructing a Ginibre ensemble of appropriate size, performing a QR decomposition on that ensemble, and then multiplying the columns of the unitary matrix Q by the sign of the corresponding diagonal entries of R.[1]

## Source code

Click on "expand" to the right to view the MATLAB source code for this function.

1. %%  RANDOMUNITARY    Generates a random unitary or orthogonal matrix
2. %   This function has one required argument:
3. %     DIM: the number of rows (and columns) of the unitary matrix
4. %
5. %   U = RandomUnitary(DIM) generates a random DIM-by-DIM unitary matrix,
6. %   uniformly distributed according to Haar measure.
7. %
8. %   This function has one optional argument:
9. %     RE (default 0)
10. %
11. %   U = RandomUnitary(DIM,RE) generates a random unitary matrix (if
12. %   RE=0) or a random real orthogonal matrix (if RE=1), uniformly
13. %   distributed according to the Haar measure.
14. %
15. %   URL: http://www.qetlab.com/RandomUnitary
16. 
17. %   requires: opt_args.m
18. %   author: Nathaniel Johnston (nathaniel@njohnston.ca)
19. %   package: QETLAB
20. %   last updated: September 30, 2014
21. 
22. function U = RandomUnitary(dim,varargin)
23. 
24. % set optional argument defaults: re=0
25. [re] = opt_args({ 0 },varargin{:});
26. 
27. % construct the Ginibre ensemble
28. gin = randn(dim);
29. if(~re)
30.     gin = gin + 1i*randn(dim);
31. end
32. 
33. % QR decomposition of the Ginibre ensemble
34. [Q,R] = qr(gin);
35. 
36. % compute U from the QR decomposition
37. R = sign(diag(R));
38. R(R==0) = 1; % protect against potentially zero diagonal entries
39. U = bsxfun(@times,Q,R.'); % much faster than the naive U = Q*diag(R)

## References

1. Māris Ozols. How to generate a random unitary matrix, 2009.