Difference between revisions of "RandomUnitary"

From QETLAB
Jump to navigation Jump to search
m
 
(8 intermediate revisions by one other user not shown)
Line 1: Line 1:
 
{{Function
 
{{Function
 
|name=RandomUnitary
 
|name=RandomUnitary
|desc=Generates a random [[unitary]] or [[orthogonal matrix]]
+
|desc=Generates a random unitary or orthogonal matrix
|req=[[opt_args]]
+
|rel=[[RandomDensityMatrix]]<br />[[RandomPOVM]]<br />[[RandomStateVector]]<br />[[RandomSuperoperator]]
|rel=[[RandomDensityMatrix]]<br />[[RandomStateVector]]<br />[[RandomSuperoperator]]
+
|cat=[[List of functions#Random_things|Random things]]
|upd=November 22, 2012
+
|upd=September 30, 2014
|v=1.00}}
+
|v=0.50}}
<tt>'''RandomUnitary'''</tt> is a [[List of functions|function]] that generates a random [[unitary]] or [[orthogonal matrix]], uniformly according to [[Haar measure]].  
+
<tt>'''RandomUnitary'''</tt> is a [[List of functions|function]] that generates a random unitary or orthogonal matrix, uniformly according to [http://en.wikipedia.org/wiki/Haar_measure Haar measure].  
  
 
==Syntax==
 
==Syntax==
 
* <tt>U = RandomUnitary(DIM)</tt>
 
* <tt>U = RandomUnitary(DIM)</tt>
 
* <tt>U = RandomUnitary(DIM,RE)</tt>
 
* <tt>U = RandomUnitary(DIM,RE)</tt>
* <tt>U = RandomUnitary(DIM,RE,DENS)</tt>
 
  
 
==Argument descriptions==
 
==Argument descriptions==
 
* <tt>DIM</tt>: The number of rows (or equivalently, columns) that <tt>U</tt> will have.
 
* <tt>DIM</tt>: The number of rows (or equivalently, columns) that <tt>U</tt> will have.
* <tt>RE</tt> (optional, default 0): A flag (either 0 or 1) indicating that <tt>U</tt> should only have real entries (<tt>RE = 1</tt>) or that it is allowed to have complex entries (<tt>RE = 1</tt>). That is, if you set <tt>RE = 1</tt> then <tt>U</tt> will be an orthogonal matrix, not just a unitary matrix.
+
* <tt>RE</tt> (optional, default 0): A flag (either 0 or 1) indicating that <tt>U</tt> should only have real entries (<tt>RE = 1</tt>) or that it is allowed to have complex entries (<tt>RE = 0</tt>). That is, if you set <tt>RE = 1</tt> then <tt>U</tt> will be an orthogonal matrix, not just a unitary matrix.
* <tt>DENS</tt> (optional, default 1): If equal to 1 then <tt>U</tt> will be a full matrix. If <tt>0 <= DEN < 1</tt> then <tt>U</tt> will be sparse with approximately <tt>DENS*DIM^2</tt> non-zero entries. Note that the number of non-zero entries in this case is *very* approximate. Furthermore, if <tt>DENS < 1</tt> then the distribution of <tt>U</tt> is no longer uniform in any mathematically well-defined sense.
 
  
 
==Examples==
 
==Examples==
===A random qubit [[gate]]===
+
===A random qubit gate===
 
To generate a random quantum gate that acts on qubits, you could use the following code:
 
To generate a random quantum gate that acts on qubits, you could use the following code:
<pre>
+
<syntaxhighlight>
 
>> RandomUnitary(2)
 
>> RandomUnitary(2)
  
Line 28: Line 26:
 
   0.2280 + 0.6126i  -0.2894 - 0.6993i
 
   0.2280 + 0.6126i  -0.2894 - 0.6993i
 
   -0.3147 + 0.6883i  -0.2501 + 0.6039i
 
   -0.3147 + 0.6883i  -0.2501 + 0.6039i
</pre>
+
</syntaxhighlight>
  
 
===A random orthogonal matrix===
 
===A random orthogonal matrix===
 
To generate a random orthogonal (rather than unitary) matrix, set <tt>RE = 1</tt>:
 
To generate a random orthogonal (rather than unitary) matrix, set <tt>RE = 1</tt>:
<pre>
+
<syntaxhighlight>
 
>> U = RandomUnitary(3,1)
 
>> U = RandomUnitary(3,1)
  
Line 40: Line 38:
 
     0.1678    0.6381    0.7515
 
     0.1678    0.6381    0.7515
 
   -0.1020  -0.7470    0.6570
 
   -0.1020  -0.7470    0.6570
</pre>
+
</syntaxhighlight>
  
 
To verify that this matrix is indeed orthogonal, we multiply it by its transpose:
 
To verify that this matrix is indeed orthogonal, we multiply it by its transpose:
<pre>
+
<syntaxhighlight>
 
>> U'*U
 
>> U'*U
  
Line 51: Line 49:
 
     0.0000    1.0000    0.0000
 
     0.0000    1.0000    0.0000
 
     0.0000    0.0000    1.0000
 
     0.0000    0.0000    1.0000
</pre>
+
</syntaxhighlight>
  
===A random sparse unitary matrix===
+
===Moments of the trace of an orthogonal matrix===
To generate an 8-by-8 sparse unitary matrix with approximately 1/4 of its entries non-zero, we can use the following line of code:
+
It was shown in [http://mathoverflow.net/questions/180110/moments-of-the-trace-of-orthogonal-matrices 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 [https://oeis.org/A099251 A099251] in the OEIS). We can use the <tt>RandomUnitary</tt> function to reproduce these values approximately as follows:
<pre>
+
<syntaxhighlight>
>> RandomUnitary(8,0,1/4)
+
>> 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 =
 
ans =
  
  (2,1)          0 + 0.7140i
+
     0.9997   2.9917  14.9298  90.4464
  (7,1)     -0.0000 + 0.7002i
+
</syntaxhighlight>
  (2,2)      0.5448 + 0.0000i
 
  (5,2)      0.6281         
 
  (7,2)    -0.5555 - 0.0000i
 
   (2,3)      0.4398 - 0.0000i
 
  (5,3)    -0.7781 + 0.0000i
 
  (7,3)    -0.4485 + 0.0000i
 
  (8,4)          0 - 1.0000i
 
  (1,5)      1.0000         
 
  (3,6)      1.0000         
 
  (4,7)      1.0000         
 
  (6,8)      1.0000         
 
</pre>
 
  
Please remember that constructing random sparse unitary matrices in this way is ''very'' approximate &ndash; it is not uniformly distributed in any meaningful sense and the number of non-zero elements will sometimes not be very close to <tt>DENS*DIM^2</tt>.
+
==Notes==
 +
The random unitary matrix is generated by constructing a Ginibre ensemble of appropriate size, performing a [http://en.wikipedia.org/wiki/QR_decomposition 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.<ref>Māris Ozols. ''[http://home.lu.lv/~sd20008/papers/essays/Random%20unitary%20%5Bpaper%5D.pdf How to generate a random unitary matrix]'', 2009.</ref>
  
==Notes==
+
{{SourceCode|name=RandomUnitary}}
The random unitary matrix is generated by constructing a [[Ginibre ensemble]] of appropriate size, performing a [http://en.wikipedia.org/wiki/QR_decomposition 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.<ref>Māris Ozols. ''[http://home.lu.lv/~sd20008/papers/essays/Random%20unitary%20%5Bpaper%5D.pdf How to generate a random unitary matrix]'', 2009.</ref>
 
  
 
==References==
 
==References==
 
<references />
 
<references />

Latest revision as of 03:57, 15 April 2019

RandomUnitary
Generates a random unitary or orthogonal matrix

Other toolboxes required none
Related functions RandomDensityMatrix
RandomPOVM
RandomStateVector
RandomSuperoperator
Function category 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 = 0). 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 here to view this function's source code on github.

References