library(QCSimulator)
library(ggplot2)
q0=matrix(c(1,0),nrow=2,ncol=1)
q1 =matrix(c(0,1),nrow=2,ncol=1)
I=matrix(c(1,0,0,1),nrow=2,ncol=2)
H=1/sqrt(2) * matrix(c(1,1,1,-1),nrow=2,ncol=2)
S=matrix(c(1,0,0,1i),nrow=2,ncol=2)
S1=matrix(c(1,0,0,-1i),nrow=2,ncol=2)
T=matrix(c(1,0,0,exp(1i*pi/4)),nrow=2,ncol=2)
q00=matrix(c(1,0,0,0),nrow=4,ncol=1)
cnot= matrix(c(1,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0),nrow=4,ncol=4)
The following functions create the dagger function for the gate (transpose of complex conjugate of unitary matrix) and also check for unitariness.
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
#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
The Quantum Gates can be chained by passing the using the invoked quantum gate as a argument to the function
# 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
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
plotMeasurement(q1)
plotMeasurement(Hadamard(q0))
a = measurement(SGate(TGate(Hadamard(TGate(Hadamard(q0))))))
plotMeasurement(a)
# 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
d=Hadamard(TGate(Hadamard(SGate(I))))
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)