Introducing QCSimulator: A 5-qubit quantum computing simulator in R


Introduction

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.

QCSimulator is now on CRAN!!!

The code for the QCSimulator package is also available at Github QCSimulator. This post has also been published at Rpubs as QCSimulator and can be downloaded as a PDF document at QCSimulator.pdf

Disclaimer: This article represents the author’s viewpoint only and doesn’t necessarily represent IBM’s positions, strategies or opinions

install.packages("QCSimulator")
library(QCSimulator)
library(ggplot2)

1. Initialize the environment and set global variables

Here I initialize the environment with global variables and then display a few of them.

rm(list=ls())
#Call the init function to initialize the environment and create global variables
init()

# Display some of global variables in environment
ls()
##  [1] "I16"     "I2"      "I4"      "I8"      "q0_"     "q00_"    "q000_"  
##  [8] "q0000_"  "q00000_" "q00001_" "q0001_"  "q00010_" "q00011_" "q001_"  
## [15] "q0010_"  "q00100_" "q00101_" "q0011_"  "q00110_" "q00111_" "q01_"   
## [22] "q010_"   "q0100_"  "q01000_" "q01001_" "q0101_"  "q01010_" "q01011_"
## [29] "q011_"   "q0110_"  "q01100_" "q01101_" "q0111_"  "q01111_" "q1_"    
## [36] "q10_"    "q100_"   "q1000_"  "q10000_" "q10001_" "q1001_"  "q10010_"
## [43] "q10011_" "q101_"   "q1010_"  "q10100_" "q10101_" "q1011_"  "q10110_"
## [50] "q10111_" "q11_"    "q110_"   "q1100_"  "q11000_" "q11001_" "q1101_" 
## [57] "q11010_" "q11011_" "q111_"   "q1110_"  "q11100_" "q11101_" "q1111_" 
## [64] "q11110_" "q11111_"
#1. 2 x 2 Identity matrix 
I2
##      [,1] [,2]
## [1,]    1    0
## [2,]    0    1
#2. 8 x 8 Identity matrix 
I8
##      [,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
#3. Qubit |00>
q00_
##      [,1]
## [1,]    1
## [2,]    0
## [3,]    0
## [4,]    0
#4. Qubit |010>
q010_
##      [,1]
## [1,]    0
## [2,]    0
## [3,]    1
## [4,]    0
## [5,]    0
## [6,]    0
## [7,]    0
## [8,]    0
#5. Qubit |0100>
q0100_
##       [,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
#6. Qubit 10010
q10010_
##       [,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

The QCSimulator implements the following gates

  1. Pauli X,Y,Z, S,S’, T, T’ gates
  2. Rotation , Hadamard,CSWAP,Toffoli gates
  3. 2,3,4,5 qubit CNOT gates e.g CNOT2_01,CNOT3_20,CNOT4_13 etc
  4. Toffoli State,SWAPQ0Q1

2. To display the unitary matrix of gates

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 & 3 qubit CNOT gates require a 4 x 4 and 8 x 8 identity matrix respectively

PauliX(I2)
##      [,1] [,2]
## [1,]    0    1
## [2,]    1    0
Hadamard(I2)
##           [,1]       [,2]
## [1,] 0.7071068  0.7071068
## [2,] 0.7071068 -0.7071068
S1Gate(I2)
##      [,1] [,2]
## [1,] 1+0i 0+0i
## [2,] 0+0i 0-1i
CNOT2_10(I4)
##      [,1] [,2] [,3] [,4]
## [1,]    1    0    0    0
## [2,]    0    0    0    1
## [3,]    0    0    1    0
## [4,]    0    1    0    0
CNOT3_20(I8)
##      [,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

3. Compute the inner product of vectors

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

phi = matrix(c(1/2,sqrt(3)/2),nrow=2,ncol=1)
si = matrix(c(1/sqrt(2),1/sqrt(2)),nrow=2,ncol=1)
angle= innerProduct(phi,si)
cat("Angle between vectors is:",angle)
## Angle between vectors is: 15

4. Compute the dagger function for a gate

The gate dagger computes and displays the transpose of the complex conjugate of the matrix

TGate(I2)
##      [,1]                 [,2]
## [1,] 1+0i 0.0000000+0.0000000i
## [2,] 0+0i 0.7071068+0.7071068i
GateDagger(TGate(I2))
##      [,1]                 [,2]
## [1,] 1+0i 0.0000000+0.0000000i
## [2,] 0+0i 0.7071068-0.7071068i

5. Invoking gates in series

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.

# Call in reverse order
# Superposition states
# |+> state
Hadamard(q0_)
##           [,1]
## [1,] 0.7071068
## [2,] 0.7071068
# |-> ==> H x Z 
PauliZ(Hadamard(q0_))
##            [,1]
## [1,]  0.7071068
## [2,] -0.7071068
# (+i) Y ==> H x  S 
 SGate(Hadamard(q0_))
##                      [,1]
## [1,] 0.7071068+0.0000000i
## [2,] 0.0000000+0.7071068i
# (-i)Y ==> H x S1
 S1Gate(Hadamard(q0_))
##                      [,1]
## [1,] 0.7071068+0.0000000i
## [2,] 0.0000000-0.7071068i
# Q1 -- TGate- Hadamard
Q1 = Hadamard(TGate(I2))

6. More gates in series

TGate of depth 2

The Quantum circuit for a TGate of Depth 2 is

Q0 — Hadamard-TGate-Hadamard-TGate-SGate-Measurement as shown in IBM’s Quantum Experience Composer

Untitled

Implementing the quantum gates in series in reverse order we have

# Invoking this in reverse order we get
a = SGate(TGate(Hadamard(TGate(Hadamard(q0_)))))
result=measurement(a)

plotMeasurement(result)

fig0-1

7. Invoking gates in parallel

To obtain the results of gates in parallel we have to take the Tensor Product Note:In the TensorProduct invocation the Identity matrix is passed as an argument to get the unitary matrix of the gate. Q0 – Hadamard-Measurement Q1 – Identity- Measurement

# 
a = TensorProd(Hadamard(I2),I2)
b = DotProduct(a,q00_)
plotMeasurement(measurement(b))

fig1-1

a = TensorProd(PauliZ(I2),Hadamard(I2))
b = DotProduct(a,q00_)
plotMeasurement(measurement(b))

fig1-2

8. Measurement

The measurement of a Quantum circuit can be obtained using the measurement function. Consider the following Quantum circuit
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

a = SGate(Hadamard(TGate(Hadamard(TGate(Hadamard(TGate(Hadamard(SGate(TGate(Hadamard(TGate(Hadamard(I2)))))))))))))
measurement(a)
##          0        1
## v 0.890165 0.109835

9. Plot measurement

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

a = SGate(Hadamard(TGate(Hadamard(TGate(Hadamard(TGate(Hadamard(SGate(TGate(Hadamard(TGate(Hadamard(I2)))))))))))))
result = measurement(a)
plotMeasurement(result)

fig2-1

10. Evaluating a Quantum Circuit

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.

Here is an evaluation of one such circuit, the Bell ZQ state using the QCSimulator (from IBM’s Quantum Experience)

pic3

# 1st composite
a = TensorProd(Hadamard(I2),I2)
# Output of CNOT
b = CNOT2_01(a)
# 2nd series
c=Hadamard(TGate(Hadamard(SGate(I2))))
#3rd composite
d= TensorProd(I2,c)
# Output of 2nd composite
e = DotProduct(b,d)
#Action of quantum circuit on |00>
f = DotProduct(e,q00_)
result= measurement(f)
plotMeasurement(result)

fig3-1

11. Toffoli State

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

pic2

The implementation of the above circuit in QCSimulator is as below

  # Computation of the Toffoli State
    H=1/sqrt(2) * matrix(c(1,1,1,-1),nrow=2,ncol=2)
    I=matrix(c(1,0,0,1),nrow=2,ncol=2)

    # 1st composite
    # H x H x H
    a = TensorProd(TensorProd(H,H),H)
    # 1st CNOT
    a1= CNOT3_12(a)

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

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

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

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

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

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

    #8th composite
    # T x H x T1
    o = TensorProd(TensorProd(TGate(I),Hadamard(I)),T1Gate(I))
    o1 = DotProduct(o,n)
    p = CNOT3_02(o1)
    result = measurement(p)
    plotMeasurement(result)

fig4-1

12. GHZ YYX measurement

Here is another Quantum circuit, namely the entangled GHZ YYX state. This is

pic1

and is implemented in QCSimulator as

# Composite 1
a = TensorProd(TensorProd(Hadamard(I2),Hadamard(I2)),PauliX(I2))
b= CNOT3_12(a)
c= CNOT3_02(b)
# Composite 2
d= TensorProd(TensorProd(Hadamard(I2),Hadamard(I2)),Hadamard(I2))
e= DotProduct(d,c)
#Composite 3
f= TensorProd(TensorProd(S1Gate(I2),S1Gate(I2)),Hadamard(I2))
g= DotProduct(f,e)
#Composite 4
i= TensorProd(TensorProd(Hadamard(I2),Hadamard(I2)),I2)
j = DotProduct(i,g)
result=measurement(j)
plotMeasurement(result)

fig5-1

Conclusion

The 5 qubit Quantum Computing Simulator is now fully functional. I hope to add more gates and functionality in the months to come.

Feel free to install the package from Github and give it a try.

Disclaimer: This article represents the author’s viewpoint only and doesn’t necessarily represent IBM’s positions, strategies or opinions

References

  1. IBM’s Quantum Experience
  2. Quantum Computing in Python by Dr. Christine Corbett Moran
  3. Lecture notes-1
  4. Lecture notes-2
  5. Quantum Mechanics and Quantum Computationat edX- UC, Berkeley

My other posts on Quantum Computing

  1. Venturing into IBM’s Quantum Experience 2.Going deeper into IBM’s Quantum Experience!
  2. A primer on Qubits, Quantum gates and Quantum Operations
  3. Exploring Quantum Gate operations with QCSimulator
  4. Taking a closer look at Quantum gates and their operations

You may also like
For more posts on other topics like Cloud Computing, IBM Bluemix, Distributed Computing, OpenCV, R, cricket please check my Index of posts

Taking a closer look at Quantum gates and their operations


This post is a continuation of my earlier post ‘Exploring Quantum gate operations with QCSimulator’. Here I take a closer look at more quantum gates and their operations, besides implementing these new gates in my Quantum Computing simulator, the  QCSimulator in R.

Disclaimer: This article represents the author’s viewpoint only and doesn’t necessarily represent IBM’s positions, strategies or opinions

In  quantum circuits, gates  are unitary matrices which operate on 1,2 or 3 qubit systems which are represented as below

1 qubit
|0> = \begin{pmatrix}1\\0\end{pmatrix} and |1> = \begin{pmatrix}0\\1\end{pmatrix}

2 qubits
|0> \otimes |0> = \begin{pmatrix}1\\ 0\\ 0\\0\end{pmatrix}
|0> \otimes |1> = \begin{pmatrix}0\\ 1\\ 0\\0\end{pmatrix}
|1> \otimes |o> = \begin{pmatrix}0\\ 0\\ 1\\0\end{pmatrix}
|1> \otimes |1> = \begin{pmatrix}0\\ 0\\ 0\\1\end{pmatrix}

3 qubits
|0> \otimes |0> \otimes |0> = \begin{pmatrix}1\\ 0\\0\\ 0\\ 0\\0\\ 0\\0\end{pmatrix}
|0> \otimes |0> \otimes |1> = \begin{pmatrix}0\\ 1\\0\\ 0\\ 0\\0\\ 0\\0\end{pmatrix}
|0> \otimes |1> \otimes |0> = \begin{pmatrix}0\\ 0\\1\\ 0\\ 0\\0\\ 0\\0\end{pmatrix}


|1> \otimes |1> \otimes |1> = \begin{pmatrix}0\\ 0\\0\\ 0\\ 0\\0\\ 0\\1\end{pmatrix}
Hence single qubit is represented as 2 x 1 matrix, 2 qubit as 4 x 1 matrix and 3 qubit as 8 x 1 matrix

1) Composing Quantum gates in series
When quantum gates are connected in a series. The overall effect of the these quantum gates in series is obtained my taking the dot product of the unitary gates in reverse. For e.g.
Untitled

In the following picture the effect of the quantum gates A,B,C is the dot product of the gates taken reverse order
result = C . B . A

This overall action of the 3 quantum gates can be represented by a single ‘transfer’ matrix which is the dot product of the gates
Untitled

If we had a Pauli X followed by a Hadamard gate the combined effect of these gates on the inputs can be deduced by constructing a truth table

Input Pauli X – Output A’ Hadamard – Output B
|0> |1> 1/√2(|0>  – |1>)
|1> |0> 1/√2(|0>  + |1>)

Or

|0> -> 1/√2(|0>  – |1>)
|1> -> 1/√2(|0>  + |1>)
which is
\begin{pmatrix}1\\0\end{pmatrix}  ->1/√2 \begin{pmatrix}1\\0\end{pmatrix}\begin{pmatrix}0\\1\end{pmatrix} = 1/√2  \begin{pmatrix}1\\-1\end{pmatrix}
\begin{pmatrix}0\\1\end{pmatrix}  ->1/√2 \begin{pmatrix}1\\0\end{pmatrix} + \begin{pmatrix}0\\1\end{pmatrix} = 1/√2  \begin{pmatrix}1\\1\end{pmatrix}
Therefore the ‘transfer’ matrix can be written as
T = 1/√2 \begin{pmatrix}1 & 1\\ -1 & 1\end{pmatrix}

2)Quantum gates in parallel
When quantum gates are in parallel then the composite effect of the gates can be obtained by taking the tensor product of the quantum gates.
Untitled

If we consider the combined action of a Pauli X gate and a Hadamard gate in parallel
Untitled

A B A’ B’
|0> |0> |1> 1/√2(|0>  + |1>)
|0> |1> |1> 1/√2(|0>  – |1>)
|1> |0> |0> 1/√2(|0>  + |1>)
|1> |1> |0> 1/√2(|0>  – |1>)

Or

|00> => |1> \otimes 1/√2(|0>  + |1>) = 1/√2 (|10> + |11>)
|01> => |1> \otimes 1/√2(|0>  – |1>) = 1/√2 (|10> – |11>)
|10> => |0> \otimes 1/√2(|0>  + |1>) = 1/√2 (|00> + |01>)
|11> => |0> \otimes 1/√2(|0>  – |1>) = 1/√2 (|10> – |11>)

|00> = \begin{pmatrix}1\\ 0\\ 0\\0\end{pmatrix} =>1/√2\begin{pmatrix} 0\\ 0\\ 1\\ 1\end{pmatrix}
|01> = \begin{pmatrix}0\\ 1\\ 0\\0\end{pmatrix} =>1/√2\begin{pmatrix} 0\\ 0\\ 1\\ -1\end{pmatrix}
|10> = \begin{pmatrix}0\\ 0\\ 1\\0\end{pmatrix} =>1/√2\begin{pmatrix} 1\\ 0\\ 1\\ -1\end{pmatrix}
|11> = \begin{pmatrix}0\\ 0\\ 0\\1\end{pmatrix} =>1/√2\begin{pmatrix} 1\\ 0\\ -1\\ -1\end{pmatrix}

Here are more Quantum gates
a) Rotation gate
U = \begin{pmatrix}cos\theta & -sin\theta\\ sin\theta & cos\theta\end{pmatrix}

b) Toffoli gate
The Toffoli gate flips the 3rd qubit if the 1st and 2nd qubit are |1>

Toffoli gate
Input Output
|000> |000>
|001> |001>
|010> |010>
|011> |011>
|100> |100>
|101> |101>
|110> |111>
|111> |110>

c) Fredkin gate
The Fredkin gate swaps the 2nd and 3rd qubits if the 1st qubit is |1>

Fredkin gate
Input Output
|000> |000>
|001> |001>
|010> |010>
|011> |011>
|100> |100>
|101> |110>
|110> |101>
|111> |111>

d) Controlled U gate
A controlled U gate can be represented as
controlled U = \begin{pmatrix}1 & 0 & 0 & 0\\ 0 &1  &0  & 0\\ 0 &0  &u11  &u12 \\ 0 & 0 &u21  &u22 \end{pmatrix}   – (A)
where U =  \begin{pmatrix}u11 &u12 \\ u21 & u22\end{pmatrix}

e) Controlled Pauli gates
Controlled Pauli gates are created based on the following identities. The CNOT gate is a controlled Pauli X gate where controlled U is a Pauli X gate and matches the CNOT unitary matrix. Pauli gates can be constructed using

a) H x X x H = Z    &
H x H = I

b) S x X x S1
S x S1 = I

the controlled Pauli X, Y , Z are contructed using the CNOT for the controlled X in the above identities
In general a controlled Pauli gate can be created as below
Untitled

f) CPauliX
Here C is the 2 x2  Identity matrix. Simulating this in my QCSimulator
CPauliX I=matrix(c(1,0,0,1),nrow=2,ncol=2)
# Compute 1st composite
a = TensorProd(I,I)
b = CNOT2_01(a)
# Compute 1st composite
c = TensorProd(I,I)
#Take dot product
d = DotProduct(c,b)
#Take dot product with qubit
e = DotProduct(d,q)
e
}

Implementing the above with I, S, H gives Pauli X, Y and Z as seen below

library(QCSimulator)
I4=matrix(c(1,0,0,0,
            0,1,0,0,
            0,0,1,0,
            0,0,0,1),nrow=4,ncol=4)

#Controlled Pauli X
CPauliX(I4)
##      [,1] [,2] [,3] [,4]
## [1,]    1    0    0    0
## [2,]    0    1    0    0
## [3,]    0    0    0    1
## [4,]    0    0    1    0
#Controlled Pauli Y
CPauliY(I4)
##      [,1] [,2] [,3] [,4]
## [1,] 1+0i 0+0i 0+0i 0+0i
## [2,] 0+0i 1+0i 0+0i 0+0i
## [3,] 0+0i 0+0i 0+0i 0-1i
## [4,] 0+0i 0+0i 0+1i 0+0i
#Controlled Pauli Z
CPauliZ(I4)
##      [,1] [,2] [,3] [,4]
## [1,]    1    0    0    0
## [2,]    0    1    0    0
## [3,]    0    0    1    0
## [4,]    0    0    0   -1

g) CSWAP gate

Untitled

q00=matrix(c(1,0,0,0),nrow=4,ncol=1)
q01=matrix(c(0,1,0,0),nrow=4,ncol=1)
q10=matrix(c(0,0,1,0),nrow=4,ncol=1)
q11=matrix(c(0,0,0,1),nrow=4,ncol=1)
CSWAP(q00)
##      [,1]
## [1,]    1
## [2,]    0
## [3,]    0
## [4,]    0
#Swap qubits 
CSWAP(q01)
##      [,1]
## [1,]    0
## [2,]    0
## [3,]    1
## [4,]    0
#Swap qubits 
CSWAP(q10)
##      [,1]
## [1,]    0
## [2,]    1
## [3,]    0
## [4,]    0
CSWAP(q11)
##      [,1]
## [1,]    0
## [2,]    0
## [3,]    0
## [4,]    1

h) Toffoli state
The Toffoli state creates a 3 qubit entangled state 1/2(|000> + |001> + |100> + |111>)
Untitled

Simulating the Toffoli state in IBM Quantum Experience we get
Untitled

h) Implementation of Toffoli state in QCSimulator 

#ToffoliState 
    # Computation of the Toffoli State
    H=1/sqrt(2) * matrix(c(1,1,1,-1),nrow=2,ncol=2)
    I=matrix(c(1,0,0,1),nrow=2,ncol=2)

    # 1st composite
    # H x H x H
    a = TensorProd(TensorProd(H,H),H)
    # 1st CNOT
    a1= CNOT3_12(a)

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

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

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

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

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

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

    #8th composite
    # T x H x T1
    o = TensorProd(TensorProd(TGate(I),Hadamard(I)),T1Gate(I))
    o1 = DotProduct(o,n)
    p = CNOT3_02(o1)
    result = measurement(p)
    plotMeasurement(result)

a-1
The measurement is identical to the that of IBM Quantum Experience

Conclusion:  This post looked at more Quantum gates. I have implemented all the gates in my QCSimulator which I hope to release in a couple of months.

Disclaimer: This article represents the author’s viewpoint only and doesn’t necessarily represent IBM’s positions, strategies or opinions

References
1. http://www1.gantep.edu.tr/~koc/qc/chapter4.pdf
2. http://iontrap.umd.edu/wp-content/uploads/2016/01/Quantum-Gates-c2.pdf
3. https://quantumexperience.ng.bluemix.net/

Also see
1.  Venturing into IBM’s Quantum Experience
2. Going deeper into IBM’s Quantum Experience!
3.  A primer on Qubits, Quantum gates and Quantum Operations
4. Exploring Quantum gate operations with QCSimulator

Take a look at my other posts at
1. Index of posts

Venturing into IBM’s Quantum Experience


UntitledIntroduction: IBM opened the doors of its Quantum Computing Environment, termed “Quantum Experience” to the general public about 10 days back. The access to IBM’s Quantum Experience is through Bluemix service , IBM’s  PaaS (Platform as a Service). So I  signed up for IBM’s quantum experience with great excitement. So here I am, an engineer trying to enter into and understand the weird,weird world of the quantum physicist!

Disclaimer: This article represents the author’s viewpoint only and doesn’t necessarily represent IBM’s positions, strategies or opinions

The idea of Quantum computing was initially mooted by Nobel Laureate Richard Feynman, Yuri Manning and Paul Benioff in the 1980s. While there was some interest in the field for the next several years, work in Quantum computing received a shot in the arm after Peter Shor’s discovery of an efficient quantum algorithm for integer factorization and discrete logarithms.

Problems that are considered to be computationally hard to solve with classical computers can be solved through quantum computing with an exponential improvement in efficiency.  Some areas that are supposed to be key candidates for quantum computing, are quantum money and cryptography.

Quantum computing will become predominant in our futures owing to 2 main reasons. The 1st reason, as already mentioned, is extraordinary performance improvements. The 2nd is due to the process of miniaturization. Ever since the advent of the transistor and the integrated circuit, the advancement in computing has led the relentless pursuit of miniaturization.,  In recent times the number of transistors has increased to such an extent, that quantum effects become apparent,  at such micro levels,  while  making the chips extremely powerful and cheap. The 18 core Xeon Haswell  Inel processoir packs 5.5 billion transistor in 661 mm2

 In classical computers the computation is based on the ‘binary digit’ or ‘bit’ which can be in either state 0 or 1. In quantum computing the unit of computation is the ‘quantum bit’ or the qubit. The quantum bit  can be in the states of 0, 1 and both simultaneously by the principle of superposition.

A qubit is a quantum system consisting of two levels, labeled |0⟩ and |1⟩ (using Dirac’s bracket notation) and is represented by a two-dimensional vector space.

|0>=\begin{pmatrix}1\\ 0\end{pmatrix}

|1>=\begin{pmatrix}0\\ 1\end{pmatrix}

Consider some physical system that can be in N different, mutually exclusive classical states. Then in the classical computing the system can be in one of the 2^{N} states. For e.g. if we had 3 bits the classical computer could be in one of {000,001,010,011,100,101,110,111} states.

A quantum computer takes advantage of a special kind of superposition that allows for exponentially many logical states at once, all the states from |00…0⟩ to |11…1⟩

Hence, the qubit need not be |0> or |1> but can be in any state |Ψ> which can be any superposition |ψ⟩=α|0⟩+β|1⟩, where α and β are the amplitudes. The superposition quantities α and β are complex numbers and obey |α|2+|β|2=1

Let us consider  a system which had N different mutually exclusive states. Using Dirac’s notation these states can be represented as |1>, |2>. . . |N>.

A pure quantum state is a superposition of all these states

Φ = α1 |1> + α2 |2> + …. + αN |N>

Where αi is the amplitude of qubit ‘I’ |i> in Φ. Hence, a system in quantum state |φi is in all classical states at the same time. It is state |1> with an amplitude of α1, in state |2> with an amplitude α2 etc.

A quantum system which is in all states at once can be either measured or allowed to evolve unitarily without measuring

Measurement

The interesting fact is that when we measure the quantum state Φ, the measured state will not be the quantum state Φ, but one  classical state |j> , where |j> is one of the states |1>,|2,.. |N>. The likelihood for the measured state to be |j> is dependent on the probability |αj |2, which is the squared norm of the corresponding amplitude αj. Hence observing the quantum state Φ results in the collapse of the quantum superposition state Φ top a classical state |j> and all the information in the amplitudes αj I

Φ = α1 |1> + α2 |2> + … + αN |N>

Unitary evolution

The other alternative is instead of measuring the quantum state Φ, is to apply a series of unitary operations and allow the quantum system to evolve.

In this post I use IBM’s Quantum Experience. The IBM’s Quantum Experience uses a type of qubit made from superconducting materials such as niobium and aluminum, patterned on a silicon substrate.

For this superconducting qubit to behave as the abstract notion of the qubit, the device is cooled down considerably. In fact, in the IBM Quantum Lab, the temperature is maintained at 5 milliKelvin, in a dilution refrigerator

The Quantum Composer a Graphical User Interface (GUI) for programming the quantum processor. With the quantum composer we can construct quantum circuits using a library of well-defined gates and measurements.

The IBM’s Quantum Composer is designed like a musical staff with 5 horizontal lines for the 5 qubits. Quantum gates can be dragged and dropped on these horizontal lines to operate on the qubits

Quantum gates are represented as unitary matrices, and operations on qubits are matrix operations, and as such require knowledge of linear algebra. It is claimed, that while the math behind quantum computing may not be too hard, the challenge is that certain aspects of quantum computing are counter-intuitive. This should be challenge. I hope that over the next few months I will be able to develop at least some basic understanding for the reason behind the efficiency of quantum algorithms

Pauli gates

The operation of a quantum gate can be represented as a matrix.  A gate that acts on one qubit is represented by a 2×2 unitary matrix. A unitary matrix is one, in which the conjugate transpose of the matrix is also its inverse. Since quantum operations need to be reversible, and preserve probability amplitudes, the matrices must be unitary.

To understand the operations of the gates on the qubits, I have used R language to represent matrices, and to perform the matrix operations. Personally , this made things a lot clearer to me!

Performing measurement

The following picture shows how the qubit state is measured in the Quantum composer

1

Simulation in the Quantum Composer

When the above measurement is simulated in the composer by clicking the ‘Simulate’ button the result is as below

2

This indicates that the measurement will display qubit |0> with a 100% probability or the qubit is in the ‘idle’ state.

A) Pauli operators

A common group of gates are the Pauli operators

a) The Pauli X

|0> ==>  X|0> ==> |1>

The Pauli X gate which is represented as below  does a bit flip

\begin{pmatrix}0&1\\1&0\end{pmatrix}

This can be composed in the Quantum composer as

3

When this simulated the Pauli X gate does a bit flip and the result is

4

which is qubit |1> which comes up as 1 (100% probability)

Pauli operator X using R code

# Qubit '0'
q0=matrix(c(1,0),nrow=2,ncol=1)
q0
##      [,1]
## [1,]    1
## [2,]    0
# Qubit '1'
q1=matrix(c(0,1),nrow=2,ncol=1)
q1
##      [,1]
## [1,]    0
## [2,]    1
# Pauli operator X
X= matrix(c(0,1,1,0),nrow=2,ncol=2)
X
##      [,1] [,2]
## [1,]    0    1
## [2,]    1    0
# Performing a X operation on q0 flips a q0 to q1
a=X%*%q0
a
##      [,1]
## [1,]    0
## [2,]    1

b) Pauli operator Z

The Z operator does a phase flip and is represented by the matrix

\begin{pmatrix}1&0\\0&-1\end{pmatrix}

Simulation in the Quantum composer
8

Pauli operator Z using R code

# Pauli operator Z
Z=matrix(c(1,0,0,-1),nrow=2,ncol=2)
Z
##      [,1] [,2]
## [1,]    1    0
## [2,]    0   -1
# Performing a Z operation changes the phase and leaves the bit 
a=Z%*%q0
a
##      [,1]
## [1,]    1
## [2,]    0

c) Pauli operator Y

The Pauli operator Y  does both  a bit and a phase flip. The Y operator is represented as

\begin{pmatrix}0&-i\\i&0\end{pmatrix}

Simulating in the composer gives the following

11

Pauli operator Y in  R code

# Pauli operator Y
Y=matrix(c(0,-1i,1i,0),nrow=2,ncol=2)
Y
##      [,1] [,2]
## [1,] 0+0i 0+1i
## [2,] 0-1i 0+0i
# Performing a Y operation does a bit flip and changes the phase 
a=Y%*%q0
a
##      [,1]
## [1,] 0+0i
## [2,] 0-1i

B) Superposition
Superposition is the concept that adding quantum states together results in a new quantum state. There are 3 gates that perform superposition of qubits the H, S and S’ gate.
a) H gate (Hadamard gate)
The H gate, also known as the Hadamard Gate when applied |0> state results in the qubit being half the time in  |0> and the other half in |1>
The H gate can be represented as

1/√2 \begin{pmatrix}1 & 1\\ 1 & -1\end{pmatrix}

# Superposition gates
# H, S & S1
H=1/sqrt(2) * matrix(c(1,1,1,-1),nrow=2,ncol=2)
H
##           [,1]       [,2]
## [1,] 0.7071068  0.7071068
## [2,] 0.7071068 -0.7071068

b) S gate
The S gate can be represented as
\begin{pmatrix}1 & 0\\ 0 & i\end{pmatrix}

S=matrix(c(1,0,0,1i),nrow=2,ncol=2)
S
##      [,1] [,2]
## [1,] 1+0i 0+0i
## [2,] 0+0i 0+1i

c) S’ gate
And the S’ gate is
\begin{pmatrix}1 & 0\\ 0 & -i\end{pmatrix}

S1=matrix(c(1,0,0,-1i),nrow=2,ncol=2)
S1
##      [,1] [,2]
## [1,] 1+0i 0+0i
## [2,] 0+0i 0-1i

d) Superposition (+)
Applying the Hadamard gate H to |0> causes it to become |+>. This is the standard superposition state
Where |+> = 1/√2 (|0> + |1>)

|0> ==> H|0>  ==>  |+>
Where the qubit is one half of the time in |0> and the other half of the time in |1>
Simulating in the Composer
14

Superposition(+) in R code
Superposition of qubit |0> results in |+> as shown below

|0> ==> H|0>  ==>  |+>

# H|0>
a <- H%*%q0
a
##           [,1]
## [1,] 0.7071068
## [2,] 0.7071068
# This is equal to 1/sqrt(2) (|0> + |1>)
b <- 1/sqrt(2) * (q0+q1)
b
##           [,1]
## [1,] 0.7071068
## [2,] 0.7071068

e) Superposition (-)

A new qubit state |-> is obtained by applying the H gates to |0> and then applying the Z gate which is known as the  diagonal basis. The H makes the above superposition and then the Z flips the phase (|1⟩ to −|1⟩)

Where |-> = 1/√2 (|0> – |1>)
|0> ==>  Z*H*|0> ==>  |->

Simulating in the Composer
15

Superposition(-) in R code

# The diagonal basis
# It can be seen that a <==>b
a <- Z%*%H%*%q0
a
##            [,1]
## [1,]  0.7071068
## [2,] -0.7071068
b <- 1/sqrt(2) * (q0-q1)
b
##            [,1]
## [1,]  0.7071068
## [2,] -0.7071068

C) Measuring superposition

But when we measure the superposition states the result is always a 0 or 1. In order to distinguish between the |+> and the |-> states we need to measure in the diagonal basis. This is done by using the H gate before the measurement

a) Superposition (+) measurement

19

Superposition(+) in R code

# Superposition (+) measurement
a <- H%*%H%*%q0
# The result is |0>
a
##      [,1]
## [1,]    1
## [2,]    0

b) Superposition (-) measurement

Simulating in composer

22

Simulating Superposition(-) in R code

# Superposition (-) measurement
a <- H%*%Z%*%H%*%q0
#The resultis |1>
a
##      [,1]
## [1,]    0
## [2,]    1

D) Y basis
A third basis us the circular or Y basis

|ac*> = 1/√2(|0> + i|1>)
ac* – the symbol is an anti-clockwise arrow

And

|c*> = 1/√2(|0> – i|1>)
c* – the symbol for clockwise arrow

a) Superposition (+i)Y

Simulating in Composer
29

Superposition (+i)Y in R code

#Superposition(+i) Y
a <- S%*%H%*%q0
a
##                      [,1]
## [1,] 0.7071068+0.0000000i
## [2,] 0.0000000+0.7071068i
b <- 1/sqrt(2)*(q0 +1i*q1)
b
##                      [,1]
## [1,] 0.7071068+0.0000000i
## [2,] 0.0000000+0.7071068i

b) Superposition(-i)Y 

Simulating Superposition (-i)Y in Quantum Composer
30

Superposition (-i)Y in R 

#Superposition(-i) Y
a <- S1%*%H%*%q0
a
##                      [,1]
## [1,] 0.7071068+0.0000000i
## [2,] 0.0000000-0.7071068i
b <- 1/sqrt(2)*(q0 -1i*q1)
b
##                      [,1]
## [1,] 0.7071068+0.0000000i
## [2,] 0.0000000-0.7071068i

To measure the circular basis we need to add a S1 and H gate

c) Superposition (+i) Y measurement

Simulation in Quantum composer

25

Superposition (+i) Y in R 

#Superposition(+i) Y measurement
a <- H%*%S1%*%S%*%H%*%q0
a
##      [,1]
## [1,] 1+0i
## [2,] 0+0i

d) Superposition (-Y) simulation

28

Superposition (-i) Y in R

#Superposition(+i) Y measurement
a <- H%*%S1%*%S%*%H%*%q0
a
##      [,1]
## [1,] 1+0i
## [2,] 0+0i

I hope to make more headway and develop the intuition for quantum algorithms in the weeks and months to come.

Watch this space. I’ll be back!

Disclaimer: This article represents the author’s viewpoint only and doesn’t necessarily represent IBM’s positions, strategies or opinions

References
1.IBM’s  Quantum Experience User Guide
2. Quantum computing lecture notes

Also see
1. Natural language processing: What would Shakespeare say?
2. Literacy in India – A deepR dive
3. Re-introducing cricketr! : An R package to analyze performances of cricketers
4. Design Principles of Scalable, Distributed Systems
5. A closer look at “Robot Horse on a Trot” in Android
6. What’s up Watson? Using IBM Watson’s QAAPI with Bluemix, NodeExpress – Part 1
7. Introducing cricket package yorkr: Part 1- Beaten by sheer pace!
8. Experiments with deblurring using OpenCV
9. Architecting a cloud based IP Multimedia System (IMS)