<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://qetlab.com/wiki/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Ian+Auckes</id>
	<title>QETLAB - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://qetlab.com/wiki/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Ian+Auckes"/>
	<link rel="alternate" type="text/html" href="https://qetlab.com/Special:Contributions/Ian_Auckes"/>
	<updated>2026-10-22T14:15:47Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.45.3</generator>
	<entry>
		<id>https://qetlab.com/wiki/index.php?title=RandomUnitary&amp;diff=942</id>
		<title>RandomUnitary</title>
		<link rel="alternate" type="text/html" href="https://qetlab.com/wiki/index.php?title=RandomUnitary&amp;diff=942"/>
		<updated>2019-04-15T03:57:17Z</updated>

		<summary type="html">&lt;p&gt;Ian Auckes: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Function&lt;br /&gt;
|name=RandomUnitary&lt;br /&gt;
|desc=Generates a random unitary or orthogonal matrix&lt;br /&gt;
|rel=[[RandomDensityMatrix]]&amp;lt;br /&amp;gt;[[RandomPOVM]]&amp;lt;br /&amp;gt;[[RandomStateVector]]&amp;lt;br /&amp;gt;[[RandomSuperoperator]]&lt;br /&gt;
|cat=[[List of functions#Random_things|Random things]]&lt;br /&gt;
|upd=September 30, 2014&lt;br /&gt;
|v=0.50}}&lt;br /&gt;
&amp;lt;tt&amp;gt;&#039;&#039;&#039;RandomUnitary&#039;&#039;&#039;&amp;lt;/tt&amp;gt; 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]. &lt;br /&gt;
&lt;br /&gt;
==Syntax==&lt;br /&gt;
* &amp;lt;tt&amp;gt;U = RandomUnitary(DIM)&amp;lt;/tt&amp;gt;&lt;br /&gt;
* &amp;lt;tt&amp;gt;U = RandomUnitary(DIM,RE)&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Argument descriptions==&lt;br /&gt;
* &amp;lt;tt&amp;gt;DIM&amp;lt;/tt&amp;gt;: The number of rows (or equivalently, columns) that &amp;lt;tt&amp;gt;U&amp;lt;/tt&amp;gt; will have.&lt;br /&gt;
* &amp;lt;tt&amp;gt;RE&amp;lt;/tt&amp;gt; (optional, default 0): A flag (either 0 or 1) indicating that &amp;lt;tt&amp;gt;U&amp;lt;/tt&amp;gt; should only have real entries (&amp;lt;tt&amp;gt;RE = 1&amp;lt;/tt&amp;gt;) or that it is allowed to have complex entries (&amp;lt;tt&amp;gt;RE = 0&amp;lt;/tt&amp;gt;). That is, if you set &amp;lt;tt&amp;gt;RE = 1&amp;lt;/tt&amp;gt; then &amp;lt;tt&amp;gt;U&amp;lt;/tt&amp;gt; will be an orthogonal matrix, not just a unitary matrix.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
===A random qubit gate===&lt;br /&gt;
To generate a random quantum gate that acts on qubits, you could use the following code:&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; RandomUnitary(2)&lt;br /&gt;
&lt;br /&gt;
ans =&lt;br /&gt;
&lt;br /&gt;
   0.2280 + 0.6126i  -0.2894 - 0.6993i&lt;br /&gt;
  -0.3147 + 0.6883i  -0.2501 + 0.6039i&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===A random orthogonal matrix===&lt;br /&gt;
To generate a random orthogonal (rather than unitary) matrix, set &amp;lt;tt&amp;gt;RE = 1&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; U = RandomUnitary(3,1)&lt;br /&gt;
&lt;br /&gt;
U =&lt;br /&gt;
&lt;br /&gt;
    0.9805   -0.1869   -0.0603&lt;br /&gt;
    0.1678    0.6381    0.7515&lt;br /&gt;
   -0.1020   -0.7470    0.6570&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To verify that this matrix is indeed orthogonal, we multiply it by its transpose:&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; U&#039;*U&lt;br /&gt;
&lt;br /&gt;
ans =&lt;br /&gt;
&lt;br /&gt;
    1.0000    0.0000    0.0000&lt;br /&gt;
    0.0000    1.0000    0.0000&lt;br /&gt;
    0.0000    0.0000    1.0000&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Moments of the trace of an orthogonal matrix===&lt;br /&gt;
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 &amp;lt;tt&amp;gt;RandomUnitary&amp;lt;/tt&amp;gt; function to reproduce these values approximately as follows:&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; s = 10^5;&lt;br /&gt;
   ct = zeros(1,4);&lt;br /&gt;
   for j = 1:s&lt;br /&gt;
      trO = trace(RandomUnitary(3,1))^2;&lt;br /&gt;
      ct = ct + trO.^(1:4);&lt;br /&gt;
   end&lt;br /&gt;
   ct/s&lt;br /&gt;
&lt;br /&gt;
ans =&lt;br /&gt;
&lt;br /&gt;
    0.9997    2.9917   14.9298   90.4464&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
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.&amp;lt;ref&amp;gt;Māris Ozols. &#039;&#039;[http://home.lu.lv/~sd20008/papers/essays/Random%20unitary%20%5Bpaper%5D.pdf How to generate a random unitary matrix]&#039;&#039;, 2009.&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{SourceCode|name=RandomUnitary}}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ian Auckes</name></author>
	</entry>
</feed>