<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by M. Sohaib Alam on Medium]]></title>
        <description><![CDATA[Stories by M. Sohaib Alam on Medium]]></description>
        <link>https://medium.com/@sohaib.alam?source=rss-ff82a07308a3------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*Npa47jTFnc37dD_y66Xqlw.png</url>
            <title>Stories by M. Sohaib Alam on Medium</title>
            <link>https://medium.com/@sohaib.alam?source=rss-ff82a07308a3------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 20:19:49 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sohaib.alam/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Unpacking the Quantum Supremacy Benchmark with Python]]></title>
            <link>https://medium.com/@sohaib.alam/unpacking-the-quantum-supremacy-benchmark-with-python-67a46709d?source=rss-ff82a07308a3------2</link>
            <guid isPermaLink="false">https://medium.com/p/67a46709d</guid>
            <category><![CDATA[python-programming]]></category>
            <category><![CDATA[quantum-supremacy]]></category>
            <category><![CDATA[quantum-physics]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[quantum-computing]]></category>
            <dc:creator><![CDATA[M. Sohaib Alam]]></dc:creator>
            <pubDate>Mon, 11 Nov 2019 20:02:24 GMT</pubDate>
            <atom:updated>2019-11-12T19:28:36.300Z</atom:updated>
            <content:encoded><![CDATA[<p>By <a href="https://medium.com/@sohaib.alam">M. Sohaib Alam</a> and <a href="http://willzeng.com/">Will Zeng</a></p><p>We recently had an exciting moment in the history of (quantum) computing. A team of collaborators led by Google published a result in <a href="https://www.nature.com/articles/s41586-019-1666-5">Nature</a> showing a benchmark against which their 53-qubit quantum processor significantly outperformed Summit — the world’s largest supercomputer. While valuable business applications are still some <a href="https://medium.com/@wjzeng/clarifying-quantum-supremacy-better-terms-for-milestones-in-quantum-computation-d15ccb53954f">future milestones</a> away, we like Scott Aaronson’s <a href="https://www.nytimes.com/2019/10/30/opinion/google-quantum-computer-sycamore.html">analogy</a>:</p><p>“…much like the Wright Flyer in 1903, or Enrico Fermi’s nuclear chain reaction in 1942, [quantum supremacy] only needs to prove a point.”</p><p>The original publication is commendably <a href="https://www.nature.com/articles/s41586-019-1666-5">readable</a> and comes with 66 pages of detailed <a href="https://arxiv.org/abs/1910.11333">supplementary information</a>.</p><p>In this blog post we’ll explore, through some short snippets of Python, the key aspects of their supremacy benchmark. We’ll show you how to run your own simulations of cross-entropy benchmark fidelity to measure if a data set of bitstrings was generated by a working quantum computer.</p><p><strong>The Key Benchmark Idea: </strong>We have very good complexity-theoretic evidence to believe that bitstring outputs generated from random quantum programs are exponentially difficult for classical computers to reproduce.[1]</p><p><strong>What is a quantum program? (quick review)</strong></p><p>We can recommend a few different introductions to quantum programming depending on your background:</p><p>For computer scientists: <a href="https://arxiv.org/abs/1608.03355">A Practical Quantum Instruction Set Architecture</a></p><p>For programmers: <a href="https://pyquil.readthedocs.io/en/stable/">Forest SDK Docs</a></p><p>For physicists: <a href="http://www.theory.caltech.edu/people/preskill/ph229/">John Preskill’s notes</a></p><p>A quantum memory is made up of <em>n</em> qubits. The state of this memory is not described by a single bitstring — like it is on normal computers, nor is it described by a probability distribution over bitstrings like it is in probabilistic computing. Instead, it is described by a vector of complex numbers called amplitudes ⍺, one for each of the 2^<em>n</em> possible bitstrings.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ljaZGrMZOaeh2_sU0VpYfw.png" /></figure><p>where the notation |x&gt; is the physicist way of writing a complex vector <strong><em>x</em></strong>. When a quantum memory is <em>measured </em>the outcome is a single bitstring with probability given by</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*X7bT9SpreFg5d2CbTKOcog.png" /></figure><p>Quantum programs operate on quantum memory and are described by unitary matrices. The operation of programs takes one state of quantum memory (a complex vector) to another state of quantum memory (another complex vector) by matrix multiplication. Thus for a quantum computer that starts with all qubits set to zero, the output of a quantum program <em>U</em> is described by</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4RtngU-OoSdfBZWue7oT8w.png" /></figure><p>where the “cross-O” notation denotes a <a href="https://en.wikipedia.org/wiki/Tensor_product">tensor product</a> of |0&gt; states. Unitary matrices are matrices of complex numbers whose conjugate transpose is its inverse, i.e.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dnWSQBE9-UnQjOis1b07yA.png" /></figure><p>This rule is needed to make sure that quantum program transformations obey the conservation of probability. You can think of unitary operations as <a href="https://en.wikipedia.org/wiki/Norm_(mathematics)">norm</a>-preserving transformations that act like rotations in the space of quantum states.</p><p>With that brief introduction behind us, we’ll shift our attention to writing code. Before we do, let’s import the following python libraries.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/352b17941bef05ee0b11a24310f397b3/href">https://medium.com/media/352b17941bef05ee0b11a24310f397b3/href</a></iframe><p>Now, let’s look at a quick code example that illustrates the basic concepts of quantum programs just introduced.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f33b6784020aa308b67e8d421fe02905/href">https://medium.com/media/f33b6784020aa308b67e8d421fe02905/href</a></iframe><p><strong>What is a random quantum program?</strong></p><p>This definition of quantum programs gives us a natural way to think about <em>random quantum programs. </em>In theory, a random quantum program is a randomly selected unitary matrix. In particular it is a<a href="https://en.wikipedia.org/wiki/Haar_measure"> Haar-random</a> unitary matrix. However, quantum programs on real quantum processors have some restrictions. Quantum processors are typically calibrated with a specific set of operations — such as one and two-qubit gates that are then composed into a quantum program. There are also often restrictions on which gates can be applied to which qubits. While we ignore such details in our simulations here, the <a href="https://www.nature.com/articles/s41586-019-1666-5">Nature paper</a> describes how the Google group builds up a random unitary out of patterns of randomly selected gates that obey the gate set and connectivity restrictions of their processor. For the purposes of our simulation, we can generate a random unitary matrix as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fc5b613f74319f3795731cba40e49e6e/href">https://medium.com/media/fc5b613f74319f3795731cba40e49e6e/href</a></iframe><p><strong>What are the special quantum statistics?</strong></p><p>Given some bitstring, a quantum program will specify the probability of sampling this bitstring. We can then ask what <em>the probability of the probability </em>of sampling this bitstring is, if we draw a quantum program at random. Because the double probability can be hard to read, you can think of this as: “How likely is it that any particular bitstring will occur with a particular probability?” This meta-probability, the probability distribution over the probability of drawing some given bitstring, is specified by the Porter-Thomas distribution, f(p) = N*exp(-N*p), where N=2^n_qubits. Let’s see how we can generate this distribution from random quantum circuits.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/36f7d0af07be23a567f62dda023b49f5/href">https://medium.com/media/36f7d0af07be23a567f62dda023b49f5/href</a></iframe><p>The function above picks some arbitrary bitstring, then generates a sample of probabilities for that bitstring resulting from drawing random quantum programs. We can use this function to generate such a sample for a specified number of qubits, and a number of random quantum programs over those qubits. We can then plot the resulting distribution of those probabilities, and compare it with the theoretical calculation, as shown below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/060658b79d3003d2451467e130aa19cc/href">https://medium.com/media/060658b79d3003d2451467e130aa19cc/href</a></iframe><p>Running the code above, you should obtain a figure like the following.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/782/0*bY-HRPc-SC80PqbM" /></figure><p>Notice the vertical line at the value 1/dim, where dim = 2^n_qubits is the dimensionality of the n-qubit space. This line corresponds to the meta-distribution that we would obtain if the output bitstrings were not sampled from a quantum computer, but instead were sampled from a uniform distribution. The uniform distribution would mean that all bitstrings occur with probability 1/dim and so the histogram in that case just looks like a <a href="https://en.wikipedia.org/wiki/Dirac_delta_function">Dirac delta function</a>.</p><p>Intuitively, what the figure above is saying is that if you fix some bitstring, e.g. 1011001…, and execute a random quantum program, it is much more likely that the resultant probability of sampling this bitstring is smaller than that given by the uniform distribution.</p><p>If instead of fixing a bitstring in advance, as we did above, we sample this bitstring according to the randomly sampled unitary itself, we generate an entirely different distribution from the Porter-Thomas distribution. This is the more relevant meta-probability distribution for the supremacy experiment, and can be generated by using the following code</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5eb909d3a3964492b39d2201883d7769/href">https://medium.com/media/5eb909d3a3964492b39d2201883d7769/href</a></iframe><p>to generate a similar plot,</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/76d7b195505e61361fc6c6f74883adf8/href">https://medium.com/media/76d7b195505e61361fc6c6f74883adf8/href</a></iframe><p>to find a distribution that looks like the following.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/771/0*4FaWypsYEiUx9q11" /></figure><p><strong>How can we benchmark these statistics?</strong></p><p>Having formed an intuition for random quantum programs, we now dive straight into the calculation of the <em>linear cross-entropy benchmark</em> <em>fidelity </em>(or <em>F_XEB</em> for short) used in the actual quantum supremacy experiment. This is the key statistic that the Google team measured to show supremacy. Essentially, it involves estimating an overlap between the experiment’s output distribution, and the ideal output distribution. More precisely, we calculate the average of the ideal probabilities of bitstrings generated from the experimental output, &lt;P(x_i)&gt;.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/433/0*7p0Bm7RePWk4xo_i" /></figure><p>The diagram above sketches the work-flow of this calculation. First, we sample a random unitary. A black box algorithm/device takes the unitary as input, and generates samples. This black box may be a noisy version of the ideal quantum program, or some classical algorithm that generates samples of bit-strings. Using a (simulated) ideal quantum program that represents the sampled unitary, we calculate ideal probabilities of the generated samples. It is these ideal probabilities that we average over. In the case of the supremacy experiment, the black box denotes the (noisy) quantum processor, and the ideal program represents classical simulations estimating, for the ones they could, the ideal probabilities for the experiment’s random quantum circuits.</p><p>The precise quantity we seek to calculate is actually normalized to produce values between 0 and 1, and is given by D*&lt;P(x_i)&gt;-1, where D=2^n is the dimension of the n-qubit space, and &lt;P(x_i)&gt; is the average ideal probability of sampling bitstring x_i, as the code below shows.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f7133101e292baa569526492404fdc92/href">https://medium.com/media/f7133101e292baa569526492404fdc92/href</a></iframe><p>Using the function defined above, we can compute F_XEB in the case where the black box is the ideal quantum program itself. For a sufficiently large number of qubits, this value should become very nearly equal to 1. For a smaller number of qubits, it is instead approximately equal to 2D/(D+1)-1. Note that this value asymptotes to 1 as D becomes very large (see [2] for more details).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6b63e62af4a814ec6558108ddb12b139/href">https://medium.com/media/6b63e62af4a814ec6558108ddb12b139/href</a></iframe><p>In the above, we used values for trials and n_samples such that the output of the calculated F_XEB has reasonably low variance. We find that the choices given in the Nature paper yield slightly higher variance, but are also quicker to calculate through simulation.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/610465f3f6bf102c80b7fb5289b818a3/href">https://medium.com/media/610465f3f6bf102c80b7fb5289b818a3/href</a></iframe><p>On the other hand, we can also compute the same quantity for a black box that just uniformly samples over all bit-strings. We’ll find that F_XEB for such a sampler is roughly equal to 0.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5e8fe81856b077ee6856197b7e655fe1/href">https://medium.com/media/5e8fe81856b077ee6856197b7e655fe1/href</a></iframe><p>In the above, we used a uniform distribution sampler (F_XEB~0) to compare against the ideal quantum computer (F_XEB~1). The essential argument is that getting a high F_XEB score achieves quantum supremacy. To support this statement, we would also need to show that any efficiently classically generated distribution scores low on F_XEB. This is a subtle question which is addressed in recent literature[3], but illustrates the core idea of the supremacy experiment: if the quantum device is generating samples that yield F_XEB close to 1, then the device is a good one. If it yields F_XEB close to 0, then it is no more powerful than a classical computer.</p><p>As the quality of the quantum processor decreases — often due to noise and error in the processor — we would expect that the F_XEB fidelity would trend towards zero. Errors are causing the processor to behave more classically as it loses quantum coherence. We’ll investigate this further in the next section.</p><p>Notice that we only used 4 qubits in the simulated experiments above. As the number of qubits grows, the simulation quickly becomes intractable for classical computers, such as your laptop. To approximate the ideal probabilities for large numbers of qubits (e.g. 40+ qubits), Google had to use state-of-the-art simulation techniques on Google data centers and Summit, the world’s fastest supercomputer. The quantum sampling was performed on a 53-qubit quantum computer named Sycamore. The quantum processor hardware is very cool, but beyond the scope of this post. We encourage you to read the <a href="https://www.nature.com/articles/s41586-019-1666-5.pdf">Nature paper</a> to learn more about it.</p><p><strong>What is the effect of noise?</strong></p><p>Real life quantum processors are noisy and have a non-zero probability of carrying out operations other than the ones intended. This can alter the statistics of the supremacy benchmark. A noise model relevant for the quantum programs executed on Sycamore is the <a href="https://en.wikipedia.org/wiki/Quantum_depolarizing_channel">depolarizing model</a>. In this model, the state produced from an execution of the intended quantum program could be thought of as a statistical ensemble of the ideal quantum state (resulting from the application of the ideal quantum program) and the totally mixed state, which represents the uniform distribution over all bitstrings.</p><p>Equivalently, we may imagine this scenario as a noisy channel in which the intended quantum program is carried out with some probability, and with remaining probability, one of the Pauli operations on this space are instead carried out[4]. This situation is simulated by the function below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c3fe07aa6097c70dacba89d0eeb8bee1/href">https://medium.com/media/c3fe07aa6097c70dacba89d0eeb8bee1/href</a></iframe><p>which we can call to estimate the F_XEB in the presence of some noise</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/af4bfa1dbf013c7aafa9e112cbeacbb9/href">https://medium.com/media/af4bfa1dbf013c7aafa9e112cbeacbb9/href</a></iframe><p>If <em>p</em> is the probability of no (depolarizing) error, then theoretically, the value of F_XEB should equal <em>p*</em>(D-1)/(D+1) where D=2^n is the dimensionality of the n-qubit space (see [2] for more details). This quantity reduces to just <em>p</em> as D becomes very large. Essentially, this means that F_XEB decreases as the noise grows, and becomes zero in the limit where noise occurs in the system with unit probability.</p><p><strong>Next Steps</strong></p><p>With the experimental demonstration of this supremacy benchmark, we look forward to an exciting future for quantum computing. The Nature paper shows that this benchmark is useful not just for performance testing, but also for calibration of quantum hardware. We hope that these small examples show that the key underlying statistics can be expressed simply.</p><p><strong>Code</strong></p><p>The code used in this blog post has been aggregated into a repository at: <a href="https://github.com/msohaibalam/xeb">https://github.com/msohaibalam/xeb</a>.</p><p><strong>Unitary Fund</strong></p><p>Interested in software for quantum computing? Unitary Fund supports open source projects in quantum programming with grant funding. Apply now at <a href="https://unitary.fund/">https://unitary.fund/</a>.</p><p><strong>Acknowledgments</strong></p><p>We wish to thank Adam Bouland, Matt Harrigan and Yousef Hindy for valuable suggestions and useful feedback.</p><p>[1] We have complexity-theoretic evidence that exactly sampling from the output of random quantum programs is classically hard, and remains hard in the presence of a small amount of noise (see <a href="https://arxiv.org/abs/1803.04402">https://arxiv.org/abs/1803.04402</a>). We also have some evidence that getting a low but non-zero F_XEB score is classically hard (see <a href="https://arxiv.org/abs/1910.12085">https://arxiv.org/abs/1910.12085</a> and <a href="https://arxiv.org/abs/1612.05903">https://arxiv.org/abs/1612.05903</a>).</p><p>[2] The derivation of this expression is outlined in a small appendix, available on the <a href="https://github.com/msohaibalam/xeb/blob/master/xeb_post_appendix.pdf">Github repo for this blog</a>.</p><p>[3] See <a href="https://arxiv.org/abs/1608.00263">https://arxiv.org/abs/1608.00263</a>, <a href="https://arxiv.org/abs/1803.04402">https://arxiv.org/abs/1803.04402</a>, and <a href="https://arxiv.org/abs/1910.12085">https://arxiv.org/abs/1910.12085</a>.</p><p>[4] Note that more realistically, one would model each separate gate operation as a depolarizing channel, however since our simulation doesn’t decompose into gates, we use this approach to show the general idea.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=67a46709d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Quantum computing a Gaussian wavefunction]]></title>
            <link>https://medium.com/@sohaib.alam/quantum-computing-a-gaussian-wavefunction-2d0be23d77b2?source=rss-ff82a07308a3------2</link>
            <guid isPermaLink="false">https://medium.com/p/2d0be23d77b2</guid>
            <category><![CDATA[quantum-computing]]></category>
            <category><![CDATA[quantum-physics]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[quantum-mechanics]]></category>
            <dc:creator><![CDATA[M. Sohaib Alam]]></dc:creator>
            <pubDate>Tue, 23 Jan 2018 05:25:53 GMT</pubDate>
            <atom:updated>2018-01-23T05:25:53.300Z</atom:updated>
            <content:encoded><![CDATA[<p>It’s an exciting time for anyone interested in quantum computing. From the perspective of someone who spent his grad school career studying and researching in quantum gravity (string theory), and now works as a data scientist, it’s especially wonderful to witness quantum mechanical equations phase transitioning from being mere chalk-dust on blackboards to becoming programmable instructions on quantum hardware.</p><p>In the spirit of such times, this post discusses an implementation of an algorithm that I hope would be of practical interest. Specifically, we’ll go over implementing an algorithm that prepares Gaussian wavefunctions using <a href="http://pyquil.readthedocs.io/en/latest/index.html">pyQuil</a>, an open-source Python library for quantum programming provided by <a href="https://www.rigetti.com/">Rigetti Quantum Computing</a>. What follows in the rest of this post assumes some basic familiarity with quantum mechanics and computing (for the interested reader, the <a href="http://pyquil.readthedocs.io/en/latest/intro_to_qc.html">intro</a> contained in the pyQuil documentation is actually a great place to start; and there are also <a href="https://www.amazon.com/Quantum-Computation-Information-10th-Anniversary/dp/1107002176">many</a> <a href="https://www.amazon.com/Introduction-Quantum-Computing-Phillip-Kaye/dp/019857049X/ref=sr_1_1?s=books&amp;ie=UTF8&amp;qid=1516498898&amp;sr=1-1&amp;keywords=kaye+laflamme+quantum+computing">excellent</a> <a href="https://www.amazon.com/Quantum-Computing-Computer-Scientists-Yanofsky/dp/0521879965/ref=sr_1_fkmr0_2?s=books&amp;ie=UTF8&amp;qid=1516498990&amp;sr=1-2-fkmr0&amp;keywords=yanofsky+manucci">textbooks</a> on the subject). The algorithm itself is described in the paper “<a href="https://arxiv.org/abs/0801.0342">Wavefunction preparation and resampling using a quantum computer</a>” by A. Kitaev and W. A. Webb, but here we will implement a slightly modified version of this algorithm, which is discussed below.</p><p>By a Gaussian wavefunction, we refer to a quantum state whose probability amplitudes are distributed according to a Gaussian (or ‘normal’) distribution. Such states are important for at least two reasons:</p><p>(a) They describe the ground state of the (quantum) <a href="https://en.wikipedia.org/wiki/Quantum_harmonic_oscillator">simple harmonic oscillator</a>, which is ubiquitous in physics;</p><p>(b) Gaussian distributions themselves play a central (no <a href="https://en.wikipedia.org/wiki/Central_limit_theorem">pun</a> intended) role in statistics, machine learning and data analysis</p><p>The states we construct are discrete approximations to the continuous case. For a step-by-step motivation of the construction, the reader is encouraged to read the <a href="https://arxiv.org/abs/0801.0342">paper</a>. Here, we will simply note that given N qubits, we can construct a function that is periodic with period 2^N and is normalized to unity, so that we can write down the following state:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/746/1*Z65qNKzoGgaik002G4dJtg.png" /></figure><p>where the |i&gt;’s represent the computational basis, and</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/354/1*T4RekvdVUoDm87peU6RTzg.png" /></figure><p>represents the normalization factor. We will make use of this factor later, so let us immediately code this in (using <a href="http://www.numpy.org/">numpy</a>):</p><pre><strong>import</strong> numpy <strong>as</strong> np</pre><pre><strong>def</strong> norm_(sigma_, mu_, N):<br>    # cutoff infinite sum at N, i.e. sum_{i=-N}^{i=N} (...)<br>    <strong>return</strong> np.sum(np.exp((-(np.arange(-N, N+1, 1) - mu_)**2)/float(sigma_**2)))</pre><p>The state we wrote down earlier approximates a Gaussian in the limit µ, 2^N - µ &gt;&gt; σ &gt;&gt; 1, a limit that says that our parameters should be greater than the discrete resolution, and that favors a mean towards the center of the computational index. You can convince yourself that in this limit, the amplitudes of the state are indeed distributed (approximately) like a Gaussian by plotting its values against <em>i</em>, and check that the amplitudes-squared indeed sum up to 1, just as they should (but you can also just scroll all the way to the bottom of this post to see what it looks like).</p><p><strong>Quantum Recursion</strong></p><p>We can also define the very same state via a recursive description:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-75hCxvIJdMGmJW6RTMuWA.png" /></figure><p>where the rotation angle is defined by</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/460/1*7K9Y5hVkrerGgPD3qjlr-A.png" /></figure><p>One can show that the previous description satisfies this recursion relation (for a proof of this, see <a href="https://github.com/msohaibalam/gaussian-wavefunction/blob/master/proof_recursive_gaussian.pdf">my github</a>). Note that at every stage, the state is entangled with the previous level of the recursion, since for no N &gt; 1 can we write the state as a product state of different recursion levels.</p><p>We’ll need the angle to create our state, so let’s code that in too</p><pre><strong>def</strong> angle_(sigma_, mu_, N=10**3):<br>    # cutoff the infinite sum in norm_(..) at 10**3 by default<br>    <strong>return</strong> np.arccos(np.sqrt(norm_(sigma_/2., mu_/2., N)/norm_(sigma_, mu_, N)))</pre><p>We can now use the recursive description to build up our state. Assuming all qubits (indexed 0 to N-1) start out in the |0〉state, we can summarize the recursive sub-routine as follows:</p><ol><li>Given inputs (σ0, µ0) = (σ, µ), rotate the 0-th qubit by angle α(σ, µ) via the rotation operator</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/392/1*FS62Wyio2d6DUK8BO_BHHw.png" /></figure><p>2. Set (σ1, µ1) = (σ0/2, µ0/2) if the previously rotated qubit is |0&gt;, and (σ1, µ1) = (σ0/2, (µ0–1)/2) if it is |1&gt;, and provide (σ1, µ1) as the input for the next qubit</p><p>3. Repeat the previous two steps for the next qubit until all N qubits have been used</p><p>In the original <a href="https://arxiv.org/abs/0801.0342">paper</a>, the authors envision implementing the recursion using supplementary memory qubits to store the value of the rotated angle at every level of the recursion and entangling this with the next level. Here, we take a slightly different approach that instead constructs and applies (<em>n</em>+1)-qubit unitary operations at (0-indexed) level <em>n</em> of the recursion.</p><p><strong>An alternative approach</strong></p><p>The main thing to note is that step 2 above implies a kind of controlled operation. The operations required for this step are</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/946/1*zm1bhM8p9PmJcueILTWP9A.png" /></figure><p>and so on.</p><p>How do we construct general unitary operators that can achieve these transformations? We illustrate first for the specific example of recursion level <em>n</em>=2, and then motivate the generalization to arbitrary <em>n</em>. We claim that the following operator achieves the necessary transformation:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8mb03ljNiwS9dgluaytVPw.png" /></figure><p>Here, the operator in the middle is a direct sum of R_α’s, each of which is a 2x2 rotation operator whose matrix representation is provided above (note that the angles in the middle have switched in order, compared to the list of recursion steps shown immediately before), and each τ_i is a transposition operator whose matrix representation is provided by</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/858/1*QXFc1L7wIH6YT2c2xWQPGw.png" /></figure><p>where <em>N</em> is the number of qubits (in this example, <em>N</em>=3). By convention, if B_<em>m</em> represents the 2-dimensional complex vector space that qubit <em>m</em> lives in, then a 3-qubit space is given by B_2 ⊗ B_1 ⊗ B_0. Essentially, what the sequence of τ operations achieves is to reverse this order of qubits:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/630/1*Qfw-sUAIDCtb_P4LcmXxrQ.png" /></figure><p>so that (τ_1 . τ_0 . τ_1) |001〉→ |100〉, (τ_1 . τ_0 . τ_1) |011〉→ |110〉, etc. More generally, each τ_i essentially swaps the vector spaces located at the (0-indexed) i-th and (i+1)-st positions, i.e. τ_i : … ⊗ B_i+1 ⊗ B_i ⊗ … ⊗ B_0 → … ⊗ B_i ⊗ B_i+1 ⊗ … ⊗ B_0. After a reversal of qubit order, we can then apply the R_α operations, and then reverse the qubit ordering via the τ_i once more to bring the indexing back into conventional form.</p><p>Schematically then, what we are really performing is the operation (τ_reverse . R . τ_reverse), where R represents the direct sum of the rotation operators and τ_reverse simply reverses the ordering of qubits as discussed above. The reader can verify with explicit matrix multiplication that we can achieve the desired transformations in this way, and just to give one example (but suppressing matrix forms for brevity):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*isyCpsNjcL5sAB3sZuuz7g.png" /></figure><p><strong>Quil and transpositions</strong></p><p>By keeping track of the possible multi-qubit states at each level of the recursion, we can in a similar manner construct precisely the controlled operations we need. But before we do so, we note that in <a href="https://arxiv.org/abs/1608.03355">Quil</a>, the quantum instruction language that pyQuil is actually based on, when we write something like</p><pre>R 0 1 ... k</pre><p>we are actually providing the instruction to carry out the (k+1)-qubit operation R on the vector space B_N-1 ⊗ B_N-2 ⊗ … ⊗ B_0 ⊗ B_1 ⊗ … ⊗ B_k. In other words, Quil automatically applies the τ_reverse on the right in (τ_reverse . R . τ_reverse) for us, and only after that applies the R operation. Moreover, it also automatically applies the remaining τ_reverse for us, in order to bring back the qubit ordering into the conventional form B_N-1 ⊗ B_N-2 ⊗ … ⊗ B_k ⊗ B_k-1 ⊗ … ⊗ B_0. All of this is much better described in <a href="https://arxiv.org/abs/1711.02086">this paper</a>.</p><p>What it means for our purposes is that we can simply focus on constructing the direct sum of the rotation operations and forget all about the transpositions. Which is great! Because the direct sum implies a nice block diagonal form</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/560/1*pyOtDHl-14qnTY6vHJ7mGg.png" /></figure><p>where the rotation angles can be indexed from 0 to 2^<em>n</em> - 1 for recursion level <em>n. </em>Thus, we need only construct and apply these unitary transformations at every level of the recursion.</p><p><strong>Code-able recipe</strong></p><p>Specifically then, given N qubits all of which are initialized to the |0〉state, starting with <em>n</em>=0 and ending at <em>n</em>=N-1, we perform the following steps at each level <em>n</em> of the recursion:</p><p>I. For <em>n</em>=0, create a list of an empty string &#39;&#39;. For <em>n</em>&gt;0, create a list that contains 2^<em>n</em> bit-string representations of the <em>n</em>-qubit computational basis elements in ascending order. For example, for <em>n</em>=2, we would have [‘00’, ‘01’, ‘10’, ‘11’] (which, in binary, reads [0, 1, 2, 3]). Let’s code this in (using <a href="https://docs.python.org/2/library/itertools.html">itertools</a>):</p><pre><strong>import</strong> itertools</pre><pre><strong>def</strong> qubit_strings(n):<br>    qubit_strings = []<br>    <strong>for</strong> q <strong>in</strong> itertools.product([&#39;0&#39;, &#39;1&#39;], repeat=n):<br>        qubit_strings.append(&#39;&#39;.join(q))<br>    <strong>return</strong> qubit_strings</pre><p>The right most bit-string in each of these strings represents the previously rotated qubit, the one to the left of it the the qubit before that, and so on; e.g. the string ‘01’ would refer to the situation when the previously rotated qubit was |1&gt;, and the one before that was |0&gt;.</p><p>II. Calculate the various µ_<em>n</em>’s associated with each of the computational basis elements at level <em>n</em> of the recursion. We can do this as follows. Define</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/456/1*pRs9rcLhWnW-8HadvFczSQ.png" /></figure><p>in terms of which we can define another function h(..) on <em>n</em>-bit strings, such that h(‘ ’; µ) = µ and otherwise</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/836/1*6RsbbXBhwIx9KEu6UZ_Haw.png" /></figure><p>For example, h(`01&#39;; µ) = g(1, g(0, µ)) = g(1, µ/2) = ((µ/2)-1)/2. Calculate the function h(..) on each of the bit-strings generated in the previous step, and collect them in a list in precisely the same order as before. These are the modified means (the µ_<em>n</em>’s), of which there are a total of 2^<em>n</em> at recursion level <em>n</em>. We can implement this step in code as follows:</p><pre><strong>def</strong> mean_qubit_combo(qub, mu):<br>    # calculate modified mean for n-bit string qub<br>    mu_out = mu<br>    <strong>for</strong> bit <strong>in</strong> qub:<br>        mu_out = (mu_out/2.) - ((1/2.)*int(bit))<br>    <strong>return</strong> mu_out</pre><pre><strong>def</strong> level_means(mu, n):<br>    # list all modified means at recursion level n<br>    list_mu_out = []<br>    qb_strings = qubit_strings(n)<br>    <strong>for</strong> qb <strong>in</strong> qb_strings:<br>        mu_out = mean_qubit_combo(qb, mu)<br>        list_mu_out.append(mu_out)<br>    <strong>return</strong> list_mu_out</pre><p>III. Calculate σ_<em>n = </em>σ/(2^<em>n</em>), and use this together with the calculated µ_<em>n</em>’s above to first calculate all the possible (and previously defined) rotation angles α(σ, µ)’<em>s</em>:</p><pre><strong>def</strong> level_angles(sigma, mu, n):<br><em>    # At recursion level n, return a list of all the angles</em><br>    sigma_out = sigma/(2.**n)<br>    list_mu = level_means(mu, n)<br>    <em># for each (sigma, mu) pair, calculate the corresponding angle</em><br>    angles_out = []<br>    <strong>for</strong> mu_ <strong>in</strong> list_mu:<br>        angles_out.append(angle_(sigma_out, mu_))<br>    <strong>return</strong> angles_out</pre><p>and then use these to calculate a 2x2 rotation block associated with each angle</p><pre><strong>def</strong> rotation_block(alpha):<br><em>    # Given a rotation angle $\alpha$, return a 2x2 rotation block</em><br>    <strong>return</strong> np.array([[np.cos(alpha), -np.sin(alpha)], [np.sin(alpha), np.cos(alpha)]])</pre><p>IV. With the rotation blocks in place, construct a 2^(<em>n</em>+1) x 2^(<em>n</em>+1) block-diagonal unitary matrix using the 2^<em>n</em> rotation blocks. The rotation blocks are assembled in such an order so that the one corresponding to the multi-qubit with the smallest index goes in the top-left corner, while that with the largest index goes in the bottom-right:</p><pre><strong>def</strong> level_gate(sigma, mu, n):<br>    <em>&quot;&quot;&quot;</em><br><em>    Generate (n+1)-qubit controlled operation as a 2^(n+1) x 2^(n+1) matrix,</em><br><em>    with 2^n rotation blocks along the diagonal</em><br><em>    &quot;&quot;&quot;</em><br>    list_row_block = []<br>    list_level_angles = level_angles(sigma, mu, n)<br>    <strong>for</strong> nn, angle <strong>in</strong> enumerate(list_level_angles):<br>        rot_block = rotation_block(angle)<br>        row_block = np.hstack((np.zeros((2, 2*nn)), rot_block, np.zeros((2, 2*(2**(n) - nn - 1)))))<br>        list_row_block.append(row_block)<br>    level_n_gate = np.vstack(tuple(list_row_block))<br>    <strong>return</strong> level_n_gate</pre><p>And that builds us just the operator R = R_α(0) ⊕ … ⊕ R_α(2^<em>n</em> - 1) we need at recursion level <em>n</em>.</p><p>V. Apply this operator. Then, move on the next recursion level <em>n</em>+1 and repeat the same steps (until <em>n=</em>N-1, as already noted).</p><p>In order to actually construct our Gaussian wavefunction, we need to collect together all the gates/unitary transformations we’ve constructed above into an ordered list, so that they can later be applied in succession:</p><pre><strong>def</strong> list_all_gates(sigma, mu, N):<br>    <em>&quot;&quot;&quot;</em><br><em>    Given sigma, mu (standard dev, mean) and the number of qubits N,</em><br><em>    return a list of all gates used for the controlled operations required</em><br><em>    to produce a Gaussian wavefunction</em><br><em>    &quot;&quot;&quot;</em><br>    list_gates = []<br>    <strong>for</strong> n <strong>in</strong> range(N):<br>        list_gates.append(level_gate(sigma, mu, n))<br>    <strong>return</strong> list_gates</pre><p>To identify the unitary matrices we’ve built as quantum gates in pyQuil, we can define the following function which, in addition to some other specified inputs, also takes as input an instance of the Program class defined in pyQuil, and which easily allows us to define our own gates:</p><pre><strong>def</strong> defn_all_gates(sigma, mu, N, prog):<br>    <em>&quot;&quot;&quot;</em><br><em>    Define all gates specified by N qubits, for (standard deviation, mean) given by (sigma, mu),</em><br><em>    into the program input which is specified by prog</em><br><em>    &quot;&quot;&quot;</em><br>    list_gates_ = list_all_gates(sigma, mu, N)<br>    <strong>for</strong> i, gate <strong>in</strong> enumerate(list_gates_):<br>        prog.defgate(&quot;Level_&quot; + str(i) + &quot;_gate&quot;, gate)</pre><p>We will also need a function to actually apply these gates in successive order:</p><pre><strong>def</strong> apply_all_gates(sigma, mu, N, prog):<br>    <em>&quot;&quot;&quot;</em><br><em>    Apply all controlled rotation gates to produce the Gaussian wavefunction,</em><br><em>    &quot;&quot;&quot;</em><br>    list_gates_ = list_all_gates(sigma, mu, N)<br>    <strong>for</strong> i, gate <strong>in</strong> enumerate(list_gates_):<br>        tup_gate = (&quot;Level_&quot; + str(i) + &quot;_gate&quot;,) + tuple(range(i+1))<br>        prog.inst(tup_gate)</pre><p>With everything now in place, we can finally create our own Gaussian wavefunction. Before running the following snippet of code, make sure that you’ve <a href="http://pyquil.readthedocs.io/en/latest/getting_started.html">installed</a> pyQuil (and also <a href="https://matplotlib.org/">matplotlib</a>, which we use for plots). We’ll first create a connection with the <a href="http://pyquil.readthedocs.io/en/latest/qvm_overview.html">Quantum Virtual Machine</a>, which will allow us to simulate our algorithm on (ideal) quantum hardware (to actually access Rigetti’s <a href="http://pyquil.readthedocs.io/en/latest/qpu_overview.html">Quantum Processing Unit</a>, see <a href="http://pyquil.readthedocs.io/en/latest/qpu_usage.html">here</a>).</p><pre><strong>%</strong>matplotlib inline<br><strong>import</strong> matplotlib.pyplot <strong>as</strong> plt<strong><br>from</strong> pyquil.quil <strong>import</strong> Program<br><strong>import</strong> pyquil.api <strong>as</strong> api</pre><pre><em># open a connection with the Quantum Virtual Machine</em><br>quantum_simulator = api.QVMConnection()</pre><pre><em># launch a program</em><br>p = Program()</pre><pre># specify number of qubits to use<br>N = 6</pre><pre># specify standard deviation and mean;<br># here we choose values<br># that (approximately) satisfy the limit<br># µ, 2^N — µ &gt;&gt; σ &gt;&gt; 1</pre><pre>sigma = (2**N)/6.<br>mu = 2**(N-1) - 0.5</pre><pre><em># define all gates to the program</em><br>defn_all_gates(sigma, mu, N, p)</pre><pre><em># apply all gates to the program</em><br>apply_all_gates(sigma, mu, N, p)</pre><pre><em># create the gaussian wavefunction</em><br>gaussian_wavefunc = quantum_simulator.wavefunction(p)</pre><pre><em># plot the probabilities</em><br>wavefcn_xlist = list(gaussian_wavefunc.get_outcome_probs().keys())<br>wavefcn_ylist = list(gaussian_wavefunc.get_outcome_probs().values())</pre><pre>plt.figure(figsize=(12, 10))<br>plt.plot(range(len(wavefcn_ylist)), wavefcn_ylist)<br>plt.xticks(range(len(wavefcn_xlist)), wavefcn_xlist, rotation=70)<br>plt.xlabel(&#39;multi-qubit states in increasing lexicographic order&#39;)<br>plt.show()</pre><p>The output of the above should show something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/716/1*2Hv9dMwvwBaqScs2B-5g0g.png" /></figure><p>As promised, we have our Gaussian wavefunction!</p><p><strong>A few words of caution</strong></p><p>Recall that our function is periodic with period 2^N, where N is the number of qubits. This means that if we change our value of mu by k * 2^N, where k is some integer, we would still get the same outcome (try this! But don’t pick too large a value for k; because we cutoff our infinite sum in norm_(..) at some finite value, this function will instead return 0.0 for some sufficiently large k, which will then raise errors further downstream).</p><p>Note also that if we pick values that don’t satisfy the limit µ, 2^N - µ &gt;&gt; σ &gt;&gt; 1, we’ll end up with a state that does not look Gaussian at all. For example, if we had picked a mu equal to the value of the highest multi-qubit index, something like</p><pre>N = 6  # number of qubits<br>sigma = (2**N)/6.<br>mu = (2**N) - 1  # mean centered at the extreme right</pre><p>we would instead obtain a function that is not Gaussian, but that also more readily manifests the periodicity we referred to earlier:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/716/1*tetMT1BsqYj5mb4YXoGhGQ.png" /></figure><p>So it’s important to pick values that would satisfy the limit µ, 2^N - µ &gt;&gt; σ &gt;&gt; 1, and one would have to judiciously choose the number of qubits needed to generate the desired Gaussian for some specified µ and σ. Of course, more qubits than needed wouldn’t hurt, and if we picked values such as</p><pre>N = 7<br>sigma = (2**5)/6.<br>mu = (2**5) - 0.5</pre><p>we would still get a Gaussian looking curve, just one stationed more towards the left, as seen below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/716/1*-xOhnY28Dry_oH4VfYoICg.png" /></figure><p><strong>Code</strong></p><p>All the code built here, together with some unit tests, and a proof of the recursive definition can be found on <a href="https://github.com/msohaibalam/gaussian-wavefunction">my github repo</a>.</p><p><strong>Acknowledgements</strong></p><p>I would like to sincerely thank everyone at the <a href="https://rigetti-forest.slack.com/">Rigetti slack channel</a> for their engaging discussions and valuable input.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2d0be23d77b2" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>