A simple Quantum System in CPP through Rcpp interface. The code is based on the marvellous implementation of "Quantum Computer Gate Playground".
A brief introduction to Quantum Computing can be found here. This article provides a brief introduction to the main concepts and operations on a Quantum Computing. All concepts are mainly based on Quantum Computing since Democritus and these lectures notes.
S4 objects are use in order to create a simple class to define a Quantum System with a vector representing qBits and one Hadamard gate to operate on the system itself.
setClass (
# Class name
"QuantumSystem",
# Defining slot type
representation (
qbitVector = "numeric",
hadamardGate= "numeric"
),
# Initializing slots
prototype = list(
qbitVector = NULL,
hadamardGate = NULL)
)
From C code, we can direclty interact with S4 objects, for instance:
S4 init_system(int nQubits) {
// Creating an object of QuantumSystem class
S4 x("QuantumSystem");
....
x.slot("hadamardGate") = ....;
return(x);
}
and also from R i easy to interact with S4 objects
quaSys <- init_system(nQubits = 1)
quaSys@hadamardGate
As long as operations on qBits can be simulated by means of matrix operations, RcppArmadillo provides an optimized way to applied optimized matrix operations. For instance, a n-times kron operation can be applied as follows:
//Kronecker tensor product
for(int i=0; i<nQubits-1; i++) {
Htimes = arma::kron(Htimes, H);
}
- Init the quantum system
# Init quantum system
quaSys <- init_system(nQubits = 1)
- System's meassurement (system collapses)
meassurement(quaSys)
# Notice the collapse produced
quaSys@qbitVector
- Apply Hadamard gate (quantum interferance)
# Check qbit states
firstQbits <- quaSys@qbitVector
applyHadamard(quaSys) # Apply Hadamard gate
secondQbits <- quaSys@qbitVector
applyHadamard(quaSys) # Apply Hadamard gate
# Check superpositions
assertthat::assert_that(all(firstQbits != secondQbits))
assertthat::are_equal(firstQbits, quaSys@qbitVector)
Note the quantum interferance effect, when Hadamard gate is applied twice, as follows:
The next figure graphically represents, how using operations provided in this simulator one state can be searched. Concepts like Hadamard gates, phase flip or quantum cancellation are used.
As example, Grover's algorithm is implemented following the next pseudocode:
# Init quantum system
quaSys <- init_system(nQubits = 13)
# Grover's search
for (i in seq(100)){
# Set target through oracle function
applyOracleFunction(quaSys, target = 1022)
# Apply hadamard
applyHadamard(quaSys)
# Apply difusion
applyDifussion(quaSys)
# Apply hadamard
applyHadamard(quaSys)
}
The next figure shows how the probability on the target qBit is higer in each iteration: