You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
3.0 KiB

#include "Core/Core.h"
#include "Core/QuantumCircuit/QCircuit.h"
#include "Core/QuantumCircuit/QGlobalVariable.h"
#include "Core/QuantumCircuit/QProgram.h"
#include "Core/Utilities/Compiler/QProgToOriginIR.h"
#include "qpanda2/QPanda.h"
#include <iostream>
/// Create a QFT circuit with the input qubits.
///
/// \param qbits: The input qbits
/// \note This is a test function for creating single circuit based on the qbits
/// and it is for test purpose only.
QCircuit QFTCircuit(QVec &qbits) {
QCircuit cirt = createEmptyCircuit();
unsigned vec_size = qbits.size();
for (unsigned i = 0; i < vec_size; i++) {
// H gate
cirt << H(qbits[i]);
// Controlled version of R_m
for (unsigned j = i + 1; j < vec_size; j++) {
cirt << CR(/* Control */ qbits[j], /* Target */ qbits[i],
/* Theta */ (2 * PI) / (pow(2, (j - i + 1))));
}
}
return cirt;
}
/// The main function is mainly used for testing purpose
int main(void) {
std::cout << "QFT Demo implementation, from 1 to N" << std::endl;
// TODO: We can check if cuda is available and use GPU if possible.
auto qvm = initQuantumMachine();
// number of qubits, increse from 1 to N.
unsigned num_bits = 1;
unsigned num_n = 1;
// Get the desired N
std::cout << "Enter N: ";
std::cin >> num_n;
// Allocate initial qubits and cbits.
auto qbits = qAllocMany(num_bits);
auto cbits = cAllocMany(num_bits);
// Create a vector of qcircuits.
std::vector<QCircuit> cirts;
// Create initiail circuit here, with 1 qubits
QCircuit cirt = createEmptyCircuit();
cirt << H(qbits[0]);
cirts.push_back(cirt);
// Create the whole program
QProg prog = QProg();
prog << cirt;
// DEBUG: print the initial program
std::cout << "Initial program: " << std::endl;
std::cout << prog;
// Loop from 1 to N, when increasing, we don't need to reset everything, we
// can, instead, try to add elements to the existing circuit.
while (num_bits < num_n) {
// TODO: we can do measurements here if needed.
// Now increase the qbits, allocate new qubits.
auto new_qbit = qAlloc();
auto new_cbit = cAlloc();
qbits.push_back(new_qbit);
cbits.push_back(new_cbit);
// Create H gate
QCircuit new_cirt = createEmptyCircuit();
new_cirt << H(new_qbit);
// Add R to the new qbit
int oldvec_size = cirts.size();
for (unsigned i = 0; i < oldvec_size; i++) {
cirts[i] << CR(/* Control */ new_qbit, /* Target */ qbits[i],
/* Theta */ (2 * PI) / (pow(2, (oldvec_size - i + 1))));
}
cirts.push_back(new_cirt);
// increase num_bits
num_bits++;
// get the prog
prog << new_cirt;
// DEBUG: print the program
std::cout << "num_bits: " << num_bits << std::endl;
std::cout << prog;
}
// auto result = runWithConfiguration(prog, cbits, 1000);
// Convert to IR if needed
std::cout << "Origin IR: " << std::endl
<< convert_qprog_to_originir(prog, qvm) << std::endl;
// done
destroyQuantumMachine(qvm);
return 0;
}