<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/introducing-qcsimulator-a-5-qubit-quantum-computing-simulator-in-r/"></div></div><div><a data-url="http://www.r-bloggers.com/introducing-qcsimulator-a-5-qubit-quantum-computing-simulator-in-r/" data-counturl="http://www.r-bloggers.com/introducing-qcsimulator-a-5-qubit-quantum-computing-simulator-in-r/" data-count="horizontal" data-text="Introducing QCSimulator: A 5-qubit quantum computing simulator in R" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://gigadom.wordpress.com/2016/06/23/introducing-qcsimulator-a-5-qubit-quantum-computing-simulator-in-r/"> R – Giga thoughts …</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>
<h2>Introduction</h2>
<p>My 5-qubit Quantum Computing Simulator,QCSimulator, is finally ready, and here it is! I have been able to successfully complete this simulator by working through a fair amount of material. To a large extent, the simulator is easy, if one understands how to solve the quantum circuit. However the theory behind quantum computing itself, is quite formidable, and I hope to scale this mountain over a period of time.</p>
<p>As of now the R package can be installed from Github. I have to complete some documentation and tie a couple of loose ends before it gets onto CRAN.</p>
<p>The code for the QCSimulator package is available at Github <a target="_blank" href="https://github.com/tvganesh/QCSimulator">QCSimulator</a>. This post has also been published at Rpubs as <a target="_blank" href="http://rpubs.com/tvganesh/QCSimulator1">QCSimulator</a> and can be downloaded as a PDF document at <a target="_blank" href="https://drive.google.com/file/d/0Bxz66bp5PxPBWlVHd2NsWFRUTzg/view?usp=sharing">QCSimulator.pdf</a></p>
<p><em>Disclaimer: This article represents the author’s viewpoint only and doesn’t necessarily represent IBM’s positions, strategies or opinions</em></p>
<pre><code><span>library</span><span>(</span><span>QCSimulator</span><span>)</span>
<span>library</span><span>(</span><span>ggplot2</span><span>)</span></code></pre>
<div>
<h3>1. Initialize the environment and set global variables</h3>
<p>Here I initialize the environment with global variables and then display a few of them.</p>
<pre><code><span>rm</span><span>(</span><span>list</span><span>=</span><span>ls</span><span>(</span><span>)</span><span>)</span>
<span>#Call the init function to initialize the environment and create global variables</span>
<span>init</span><span>(</span><span>)</span>

<span># Display some of global variables in environment</span>
<span>ls</span><span>(</span><span>)</span></code></pre>
<pre><code>##  [1] &quot;I16&quot;     &quot;I2&quot;      &quot;I4&quot;      &quot;I8&quot;      &quot;q0_&quot;     &quot;q00_&quot;    &quot;q000_&quot;  
##  [8] &quot;q0000_&quot;  &quot;q00000_&quot; &quot;q00001_&quot; &quot;q0001_&quot;  &quot;q00010_&quot; &quot;q00011_&quot; &quot;q001_&quot;  
## [15] &quot;q0010_&quot;  &quot;q00100_&quot; &quot;q00101_&quot; &quot;q0011_&quot;  &quot;q00110_&quot; &quot;q00111_&quot; &quot;q01_&quot;   
## [22] &quot;q010_&quot;   &quot;q0100_&quot;  &quot;q01000_&quot; &quot;q01001_&quot; &quot;q0101_&quot;  &quot;q01010_&quot; &quot;q01011_&quot;
## [29] &quot;q011_&quot;   &quot;q0110_&quot;  &quot;q01100_&quot; &quot;q01101_&quot; &quot;q0111_&quot;  &quot;q01111_&quot; &quot;q1_&quot;    
## [36] &quot;q10_&quot;    &quot;q100_&quot;   &quot;q1000_&quot;  &quot;q10000_&quot; &quot;q10001_&quot; &quot;q1001_&quot;  &quot;q10010_&quot;
## [43] &quot;q10011_&quot; &quot;q101_&quot;   &quot;q1010_&quot;  &quot;q10100_&quot; &quot;q10101_&quot; &quot;q1011_&quot;  &quot;q10110_&quot;
## [50] &quot;q10111_&quot; &quot;q11_&quot;    &quot;q110_&quot;   &quot;q1100_&quot;  &quot;q11000_&quot; &quot;q11001_&quot; &quot;q1101_&quot; 
## [57] &quot;q11010_&quot; &quot;q11011_&quot; &quot;q111_&quot;   &quot;q1110_&quot;  &quot;q11100_&quot; &quot;q11101_&quot; &quot;q1111_&quot; 
## [64] &quot;q11110_&quot; &quot;q11111_&quot;</code></pre>
<pre><code><span>#1. 2 x 2 Identity matrix </span>
<span>I2</span></code></pre>
<pre><code>##      [,1] [,2]
## [1,]    1    0
## [2,]    0    1</code></pre>
<pre><code><span>#2. 8 x 8 Identity matrix </span>
<span>I8</span></code></pre>
<pre><code>##      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,]    1    0    0    0    0    0    0    0
## [2,]    0    1    0    0    0    0    0    0
## [3,]    0    0    1    0    0    0    0    0
## [4,]    0    0    0    1    0    0    0    0
## [5,]    0    0    0    0    1    0    0    0
## [6,]    0    0    0    0    0    1    0    0
## [7,]    0    0    0    0    0    0    1    0
## [8,]    0    0    0    0    0    0    0    1</code></pre>
<pre><code><span>#3. Qubit |00></span>
<span>q00_</span></code></pre>
<pre><code>##      [,1]
## [1,]    1
## [2,]    0
## [3,]    0
## [4,]    0</code></pre>
<pre><code><span>#4. Qubit |010></span>
<span>q010_</span></code></pre>
<pre><code>##      [,1]
## [1,]    0
## [2,]    0
## [3,]    1
## [4,]    0
## [5,]    0
## [6,]    0
## [7,]    0
## [8,]    0</code></pre>
<pre><code><span>#5. Qubit |0100></span>
<span>q0100_</span></code></pre>
<pre><code>##       [,1]
##  [1,]    0
##  [2,]    0
##  [3,]    0
##  [4,]    0
##  [5,]    1
##  [6,]    0
##  [7,]    0
##  [8,]    0
##  [9,]    0
## [10,]    0
## [11,]    0
## [12,]    0
## [13,]    0
## [14,]    0
## [15,]    0
## [16,]    0</code></pre>
<pre><code><span>#6. Qubit 10010</span>
<span>q10010_</span></code></pre>
<pre><code>##       [,1]
##  [1,]    0
##  [2,]    0
##  [3,]    0
##  [4,]    0
##  [5,]    0
##  [6,]    0
##  [7,]    0
##  [8,]    0
##  [9,]    0
## [10,]    0
## [11,]    0
## [12,]    0
## [13,]    0
## [14,]    0
## [15,]    0
## [16,]    0
## [17,]    0
## [18,]    0
## [19,]    1
## [20,]    0
## [21,]    0
## [22,]    0
## [23,]    0
## [24,]    0
## [25,]    0
## [26,]    0
## [27,]    0
## [28,]    0
## [29,]    0
## [30,]    0
## [31,]    0
## [32,]    0</code></pre>
<p>The QCSimulator implements the following gates</p>
<ol>
<li>Pauli X,Y,Z, S,S’, T, T’ gates</li>
<li>Rotation , Hadamard,CSWAP,Toffoli gates</li>
<li>2,3,4,5 qubit CNOT gates e.g CNOT2_01,CNOT3_20,CNOT4_13 etc</li>
<li>Toffoli State,SWAPQ0Q1</li>
</ol>
</div>
<div>
<h3>2. To display the unitary matrix of gates</h3>
<p>To check the unitary matrix of gates, we need to pass the appropriate identity matrix as an argument. Hence below the qubit gates require a 2 x 2 unitary matrix and the 2 &amp; 3 qubit CNOT gates require a 4 x 4 and 8 x 8 identity matrix respectively</p>
<pre><code><span>PauliX</span><span>(</span><span>I2</span><span>)</span></code></pre>
<pre><code>##      [,1] [,2]
## [1,]    0    1
## [2,]    1    0</code></pre>
<pre><code><span>Hadamard</span><span>(</span><span>I2</span><span>)</span></code></pre>
<pre><code>##           [,1]       [,2]
## [1,] 0.7071068  0.7071068
## [2,] 0.7071068 -0.7071068</code></pre>
<pre><code><span>S1Gate</span><span>(</span><span>I2</span><span>)</span></code></pre>
<pre><code>##      [,1] [,2]
## [1,] 1+0i 0+0i
## [2,] 0+0i 0-1i</code></pre>
<pre><code><span>CNOT2_10</span><span>(</span><span>I4</span><span>)</span></code></pre>
<pre><code>##      [,1] [,2] [,3] [,4]
## [1,]    1    0    0    0
## [2,]    0    0    0    1
## [3,]    0    0    1    0
## [4,]    0    1    0    0</code></pre>
<pre><code><span>CNOT3_20</span><span>(</span><span>I8</span><span>)</span></code></pre>
<pre><code>##      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,]    1    0    0    0    0    0    0    0
## [2,]    0    0    0    0    0    1    0    0
## [3,]    0    0    1    0    0    0    0    0
## [4,]    0    0    0    0    0    0    0    1
## [5,]    0    0    0    0    1    0    0    0
## [6,]    0    1    0    0    0    0    0    0
## [7,]    0    0    0    0    0    0    1    0
## [8,]    0    0    0    1    0    0    0    0</code></pre>
</div>
<div>
<h3>3. Compute the inner product of vectors</h3>
<p>For example of phi = 1/2|0> + sqrt(3)/2|1> and si= 1/sqrt(2)(10> + |1>) then the inner product is the dot product of the vectors</p>
<pre><code><span>phi</span> <span>=</span> <span>matrix</span><span>(</span><span>c</span><span>(</span><span>1</span><span>/</span><span>2</span>,<span>sqrt</span><span>(</span><span>3</span><span>)</span><span>/</span><span>2</span><span>)</span>,<span>nrow</span><span>=</span><span>2</span>,<span>ncol</span><span>=</span><span>1</span><span>)</span>
<span>si</span> <span>=</span> <span>matrix</span><span>(</span><span>c</span><span>(</span><span>1</span><span>/</span><span>sqrt</span><span>(</span><span>2</span><span>)</span>,<span>1</span><span>/</span><span>sqrt</span><span>(</span><span>2</span><span>)</span><span>)</span>,<span>nrow</span><span>=</span><span>2</span>,<span>ncol</span><span>=</span><span>1</span><span>)</span>
<span>angle</span><span>=</span> <span>innerProduct</span><span>(</span><span>phi</span>,<span>si</span><span>)</span>
<span>cat</span><span>(</span><span>&quot;Angle between vectors is:&quot;</span>,<span>angle</span><span>)</span></code></pre>
<pre><code>## Angle between vectors is: 15</code></pre>
</div>
<div>
<h3>4. Compute the dagger function for a gate</h3>
<p>The gate dagger computes and displays the transpose of the complex conjugate of the matrix</p>
<pre><code><span>TGate</span><span>(</span><span>I2</span><span>)</span></code></pre>
<pre><code>##      [,1]                 [,2]
## [1,] 1+0i 0.0000000+0.0000000i
## [2,] 0+0i 0.7071068+0.7071068i</code></pre>
<pre><code><span>GateDagger</span><span>(</span><span>TGate</span><span>(</span><span>I2</span><span>)</span><span>)</span></code></pre>
<pre><code>##      [,1]                 [,2]
## [1,] 1+0i 0.0000000+0.0000000i
## [2,] 0+0i 0.7071068-0.7071068i</code></pre>
</div>
<div>
<h3>5. Invoking gates in series</h3>
<p>The Quantum gates can be chained by passing each preceding Quantum gate as the argument. The final gate in the chain will have the qubit or the identity matrix passed to it.</p>
<pre><code><span># Call in reverse order</span>
<span># Superposition states</span>
<span># |+> state</span>
<span>Hadamard</span><span>(</span><span>q0_</span><span>)</span></code></pre>
<pre><code>##           [,1]
## [1,] 0.7071068
## [2,] 0.7071068</code></pre>
<pre><code><span># |-> ==> H x Z </span>
<span>PauliZ</span><span>(</span><span>Hadamard</span><span>(</span><span>q0_</span><span>)</span><span>)</span></code></pre>
<pre><code>##            [,1]
## [1,]  0.7071068
## [2,] -0.7071068</code></pre>
<pre><code><span># (+i) Y ==> H x S </span>
 <span>SGate</span><span>(</span><span>Hadamard</span><span>(</span><span>q0_</span><span>)</span><span>)</span></code></pre>
<pre><code>##                      [,1]
## [1,] 0.7071068+0.0000000i
## [2,] 0.0000000+0.7071068i</code></pre>
<pre><code><span># (-i)Y ==> H x S1</span>
 <span>S1Gate</span><span>(</span><span>Hadamard</span><span>(</span><span>q0_</span><span>)</span><span>)</span></code></pre>
<pre><code>##                      [,1]
## [1,] 0.7071068+0.0000000i
## [2,] 0.0000000-0.7071068i</code></pre>
<pre><code><span># Q1 -- TGate- Hadamard</span>
<span>Q1</span> <span>=</span> <span>Hadamard</span><span>(</span><span>TGate</span><span>(</span><span>I2</span><span>)</span><span>)</span></code></pre>
</div>
<div>
<h3>6. More gates in series</h3>
<div>
<h4>TGate of depth 2</h4>
<p>The Quantum circuit for a TGate of Depth 2 is</p>
<p>Q0 — Hadamard-TGate-Hadamard-TGate-SGate-Measurement as shown in IBM’s Quantum Experience Composer</p>
<p><a target="_blank" rel="attachment wp-att-4744" href="https://gigadom.wordpress.com/2016/06/23/introducing-qcsimulator-a-5-qubit-quantum-computing-simulator-in-r/untitled-36/"><img height="265" alt="Untitled" width="676" class="wp-image-4744" src="https://gigadom.files.wordpress.com/2016/06/untitled22.png?w=676&h=265"></a></p>
<p>Implementing the quantum gates in series in reverse order we have</p>
<pre><code><span># Invoking this in reverse order we get</span>
<span>a</span> <span>=</span> <span>SGate</span><span>(</span><span>TGate</span><span>(</span><span>Hadamard</span><span>(</span><span>TGate</span><span>(</span><span>Hadamard</span><span>(</span><span>q0_</span><span>)</span><span>)</span><span>)</span><span>)</span><span>)</span>
<span>result</span><span>=</span><span>measurement</span><span>(</span><span>a</span><span>)</span>

<span>plotMeasurement</span><span>(</span><span>result</span><span>)</span></code></pre>
<p><img><a target="_blank" rel="attachment wp-att-4728" href="https://gigadom.wordpress.com/2016/06/23/introducing-qcsimulator-a-5-qubit-quantum-computing-simulator-in-r/fig0-1/"><img height="483" alt="fig0-1" width="676" class="wp-image-4728" src="https://gigadom.files.wordpress.com/2016/06/fig0-1.png?w=676&h=483"></a></p>
</div>
</div>
<div>
<h3>7. Invoking gates in parallel</h3>
<p>To obtain the results of gates in parallel we have to take the Tensor Product <em>Note:In the TensorProduct invocation the Identity matrix is passed as an argument to get the unitary matrix of the gate.</em> Q0 – Hadamard-Measurement Q1 – Identity- Measurement</p>
<pre><code><span># </span>
<span>a</span> <span>=</span> <span>TensorProd</span><span>(</span><span>Hadamard</span><span>(</span><span>I2</span><span>)</span>,<span>I2</span><span>)</span>
<span>b</span> <span>=</span> <span>DotProduct</span><span>(</span><span>a</span>,<span>q00_</span><span>)</span>
<span>plotMeasurement</span><span>(</span><span>measurement</span><span>(</span><span>b</span><span>)</span><span>)</span></code></pre>
<p><img><a target="_blank" rel="attachment wp-att-4729" href="https://gigadom.wordpress.com/2016/06/23/introducing-qcsimulator-a-5-qubit-quantum-computing-simulator-in-r/fig1-1/"><img height="483" alt="fig1-1" width="676" class="wp-image-4729" src="https://gigadom.files.wordpress.com/2016/06/fig1-1.png?w=676&h=483"></a></p>
<pre><code><span>a</span> <span>=</span> <span>TensorProd</span><span>(</span><span>PauliZ</span><span>(</span><span>I2</span><span>)</span>,<span>Hadamard</span><span>(</span><span>I2</span><span>)</span><span>)</span>
<span>b</span> <span>=</span> <span>DotProduct</span><span>(</span><span>a</span>,<span>q00_</span><span>)</span>
<span>plotMeasurement</span><span>(</span><span>measurement</span><span>(</span><span>b</span><span>)</span><span>)</span></code></pre>
<p><img><a target="_blank" rel="attachment wp-att-4730" href="https://gigadom.wordpress.com/2016/06/23/introducing-qcsimulator-a-5-qubit-quantum-computing-simulator-in-r/fig1-2-2/"><img height="483" alt="fig1-2" width="676" class="wp-image-4730" src="https://gigadom.files.wordpress.com/2016/06/fig1-2.png?w=676&h=483"></a></p>
<h3>8. Measurement</h3>
</div>
<div>
<p>The measurement of a Quantum circuit can be obtained using the measurement function. Consider the following Quantum circuit<br>
Q0 – H-T-H-T-S-H-T-H-T-H-T-H-S-Measurement where H – Hadamard gate, T – T Gate and S- S Gate</p>
<pre><code><span>a</span> <span>=</span> <span>SGate</span><span>(</span><span>Hadamard</span><span>(</span><span>TGate</span><span>(</span><span>Hadamard</span><span>(</span><span>TGate</span><span>(</span><span>Hadamard</span><span>(</span><span>TGate</span><span>(</span><span>Hadamard</span><span>(</span><span>SGate</span><span>(</span><span>TGate</span><span>(</span><span>Hadamard</span><span>(</span><span>TGate</span><span>(</span><span>Hadamard</span><span>(</span><span>I2</span><span>)</span><span>)</span><span>)</span><span>)</span><span>)</span><span>)</span><span>)</span><span>)</span><span>)</span><span>)</span><span>)</span><span>)</span><span>)</span>
<span>measurement</span><span>(</span><span>a</span><span>)</span></code></pre>
<pre><code>##          0        1
## v 0.890165 0.109835</code></pre>
</div>
<div>
<h3>9. Plot measurement</h3>
<p>Using the same example as above Q0 – H-T-H-T-S-H-T-H-T-H-T-H-S-Measurement where H – Hadamard gate, T – T Gate and S- S Gate we can plot the measurement</p>
<pre><code><span>a</span> <span>=</span> <span>SGate</span><span>(</span><span>Hadamard</span><span>(</span><span>TGate</span><span>(</span><span>Hadamard</span><span>(</span><span>TGate</span><span>(</span><span>Hadamard</span><span>(</span><span>TGate</span><span>(</span><span>Hadamard</span><span>(</span><span>SGate</span><span>(</span><span>TGate</span><span>(</span><span>Hadamard</span><span>(</span><span>TGate</span><span>(</span><span>Hadamard</span><span>(</span><span>I2</span><span>)</span><span>)</span><span>)</span><span>)</span><span>)</span><span>)</span><span>)</span><span>)</span><span>)</span><span>)</span><span>)</span><span>)</span><span>)</span>
<span>result</span> <span>=</span> <span>measurement</span><span>(</span><span>a</span><span>)</span>
<span>plotMeasurement</span><span>(</span><span>result</span><span>)</span></code></pre>
<p><img><a target="_blank" rel="attachment wp-att-4731" href="https://gigadom.wordpress.com/2016/06/23/introducing-qcsimulator-a-5-qubit-quantum-computing-simulator-in-r/fig2-1/"><img height="483" alt="fig2-1" width="676" class="wp-image-4731" src="https://gigadom.files.wordpress.com/2016/06/fig2-1.png?w=676&h=483"></a></p>
</div>
<div>
<h3>10. Evaluating a Quantum Circuit</h3>
<p>The above procedures for evaluating a quantum gates in series and parallel can be used to evalute more complex quantum circuits where the quantum gates are in series and in parallel.</p>
<p>Here is an evaluation of one such circuit, the Bell ZQ state using the QCSimulator (from IBM’s Quantum Experience)</p>
<p><a target="_blank" rel="attachment wp-att-4736" href="https://gigadom.wordpress.com/2016/06/23/introducing-qcsimulator-a-5-qubit-quantum-computing-simulator-in-r/pic3/"><img height="380" alt="pic3" width="676" class="wp-image-4736" src="https://gigadom.files.wordpress.com/2016/06/pic3.png?w=676&h=380"></a></p>
<pre><code><span># 1st composite</span>
<span>a</span> <span>=</span> <span>TensorProd</span><span>(</span><span>Hadamard</span><span>(</span><span>I2</span><span>)</span>,<span>I2</span><span>)</span>
<span># Output of CNOT</span>
<span>b</span> <span>=</span> <span>CNOT2_01</span><span>(</span><span>a</span><span>)</span>
<span># 2nd series</span>
<span>c</span><span>=</span><span>Hadamard</span><span>(</span><span>TGate</span><span>(</span><span>Hadamard</span><span>(</span><span>SGate</span><span>(</span><span>I2</span><span>)</span><span>)</span><span>)</span><span>)</span>
<span>#3rd composite</span>
<span>d</span><span>=</span> <span>TensorProd</span><span>(</span><span>I2</span>,<span>c</span><span>)</span>
<span># Output of 2nd composite</span>
<span>e</span> <span>=</span> <span>DotProduct</span><span>(</span><span>b</span>,<span>d</span><span>)</span>
<span>#Action of quantum circuit on |00></span>
<span>f</span> <span>=</span> <span>DotProduct</span><span>(</span><span>e</span>,<span>q00_</span><span>)</span>
<span>result</span><span>=</span> <span>measurement</span><span>(</span><span>f</span><span>)</span>
<span>plotMeasurement</span><span>(</span><span>result</span><span>)</span></code></pre>
<p><img><a target="_blank" rel="attachment wp-att-4732" href="https://gigadom.wordpress.com/2016/06/23/introducing-qcsimulator-a-5-qubit-quantum-computing-simulator-in-r/fig3-1/"><img height="483" alt="fig3-1" width="676" class="wp-image-4732" src="https://gigadom.files.wordpress.com/2016/06/fig3-1.png?w=676&h=483"></a></p>
</div>
<div>
<h3>11. Toffoli State</h3>
<p>This circuit for this comes from IBM’s Quantum Experience. This circuit is available in the package. This is how the state was constructed. This circuit is shown below</p>
<p><a target="_blank" rel="attachment wp-att-4737" href="https://gigadom.wordpress.com/2016/06/23/introducing-qcsimulator-a-5-qubit-quantum-computing-simulator-in-r/pic2-3/"><img height="380" alt="pic2" width="676" class="wp-image-4737" src="https://gigadom.files.wordpress.com/2016/06/pic2.png?w=676&h=380"></a></p>
<p>The implementation of the above circuit in QCSimulator is as below</p>
<pre><code>  <span># Computation of the Toffoli State</span>
    <span>H</span><span>=</span><span>1</span><span>/</span><span>sqrt</span><span>(</span><span>2</span><span>)</span> <span>*</span> <span>matrix</span><span>(</span><span>c</span><span>(</span><span>1</span>,<span>1</span>,<span>1</span>,<span>-</span><span>1</span><span>)</span>,<span>nrow</span><span>=</span><span>2</span>,<span>ncol</span><span>=</span><span>2</span><span>)</span>
    <span>I</span><span>=</span><span>matrix</span><span>(</span><span>c</span><span>(</span><span>1</span>,<span>0</span>,<span>0</span>,<span>1</span><span>)</span>,<span>nrow</span><span>=</span><span>2</span>,<span>ncol</span><span>=</span><span>2</span><span>)</span>

    <span># 1st composite</span>
    <span># H x H x H</span>
    <span>a</span> <span>=</span> <span>TensorProd</span><span>(</span><span>TensorProd</span><span>(</span><span>H</span>,<span>H</span><span>)</span>,<span>H</span><span>)</span>
    <span># 1st CNOT</span>
    <span>a1</span><span>=</span> <span>CNOT3_12</span><span>(</span><span>a</span><span>)</span>

    <span># 2nd composite</span>
    <span># I x I x T1Gate</span>
    <span>b</span> <span>=</span> <span>TensorProd</span><span>(</span><span>TensorProd</span><span>(</span><span>I</span>,<span>I</span><span>)</span>,<span>T1Gate</span><span>(</span><span>I</span><span>)</span><span>)</span>
    <span>b1</span> <span>=</span> <span>DotProduct</span><span>(</span><span>b</span>,<span>a1</span><span>)</span>
    <span>c</span> <span>=</span> <span>CNOT3_02</span><span>(</span><span>b1</span><span>)</span>

    <span># 3rd composite</span>
    <span># I x I x TGate</span>
    <span>d</span> <span>=</span> <span>TensorProd</span><span>(</span><span>TensorProd</span><span>(</span><span>I</span>,<span>I</span><span>)</span>,<span>TGate</span><span>(</span><span>I</span><span>)</span><span>)</span>
    <span>d1</span> <span>=</span> <span>DotProduct</span><span>(</span><span>d</span>,<span>c</span><span>)</span>
    <span>e</span> <span>=</span> <span>CNOT3_12</span><span>(</span><span>d1</span><span>)</span>

    <span># 4th composite</span>
    <span># I x I x T1Gate</span>
    <span>f</span> <span>=</span> <span>TensorProd</span><span>(</span><span>TensorProd</span><span>(</span><span>I</span>,<span>I</span><span>)</span>,<span>T1Gate</span><span>(</span><span>I</span><span>)</span><span>)</span>
    <span>f1</span> <span>=</span> <span>DotProduct</span><span>(</span><span>f</span>,<span>e</span><span>)</span>
    <span>g</span> <span>=</span> <span>CNOT3_02</span><span>(</span><span>f1</span><span>)</span>

    <span>#5th composite</span>
    <span># I x T x T</span>
    <span>h</span> <span>=</span> <span>TensorProd</span><span>(</span><span>TensorProd</span><span>(</span><span>I</span>,<span>TGate</span><span>(</span><span>I</span><span>)</span><span>)</span>,<span>TGate</span><span>(</span><span>I</span><span>)</span><span>)</span>
    <span>h1</span> <span>=</span> <span>DotProduct</span><span>(</span><span>h</span>,<span>g</span><span>)</span>
    <span>i</span> <span>=</span> <span>CNOT3_12</span><span>(</span><span>h1</span><span>)</span>

    <span>#6th composite</span>
    <span># I x H x H</span>
    <span>j</span> <span>=</span> <span>TensorProd</span><span>(</span><span>TensorProd</span><span>(</span><span>I</span>,<span>Hadamard</span><span>(</span><span>I</span><span>)</span><span>)</span>,<span>Hadamard</span><span>(</span><span>I</span><span>)</span><span>)</span>
    <span>j1</span> <span>=</span> <span>DotProduct</span><span>(</span><span>j</span>,<span>i</span><span>)</span>
    <span>k</span> <span>=</span> <span>CNOT3_12</span><span>(</span><span>j1</span><span>)</span>

    <span># 7th composite</span>
    <span># I x H x H</span>
    <span>l</span> <span>=</span> <span>TensorProd</span><span>(</span><span>TensorProd</span><span>(</span><span>I</span>,<span>Hadamard</span><span>(</span><span>I</span><span>)</span><span>)</span>,<span>Hadamard</span><span>(</span><span>I</span><span>)</span><span>)</span>
    <span>l1</span> <span>=</span> <span>DotProduct</span><span>(</span><span>l</span>,<span>k</span><span>)</span>
    <span>m</span> <span>=</span> <span>CNOT3_12</span><span>(</span><span>l1</span><span>)</span>
    <span>n</span> <span>=</span> <span>CNOT3_02</span><span>(</span><span>m</span><span>)</span>

    <span>#8th composite</span>
    <span># T x H x T1</span>
    <span>o</span> <span>=</span> <span>TensorProd</span><span>(</span><span>TensorProd</span><span>(</span><span>TGate</span><span>(</span><span>I</span><span>)</span>,<span>Hadamard</span><span>(</span><span>I</span><span>)</span><span>)</span>,<span>T1Gate</span><span>(</span><span>I</span><span>)</span><span>)</span>
    <span>o1</span> <span>=</span> <span>DotProduct</span><span>(</span><span>o</span>,<span>n</span><span>)</span>
    <span>p</span> <span>=</span> <span>CNOT3_02</span><span>(</span><span>o1</span><span>)</span>
    <span>result</span> <span>=</span> <span>measurement</span><span>(</span><span>p</span><span>)</span>
    <span>plotMeasurement</span><span>(</span><span>result</span><span>)</span></code></pre>
<p><img><a target="_blank" rel="attachment wp-att-4733" href="https://gigadom.wordpress.com/2016/06/23/introducing-qcsimulator-a-5-qubit-quantum-computing-simulator-in-r/fig4-1/"><img height="483" alt="fig4-1" width="676" class="wp-image-4733" src="https://gigadom.files.wordpress.com/2016/06/fig4-1.png?w=676&h=483"></a></p>
</div>
<div>
<h3>12. GHZ YYX measurement</h3>
<p>Here is another Quantum circuit, namely the entangled GHZ YYX state. This is</p>
<p><a target="_blank" rel="attachment wp-att-4738" href="https://gigadom.wordpress.com/2016/06/23/introducing-qcsimulator-a-5-qubit-quantum-computing-simulator-in-r/pic1-2/"><img height="380" alt="pic1" width="676" class="wp-image-4738" src="https://gigadom.files.wordpress.com/2016/06/pic1.png?w=676&h=380"></a></p>
<p>and is implemented in QCSimulator as</p>
<pre><code><span># Composite 1</span>
<span>a</span> <span>=</span> <span>TensorProd</span><span>(</span><span>TensorProd</span><span>(</span><span>Hadamard</span><span>(</span><span>I2</span><span>)</span>,<span>Hadamard</span><span>(</span><span>I2</span><span>)</span><span>)</span>,<span>PauliX</span><span>(</span><span>I2</span><span>)</span><span>)</span>
<span>b</span><span>=</span> <span>CNOT3_12</span><span>(</span><span>a</span><span>)</span>
<span>c</span><span>=</span> <span>CNOT3_02</span><span>(</span><span>b</span><span>)</span>
<span># Composite 2</span>
<span>d</span><span>=</span> <span>TensorProd</span><span>(</span><span>TensorProd</span><span>(</span><span>Hadamard</span><span>(</span><span>I2</span><span>)</span>,<span>Hadamard</span><span>(</span><span>I2</span><span>)</span><span>)</span>,<span>Hadamard</span><span>(</span><span>I2</span><span>)</span><span>)</span>
<span>e</span><span>=</span> <span>DotProduct</span><span>(</span><span>d</span>,<span>c</span><span>)</span>
<span>#Composite 3</span>
<span>f</span><span>=</span> <span>TensorProd</span><span>(</span><span>TensorProd</span><span>(</span><span>S1Gate</span><span>(</span><span>I2</span><span>)</span>,<span>S1Gate</span><span>(</span><span>I2</span><span>)</span><span>)</span>,<span>Hadamard</span><span>(</span><span>I2</span><span>)</span><span>)</span>
<span>g</span><span>=</span> <span>DotProduct</span><span>(</span><span>f</span>,<span>e</span><span>)</span>
<span>#Composite 4</span>
<span>i</span><span>=</span> <span>TensorProd</span><span>(</span><span>TensorProd</span><span>(</span><span>Hadamard</span><span>(</span><span>I2</span><span>)</span>,<span>Hadamard</span><span>(</span><span>I2</span><span>)</span><span>)</span>,<span>I2</span><span>)</span>
<span>j</span> <span>=</span> <span>DotProduct</span><span>(</span><span>i</span>,<span>g</span><span>)</span>
<span>result</span><span>=</span><span>measurement</span><span>(</span><span>j</span><span>)</span>
<span>plotMeasurement</span><span>(</span><span>result</span><span>)</span></code></pre>
<p><img><a target="_blank" rel="attachment wp-att-4734" href="https://gigadom.wordpress.com/2016/06/23/introducing-qcsimulator-a-5-qubit-quantum-computing-simulator-in-r/fig5-1/"><img height="483" alt="fig5-1" width="676" class="wp-image-4734" src="https://gigadom.files.wordpress.com/2016/06/fig5-1.png?w=676&h=483"></a></p>
</div>
</div>
<div>
<h2>Conclusion</h2>
<p>The 5 qubit Quantum Computing Simulator is now fully functional. I hope to add more gates and functionality in the months to come.</p>
<p>Feel free to install the package from Github and give it a try.</p>
<p><em>Disclaimer: This article represents the author’s viewpoint only and doesn’t necessarily represent IBM’s positions, strategies or opinions</em></p>
<p><em>References</em></p>
<ol>
<li><a target="_blank" href="https://quantumexperience.ng.bluemix.net/">IBM’s Quantum Experience</a></li>
<li><a target="_blank" href="https://github.com/corbett/QuantumComputing">Quantum Computing in Python by Dr. Christine Corbett Moran</a></li>
<li><a target="_blank" href="http://www1.gantep.edu.tr/~koc/qc/chapter4.pdf">Lecture notes-1</a></li>
<li><a target="_blank" href="http://iontrap.umd.edu/wp-content/uploads/2016/01/Quantum-Gates-c2.pdf">Lecture notes-2</a></li>
<li><a target="_blank" href="https://www.edx.org/course/quantum-mechanics-quantum-computation-uc-berkeleyx-cs-191x">Quantum Mechanics and Quantum Computationat edX- UC, Berkeley</a></li>
</ol>
<p><strong>My other posts on Quantum Computing</strong></p>
<ol>
<li><a target="_blank" href="https://gigadom.wordpress.com/2016/05/18/venturing-into-ibms-quantum-computing-experience/">Venturing into IBM’s Quantum Experience</a> 2.<a target="_blank" href="https://gigadom.wordpress.com/2016/05/24/going-deeper-into-ibms-quantum-experience/">Going deeper into IBM’s Quantum Experience!</a></li>
<li><a target="_blank" href="https://gigadom.wordpress.com/2016/06/01/a-primer-on-qubits-quantum-gates-and-quantum-operations/">A primer on Qubits, Quantum gates and Quantum Operations</a></li>
<li><a target="_blank" href="https://gigadom.wordpress.com/2016/06/05/exploring-quantum-gate-operations-with-qcsimulator/">Exploring Quantum Gate operations with QCSimulator</a></li>
<li><a target="_blank" href="https://gigadom.wordpress.com/2016/06/15/taking-a-closer-look-at-quantum-gates-and-their-operations/">Taking a closer look at Quantum gates and their operations</a></li>
</ol>
<p><strong>You may also like</strong><br>
For more posts on other topics like Cloud Computing, IBM Bluemix, Distributed Computing, OpenCV, R, cricket please check my <a target="_blank" href="https://gigadom.wordpress.com/aa-2/">Index of posts</a></p>
</div>
<div></div>
<p> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gigadom.wordpress.com/4725/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/gigadom.wordpress.com/4725/" border="0"></a> <img height="1" alt="" width="1" src="https://pixel.wp.com/b.gif?host=gigadom.wordpress.com&blog=14647342&%23038;post=4725&%23038;subd=gigadom&%23038;ref=&%23038;feed=1" border="0"></p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="http://www.r-bloggers.com/introducing-qcsimulator-a-5-qubit-quantum-computing-simulator-in-r/"></div></div><div><a data-url="http://www.r-bloggers.com/introducing-qcsimulator-a-5-qubit-quantum-computing-simulator-in-r/" data-counturl="http://www.r-bloggers.com/introducing-qcsimulator-a-5-qubit-quantum-computing-simulator-in-r/" data-count="vertical" data-text="Introducing QCSimulator: A 5-qubit quantum computing simulator in R" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://gigadom.wordpress.com/2016/06/23/introducing-qcsimulator-a-5-qubit-quantum-computing-simulator-in-r/"> R – Giga thoughts …</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/T5GlRBrIsqI"></a></div>