{"id":1785,"date":"2018-03-29T15:47:23","date_gmt":"2018-03-29T14:47:23","guid":{"rendered":"https:\/\/www.quantum-bits.org\/?p=1785"},"modified":"2022-08-12T17:17:49","modified_gmt":"2022-08-12T16:17:49","slug":"having-fun-with-bell-states-and-q","status":"publish","type":"post","link":"https:\/\/www.quantum-bits.org\/?p=1785","title":{"rendered":"Having fun with Bell states and Q#"},"content":{"rendered":"<p>In a <a href=\"https:\/\/www.quantum-bits.org\/?p=1611\" target=\"_blank\" rel=\"noopener\">previous post<\/a>, we introduced the basic concepts of quantum computing, during which quantum entanglement, Bell states and usual quantum gates such as Hadamard&#8217;s or Pauli&#8217;s were quickly addressed.<\/p>\n<p>Today, we&#8217;re going to explore some of these concepts and start having fun with <strong>quantum programming<\/strong> to illustrate these ideas.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-1612\" src=\"https:\/\/www.quantum-bits.org\/wp-content\/uploads\/2018\/03\/quantum-physics-formulas-over-blackboard.jpg\" alt=\"\" width=\"850\" height=\"332\" srcset=\"https:\/\/www.quantum-bits.org\/wp-content\/uploads\/2018\/03\/quantum-physics-formulas-over-blackboard.jpg 768w, https:\/\/www.quantum-bits.org\/wp-content\/uploads\/2018\/03\/quantum-physics-formulas-over-blackboard-300x117.jpg 300w\" sizes=\"(max-width: 850px) 100vw, 850px\" \/><\/p>\n<p><strong>Quantum entanglement<\/strong><\/p>\n<p>Quantum entanglement&nbsp;is a phenomenon which occurs when pairs (or groups) of&nbsp;particles are generated (or interact) in such a way that the&nbsp;quantum state&nbsp;of each particle cannot be described independently of the state of the other(s), even when the particles are separated by a large distance:&nbsp; the quantum state must be described for the system as a whole.<\/p>\n<p>Measurements&nbsp;of physical properties (position,&nbsp;momentum,&nbsp;spin, polarization &#8230;) performed on entangled particles are found to be&nbsp;correlated. Let&#8217;s imagine a pair of particles generated in such a way that their total spin is known to be zero.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-1792\" src=\"https:\/\/www.quantum-bits.org\/wp-content\/uploads\/2018\/03\/entanglement-figure.png\" alt=\"\" width=\"350\" height=\"451\" srcset=\"https:\/\/www.quantum-bits.org\/wp-content\/uploads\/2018\/03\/entanglement-figure.png 900w, https:\/\/www.quantum-bits.org\/wp-content\/uploads\/2018\/03\/entanglement-figure-233x300.png 233w, https:\/\/www.quantum-bits.org\/wp-content\/uploads\/2018\/03\/entanglement-figure-768x990.png 768w, https:\/\/www.quantum-bits.org\/wp-content\/uploads\/2018\/03\/entanglement-figure-794x1024.png 794w\" sizes=\"(max-width: 350px) 100vw, 350px\" \/><\/p>\n<p>Now, let&#8217;s imagine that one particle is found to have clockwise spin on a certain axis. Because they are entangled, the spin of the other particle, measured on the same axis, will be found to be counterclockwise !<\/p>\n<p>This behavior gives rise to&nbsp;paradoxical&nbsp;effects: any measurement of a property of a particle can be seen as acting on that particle. In the case of entangled particles, such a measurement will be on the entangled system as a whole.<\/p>\n<p>It seems to appear that one particle of an entangled pair &#8220;knows&#8221; what measurement has been performed on the other, and with what outcome, even though there is no known means for such information to be communicated between the particles, which at the time of measurement may be separated by any arbitrarily large distances (Einstein referring to it as &#8220;spooky action at a distance&#8221;).<\/p>\n<p><strong>EPR paradox<\/strong><\/p>\n<p>Such phenomena were the subject of a 1935 paper by&nbsp;<a title=\"Albert Einstein\" href=\"https:\/\/en.wikipedia.org\/wiki\/Albert_Einstein\" target=\"_blank\" rel=\"noopener\">Albert Einstein<\/a>,&nbsp;<a title=\"Boris Podolsky\" href=\"https:\/\/en.wikipedia.org\/wiki\/Boris_Podolsky\" target=\"_blank\" rel=\"noopener\">Boris Podolsky<\/a>, and&nbsp;<a title=\"Nathan Rosen\" href=\"https:\/\/en.wikipedia.org\/wiki\/Nathan_Rosen\" target=\"_blank\" rel=\"noopener\">Nathan Rosen<\/a> (and several papers by&nbsp;<a title=\"Erwin Schr\u00f6dinger\" href=\"https:\/\/en.wikipedia.org\/wiki\/Erwin_Schr%C3%B6dinger\" target=\"_blank\" rel=\"noopener\">Erwin Schr\u00f6dinger<\/a>) describing what came to be known as the&nbsp;<a title=\"EPR paradox\" href=\"https:\/\/en.wikipedia.org\/wiki\/EPR_paradox\" target=\"_blank\" rel=\"noopener\">EPR paradox<\/a>.<\/p>\n<p>This <a href=\"https:\/\/en.wikipedia.org\/wiki\/Thought_experiment\" target=\"_blank\" rel=\"noopener\">thought experiment<\/a> aims at the&nbsp;<a title=\"Copenhagen interpretation\" href=\"https:\/\/en.wikipedia.org\/wiki\/Copenhagen_interpretation\">Copenhagen interpretation<\/a> (more precisely, at <a href=\"https:\/\/en.wikipedia.org\/wiki\/Wave_function_collapse\" target=\"_blank\" rel=\"noopener\">the wave function collapse<\/a>). Resolutions of the paradox have important implications for the&nbsp;<a class=\"mw-redirect\" title=\"Interpretation of quantum mechanics\" href=\"https:\/\/en.wikipedia.org\/wiki\/Interpretation_of_quantum_mechanics\" target=\"_blank\" rel=\"noopener\">interpretation of quantum mechanics<\/a>.<\/p>\n<p>In&nbsp;quantum mechanics, a particle is described by a&nbsp;quantum state. This quantum state can be represented as a&nbsp;superposition&nbsp;(i.e. a&nbsp;linear combination) of&nbsp;basis&nbsp;states. In principle one is free to choose the set of basis states, as long as they&nbsp;span&nbsp;the space. If one chooses the&nbsp;<a title=\"Eigenfunction\" href=\"https:\/\/en.wikipedia.org\/wiki\/Eigenfunction\" target=\"_blank\" rel=\"noopener\">eigenfunctions<\/a>&nbsp;of the&nbsp;<a title=\"Position operator\" href=\"https:\/\/en.wikipedia.org\/wiki\/Position_operator\" target=\"_blank\" rel=\"noopener\">position operator<\/a> <img src='https:\/\/s0.wp.com\/latex.php?latex=q&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='q' title='q' class='latex' \/> as a set of basis functions, one speaks of a state as a&nbsp;<a title=\"Wave function\" href=\"https:\/\/en.wikipedia.org\/wiki\/Wave_function\" target=\"_blank\" rel=\"noopener\">wave function<\/a> position space.<\/p>\n<p>For a quick recap on bra-ket notations, inner products, operators, observables and all that jazz, please, have a look at <a href=\"https:\/\/www.quantum-bits.org\/?p=55\" target=\"_blank\" rel=\"noopener\">this (old) post<\/a>.<\/p>\n<p>Let&#8217;s define the ket <img src='https:\/\/s0.wp.com\/latex.php?latex=%7C%5Cpsi%5Crangle&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='|\\psi\\rangle' title='|\\psi\\rangle' class='latex' \/>, representing a quantum state. The wave function (position space) <img src='https:\/\/s0.wp.com\/latex.php?latex=%5Cpsi%28q%29&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\\psi(q)' title='\\psi(q)' class='latex' \/> can be defined as <img src='https:\/\/s0.wp.com\/latex.php?latex=%5Cpsi%28q%29+%3D+%5Clangle+q+%7C+%5Cpsi%5Crangle+&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\\psi(q) = \\langle q | \\psi\\rangle ' title='\\psi(q) = \\langle q | \\psi\\rangle ' class='latex' \/>.<\/p>\n<p>This wave function is interpreted as a (complex-valued) probability amplitude. The probabilities <img src='https:\/\/s0.wp.com\/latex.php?latex=%5Cmathcal%7BP%7D%28q%29&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\\mathcal{P}(q)' title='\\mathcal{P}(q)' class='latex' \/> for the possible results of measurements made on the system are :<\/p>\n<p style=\"text-align: center;\"><img src='https:\/\/s0.wp.com\/latex.php?latex=%5Cmathcal%7BP%7D%28q%29+%3D+%7C%5Cpsi%28q%29%7C%5E2+%3D+%5Cpsi%28q%29%5E%2A%5Cpsi%28q%29%3D%7C%5Clangle+q+%7C+%5Cpsi%5Crangle%7C%5E2&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\\mathcal{P}(q) = |\\psi(q)|^2 = \\psi(q)^*\\psi(q)=|\\langle q | \\psi\\rangle|^2' title='\\mathcal{P}(q) = |\\psi(q)|^2 = \\psi(q)^*\\psi(q)=|\\langle q | \\psi\\rangle|^2' class='latex' \/><\/p>\n<p>Quantum frameworks do not provide single measurement outcomes in a deterministic way. According to the understanding of quantum mechanics known as the&nbsp;Copenhagen interpretation, measurement causes an instantaneous collapse of the&nbsp;wave function&nbsp;describing the quantum system into an&nbsp;eigenstate&nbsp;of the&nbsp;observable&nbsp;that was measured.<\/p>\n<p>Einstein was one of the most prominent opponent of the&nbsp;Copenhagen interpretation. In his view, quantum mechanics was incomplete. Commenting on this, other writers (such as&nbsp;<a title=\"John von Neumann\" href=\"https:\/\/en.wikipedia.org\/wiki\/John_von_Neumann\" target=\"_blank\" rel=\"noopener\">John von Neumann<\/a>&nbsp;and&nbsp;<a title=\"David Bohm\" href=\"https:\/\/en.wikipedia.org\/wiki\/David_Bohm\" target=\"_blank\" rel=\"noopener\">David Bohm<\/a><sup id=\"cite_ref-10\" class=\"reference\"><\/sup>) hypothesized that consequently there would have to be&nbsp;<a title=\"Hidden variable theory\" href=\"https:\/\/en.wikipedia.org\/wiki\/Hidden_variable_theory\" target=\"_blank\" rel=\"noopener\">&#8216;hidden&#8217; variables<\/a> responsible for random measurement results.<\/p>\n<p>The article that first brought forth these matters, &#8220;Can Quantum-Mechanical Description of Physical Reality Be Considered Complete?&#8221; was published in 1935 by Einstein, Podolsky, and Rosen (EPR). The paper prompted a response by <a href=\"https:\/\/en.wikipedia.org\/wiki\/Niels_Bohr\" target=\"_blank\" rel=\"noopener\">Niels Bohr<\/a>, which he published in the same journal, in the same year, using &#8230; the same title \ud83d\ude42<\/p>\n<p>Einstein had been skeptical of the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Uncertainty_principle\" target=\"_blank\" rel=\"noopener\">Heisenberg uncertainty principle<\/a> and the role of chance in quantum theory.<\/p>\n<p>But the crux of this debate was not about chance, but something deeper:<\/p>\n<ul>\n<li>Is there one objective physical reality, which every observer sees from his own vantage? (Einstein&#8217;s view)<\/li>\n<li>Or does the observer co-create physical reality by the questions he poses with experiments? (Bohr&#8217;s view).<\/li>\n<\/ul>\n<p>The EPR description involves &#8220;two particles, A and B, which interact briefly and then move off in opposite directions.&#8221; <sup id=\"cite_ref-Kumar2011_13-0\" class=\"reference\"><\/sup>According to&nbsp;Heisenberg&#8217;s uncertainty principle, it is impossible to measure both the momentum and the position of particle B exactly. However, it is possible to measure the exact position of particle A. By calculation, therefore, with the exact position of particle A known, the exact position of particle B can be known. Alternatively, the exact momentum of particle A can be measured, so the exact momentum of particle B can be worked out.<\/p>\n<p>EPR tried to set up a paradox to question the range of true application of quantum mechanics: quantum theory predicts that both values cannot be known for a particle, and yet the EPR thought experiment purports to show that they must all have determinate values.<\/p>\n<p>Since 1972, experiments analogous to the one described in the EPR paper have been carried out, notably at the&nbsp;<a title=\"Saclay Nuclear Research Centre\" href=\"https:\/\/en.wikipedia.org\/wiki\/Saclay_Nuclear_Research_Centre\" target=\"_blank\" rel=\"noopener\">Saclay Nuclear Research Centre<\/a>. These experiments appear to show that Einstein&#8217;s <a class=\"mw-redirect\" title=\"Local realism\" href=\"https:\/\/en.wikipedia.org\/wiki\/Local_realism\" target=\"_blank\" rel=\"noopener\">local realism<\/a>&nbsp;idea is false,<sup id=\"cite_ref-Gribbin_7-0\" class=\"reference\"> <\/sup>vindicating Bohr !<\/p>\n<p><strong>Bell&#8217;s theorem and experiments<\/strong><\/p>\n<p>The original EPR paradox challenges the prediction of quantum mechanics that it is impossible to know both the position and the momentum of a quantum particle. This challenge can however be extended to other pairs of physical properties<\/p>\n<p>While EPR felt that the paradox showed that quantum theory was incomplete and should be extended with&nbsp;hidden variables, the usual modern resolution is to say that due to the common preparation of the two particles (for example the creation of an electron-positron pair from a photon) the property we want to measure has a well defined meaning only when analyzed for the whole system while the same property for the parts individually remains undefined.<\/p>\n<p>Therefore, if similar measurements are being performed on the two entangled subsystems, there will always be a correlation between the outcomes resulting in a well defined global outcome i.e. for both subsystems together. However, the outcomes for each subsystem separately at each repetition of the experiment will not be well defined or predictable.<\/p>\n<p>Note that this correlation does not imply any action of the measurement of one particle on the measurement of the other. Therefore it does not imply any form of (spooky) action at a distance or any violation of special relativity (effects propagating faster than light). This modern resolution eliminates the need for hidden variables, action at a distance or other structures introduced over time in order to explain the phenomenon.<\/p>\n<p>In 1964,&nbsp;<a title=\"John Stewart Bell\" href=\"https:\/\/en.wikipedia.org\/wiki\/John_Stewart_Bell\" target=\"_blank\" rel=\"noopener\">John Bell<\/a>&nbsp;showed that the predictions of quantum mechanics in the EPR thought experiment are significantly different from the predictions of a particular class of hidden variable theories (the&nbsp;local&nbsp;hidden variable theories).<\/p>\n<p>These differences, expressed using&nbsp;inequality relations&nbsp;known as &#8220;Bell&#8217;s inequalities&#8221;, are in principle experimentally detectable.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-1806\" src=\"https:\/\/www.quantum-bits.org\/wp-content\/uploads\/2018\/03\/bell-exp.png\" alt=\"\" width=\"300\" height=\"107\" srcset=\"https:\/\/www.quantum-bits.org\/wp-content\/uploads\/2018\/03\/bell-exp.png 450w, https:\/\/www.quantum-bits.org\/wp-content\/uploads\/2018\/03\/bell-exp-300x107.png 300w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>Bell&#8217;s theorem&nbsp;is a &#8220;no-go theorem&#8221; that draws an important distinction between&nbsp;quantum mechanics&nbsp;and the world as described by&nbsp;classical mechanics. Bell&#8217;s theorem rules out&nbsp;local hidden variables&nbsp;as a viable explanation of quantum mechanics (though it still leaves the door open for non-local hidden variables)<\/p>\n<p>After the publication of Bell&#8217;s paper &#8220;On the Einstein Podolsky Rosen paradox&#8221;, a variety of experiments to test Bell&#8217;s inequalities were devised. These generally relied on measurement of&nbsp;photon polarization.<\/p>\n<p>All experiments, like <a href=\"https:\/\/en.wikipedia.org\/wiki\/Alain_Aspect\" target=\"_blank\" rel=\"noopener\">Alain Aspect<\/a>&#8216;s, conducted to date have found behavior in line with the predictions of standard quantum mechanics theory.<\/p>\n<p><strong>Back to quantum computing<\/strong><\/p>\n<p>In our <a href=\"https:\/\/www.quantum-bits.org\/?p=1611\" target=\"_blank\" rel=\"noopener\">previous post<\/a>, we encountered an entangled state know as &#8220;Bell state&#8221;:<\/p>\n<div style=\"text-align: center;\">&nbsp;<img src='https:\/\/s0.wp.com\/latex.php?latex=%5Cdisplaystyle+%7C+%5Cpsi_e+%5Crangle+%3D+%5Cfrac%7B1%7D%7B%5Csqrt%7B2%7D%7D+%5Cbig%28+%7C+00+%5Crangle+%2B%7C+11+%5Crangle+%5Cbig%29+&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\\displaystyle | \\psi_e \\rangle = \\frac{1}{\\sqrt{2}} \\big( | 00 \\rangle +| 11 \\rangle \\big) ' title='\\displaystyle | \\psi_e \\rangle = \\frac{1}{\\sqrt{2}} \\big( | 00 \\rangle +| 11 \\rangle \\big) ' class='latex' \/><\/div>\n<div>&nbsp;<\/div>\n<div>Bell states&nbsp;are an import concept in&nbsp;quantum information science&nbsp;and represent the simplest examples of&nbsp;entanglement.&nbsp;An&nbsp;EPR pair&nbsp;is a pair of&nbsp;qubits that are in a Bell state together. Because of the entanglement, measurement of one qubit will assign a value to the other qubit immediately in one of four ways, where the value assigned depends on which Bell state the two qubits are in.<\/div>\n<div>&nbsp;<\/div>\n<div>The phenomena can be used for example to agree upon random numbers in less time than it would take to communicate the numbers at the speed of light over the distance between the peers.<\/div>\n<div>&nbsp;<\/div>\n<div>We also introduced Pauli, Hadamard and CNOT gates, as well as quantum circuits.<\/div>\n<div>&nbsp;<\/div>\n<div>CNOT gates are of particular importance because they can be used to entangle and disentangle EPR states. Actually, any quantum circuit can be simulated to an arbitrary degree of accuracy using a combination of CNOT gates and single qubit rotations.<\/div>\n<div>&nbsp;<\/div>\n<div>The CNOT gate operates on a 2-qubits quantum register. The CNOT gate flips the second qubit (the target qubit) if and only if the first qubit (the control qubit) is&nbsp; <img src='https:\/\/s0.wp.com\/latex.php?latex=%7C+1+%5Crangle&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='| 1 \\rangle' title='| 1 \\rangle' class='latex' \/>.<\/div>\n<div>&nbsp;<\/div>\n<div style=\"text-align: center;\"><img src='https:\/\/s0.wp.com\/latex.php?latex=%5Ctextsf%7BCNOT%7D+%3D+%5Cbegin%7Bbmatrix%7D+1+%26+0+%26+0+%26+0+%5C%5C+0+%26+1+%26+0+%26+0+%5C%5C+0+%26+0+%26+0+%26+1+%5C%5C+0+%26+0+%26+1+%26+0+%5Cend%7Bbmatrix%7D+&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\\textsf{CNOT} = \\begin{bmatrix} 1 &amp; 0 &amp; 0 &amp; 0 \\\\ 0 &amp; 1 &amp; 0 &amp; 0 \\\\ 0 &amp; 0 &amp; 0 &amp; 1 \\\\ 0 &amp; 0 &amp; 1 &amp; 0 \\end{bmatrix} ' title='\\textsf{CNOT} = \\begin{bmatrix} 1 &amp; 0 &amp; 0 &amp; 0 \\\\ 0 &amp; 1 &amp; 0 &amp; 0 \\\\ 0 &amp; 0 &amp; 0 &amp; 1 \\\\ 0 &amp; 0 &amp; 1 &amp; 0 \\end{bmatrix} ' class='latex' \/><\/div>\n<div>&nbsp;<\/div>\n<div>On the other hand, the Hadamard gate is a 1-qubit rotation:<\/div>\n<div>&nbsp;<\/div>\n<div style=\"text-align: center;\"><img src='https:\/\/s0.wp.com\/latex.php?latex=%5Cdisplaystyle+H+%3D+%5Cfrac%7B1%7D%7B%5Csqrt%7B2%7D%7D+%5Cbegin%7Bbmatrix%7D+1+%26+1+%5C%5C+1+%26+-1+%5Cend%7Bbmatrix%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\\displaystyle H = \\frac{1}{\\sqrt{2}} \\begin{bmatrix} 1 &amp; 1 \\\\ 1 &amp; -1 \\end{bmatrix}' title='\\displaystyle H = \\frac{1}{\\sqrt{2}} \\begin{bmatrix} 1 &amp; 1 \\\\ 1 &amp; -1 \\end{bmatrix}' class='latex' \/><\/div>\n<div>&nbsp;<\/div>\n<div>Let&#8217;s now consider this simple quantum circuit:<\/div>\n<div>&nbsp;<\/div>\n<div><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-1815\" src=\"https:\/\/www.quantum-bits.org\/wp-content\/uploads\/2018\/03\/gate-entangle.png\" alt=\"\" width=\"76\" height=\"60\"><br \/>\nThis gate sequence is of fundamental significance to quantum computing because it creates a maximally entangled 2-qubit state: a Bell state.<\/div>\n<div>&nbsp;<\/div>\n<div style=\"text-align: center;\"><img src='https:\/\/s0.wp.com\/latex.php?latex=%5Cdisplaystyle+%5Ctextsf%7BCNOT%7D%28H+%5Cotimes+%5Cmathbf%7B1%7D%29+%7C+00+%5Crangle+%3D+%5Cfrac%7B1%7D%7B%5Csqrt%7B2%7D%7D+%5Cbig%28+%7C+00+%5Crangle+%2B%7C+11+%5Crangle+%5Cbig%29&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\\displaystyle \\textsf{CNOT}(H \\otimes \\mathbf{1}) | 00 \\rangle = \\frac{1}{\\sqrt{2}} \\big( | 00 \\rangle +| 11 \\rangle \\big)' title='\\displaystyle \\textsf{CNOT}(H \\otimes \\mathbf{1}) | 00 \\rangle = \\frac{1}{\\sqrt{2}} \\big( | 00 \\rangle +| 11 \\rangle \\big)' class='latex' \/><\/div>\n<p><strong>Playing with Q#<\/strong><\/p>\n<p>Having all this in mind, it is now time to have a little fun and experiment quantum programming.<\/p>\n<p>Q# is a programming language&nbsp;used for expressing&nbsp;quantum algorithms. It was initially released to the public by&nbsp;Microsoft&nbsp;as part of its Quantum Development Kit.<\/p>\n<p>This kit is composed of several components, the main parts being:<\/p>\n<ul>\n<li><strong>Q# language and compiler<\/strong>: Q# is a domain-specific programming language used for expressing quantum algorithms. It is used for writing sub-programs that execute on an adjunct quantum processor under the control of a classical host program and computer.<\/li>\n<li><strong>Q# standard library<\/strong>: The library contains operations and functions that support both the classical language control requirement and the Q# quantum algorithms.<\/li>\n<li><strong>Local quantum machine simulator<\/strong>: A full state vector simulator optimized for accurate vector simulation and speed.<\/li>\n<\/ul>\n<p>The quantum simulator that is shipped with the Quantum Development Kit is capable of processing up to 32 qubits (and up to 40 qubits on&nbsp;<a title=\"Microsoft Azure\" href=\"https:\/\/en.wikipedia.org\/wiki\/Microsoft_Azure\" target=\"_blank\" rel=\"noopener\">Azure<\/a>).<\/p>\n<p>It runs on Windows boxes, as well as on real operating systems such as GNU\/Linux or MacOS.<\/p>\n<p>A typical program is twofold:<\/p>\n<ul>\n<li>A &#8220;classical&#8221; part (using a traditional .Net programming language) is used to invoke the quantum simulator, provide input data and read output data from it<\/li>\n<li>A &#8220;quantum&#8221; part (using the Q# programming language) with the ability to create and use qubits for algorithms<\/li>\n<\/ul>\n<p>The most prominent features of Q# are the ability to&nbsp;entangle&nbsp;and introduce&nbsp;superpositioning&nbsp;to qubits via CNOT and Hadamard gates.<\/p>\n<p>The primitive operations are defined in the Q# standard library, like:<\/p>\n<ul class=\"lf-text-block lf-block\" data-lf-anchor-id=\"9a13450b1884f3cad96c6f4a5aa84f0d:0\">\n<li>Common 1-qubit unitary operations: Pauli gates (X, Y, Z), 1-qubit <a href=\"https:\/\/en.wikipedia.org\/wiki\/Clifford_algebra\" target=\"_blank\" rel=\"noopener\">Cliffords<\/a> like the Hadamard gate (H), &#8230;<\/li>\n<li>Phase shift gates like the T operation or Rx, Ry, Rz, &#8230; rotations<\/li>\n<li>Multi-qubits operations like CNOT, CCNOT, SWAP, &#8230;<\/li>\n<li>Measurements on single qubits like the M operation (which measures Pauli-Z operations on a single qubits) or the Mesure operation (which performs a joint measurement of one or more qubits in the specified product of Pauli operators)<\/li>\n<li>Classical functions: built-in data types (Int, Double, Range &#8230;), the Random operation, &#8230;<\/li>\n<li>Usual math operations, type conversions and bitwise operations<\/li>\n<\/ul>\n<p>It sounds good to build our little playground ! But be aware that (up to now), only the libraries and samples have been open sourced (MIT license) &#8230;<\/p>\n<p>Note: the following example comes almost straight out of the Q# tutorial, since it suits nicely our pedagogical purposes.<\/p>\n<p>Let&#8217;s define our first operation:<\/p>\n<ul>\n<li>measuring a qubit (M operation)<\/li>\n<li>if it&#8217;s in the state we want, we leave it alone, otherwise, we flip it with the Pauli-X gate (X operation):<\/li>\n<\/ul>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-1828\" src=\"https:\/\/www.quantum-bits.org\/wp-content\/uploads\/2018\/03\/qsharp-set.png\" alt=\"\" width=\"650\" height=\"332\" srcset=\"https:\/\/www.quantum-bits.org\/wp-content\/uploads\/2018\/03\/qsharp-set.png 650w, https:\/\/www.quantum-bits.org\/wp-content\/uploads\/2018\/03\/qsharp-set-300x153.png 300w\" sizes=\"(max-width: 650px) 100vw, 650px\" \/><\/p>\n<p>Rather straightforward ! Now, let&#8217;s introduce a second operation (Bell Test):<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-1835\" src=\"https:\/\/www.quantum-bits.org\/wp-content\/uploads\/2018\/03\/bell-state0.png\" alt=\"\" width=\"744\" height=\"592\" srcset=\"https:\/\/www.quantum-bits.org\/wp-content\/uploads\/2018\/03\/bell-state0.png 744w, https:\/\/www.quantum-bits.org\/wp-content\/uploads\/2018\/03\/bell-state0-300x239.png 300w\" sizes=\"(max-width: 744px) 100vw, 744px\" \/><\/p>\n<p>This operation loops for count iterations, sets a specified initial value on a qubit and them measures the result. It gathers statistics on how many zeros and ones are measured and returns them to the caller (&#8220;classical&#8221; part of the application). It also resets the qubit to a known state (Zero) before returning it, allowing others to allocate this qubit in a known state (required by the &#8220;using&#8221; statement, used to allocate an array of qubits for use in a block of code).<\/p>\n<p>Now comes the non-quantum part :<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-1834\" src=\"https:\/\/www.quantum-bits.org\/wp-content\/uploads\/2018\/03\/qsharp-drivers1.png\" alt=\"\" width=\"777\" height=\"490\" srcset=\"https:\/\/www.quantum-bits.org\/wp-content\/uploads\/2018\/03\/qsharp-drivers1.png 777w, https:\/\/www.quantum-bits.org\/wp-content\/uploads\/2018\/03\/qsharp-drivers1-300x189.png 300w, https:\/\/www.quantum-bits.org\/wp-content\/uploads\/2018\/03\/qsharp-drivers1-768x484.png 768w\" sizes=\"(max-width: 777px) 100vw, 777px\" \/><\/p>\n<p>Simple enough, it:<\/p>\n<ul>\n<li>spawns a new Quantum simulator<\/li>\n<li>sets the input parameters needed for the quantum algorithm<\/li>\n<li>runs 1 000 times (asynchronously) for each initial values (0 and 1) the quantum algorithm and fetches the results : a tuple of the number of zeros (numZeros) and number of ones (numOnes) measured by the simulator<\/li>\n<li>Outputs the resulting statistics<\/li>\n<\/ul>\n<p>The results, unsurprisingly (since initial states are prepared to be either <img src='https:\/\/s0.wp.com\/latex.php?latex=%7C+0+%5Crangle&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='| 0 \\rangle' title='| 0 \\rangle' class='latex' \/> or <img src='https:\/\/s0.wp.com\/latex.php?latex=%7C+1+%5Crangle&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='| 1 \\rangle' title='| 1 \\rangle' class='latex' \/>), are :<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-1837\" src=\"https:\/\/www.quantum-bits.org\/wp-content\/uploads\/2018\/03\/qsharp-result1.png\" alt=\"\" width=\"673\" height=\"81\" srcset=\"https:\/\/www.quantum-bits.org\/wp-content\/uploads\/2018\/03\/qsharp-result1.png 673w, https:\/\/www.quantum-bits.org\/wp-content\/uploads\/2018\/03\/qsharp-result1-300x36.png 300w\" sizes=\"(max-width: 673px) 100vw, 673px\" \/><\/p>\n<p>Of course, if one flips the qubits with a X gate before we measure it (in the BellTest operation):<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-1838\" src=\"https:\/\/www.quantum-bits.org\/wp-content\/uploads\/2018\/03\/bell-flip.png\" alt=\"\" width=\"520\" height=\"87\" srcset=\"https:\/\/www.quantum-bits.org\/wp-content\/uploads\/2018\/03\/bell-flip.png 520w, https:\/\/www.quantum-bits.org\/wp-content\/uploads\/2018\/03\/bell-flip-300x50.png 300w\" sizes=\"(max-width: 520px) 100vw, 520px\" \/><\/p>\n<p>And the results are thus reversed:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-1841\" src=\"https:\/\/www.quantum-bits.org\/wp-content\/uploads\/2018\/03\/qsharp-result-flip.png\" alt=\"\" width=\"673\" height=\"81\" srcset=\"https:\/\/www.quantum-bits.org\/wp-content\/uploads\/2018\/03\/qsharp-result-flip.png 673w, https:\/\/www.quantum-bits.org\/wp-content\/uploads\/2018\/03\/qsharp-result-flip-300x36.png 300w\" sizes=\"(max-width: 673px) 100vw, 673px\" \/><\/p>\n<p>So far, these are &#8220;only&#8221; classical results. Let&#8217;s add a bit of quantum weirdness !<\/p>\n<p>All we have to do is replace the X gate by an Hadamard H gate : instead of flipping the qubit all the way from <img src='https:\/\/s0.wp.com\/latex.php?latex=%7C+0+%5Crangle&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='| 0 \\rangle' title='| 0 \\rangle' class='latex' \/> to <img src='https:\/\/s0.wp.com\/latex.php?latex=%7C+1+%5Crangle&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='| 1 \\rangle' title='| 1 \\rangle' class='latex' \/>, we will only flip it halfway:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-1842\" src=\"https:\/\/www.quantum-bits.org\/wp-content\/uploads\/2018\/03\/bell-h.png\" alt=\"\" width=\"534\" height=\"101\" srcset=\"https:\/\/www.quantum-bits.org\/wp-content\/uploads\/2018\/03\/bell-h.png 534w, https:\/\/www.quantum-bits.org\/wp-content\/uploads\/2018\/03\/bell-h-300x57.png 300w\" sizes=\"(max-width: 534px) 100vw, 534px\" \/><\/p>\n<p>And the results are:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-1844\" src=\"https:\/\/www.quantum-bits.org\/wp-content\/uploads\/2018\/03\/qsharp-result-sup.png\" alt=\"\" width=\"673\" height=\"81\" srcset=\"https:\/\/www.quantum-bits.org\/wp-content\/uploads\/2018\/03\/qsharp-result-sup.png 673w, https:\/\/www.quantum-bits.org\/wp-content\/uploads\/2018\/03\/qsharp-result-sup-300x36.png 300w\" sizes=\"(max-width: 673px) 100vw, 673px\" \/><\/p>\n<p>Every time a measure is performed, the qubit is halfway between <img src='https:\/\/s0.wp.com\/latex.php?latex=%7C+0+%5Crangle&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='| 0 \\rangle' title='| 0 \\rangle' class='latex' \/> and <img src='https:\/\/s0.wp.com\/latex.php?latex=%7C+1+%5Crangle&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='| 1 \\rangle' title='| 1 \\rangle' class='latex' \/>), so one gets (statistically) 0 half the time and 1 half the time.<\/p>\n<p>We have created a superposition. This gives us our first real view into a quantum state with Q# !<\/p>\n<p>And now, let&#8217;s get into quantum entanglement !<\/p>\n<p>For this :<\/p>\n<ul>\n<li>we will have to allocate 2 qubits instead of one in the BellTest operation<\/li>\n<li>this will allow us to add a CNOT gate before measurement<\/li>\n<li>we will have to add another Set operation to initialize the second qubit (to make sure it&#8217;s always in the <img src='https:\/\/s0.wp.com\/latex.php?latex=%7C+0+%5Crangle&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='| 0 \\rangle' title='| 0 \\rangle' class='latex' \/> state when we start)<\/li>\n<li>we also need to reset the second qubit before releasing it<\/li>\n<li>we&#8217;re really interested in is how the second qubit reacts to the first being measured, so we&#8217;ll have to add this statistic to the BellTest operation<\/li>\n<\/ul>\n<p>The modified BellTest operation now looks like:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-1848\" src=\"https:\/\/www.quantum-bits.org\/wp-content\/uploads\/2018\/03\/qsharp-belltest2.png\" alt=\"\" width=\"729\" height=\"685\" srcset=\"https:\/\/www.quantum-bits.org\/wp-content\/uploads\/2018\/03\/qsharp-belltest2.png 729w, https:\/\/www.quantum-bits.org\/wp-content\/uploads\/2018\/03\/qsharp-belltest2-300x282.png 300w\" sizes=\"(max-width: 729px) 100vw, 729px\" \/><\/p>\n<p>We introduced a new return value (named &#8220;agree&#8221;) that will keep track of every time the measurement from the first qubit matches the measurement of the second qubit.<\/p>\n<p>Of course, we will also have to update the classical part accordingly:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-1849\" src=\"https:\/\/www.quantum-bits.org\/wp-content\/uploads\/2018\/03\/qsharp-drivers2.png\" alt=\"\" width=\"901\" height=\"494\" srcset=\"https:\/\/www.quantum-bits.org\/wp-content\/uploads\/2018\/03\/qsharp-drivers2.png 901w, https:\/\/www.quantum-bits.org\/wp-content\/uploads\/2018\/03\/qsharp-drivers2-300x164.png 300w, https:\/\/www.quantum-bits.org\/wp-content\/uploads\/2018\/03\/qsharp-drivers2-768x421.png 768w\" sizes=\"(max-width: 901px) 100vw, 901px\" \/><\/p>\n<p>And &#8230; results are:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-1850\" src=\"https:\/\/www.quantum-bits.org\/wp-content\/uploads\/2018\/03\/qsharp-result-bell.png\" alt=\"\" width=\"673\" height=\"81\" srcset=\"https:\/\/www.quantum-bits.org\/wp-content\/uploads\/2018\/03\/qsharp-result-bell.png 673w, https:\/\/www.quantum-bits.org\/wp-content\/uploads\/2018\/03\/qsharp-result-bell-300x36.png 300w\" sizes=\"(max-width: 673px) 100vw, 673px\" \/><\/p>\n<p>Our statistics for the first qubit haven&#8217;t changed (50-50 chance of a <img src='https:\/\/s0.wp.com\/latex.php?latex=%7C+0+%5Crangle&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='| 0 \\rangle' title='| 0 \\rangle' class='latex' \/> or a <img src='https:\/\/s0.wp.com\/latex.php?latex=%7C+1+%5Crangle&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='| 1 \\rangle' title='| 1 \\rangle' class='latex' \/>).<\/p>\n<p>But now when we measure the second qubit, it is <strong>always <\/strong>the same as what we measured for the first qubit. Our CNOT gate has <strong>entangled<\/strong> the two qubits, so that whatever happens to one of them, happens to the other !<\/p>\n<p>Please note that if you reversed the measurements (the second qubit before the first), the same thing would also happen.<\/p>\n<p>And there we go, these were our first steps with quantum computing using Q# \ud83d\ude42<\/p>\n<p><span style=\"font-size: 8pt;\">Note: to speedup the writing of this post, a few paragraphs and illustrations are based on wikipedia&#8217;s entries on quantum computing. Q# examples are based on MS Quantum Development Kit documentations<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In a previous post, we introduced the basic concepts of quantum computing, during which quantum entanglement, Bell states and usual quantum gates such as Hadamard&#8217;s or Pauli&#8217;s were quickly addressed. &#8230;<\/p>\n","protected":false},"author":1,"featured_media":3846,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0},"categories":[6],"tags":[],"_links":{"self":[{"href":"https:\/\/www.quantum-bits.org\/index.php?rest_route=\/wp\/v2\/posts\/1785"}],"collection":[{"href":"https:\/\/www.quantum-bits.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.quantum-bits.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.quantum-bits.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.quantum-bits.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1785"}],"version-history":[{"count":0,"href":"https:\/\/www.quantum-bits.org\/index.php?rest_route=\/wp\/v2\/posts\/1785\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.quantum-bits.org\/index.php?rest_route=\/wp\/v2\/media\/3846"}],"wp:attachment":[{"href":"https:\/\/www.quantum-bits.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1785"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.quantum-bits.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1785"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.quantum-bits.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1785"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}