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

Implementing the quantum gates in series in reverse order we have

# Invoking this in reverse order we get
result=measurement(a)

plotMeasurement(result)

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

#
b = DotProduct(a,q00_)
plotMeasurement(measurement(b))

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

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)

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)

# 1st composite
# Output of CNOT
b = CNOT2_01(a)
# 2nd series
#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)

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

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
j1 = DotProduct(j,i)
k = CNOT3_12(j1)

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

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

12. GHZ YYX measurement

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

and is implemented in QCSimulator as

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

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

My other posts on Quantum Computing

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.

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

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.

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

 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

f) CPauliX
Here C is the 2 x2  Identity matrix. Simulating this in my QCSimulator
CPauliX <- function(q){ 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

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>)

Simulating the Toffoli state in IBM Quantum Experience we get

h) Implementation of Toffoli state in QCSimulator

#ToffoliState <- function(q)
# 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
j1 = DotProduct(j,i)
k = CNOT3_12(j1)

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

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

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
3. https://quantumexperience.ng.bluemix.net/

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

Exploring Quantum Gate operations with QCSimulator

Introduction: Ever since I was initiated into Quantum Computing, through IBM’s Quantum Experience I have been hooked. My initial encounter with domain made me very excited and all wound up. The reason behind this, I think, is because there is an air of mystery around ‘Quantum’ anything.  After my early rush with the Quantum Experience, I have deliberately slowed down to digest the heady stuff.

This post also includes my early prototype of a Quantum Computing Simulator( QCSimulator) which I am creating in R. I hope to have a decent Quantum Computing simulator available, in the next couple of months. The ideas for this simulator are based on IBM’s Quantum Experience and the lectures on Quantum Mechanics and Quantum Computation by Prof Umesh Vazirani from University of California at Berkeley at edX. This calls to this simulator have been included in R Markdown file and has been published at RPubs as Quantum Computing Simulator

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

In this post I explore quantum gate operations

A) Quantum Gates
Quantum gates are represented as a n x n unitary matrix. In mathematics, a complex square matrix U is unitary if its conjugate transpose Uǂ is also its inverse – that is, if
U ǂU =U U ǂ=I

a) Clifford Gates
The following gates are known as Clifford Gates and are represented as the unitary matrix below

1. Pauli X
$\begin{pmatrix}0&1\\1&0\end{pmatrix}$

2.Pauli Y
$\begin{pmatrix}0&-i\\i&0\end{pmatrix}$

3. Pauli Z
$\begin{pmatrix}1&0\\0&-1\end{pmatrix}$

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

5. S Gate
$\begin{pmatrix}1 & 0\\ 0 & i\end{pmatrix}$

6. S1 Gate
$\begin{pmatrix}1 & 0\\ 0 & -i\end{pmatrix}$

7. CNOT
$\begin{pmatrix}1 & 0 & 0 &0 \\ 0 & 1 & 0 &0 \\ 0& 0 & 0&1 \\ 0 & 0 & 1 & 0\end{pmatrix}$

b) Non-Clifford Gate
The following are the non-Clifford gates
1. Toffoli Gate
T = $\begin{pmatrix}1 & 0\\ 0 & e^{^{i\prod /4}}\end{pmatrix}$

2. Toffoli1 Gate
T1 =$\begin{pmatrix}1 & 0\\ 0 & e^{^{-i\prod /4}}\end{pmatrix}$

B) Evolution of a 1 qubit Quantum System
The diagram below shows how a 1 qubit system evolves on the application of Quantum Gates.

C) Evolution of a 2 Qubit  System
The following diagram depicts the evolution of a 2 qubit system. The 4 different maximally entangled states can be obtained by using a Hadamard and a CNOT gate to |00>, |01>, |10> & |11> resulting in the entangled states  Φ+, Ψ+, Φ, Ψrespectively

D) Verifying Unitary’ness
XXǂ = XǂX= I
TTǂ = TǂT=I
SSǂ = SǂS=I
The Uǂ  function in the simulator is
Uǂ = GateDagger(U)

E) A look at some Simulator functions
The unitary functions for the Clifford and non-Clifford gates have been implemented functions. The unitary functions can be chained together by invoking each successive Gate as argument to the function.

1. Creating the dagger function
TDagger = GateDagger(TGate)
TDagger x TGate

H
##           [,1]       [,2]
## [1,] 0.7071068  0.7071068
## [2,] 0.7071068 -0.7071068
HDagger = GateDagger(H)
HDagger
##           [,1]       [,2]
## [1,] 0.7071068  0.7071068
## [2,] 0.7071068 -0.7071068
HDagger %*% H
##      [,1] [,2]
## [1,]    1    0
## [2,]    0    1
T
##      [,1]                 [,2]
## [1,] 1+0i 0.0000000+0.0000000i
## [2,] 0+0i 0.7071068+0.7071068i
TDagger = GateDagger(T)
TDagger
##      [,1]                 [,2]
## [1,] 1+0i 0.0000000+0.0000000i
## [2,] 0+0i 0.7071068-0.7071068i
TDagger %*% T
##      [,1] [,2]
## [1,] 1+0i 0+0i
## [2,] 0+0i 1+0i

2. Angle between 2 vectors – Inner product
The angle between 2 vectors can be obtained by taking the inner product between the vectors

#1. a is the diagonal vector 1/2 |0> + 1/2 |1> and b = q0 = |0>
diagonal <-  matrix(c(1/sqrt(2),1/sqrt(2)),nrow=2,ncol=1)
q0=matrix(c(1,0),nrow=2,ncol=1)
innerProduct(diagonal,q0)
##      [,1]
## [1,]   45
#2. a = 1/2|0> + sqrt(3)/2|1> and  b= 1/sqrt(2) |0> + 1/sqrt(2) |1>
a = matrix(c(1/2,sqrt(3)/2),nrow=2,ncol=1)
b = matrix(c(1/sqrt(2),1/sqrt(2)),nrow=2,ncol=1)
innerProduct(a,b)
##      [,1]
## [1,]   15

3. Chaining Quantum Gates
For e.g.
H x q0
S x H x q0 == > SGate(Hadamard(q0))

Or
H x S x S x H x q0 == > Hadamard(SGate(SGate(Hadamard))))

# H x q0
Hadamard(q0)
##           [,1]
## [1,] 0.7071068
## [2,] 0.7071068
# S x H x q0
SGate(Hadamard(q0))
##                      [,1]
## [1,] 0.7071068+0.0000000i
## [2,] 0.0000000+0.7071068i
# H x S x S x H x q0
Hadamard(SGate(SGate(Hadamard(q0))))
##      [,1]
## [1,] 0+0i
## [2,] 1+0i
# S x T x H x T x H x q0
SGate(TGate(Hadamard(TGate(Hadamard(q0)))))
##                      [,1]
## [1,] 0.8535534+0.3535534i
## [2,] 0.1464466+0.3535534i

4. Measurement
The output of Quantum Gate operations can be measured with
measurement(a)
measurement(q0)
measurement(a)

measurement(q0)
##   0 1
## v 1 0
measurement(Hadamard(q0))
##     0   1
## v 0.5 0.5
a <- SGate(TGate(Hadamard(TGate(Hadamard(q0)))))
measurement(a)
##           0         1
## v 0.8535534 0.1464466

5. Plot the measurements

plotMeasurement(q1)

plotMeasurement(Hadamard(q0))

a = measurement(SGate(TGate(Hadamard(TGate(Hadamard(q0))))))
plotMeasurement(a)

6. Using the QCSimulator for one of the Bell tests
Here I compute the following measurement of  Bell state ZW  with the QCSimulator

When this is simulated on IBM’s Quantum Experience the result is

Below I simulate the same on my R based QCSimulator

# Compute the effect of the Composite H gate with the Identity matrix (I)
a=kronecker(H,I,"*")
a
##           [,1]      [,2]       [,3]       [,4]
## [1,] 0.7071068 0.0000000  0.7071068  0.0000000
## [2,] 0.0000000 0.7071068  0.0000000  0.7071068
## [3,] 0.7071068 0.0000000 -0.7071068  0.0000000
## [4,] 0.0000000 0.7071068  0.0000000 -0.7071068
# Compute the applcation of CNOT on this result
b = CNOT(a)
b
##           [,1]      [,2]       [,3]       [,4]
## [1,] 0.7071068 0.0000000  0.7071068  0.0000000
## [2,] 0.0000000 0.7071068  0.0000000  0.7071068
## [3,] 0.0000000 0.7071068  0.0000000 -0.7071068
## [4,] 0.7071068 0.0000000 -0.7071068  0.0000000
# Obtain the result of CNOT on q00
c = b %*% q00
c
##           [,1]
## [1,] 0.7071068
## [2,] 0.0000000
## [3,] 0.0000000
## [4,] 0.7071068
# Compute the effect of the composite HxTxHxS gates and the Identity matrix(I) for measurement
e=kronecker(I, d,"*")

# Applying  the composite gate on the output 'c'
f = e %*% c
# Measure the output
g <- measurement(f)
g
##          00        01        10        11
## v 0.4267767 0.0732233 0.0732233 0.4267767
#Plot the measurement
plotMeasurement(g)


aa
which is exactly the result obtained with IBM’s Quantum Experience!

Conclusion : In  this post I dwell on 1 and 2-qubit quantum gates and explore their operation. I have started to construct a  R based Quantum Computing Simulator. I hope to have a reasonable simulator in the next couple of months. Let’s see.

Watch this space!

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

Index of posts

A primer on Qubits, Quantum gates and Quantum Operations

Introduction: After my initial encounter with IBM’s Quantum Experience, and my playing around with qubits, quantum gates and trying out the Bell experiment I can now say that I am fairly hooked to quantum computing.

So, I decided that  before going any  further,  that I  needed to  spend a little time more, getting to know more about the basics of Dirac’s bra-ket notation, qubits and ensuring that my knowledge is properly “chunked” ( See Learning to Learn: Powerful mental tools to master tough subjects, a really good course!!)

So, I started to look around for material on Quantum Computing, and finally landed on the classic course “Quantum Mechanics and Quantum Computing”, from University of California, Berkeley by Prof Umesh V Vazirani at edX. I have started to audit the course (listen in, without doing the assignments). The Prof is unbelievably good, and makes the topic both interesting and absorbing. This post is based on my notes of week 1 & 2 lectures. I have tried to articulate as best as I can, what I have understood of the lectures, though I would strongly recommend  you to, at least audit the archived course. By the way, I also had to refresh my knowledge of basic trigonometry and linear algebra. My knowledge of the basics of matrix manipulation, vectors etc. were buried deep within the sands of time. Luckily for me, they were reasonably intact.

A) Quantum states
A hydrogen atom has 1 electron in orbit. The electron can be either in the idle state or in the excited state. We can represent the idle state with |0> and the excited state with |1>, which is Dirac’s ‘ket’ notation.

This electron will be in a superposition state which is represented by
Ψ = α |0> + β |1> where α & β are complex numbers and obey | α|2 + | β|2 = 1
For e.g. we could have the superposition state
$\varphi= \frac{1}{2} + \frac{1i}{2}|0> + \frac{1}{\sqrt{2}}|1>$
It can be seen that | α|2 = $\frac{1}{2}$ and | β|2 = = $\frac{1}{2}$
For a complex number α = a+ bi  == > | α| = $\sqrt{a^{2} + b^{2}}$

B) Measurement
However, when the electron or the qubit is measured,  the state of superposition collapses to either |0> or |1> with the following probability’s

The resulting state is
|0> with probability | α|2
|1> with probability | β |2

And | α|2 + | β|2 = 1 because the sum of the probabilities must add up to 1  i.e. $\sum p_{i} = 1$

C) Geometric interpretation
Let us consider a qubit that is in a superposition state
Ψ = α |0> + β |1> where α & β are complex numbers and obey | α|2 + | β|2 = 1

We can write this as a vector  $\begin{pmatrix} \alpha\\ \beta \end{pmatrix}$ representing the state of the electron

It can be seen that if
$\alpha=1$ and $\beta=0$ then |0> = $\begin{pmatrix}1\\ 0\end{pmatrix}$
and
$\alpha=0$ and $\beta=1$  then |1> = $\begin{pmatrix}0\\ 1\end{pmatrix}$

Measuring a qubit in standard basis
If we represent the qubit geometrically then the superposition can be represented as a vector which makes an angle $theta$ with |0>
$\varphi = cos\theta|0> + sin\theta|1>$

Measuring this  $\varphi$ is the projection of on one of the standard basis |0> or |1>
The output is is |0> with probability $cos^{2}\theta$ and
|1> with probability $sin^{2}\theta$

D) Measuring a qubit in any basis

The qubit can be measured in any arbitrary basis. For e.g.  if
Ψ = α |0> + β |1>   and we have the diagonal basis
|u>| and |u’> as shown and Ψ makes an angle $\theta$ with |u> then we can write

|u> with probability cos2Θ
And |u’> with probability sin2Θ

E) K Qubit system
Let us assume that we have a Quantum System with k qubits
|0>, |1>, |2>… |k-1>

The qubit will be in a superimposed state
Ψ = α0 |0>+ α1 |1> + α2|2> + … + αk-1 |k-1>
Where αj is a complex vector with the property ∑ αj = 1

Here Ψ is a unit vector in a K dimensional complex vector space, known as Hilbert Space
For e.g. a 3 qubit quantum system
$\varphi = \frac{1}{2}+\frac{i}{2}|0> - \frac{1}{2} |1> + \frac{1}{2}|2>$
Then P(0) = ½ P(1) = ¼ and P(2) = ¼   ∑Pj = 1

We could also write
$\varphi = \begin{pmatrix} \alpha_{0}\\ \alpha_{1}\\ .. \\\alpha_{k-1}\\\end{pmatrix}$
Or
Ψ = α0 |0>+ α1 |1> + α2|2> + … + αk-1 |k-1>

F) Measuring the angle between 2 complex vectors
To measure the angle between 2 complex vectors
$\varphi = \begin{pmatrix} \alpha_{0}\\ \alpha_{1}\\\alpha_{2}\\\end{pmatrix}$
And
$\phi = \begin{pmatrix} \beta_{0}\\ \beta_{1}\\\beta_{2}\\\end{pmatrix}$
we need to take the inner product of the complex conjugate of the 1st vector and the 2nd
cosΘ = inner product  => $\bar{\varphi} . \phi$
$cos\theta = \bar\alpha_{0}\beta_{0} + \bar\alpha_{1}\beta_{1} + \bar\alpha_{2}\beta_{2}$

For e.g.
If $\varphi = \frac{1}{2} |0> + \frac{\sqrt 3}{2}|1>$ which makes 60 degrees with the |0> basis
And
$\phi = \frac{1}{\sqrt 2} |0> + \frac{1}{\sqrt 2}|1>$ which is the diagonal |+> basis
Then the angle between these 2 vectors are obtained by taking the inner product
cosΘ =  ½ * 1/√2 + √3/2 * ½

G) Measuring Ψ in |+> or |-> basis
For e.g. if
$\varphi = \frac{1}{2} |0> + \frac{\sqrt 3}{2}|1>$   – (A)

Then we can specify/measure Ψ in |+> or |-> basis as follow
|+> = 1/√2(|0> + |1>) and |-> = 1/√2(|0> – |1>)
Or |0> = 1/√2(|+> + |->)   and |1> = 1/√2(|+> – |->)

We can write
Ψ= α|+> + β|->

Substituting for |0> and |1>  in (A) we get
$\varphi = \frac{1+ \sqrt 3}{2 \sqrt 2} |+> \frac{1 - \sqrt 3 }{2 \sqrt 2} |->$

H) Quantum gates
I) Clifford gates
Pauli gates
a) Pauli X
The Pauli X gate does a bit flip
|0> ==>  X|0> ==> |1>
|1> ==>  X|1> ==> |0>
and is represented
$\begin{pmatrix}0&1\\1&0\end{pmatrix}$

b) Pauli Z
This gates does a phase flip and is represented as the 2 x 2 unitary matrix
$\begin{pmatrix}1&0\\0&-1\end{pmatrix}$

c) Pauli 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}$

K) Superposition gates
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}$

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

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

L) Non-Clifford Gates
The quantum gates discussed in my earlier post Pauli X, Y, Z, H, S and S1 are members of a special group of gates known as the ‘Clifford group’.
The non-Clifford gates, discussed are the T and  Tǂ gates

These are given by
T = $\begin{pmatrix}1 & 0\\ 0 & e^{^{i\prod /4}}\end{pmatrix}$
Tǂ =$\begin{pmatrix}1 & 0\\ 0 & e^{^{-i\prod /4}}\end{pmatrix}$

M) 2 qubit system
A 2 qubit system

A 2 qubit system is a superposition of all possible 2 qubit states. A 2 qubit system and can be represented as

Ψ = α00 |00> + α01 |01> + α10 |10> + α11 |11>

Measuring the 2 qubit system, as earlier, results in the collapse of the superposition and the result is one of 4 qubit states. The probability of the measure state is the square of the amplitude | αij|2

N) Entanglement
A  2 qubit system in which we have
Ψ = α0 |0> + α1 |1> and Φ= β0 |0> + β1 |1> the superimposed state is obtained by taking the tensor product of the 2 qubits
$\chi = (\alpha_{0} |0> + \alpha_{1}|1>)\otimes (\beta_{0} |0> + \beta_{1} |1>)$
Where $\otimes$ is the tensor product

The state
Ψ = 1/√2|00> + 1/√2|11>
Is called an ‘entangled’ state because it cannot  be reduced to a product of 2 vectors

N) 2 qubit gates

A 2 qubit system is a superposition of all possible 2 qubit states. A 2 qubit system and can be represented as
Ψ = α00 |00> + α01 |01> + α10 |10> + α11 |11>

Measuring the 2 qubit system, as earlier, results in the collapse of the superposition and the result is one of 4 qubit states. The probability of the measure state is the square of the amplitude | αij|2
More specifically a 2 qubit system in which we have
Ψ = α0 |0> + α1 |1> and Φ= β0 |0> + β1 |1> the superimposed state is obtained by taking the tensor product of the 2 qubits
$\chi = (\alpha_{0} |0> + \alpha_{1}|1>)\otimes (\beta_{0} |0> + \beta_{1} |1>)$

Where $\otimes$ is the tensor product
The state
Ψ = 1/√2|00> + 1/√2|11>
Is called an ‘entangled’ state because it cannot  be reduced to a product of 2 vectors
A quantum gate is a 2 x 2 unitary matrix U such that
α0 |0> + α1 |1>    == > Quantum Gate == > β0|0> + β1 |1>

Unitary functions: In mathematics, a complex square matrix U is unitary if its conjugate transpose U* is also its inverse
If
U=$\begin{pmatrix}a & c \\b & d\end{pmatrix}$
And U* = $\begin{pmatrix}\bar a & \bar b \\ \bar c & \bar d\end{pmatrix}$
Then
UU* = I where I is the Identity matrix

2 qubit gates is  4 x 4 unitary matrix
For a 2 qubit that is in the superposition state
Ψ = α00 |00> + α01 |01> + α10 |10> + α11 |11>

A 2 qubit gate’s operation on Ψ is
$\begin{pmatrix}a & e & i & m \\ b & f & j & n\\ c & g & k & o\\ d & h & l & p\end{pmatrix}$ * $\begin{pmatrix}\alpha_{00}\\ \alpha_{01}\\ \alpha_{10}\\\alpha_{11}\end{pmatrix}$

One important 2 qubit gate is the CNOT gate which is shown  below

The CNOT gate is represented by the following unitary matrix
$\begin{pmatrix}1 & 0 & 0 & 0\\ 0 & 1 & 0 & 0 \\0 & 0 & 0 & 1\\ 0 & 0 & 1 & 0\end{pmatrix}$

If 2  2×2 qubit gates were applied to 2 qubits the composite gate would be Tensor product of the 2 matrices

u1 = $\begin{pmatrix}a & c\\ b & d\end{pmatrix}$
u2 = $\begin{pmatrix}e & g\\ f & h\end{pmatrix}$
then
U= $u1 \otimes u2$
U = $\begin{pmatrix}a\begin{pmatrix}e & g\\ f & h\end{pmatrix} & c\begin{pmatrix}a & c\\ b & d\end{pmatrix}\\ b \begin{pmatrix}a & c \\ b & d\end{pmatrix}& d\begin{pmatrix}a & c\\ b & d\end{pmatrix}\end{pmatrix}$
The above product is also known as the Kronecker product

O) Tensor product of 2 qubits
Ψ = α0 |0> + α1 |1> and Φ= β0 |0> + β1 |1>
$latex \varphi \otimes \phi= α0 β0|0>|0> + α0 β1|0>|1> + α1 β0|1>|0> + α1 β1|1>|1> = α0 β0|00> + α0 β1|01> + α1 β0|10> + α1 β1|11> If a Z gate and a Hadamard gate H were applied on 2 qubits, it is interest to know what the resulting composite gate would be. Z = $\begin{pmatrix}1 & 0\\ 0 & -1\end{pmatrix}$ and H = $\begin{pmatrix}\frac{1}{\sqrt 2} & \frac{1}{\sqrt 2} \\ \frac{1}{\sqrt 2} & \frac{-1}{\sqrt 2}\end{pmatrix}$ The composite gate is obtained by the tensor product of $Z \otimes H$ Hence the result of the composite gate is =$\begin{pmatrix}1\begin{pmatrix}\frac{1}{\sqrt 2} & \frac{1}{\sqrt 2}\\ \frac{1}{\sqrt 2}& \frac{-1}{\sqrt 2}\end{pmatrix} & 0\\ 0 & -1\begin{pmatrix}\frac{1}{\sqrt 2} & \frac{1}{\sqrt 2}\\ \frac{1}{\sqrt 2} &\frac{-1}{\sqrt 2} \end{pmatrix}\end{pmatrix}$ =$\begin{pmatrix}\begin{pmatrix}\frac{1}{\sqrt 2} & \frac{1}{\sqrt 2}\\ \frac{1}{\sqrt 2}& \frac{-1}{\sqrt 2}\end{pmatrix} & 0\\ 0 & -\begin{pmatrix}\frac{-1}{\sqrt 2} & \frac{-1}{\sqrt 2}\\ \frac{-1}{\sqrt 2} &\frac{1}{\sqrt 2} \end{pmatrix}\end{pmatrix}$ which is the entangled state. Conclusion: This post includes most of the required basics to get started on Quantum Computing. I will probably add another post detailing the operations of the Quantum Gates on qubits. Note: 1.The equations and matrices have been created using LaTeX notation using the online LaTex equation creator 2. The figures have been created using the app Bamboo Paper, which I think is cooler than creating in Powerpoint Going deeper into IBM’s Quantum Experience! Introduction In this post I delve deeper into IBM’s Quantum Experience. As mentioned in my earlier post “Venturing into IBM’s Quantum Experience”, IBM, has opened up its Quantum computing environment, to the general public, as the Quantum Experience. The access to Quantum Experience is through IBM’s Platform as a Service (PaaS) offering, Bluemix™. Clearly this is a great engineering feat, which integrates the highly precise environment of Quantum Computing, where the qubits are maintained at 5 milliKelvin, and the IBM Bluemix PaaS environment on Softlayer. The Quantum Experience, is in fact Quantum Computing as a Service (QCaaS). In my opinion, the Quantum Experience, provides a glimpse of tomorrow, today, Disclaimer: This article represents the author’s viewpoint only and doesn’t necessarily represent IBM’s positions, strategies or opinions Note: Also by the way, feel free to holler if you find anything incorrect or off the mark in my post. I am just getting started on quantum computing so there may be slip ups. A) Bloch sphere In my earlier post the operations of the X, Y, Z, H, S, and S1 were measured using the standard or diagonal basis and the results were in probabilities of the qubit(s). However, the probabilities alone, in the standard basis, are not enough to specify a quantum state because it does not capture the phase of the superposition. A convenient representation for a qubit is the Bloch sphere. The general state of a quantum two-level system can be written in the form |ψ⟩=α|0⟩+β|1⟩, Where α and β are complex numbers with |α|2+|β|2=1. This leads to the “canonical” parameterized form |ψ⟩= cos Θ/2 |0> + e sin Θ/2 |1> (A) in terms of only two real numbers θ and φ, with natural ranges 0 ≤ θ ≤ π and 0 ≤ φ ≤ 2π. These are the same as the polar angles in 3-dimensional spherical coordinates, and this leads to the representation of the state (1) as a point on a unit sphere called the Bloch sphere. In the notation of (A), the state |0> is represented by the North pole, and the state |1> by the South pole. Note: The states |0> and |1> , n the Bloch sphere are not orthogonal to each other. The states on the equator of the Bloch sphere, correspond to superpositions of |0> and |1> with equal weights (θ = π∕2), and different phases, parameterized by the azimuthal angle φ (the “longitude”) In the picture below Bloch measurements are performed on the operations on the qubits The results of the Bloch measurements for the combination of quantum gates are shown below i) Quantum gate operations and Bloch measurements ii) Quantum gate operations as Superposition operations B) Classical vs Quantum computing A classical computer that has N-bits has $2^{N}$possible configurations. However, at any one point in time, it can be in one, and only one of 2N configurations. Interestingly, the quantum computer also takes in a n -bit number and outputs a n -bit number; but because of the superposition principle and the possibility of entanglement, the intermediate state is very different. A system which had N different mutually exclusive states can be represented as |1>, |2>. . . |N> using the Dirac’s bra-ket notation A pure quantum state is a superposition of all these states Φ = α1 1> + α2 2> + …. + αN N> To describe it requires complex numbers, giving a lot more room for maneuvering. C) The CNOT gate The CNOT gate or the Controlled-Not gate is an example of a two-qubit quantum gate. The CNOT gate’s action is to flip (apply a NOT or X gate to) the target qubit if the control qubit is 1; otherwise it does nothing. The CNOT plays the role of the classical XOR gate, but unlike the XOR, The CNOT gate is a two-output gate and is reversible It is represented by the matrix by the following 4 x 4 matrix $\begin{pmatrix}1 & 0 & 0 &0 \\ 0 & 1 & 0 &0 \\ 0& 0 & 0&1 \\ 0 & 0 & 1 & 0\end{pmatrix}$ The CNOT gate flips the target bit if the control bit is 1, otherwise it does nothing if it’s 0: More specifically CNOT|0>|b> = |0>|b> CNOT|1>|b>= |1>|1 – b> The operation of the CNOT gate can be elaborated as below The 2-qubit basis states can be represented as four-dimensional vectors |00> = $\begin{pmatrix} 1& 0 & 0 & 0 \end{pmatrix}^{T}$ |01> = $\begin{pmatrix} 0& 1 & 0 & 0 \end{pmatrix}^{T}$ |10> = $\begin{pmatrix} 0& 0 & 1 & 0 \end{pmatrix}^{T}$ |11> = $\begin{pmatrix} 0& 0 & 0 & 1 \end{pmatrix}^{T}$ For example, a quantum state may be expanded as a linear combination of this basis: |ψ⟩=a|00⟩+b|01⟩+c|10⟩+d|11⟩ The CNOT matrix can be applied as below CNOT*|ψ⟩=CNOT*(a|00⟩+b|01⟩+c|10⟩+d|11⟩) = a*CNOT*|00⟩+…+d*CNOT*|11⟩ where you perform standard matrix multiplication on the basis vectors to get: CNOT*|ψ⟩=a|00⟩+b|01⟩+c|11⟩+d|10⟩ In other words, the CNOT gate has transformed |10⟩↦|11⟩ and |11⟩↦|10⟩ i) CNOT operations in R code # CNOT gate cnot= matrix(c(1,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0),nrow=4,ncol=4) cnot ## [,1] [,2] [,3] [,4] ## [1,] 1 0 0 0 ## [2,] 0 1 0 0 ## [3,] 0 0 0 1 ## [4,] 0 0 1 0 #a. Qubit |00> q00=matrix(c(1,0,0,0),nrow=4,ncol=1) q00 ## [,1] ## [1,] 1 ## [2,] 0 ## [3,] 0 ## [4,] 0 # CNOT *q00 ==> q00 a <- cnot %*% q00 a ## [,1] ## [1,] 1 ## [2,] 0 ## [3,] 0 ## [4,] 0 #b.Qubit |01> q01=matrix(c(0,1,0,0),nrow=4,ncol=1) q01 ## [,1] ## [1,] 0 ## [2,] 1 ## [3,] 0 ## [4,] 0 # CNOT *q01 ==> q01 a <- cnot %*% q01 a ## [,1] ## [1,] 0 ## [2,] 1 ## [3,] 0 ## [4,] 0 #c. Qubit |10> q10=matrix(c(0,0,1,0),nrow=4,ncol=1) q10 ## [,1] ## [1,] 0 ## [2,] 0 ## [3,] 1 ## [4,] 0 # CNOT *q10 ==> q11 a <- cnot %*% q10 a ## [,1] ## [1,] 0 ## [2,] 0 ## [3,] 0 ## [4,] 1 #d. Qubit |11> q11=matrix(c(0,0,0,1),nrow=4,ncol=1) q11 ## [,1] ## [1,] 0 ## [2,] 0 ## [3,] 0 ## [4,] 1 # CNOT *q11 ==> q10 a <- cnot %*% q11 a ## [,1] ## [1,] 0 ## [2,] 0 ## [3,] 1 ## [4,] 0 D) Non Clifford gates The quantum gates discussed in my earlier post (Pauli X, Y, Z, H, S and S1) and the CNOT are members of a special group of gates known as the ‘Clifford group’. These gates can be simulated efficiently on a classical computer. Therefore, the Clifford group is not universal. A finite set of gates that can approximate any arbitrary unitary matrix is known as a universal gate set. This is similar, to how certain sets of classical logic gates, such as {AND, NOT}, are functionally complete and can be used to build any Boolean function ( I remember this axiom/fact from my Digital Electronics -101 class about 3 decades back!). Adding almost any non-Clifford gate to single-qubit Clifford gates and CNOT gates makes the group universal which I presume can simulate any arbitrary unitary matrix. The non-Clifford gates, discussed are the T and Tǂ gates These are given by T = $\begin{pmatrix}1 & 0\\ 0 & e^{^{i\prod /4}}\end{pmatrix}$ Tǂ =$\begin{pmatrix}1 & 0\\ 0 & e^{^{-i\prod /4}}\end{pmatrix}$ i) T gate operations The T gate makes it possible to reach different points of the Bloch sphere. By increasing the number of T-gates in the quantum circuit ( we start to cover the Bloch sphere more densely with states that can be reached ii) T Gates of depth 2 – Computational Measurement Simulating in Composer iii) Simulating in R Code Measurement only gives the real part and does not provide info on phase # T Gate T=matrix(c(1,0,0,exp(1i*pi/4)),nrow=2,ncol=2) # Simulating T Gate depth-2 - Computational measurement a=S%*%T%*%H%*%T%*%H%*%q0 a ## [,1] ## [1,] 0.8535534+0.3535534i ## [2,] 0.1464466+0.3535534i iv) 2 T Gates – Bloch Measurement Bloch measurement v) Simulating T gate in R code This gives the phase values as shown in the Bloch sphere # Simulating T Gate depth-2 - Bloch measurement (use a diagonal basis H gate in front) a=H%*%S%*%T%*%H%*%T%*%H%*%q0 a ## [,1] ## [1,] 0.7071068+0.5i ## [2,] 0.5000000+0.0i E) Quantum Entanglement – The case of ‘The Spooky action at a distance’ One of the infamous counter-intuitive ideas of quantum mechanics is that two systems that appear too far apart to influence each other can nevertheless behave in ways that, though individually random, are too strongly correlated to be described by any classical local theory. For e.g. when the 1st qubit of a pair of “quantum entangled” qubits are measured, this automatically determines the 2nd qubit, though the individual qubits may be separated by extremely large distances. It appears that the measurement of the first qubit cannot affect the 2nd qubit which is too far apart to be be influenced and also given the fact that nothing can travel faster than the speed of light. More specifically “Suppose Alice has the first qubit of the pair, and Bob has the second. If Alice measures her qubit in the computational basis and gets outcome b ∈ {0, 1}, then the state collapses to |bb> . In other words the measurements and outcome of the 1st qubit determines the outcome of the 2nd qubit . How weird is that? Similarly, if Alice measures her qubit in some other basis, this will collapse the joint state (including Bob’s qubit) to some state that depends on her measurement basis as well as its outcome. Somehow Alice’s action seems to have an instantaneous effect on Bob’s side—even if the two qubits are light-years apart!” How weird is that! Einstein, whose theory of relativity posits that information and causation cannot travel faster than the speed of light, was greatly troubled by this, . Einstein called such effects of entanglement “spooky action at a distance”. In the 1960s, John Bell devised entanglement-based experiments whose behavior cannot be reproduced by any “local realist” theory where the implication of local and realist is given below Locality: No information can travel faster than the speed of light. There is a hidden variable that defines all the correlations. Realism: All observables have a definite value independent of the measurement i) Bell state measurements The mathematical proof for the Bell tests are quite abstract and mostly escaped my grasp. I hope to get my arms around this beast, in the weeks and months to come. However, I understood how to run the tests and perform the calculations which are included below. I have executed the Bell Tests on a) Ideal Quantum Processor (Simulator with ideal conditions) b) Realistic Quantum Processor (Simulator with realistic conditions) c) Real Quantum Processor. For this I used 8192 ‘shots’ repeats of the experiment I finally calculate |C| for all 3 tests The steps involved in calculating |C| 1. Execute ZW, ZV, XW, XV 2. Calculate <AB> = P(00) + P(11) – P(01) – P(10) 3. Finally |C| = ZW + ZV + XW – XV Preparation of Qubit |00> The qubits are in state |00> The H gate takes the first qubit to the equal superposition 1/√2(|00> + |10>) and the CNOT gate flips the second qubit if the first is excited, making the state 1/√2(|00> + |11>). This is the entangled state (commonly called a Bell state) Simulating in Composer This prepares the entangled state 1/√2(|00> + |11>) It can be seen that the the qubits |00> and |11> are created 1) Simulations on Ideal Quantum processor a) Bell state ZW (Ideal) Simulation P(00) = 0.427 P(01) = 0.073 P(10) =0.073 P(11) = 0.427 b) Bell state ZV (Ideal) Simulation P(00) = 0.427 P(01) = 0.073 P(10) =0.073 P(11) = 0.427 c) Bell state XW (Ideal) Measurement P(00) = 0.427 P(01) = 0.073 P(10) =0.073 P(11) = 0.427 d) Bell state XV (Ideal) Simulating Bell State XV in Composer P(00) = 0.073 P(01) = 0.473 P(10) = 0.473 P(11) =0.73 Bell test measurement in Ideal Quantum Processor are given below For the Ideal Quantum Processor |C) = 2.832 2) Simulations on the Realistic Quantum Processor The Bell tests above were simulated on Realistic Quantum Processor. The results are included below For the Realistic Quantum Processor |C) = 2.523 3) Real IBM Quantum Processor (8192 shots) Finally the Bell Tests were executed in IBM’s Real Quantum Processor for 8192 shots, each requiring 5 standard units. The tests were queued, executed and the results sent by mail. The results are included below a) Bell State ZW measurement (Real) b) Bell state ZV measurement (Real) c) Bell State XW measurement (Real) d) Bell state XV measurement (Real) ; The results were tabulated and |C| computed. Bell test measurement in Real Quantum Processor are given below The Bell measurements on the Reak Quantum Processor is |C) = 2.509 Conclusion This post included details on the CNOT and the non-Clifford gates. The Bell tests were performed on all 3 processors Ideal, Realistic and Real Quantum Processors and in each case the |C| > 2. While I have been to execute the tests I will definitely have to spend more time understanding the nuances. I hope to continue this journey into quantum computing the months to come. Watch this space! Disclaimer: This article represents the author’s viewpoint only and doesn’t necessarily represent IBM’s positions, strategies or opinions Venturing into IBM’s Quantum Experience Introduction: 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 Simulation in the Quantum Composer When the above measurement is simulated in the composer by clicking the ‘Simulate’ button the result is as below 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 When this simulated the Pauli X gate does a bit flip and the result is 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 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 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 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 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 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 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 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 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 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 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 Re-introducing cricketr! : An R package to analyze performances of cricketers In this post I re-introduce R package cricketr. I have added 8 new functions to my R package cricketr, available from version cricketr_0.0.13 namely 1. batsmanCumulativeAverageRuns 2. batsmanCumulativeStrikeRate 3. bowlerCumulativeAvgEconRate 4. bowlerCumulativeAvgWicketRate 5. relativeBatsmanCumulativeAvgRuns 6. relativeBatsmanCumulativeStrikeRate 7. relativeBowlerCumulativeAvgWickets 8. relativeBowlerCumulativeAvgEconRate This post updates my earlier post Introducing cricketr:An R package for analyzing performances of cricketrs Yet all experience is an arch wherethro’ Gleams that untravell’d world whose margin fades For ever and forever when I move. How dull it is to pause, to make an end, To rust unburnish’d, not to shine in use! Ulysses by Alfred Tennyson Introduction This is an initial post in which I introduce a cricketing package ‘cricketr’ which I have created. This package was a natural culmination to my earlier posts on cricket and my finishing 10 modules of Data Science Specialization, from John Hopkins University at Coursera. The thought of creating this package struck me some time back, and I have finally been able to bring this to fruition. So here it is. My R package ‘cricketr!!!’ This package uses the statistics info available in ESPN Cricinfo Statsguru. The current version of this package can handle all formats of the game including Test, ODI and Twenty20 cricket. You should be able to install the package from GitHub and use many of the functions available in the package. Please be mindful of ESPN Cricinfo Terms of Use Note: This page is also hosted as a GitHub page at cricketr This post is also hosted on Rpubs at Reintroducing cricketr. You can also down the pdf version of this post at reintroducing_cricketr.pdf (Take a look at my short video tutorial on my R package cricketr on Youtube – R package cricketr – A short tutorial) Do check out my interactive Shiny app implementation using the cricketr package – Sixer – R package cricketr’s new Shiny avatar Take a look at my book with all my articles related to cricket – Cricket analytics with cricketr!!!. The book is also available in paperback and kindle versions at Amazon which has, by the way, better formatting! Also see my 2nd book “Beaten by sheer pace” based on my R package yorkr which is now available in paperback and kindle versions at Amazon The cricketr package The cricketr package has several functions that perform several different analyses on both batsman and bowlers. The package has functions that plot percentage frequency runs or wickets, runs likelihood for a batsman, relative run/strike rates of batsman and relative performance/economy rate for bowlers are available. Other interesting functions include batting performance moving average, forecast and a function to check whether the batsman/bowler is in in-form or out-of-form. The data for a particular player can be obtained with the getPlayerData() function from the package. To do this you will need to go to ESPN CricInfo Player and type in the name of the player for e.g Ricky Ponting, Sachin Tendulkar etc. This will bring up a page which have the profile number for the player e.g. for Sachin Tendulkar this would be http://www.espncricinfo.com/india/content/player/35320.html. Hence, Sachin’s profile is 35320. This can be used to get the data for Tendulkar as shown below The cricketr package is now available from CRAN!!!. You should be able to install directly with if (!require("cricketr")){ install.packages("cricketr",lib = "c:/test") } library(cricketr)  The cricketr package includes some pre-packaged sample (.csv) files. You can use these sample to test functions as shown below # Retrieve the file path of a data file installed with cricketr pathToFile <- system.file("data", "tendulkar.csv", package = "cricketr") batsman4s(pathToFile, "Sachin Tendulkar") # The general format is pkg-function(pathToFile,par1,...) batsman4s(<path-To-File>,"Sachin Tendulkar")  Alternatively, the cricketr package can be installed from GitHub with if (!require("cricketr")){ library(devtools) install_github("tvganesh/cricketr") } library(cricketr)  The pre-packaged files can be accessed as shown above. To get the data of any player use the function getPlayerData() tendulkar <- getPlayerData(35320,dir="..",file="tendulkar.csv",type="batting",homeOrAway=c(1,2), result=c(1,2,4)) Important Note This needs to be done only once for a player. This function stores the player’s data in a CSV file (for e.g. tendulkar.csv as above) which can then be reused for all other functions. Once we have the data for the players many analyses can be done. This post will use the stored CSV file obtained with a prior getPlayerData for all subsequent analyses Sachin Tendulkar’s performance – Basic Analyses The 3 plots below provide the following for Tendulkar 1. Frequency percentage of runs in each run range over the whole career 2. Mean Strike Rate for runs scored in the given range 3. A histogram of runs frequency percentages in runs ranges par(mfrow=c(1,3)) par(mar=c(4,4,2,2)) batsmanRunsFreqPerf("./tendulkar.csv","Sachin Tendulkar") batsmanMeanStrikeRate("./tendulkar.csv","Sachin Tendulkar") batsmanRunsRanges("./tendulkar.csv","Sachin Tendulkar") dev.off() ## null device ## 1  More analyses par(mfrow=c(1,3)) par(mar=c(4,4,2,2)) batsman4s("./tendulkar.csv","Tendulkar") batsman6s("./tendulkar.csv","Tendulkar") batsmanDismissals("./tendulkar.csv","Tendulkar")   3D scatter plot and prediction plane The plots below show the 3D scatter plot of Sachin’s Runs versus Balls Faced and Minutes at crease. A linear regression model is then fitted between Runs and Balls Faced + Minutes at crease battingPerf3d("./tendulkar.csv","Sachin Tendulkar") Average runs at different venues The plot below gives the average runs scored by Tendulkar at different grounds. The plot also displays the number of innings at each ground as a label at x-axis. It can be seen Tendulkar did great in Colombo (SSC), Melbourne ifor matches overseas and Mumbai, Mohali and Bangalore at home batsmanAvgRunsGround("./tendulkar.csv","Sachin Tendulkar")  Average runs against different opposing teams This plot computes the average runs scored by Tendulkar against different countries. The x-axis also gives the number of innings against each team batsmanAvgRunsOpposition("./tendulkar.csv","Tendulkar")  Highest Runs Likelihood The plot below shows the Runs Likelihood for a batsman. For this the performance of Sachin is plotted as a 3D scatter plot with Runs versus Balls Faced + Minutes at crease using. K-Means. The centroids of 3 clusters are computed and plotted. In this plot. Sachin Tendulkar’s highest tendencies are computed and plotted using K-Means batsmanRunsLikelihood("./tendulkar.csv","Sachin Tendulkar") ## Summary of Sachin Tendulkar 's runs scoring likelihood ## ************************************************** ## ## There is a 16.51 % likelihood that Sachin Tendulkar will make 139 Runs in 251 balls over 353 Minutes ## There is a 58.41 % likelihood that Sachin Tendulkar will make 16 Runs in 31 balls over 44 Minutes ## There is a 25.08 % likelihood that Sachin Tendulkar will make 66 Runs in 122 balls over 167 Minutes A look at the Top 4 batsman – Tendulkar, Kallis, Ponting and Sangakkara The batsmen with the most hundreds in test cricket are 1. Sachin Tendulkar :Average:53.78,100’s – 51, 50’s – 68 2. Jacques Kallis : Average: 55.47, 100’s – 45, 50’s – 58 3. Ricky Ponting : Average: 51.85, 100’s – 41 , 50’s – 62 4. Kumara Sangakarra: Average: 58.04 ,100’s – 38 , 50’s – 52 in that order. The following plots take a closer at their performances. The box plots show the mean (red line) and median (blue line). The two ends of the boxplot display the 25th and 75th percentile. Box Histogram Plot This plot shows a combined boxplot of the Runs ranges and a histogram of the Runs Frequency. The calculated Mean differ from the stated means possibly because of data cleaning. Also not sure how the means were arrived at ESPN Cricinfo for e.g. when considering not out.. batsmanPerfBoxHist("./tendulkar.csv","Sachin Tendulkar") batsmanPerfBoxHist("./kallis.csv","Jacques Kallis") batsmanPerfBoxHist("./ponting.csv","Ricky Ponting") batsmanPerfBoxHist("./sangakkara.csv","K Sangakkara") Contribution to won and lost matches The plot below shows the contribution of Tendulkar, Kallis, Ponting and Sangakarra in matches won and lost. The plots show the range of runs scored as a boxplot (25th & 75th percentile) and the mean scored. The total matches won and lost are also printed in the plot. All the players have scored more in the matches they won than the matches they lost. Ricky Ponting is the only batsman who seems to have more matches won to his credit than others. This could also be because he was a member of strong Australian team For the next 2 functions below you will have to use the getPlayerDataSp() function. I have commented this as I already have these files tendulkarsp <- getPlayerDataSp(35320,tdir=".",tfile="tendulkarsp.csv",ttype="batting") kallissp <- getPlayerDataSp(45789,tdir=".",tfile="kallissp.csv",ttype="batting") pontingsp <- getPlayerDataSp(7133,tdir=".",tfile="pontingsp.csv",ttype="batting") sangakkarasp <- getPlayerDataSp(50710,tdir=".",tfile="sangakkarasp.csv",ttype="batting")  par(mfrow=c(2,2)) par(mar=c(4,4,2,2)) batsmanContributionWonLost("tendulkarsp.csv","Tendulkar") batsmanContributionWonLost("kallissp.csv","Kallis") batsmanContributionWonLost("pontingsp.csv","Ponting") batsmanContributionWonLost("sangakkarasp.csv","Sangakarra") dev.off() ## null device ## 1  Performance at home and overseas From the plot below it can be seen Tendulkar has more matches overseas than at home and his performance is consistent in all venues at home or abroad. Ponting has lesser innings than Tendulkar and has an equally good performance at home and overseas.Kallis and Sangakkara’s performance abroad is lower than the performance at home. This function also requires the use of getPlayerDataSp() as shown above par(mfrow=c(2,2)) par(mar=c(4,4,2,2)) batsmanPerfHomeAway("tendulkarsp.csv","Tendulkar") batsmanPerfHomeAway("kallissp.csv","Kallis") batsmanPerfHomeAway("pontingsp.csv","Ponting") batsmanPerfHomeAway("sangakkarasp.csv","Sangakarra") dev.off()  dev.off() ## null device ## 1   Moving Average of runs in career Take a look at the Moving Average across the career of the Top 4. Clearly . Kallis and Sangakkara have a few more years of great batting ahead. They seem to average on 50. . Tendulkar and Ponting definitely show a slump in the later years par(mfrow=c(2,2)) par(mar=c(4,4,2,2)) batsmanMovingAverage("./tendulkar.csv","Sachin Tendulkar") batsmanMovingAverage("./kallis.csv","Jacques Kallis") batsmanMovingAverage("./ponting.csv","Ricky Ponting") batsmanMovingAverage("./sangakkara.csv","K Sangakkara") dev.off() ## null device ## 1 Cumulative Average runs of batsman in career This function provides the cumulative average runs of the batsman over the career. Tendulkar averages around 50, while Sangakkarra touches 55 towards the end of his career par(mfrow=c(2,2)) par(mar=c(4,4,2,2)) batsmanCumulativeAverageRuns("./tendulkar.csv","Tendulkar") batsmanCumulativeAverageRuns("./kallis.csv","Kallis") batsmanCumulativeAverageRuns("./ponting.csv","Ponting") batsmanCumulativeAverageRuns("./sangakkara.csv","Sangakkara") dev.off() ## null device ## 1 Cumulative Average strike rate of batsman in career This function gives the cumulative strike rate of the batsman over the career par(mfrow=c(2,2)) par(mar=c(4,4,2,2)) batsmanCumulativeStrikeRate("./tendulkar.csv","Tendulkar") batsmanCumulativeStrikeRate("./kallis.csv","Kallis") batsmanCumulativeStrikeRate("./ponting.csv","Ponting") batsmanCumulativeStrikeRate("./sangakkara.csv","Sangakkara") dev.off() ## null device ## 1 Future Runs forecast Here are plots that forecast how the batsman will perform in future. In this case 90% of the career runs trend is uses as the training set. the remaining 10% is the test set. A Holt-Winters forecating model is used to forecast future performance based on the 90% training set. The forecated runs trend is plotted. The test set is also plotted to see how close the forecast and the actual matches Take a look at the runs forecasted for the batsman below. • Tendulkar’s forecasted performance seems to tally with his actual performance with an average of 50 • Kallis the forecasted runs are higher than the actual runs he scored • Ponting seems to have a good run in the future • Sangakkara has a decent run in the future averaging 50 runs par(mfrow=c(2,2)) par(mar=c(4,4,2,2)) batsmanPerfForecast("./tendulkar.csv","Sachin Tendulkar") batsmanPerfForecast("./kallis.csv","Jacques Kallis") batsmanPerfForecast("./ponting.csv","Ricky Ponting") batsmanPerfForecast("./sangakkara.csv","K Sangakkara") dev.off() ## null device ## 1 Relative Mean Strike Rate plot The plot below compares the Mean Strike Rate of the batsman for each of the runs ranges of 10 and plots them. The plot indicate the following Range 0 – 50 Runs – Ponting leads followed by Tendulkar Range 50 -100 Runs – Ponting followed by Sangakkara Range 100 – 150 – Ponting and then Tendulkar frames <- list("./tendulkar.csv","./kallis.csv","ponting.csv","sangakkara.csv") names <- list("Tendulkar","Kallis","Ponting","Sangakkara") relativeBatsmanSR(frames,names) Relative Runs Frequency plot The plot below gives the relative Runs Frequency Percetages for each 10 run bucket. The plot below show Sangakkara leads followed by Ponting frames <- list("./tendulkar.csv","./kallis.csv","ponting.csv","sangakkara.csv") names <- list("Tendulkar","Kallis","Ponting","Sangakkara") relativeRunsFreqPerf(frames,names) Relative cumulative average runs in career The plot below compares the relative cumulative runs of the batsmen over the career. While Tendulkar seems to lead over the others with a cumulative average of 50, we can see that Sangakkara goes over everybody else between 180-220th inning. It is likely that Sangakkarra may have overtaken Tendulkar if he had played more frames <- list("./tendulkar.csv","./kallis.csv","ponting.csv","sangakkara.csv") names <- list("Tendulkar","Kallis","Ponting","Sangakkara") relativeBatsmanCumulativeAvgRuns(frames,names) Relative cumulative average strike rate in career As seen in earlier charts Ponting has the best overall strike rate, followed by Sangakkara and then Tendulkar frames <- list("./tendulkar.csv","./kallis.csv","ponting.csv","sangakkara.csv") names <- list("Tendulkar","Kallis","Ponting","Sangakkara") relativeBatsmanCumulativeStrikeRate(frames,names) Check Batsman In-Form or Out-of-Form The below computation uses Null Hypothesis testing and p-value to determine if the batsman is in-form or out-of-form. For this 90% of the career runs is chosen as the population and the mean computed. The last 10% is chosen to be the sample set and the sample Mean and the sample Standard Deviation are caculated. The Null Hypothesis (H0) assumes that the batsman continues to stay in-form where the sample mean is within 95% confidence interval of population mean The Alternative (Ha) assumes that the batsman is out of form the sample mean is beyond the 95% confidence interval of the population mean. A significance value of 0.05 is chosen and p-value us computed If p-value >= .05 – Batsman In-Form If p-value < 0.05 – Batsman Out-of-Form Note Ideally the p-value should be done for a population that follows the Normal Distribution. But the runs population is usually left skewed. So some correction may be needed. I will revisit this later This is done for the Top 4 batsman checkBatsmanInForm("./tendulkar.csv","Sachin Tendulkar") ## ******************************************************************************************* ## ## Population size: 294 Mean of population: 50.48 ## Sample size: 33 Mean of sample: 32.42 SD of sample: 29.8 ## ## Null hypothesis H0 : Sachin Tendulkar 's sample average is within 95% confidence interval ## of population average ## Alternative hypothesis Ha : Sachin Tendulkar 's sample average is below the 95% confidence ## interval of population average ## ## [1] "Sachin Tendulkar 's Form Status: Out-of-Form because the p value: 0.000713 is less than alpha= 0.05" ## ******************************************************************************************* checkBatsmanInForm("./kallis.csv","Jacques Kallis") ## ******************************************************************************************* ## ## Population size: 240 Mean of population: 47.5 ## Sample size: 27 Mean of sample: 47.11 SD of sample: 59.19 ## ## Null hypothesis H0 : Jacques Kallis 's sample average is within 95% confidence interval ## of population average ## Alternative hypothesis Ha : Jacques Kallis 's sample average is below the 95% confidence ## interval of population average ## ## [1] "Jacques Kallis 's Form Status: In-Form because the p value: 0.48647 is greater than alpha= 0.05" ## ******************************************************************************************* checkBatsmanInForm("./ponting.csv","Ricky Ponting") ## ******************************************************************************************* ## ## Population size: 251 Mean of population: 47.5 ## Sample size: 28 Mean of sample: 36.25 SD of sample: 48.11 ## ## Null hypothesis H0 : Ricky Ponting 's sample average is within 95% confidence interval ## of population average ## Alternative hypothesis Ha : Ricky Ponting 's sample average is below the 95% confidence ## interval of population average ## ## [1] "Ricky Ponting 's Form Status: In-Form because the p value: 0.113115 is greater than alpha= 0.05" ## ******************************************************************************************* checkBatsmanInForm("./sangakkara.csv","K Sangakkara") ## ******************************************************************************************* ## ## Population size: 193 Mean of population: 51.92 ## Sample size: 22 Mean of sample: 71.73 SD of sample: 82.87 ## ## Null hypothesis H0 : K Sangakkara 's sample average is within 95% confidence interval ## of population average ## Alternative hypothesis Ha : K Sangakkara 's sample average is below the 95% confidence ## interval of population average ## ## [1] "K Sangakkara 's Form Status: In-Form because the p value: 0.862862 is greater than alpha= 0.05" ## ******************************************************************************************* 3D plot of Runs vs Balls Faced and Minutes at Crease The plot is a scatter plot of Runs vs Balls faced and Minutes at Crease. A prediction plane is fitted par(mfrow=c(1,2)) par(mar=c(4,4,2,2)) battingPerf3d("./tendulkar.csv","Tendulkar") battingPerf3d("./kallis.csv","Kallis") par(mfrow=c(1,2)) par(mar=c(4,4,2,2)) battingPerf3d("./ponting.csv","Ponting") battingPerf3d("./sangakkara.csv","Sangakkara") dev.off() ## null device ## 1 Predicting Runs given Balls Faced and Minutes at Crease A multi-variate regression plane is fitted between Runs and Balls faced +Minutes at crease. A sample sequence of Balls Faced(BF) and Minutes at crease (Mins) is setup as shown below. The fitted model is used to predict the runs for these values BF <- seq( 10, 400,length=15) Mins <- seq(30,600,length=15) newDF <- data.frame(BF,Mins) tendulkar <- batsmanRunsPredict("./tendulkar.csv","Tendulkar",newdataframe=newDF) kallis <- batsmanRunsPredict("./kallis.csv","Kallis",newdataframe=newDF) ponting <- batsmanRunsPredict("./ponting.csv","Ponting",newdataframe=newDF) sangakkara <- batsmanRunsPredict("./sangakkara.csv","Sangakkara",newdataframe=newDF) The fitted model is then used to predict the runs that the batsmen will score for a given Balls faced and Minutes at crease. It can be seen Ponting has the will score the highest for a given Balls Faced and Minutes at crease. Ponting is followed by Tendulkar who has Sangakkara close on his heels and finally we have Kallis. This is intuitive as we have already seen that Ponting has a highest strike rate. batsmen <-cbind(round(tendulkar$Runs),round(kallis$Runs),round(ponting$Runs),round(sangakkara$Runs)) colnames(batsmen) <- c("Tendulkar","Kallis","Ponting","Sangakkara") newDF <- data.frame(round(newDF$BF),round(newDF$Mins)) colnames(newDF) <- c("BallsFaced","MinsAtCrease") predictedRuns <- cbind(newDF,batsmen) predictedRuns ## BallsFaced MinsAtCrease Tendulkar Kallis Ponting Sangakkara ## 1 10 30 7 6 9 2 ## 2 38 71 23 20 25 18 ## 3 66 111 39 34 42 34 ## 4 94 152 54 48 59 50 ## 5 121 193 70 62 76 66 ## 6 149 234 86 76 93 82 ## 7 177 274 102 90 110 98 ## 8 205 315 118 104 127 114 ## 9 233 356 134 118 144 130 ## 10 261 396 150 132 161 146 ## 11 289 437 165 146 178 162 ## 12 316 478 181 159 194 178 ## 13 344 519 197 173 211 194 ## 14 372 559 213 187 228 210 ## 15 400 600 229 201 245 226 Analysis of Top 3 wicket takers The top 3 wicket takes in test history are 1. M Muralitharan:Wickets: 800, Average = 22.72, Economy Rate – 2.47 2. Shane Warne: Wickets: 708, Average = 25.41, Economy Rate – 2.65 3. Anil Kumble: Wickets: 619, Average = 29.65, Economy Rate – 2.69 How do Anil Kumble, Shane Warne and M Muralitharan compare with one another with respect to wickets taken and the Economy Rate. The next set of plots compute and plot precisely these analyses. Wicket Frequency Plot This plot below computes the percentage frequency of number of wickets taken for e.g 1 wicket x%, 2 wickets y% etc and plots them as a continuous line par(mfrow=c(1,3)) par(mar=c(4,4,2,2)) bowlerWktsFreqPercent("./kumble.csv","Anil Kumble") bowlerWktsFreqPercent("./warne.csv","Shane Warne") bowlerWktsFreqPercent("./murali.csv","M Muralitharan") dev.off() ## null device ## 1  Wickets Runs plot par(mfrow=c(1,3)) par(mar=c(4,4,2,2)) bowlerWktsRunsPlot("./kumble.csv","Kumble") bowlerWktsRunsPlot("./warne.csv","Warne") bowlerWktsRunsPlot("./murali.csv","Muralitharan")  dev.off() ## null device ## 1 Average wickets at different venues The plot gives the average wickets taken by Muralitharan at different venues. Muralitharan has taken an average of 8 and 6 wickets at Oval & Wellington respectively in 2 different innings. His best performances are at Kandy and Colombo (SSC) bowlerAvgWktsGround("./murali.csv","Muralitharan") Average wickets against different opposition The plot gives the average wickets taken by Muralitharan against different countries. The x-axis also includes the number of innings against each team bowlerAvgWktsOpposition("./murali.csv","Muralitharan")  Wickets taken moving average From th eplot below it can be see 1. Shane Warne’s performance at the time of his retirement was still at a peak of 3 wickets 2. M Muralitharan seems to have become ineffective over time with his peak years being 2004-2006 3. Anil Kumble also seems to slump down and become less effective. par(mfrow=c(1,3)) par(mar=c(4,4,2,2)) bowlerMovingAverage("./kumble.csv","Anil Kumble") bowlerMovingAverage("./warne.csv","Shane Warne") bowlerMovingAverage("./murali.csv","M Muralitharan") dev.off() ## null device ## 1  Cumulative average wickets taken The plots below give the cumulative average wickets taken by the bowlers par(mfrow=c(1,3)) par(mar=c(4,4,2,2)) bowlerCumulativeAvgWickets("./kumble.csv","Kumble") bowlerCumulativeAvgWickets("./warne.csv","Warne") bowlerCumulativeAvgWickets("./murali.csv","Muralitharan") dev.off() ## null device ## 1 Cumulative average economy rate The plots below give the cumulative average economy rate of the bowlers par(mfrow=c(1,3)) par(mar=c(4,4,2,2)) bowlerCumulativeAvgEconRate("./kumble.csv","Kumble") bowlerCumulativeAvgEconRate("./warne.csv","Warne") bowlerCumulativeAvgEconRate("./murali.csv","Muralitharan") dev.off() ## null device ## 1 Future Wickets forecast Here are plots that forecast how the bowler will perform in future. In this case 90% of the career wickets trend is used as the training set. the remaining 10% is the test set. A Holt-Winters forecating model is used to forecast future performance based on the 90% training set. The forecated wickets trend is plotted. The test set is also plotted to see how close the forecast and the actual matches Take a look at the wickets forecasted for the bowlers below. – Shane Warne and Muralitharan have a fairly consistent forecast – Kumble forecast shows a small dip par(mfrow=c(1,3)) par(mar=c(4,4,2,2)) bowlerPerfForecast("./kumble.csv","Anil Kumble") bowlerPerfForecast("./warne.csv","Shane Warne") bowlerPerfForecast("./murali.csv","M Muralitharan") dev.off() ## null device ## 1 Contribution to matches won and lost The plot below is extremely interesting 1. Kumble wickets range from 2 to 4 wickets in matches wons with a mean of 3 2. Warne wickets in won matches range from 1 to 4 with more matches won. Clearly there are other bowlers contributing to the wins, possibly the pacers 3. Muralitharan wickets range in winning matches is more than the other 2 and ranges ranges 3 to 5 and clearly had a hand (pun unintended) in Sri Lanka’s wins As discussed above the next 2 charts require the use of getPlayerDataSp() kumblesp <- getPlayerDataSp(30176,tdir=".",tfile="kumblesp.csv",ttype="bowling") warnesp <- getPlayerDataSp(8166,tdir=".",tfile="warnesp.csv",ttype="bowling") muralisp <- getPlayerDataSp(49636,tdir=".",tfile="muralisp.csv",ttype="bowling") par(mfrow=c(1,3)) par(mar=c(4,4,2,2)) bowlerContributionWonLost("kumblesp.csv","Kumble") bowlerContributionWonLost("warnesp.csv","Warne") bowlerContributionWonLost("muralisp.csv","Murali") dev.off() ## null device ## 1  Performance home and overseas From the plot below it can be seen that Kumble & Warne have played more matches overseas than Muralitharan. Both Kumble and Warne show an average of 2 wickers overseas, Murali on the other hand has an average of 2.5 wickets overseas but a slightly less number of matches than Kumble & Warne par(mfrow=c(1,3)) par(mar=c(4,4,2,2)) bowlerPerfHomeAway("kumblesp.csv","Kumble") bowlerPerfHomeAway("warnesp.csv","Warne") bowlerPerfHomeAway("muralisp.csv","Murali")  dev.off() ## null device ## 1   Relative Wickets Frequency Percentage The Relative Wickets Percentage plot shows that M Muralitharan has a large percentage of wickets in the 3-8 wicket range frames <- list("./kumble.csv","./murali.csv","warne.csv") names <- list("Anil KUmble","M Muralitharan","Shane Warne") relativeBowlingPerf(frames,names) Relative Economy Rate against wickets taken Clearly from the plot below it can be seen that Muralitharan has the best Economy Rate among the three frames <- list("./kumble.csv","./murali.csv","warne.csv") names <- list("Anil KUmble","M Muralitharan","Shane Warne") relativeBowlingER(frames,names) Relative cumulative average wickets of bowlers in career The plot below shows that Murali has the best cumulative average wickets taken followed by Kumble and then Warne frames <- list("./kumble.csv","./murali.csv","warne.csv") names <- list("Anil KUmble","M Muralitharan","Shane Warne") relativeBowlerCumulativeAvgWickets(frames,names) Relative cumulative average economy rate of bowlers Muralitharan has the best economy rate followed by Warne and then Kumble frames <- list("./kumble.csv","./murali.csv","warne.csv") names <- list("Anil KUmble","M Muralitharan","Shane Warne") relativeBowlerCumulativeAvgEconRate(frames,names) Check for bowler in-form/out-of-form The below computation uses Null Hypothesis testing and p-value to determine if the bowler is in-form or out-of-form. For this 90% of the career wickets is chosen as the population and the mean computed. The last 10% is chosen to be the sample set and the sample Mean and the sample Standard Deviation are caculated. The Null Hypothesis (H0) assumes that the bowler continues to stay in-form where the sample mean is within 95% confidence interval of population mean The Alternative (Ha) assumes that the bowler is out of form the sample mean is beyond the 95% confidence interval of the population mean. A significance value of 0.05 is chosen and p-value us computed If p-value >= .05 – Batsman In-Form If p-value < 0.05 – Batsman Out-of-Form Note Ideally the p-value should be done for a population that follows the Normal Distribution. But the runs population is usually left skewed. So some correction may be needed. I will revisit this later Note: The check for the form status of the bowlers indicate 1. That both Kumble and Muralitharan were out of form. This also shows in the moving average plot 2. Warne is still in great form and could have continued for a few more years. Too bad we didn’t see the magic later checkBowlerInForm("./kumble.csv","Anil Kumble") ## ******************************************************************************************* ## ## Population size: 212 Mean of population: 2.69 ## Sample size: 24 Mean of sample: 2.04 SD of sample: 1.55 ## ## Null hypothesis H0 : Anil Kumble 's sample average is within 95% confidence interval ## of population average ## Alternative hypothesis Ha : Anil Kumble 's sample average is below the 95% confidence ## interval of population average ## ## [1] "Anil Kumble 's Form Status: Out-of-Form because the p value: 0.02549 is less than alpha= 0.05" ## ******************************************************************************************* checkBowlerInForm("./warne.csv","Shane Warne") ## ******************************************************************************************* ## ## Population size: 240 Mean of population: 2.55 ## Sample size: 27 Mean of sample: 2.56 SD of sample: 1.8 ## ## Null hypothesis H0 : Shane Warne 's sample average is within 95% confidence interval ## of population average ## Alternative hypothesis Ha : Shane Warne 's sample average is below the 95% confidence ## interval of population average ## ## [1] "Shane Warne 's Form Status: In-Form because the p value: 0.511409 is greater than alpha= 0.05" ## ******************************************************************************************* checkBowlerInForm("./murali.csv","M Muralitharan") ## ******************************************************************************************* ## ## Population size: 207 Mean of population: 3.55 ## Sample size: 23 Mean of sample: 2.87 SD of sample: 1.74 ## ## Null hypothesis H0 : M Muralitharan 's sample average is within 95% confidence interval ## of population average ## Alternative hypothesis Ha : M Muralitharan 's sample average is below the 95% confidence ## interval of population average ## ## [1] "M Muralitharan 's Form Status: Out-of-Form because the p value: 0.036828 is less than alpha= 0.05" ## ******************************************************************************************* dev.off() ## null device ## 1 Key Findings The plots above capture some of the capabilities and features of my cricketr package. Feel free to install the package and try it out. Please do keep in mind ESPN Cricinfo’s Terms of Use. Here are the main findings from the analysis above Analysis of Top 4 batsman The analysis of the Top 4 test batsman Tendulkar, Kallis, Ponting and Sangakkara show the folliwing 1. Sangakkara has the highest average, followed by Tendulkar, Kallis and then Ponting. 2. Ponting has the highest strike rate followed by Tendulkar,Sangakkara and then Kallis 3. The predicted runs for a given Balls faced and Minutes at crease is highest for Ponting, followed by Tendulkar, Sangakkara and Kallis 4. The moving average for Tendulkar and Ponting shows a downward trend while Kallis and Sangakkara retired too soon 5. Tendulkar was out of form about the time of retirement while the rest were in-form. But this result has to be taken along with the moving average plot. Ponting was clearly on the way out. 6. The home and overseas performance indicate that Tendulkar is the clear leader. He has the highest number of matches played overseas and his performance has been consistent. He is followed by Ponting, Kallis and finally Sangakkara Analysis of Top 3 legs spinners The analysis of Anil Kumble, Shane Warne and M Muralitharan show the following 1. Muralitharan has the highest wickets and best economy rate followed by Warne and Kumble 2. Muralitharan has higher wickets frequency percentage between 3 to 8 wickets 3. Muralitharan has the best Economy Rate for wickets between 2 to 7 4. The moving average plot shows that the time was up for Kumble and Muralitharan but Warne had a few years ahead 5. The check for form status shows that Muralitharan and Kumble time was over while Warne still in great form 6. Kumble’s has more matches abroad than the other 2, yet Kumble averages of 3 wickets at home and 2 wickets overseas liek Warne . Murali has played few matches but has an average of 4 wickets at home and 3 wickets overseas. Final thoughts Here are my final thoughts Batting Among the 4 batsman Tendulkar, Kallis, Ponting and Sangakkara the clear leader is Tendulkar for the following reasons 1. Tendulkar has the highest test centuries and runs of all time.Tendulkar’s average is 2nd to Sangakkara, Tendulkar’s predicted runs for a given Balls faced and Minutes at Crease is 2nd and is behind Ponting. Also Tendulkar’s performance at home and overseas are consistent throughtout despite the fact that he has a highest number of overseas matches 2. Ponting takes the 2nd spot with the 2nd highest number of centuries, 1st in Strike Rate and 2nd in home and away performance. 3. The 3rd spot goes to Sangakkara, with the highest average, 3rd highest number of centuries, reasonable run frequency percentage in different run ranges. However he has a fewer number of matches overseas and his performance overseas is significantly lower than at home 4. Kallis has the 2nd highest number of centuries but his performance overseas and strike rate are behind others 5. Finally Kallis and Sangakkara had a few good years of batting still left in them (pity they retired!) while Tendulkar and Ponting’s time was up 6. While Tendulkars cumulative average stays around 50 runs, Sangakkara briefly overtakes Tendulkar towards the end of his career. Sangakkara may have finished with a better average if he had played for a few more years 7. Ponting has the best overall strike rate followed by Sangakkara Bowling Muralitharan leads the way followed closely by Warne and finally Kumble. The reasons are 1. Muralitharan has the highest number of test wickets with the best Wickets percentage and the best Economy Rate. Murali on average gas taken 4 wickets at home and 3 wickets overseas 2. Warne follows Murali in the highest wickets taken, however Warne has less matches overseas than Murali and average 3 wickets home and 2 wickets overseas 3. Kumble has the 3rd highest wickets, with 3 wickets on an average at home and 2 wickets overseas. However Kumble has played more matches overseas than the other two. In that respect his performance is great. Also Kumble has played less matches at home otherwise his numbers would have looked even better. 4. Also while Kumble and Muralitharan’s career was on the decline , Warne was going great and had a couple of years ahead. 5. Muralitharan has the best cumulative wicket rate and economy rate. Kumble has a better wicket rate than Warne but is more expensive than Warne You can download this analysis at Introducing cricketrYou can download this analysis at Re-Introducing cricketr Also see Beaten by sheer pace! Cricket analytics with yorkr in paperback and Kindle versions My book “Beaten by sheer pace! Cricket analytics with yorkr” is now available in paperback and Kindle versions. The paperback is available from Amazon (US, UK and Europe) for$ 54.95. The Kindle version can be downloaded from the Kindle store for $4.99 (Rs 332/-). Do pick up your copy. It should be a good read for a Sunday afternoon. This book of mine contains my posts based on my R package ‘yorkr’ now in CRAN. The package yorkr uses the data from Cricsheet (http://cricsheet.org/) and can perform analysis of ODI and T20 matches. yorkr can analyze teams against a specific opposition or all oppositions, besides providing details on batsmen or bowlers individual performances The analyses include team batting partnerships, performances of batsmen against bowlers, bowlers against batsmen, bowlers best performances etc. Individual analyses of batsmen strike rate, cumulative average, bowler economy rate, bowler moving average etc can be performances The book includes the following chapters based on my R package yorkr. CONTENTS Preface Foreword 1.Introducing cricket package yorkr: Part 1- Beaten by sheer pace! 2.Introducing cricket package yorkr: Part 2-Trapped leg before wicket! 3.Introducing cricket package yorkr: Part 3-Foxed by flight! 4.Introducing cricket package yorkr:Part 4-In the block hole! 5.yorkr pads up for the Twenty20s: Part 1- Analyzing team’s match performance! 6.yorkr pads up for the Twenty20s: Part 2-Head to head confrontation between teams 7.yorkr pads up for the Twenty20s:Part 3:Overall team performance against all oppositions! 8.yorkr pads up for Twenty20s:Part 4- Individual batting and bowling performances! 9.yorkr crashes the IPL party ! – Part 1 10.yorkr crashes the IPL party! – Part 2 11.yorkr crashes the IPL party! – Part 3! 12.yorkr crashes the IPL party! – Part 4 13.yorkr ranks IPL batsmen and bowlers 14.yorkr ranks T20 batsmen and bowlers 15.yorkr ranks ODI batsmen and bowlers 16.yorkr is generic! Important links Afterword Other books by author About the author Beaten by sheer pace – Cricket analytics with yorkr My ebook “Beaten by sheer pace – Cricket analytics with yorkr’ has been published in Leanpub. You can now download the book (hot off the press!) for all formats to your favorite device (mobile, iPad, tablet, Kindle) from the Leanpub “Beaten by sheer pace!”. The book has been published in the following formats namely • PDF (for your computer) • EPUB (for iPad or tablets. Save the file cricketAnalyticsWithYorkr.epub to Google Drive/Dropbox and choose “Open in” iBooks for iPad) • MOBI (for Kindle. For this format, I suggest that you download & install SendToKindle for PC/Mac. You can then right click the downloaded cricketAnalyticsWithYorkr.mobi and choose SendToKindle. You will need to login to your Kindle account) From Leanpub Leanpub uses a variable pricing model. I have priced the book attractively (I think!). You can choose a price between FREE to$4.99 . The link is “Beaten by sheer pace!

This format works with all type Kindle device, Kindle app, Android tablet, iPad.

yorkr is generic!

The features and functionality in my yorkr package is now complete. My R package yorkr, is totally generic, which means that the R package  can be used for all ODI, T20 matches. Hence yorkr can be used for professional or amateur ODI and T20 matches. The R package can be used for both men and women ODI, T20 international or domestic matches. The main requirement is, that the match data  be created as a Yaml file in the format Cricsheet (Required yaml format for the match data).

I have successfully used my R functions for the Indian Premier League (IPL) matches with changes only to the convertAllYamlFiles2RDataFramesXX (please see posts below)

The convertAllYamlFiles2RDataframes &convertAllYamlFiles2RDataFramesT20 will have to be customized for the names of the teams playing in the domestic professional or amateur matches. All other classes of functions namely Class1, Class2, Class 3 and Class 4 as discussed in my post Introducing cricket package yorkr-Part 1: Beaten by sheer pace can be used as is without any changes.

There are numerous professional & amateur T20 matches that are played around the world. Here are a list of domestic T20 tournaments that are played around the world (from Wikipedia). The yorkr package can be used for any of these matches once the match data is saved as yaml as mentioned above.

So do go ahead and have fun, analyzing cricket performances with yorkr!

Take a look at my book with all my articles related to yorkr now available at Amazon in paperback and Kindle formats  Beaten by sheer pace! Cricket analytics with yorkr. The book is also available at Leanpub, which has a variable pricing Beaten by sheer pace! Cricket analytics with yorkr.

Please take a look at my posts on how to use yorkr for ODI, Twenty20 matches.