Skip to content
Snippets Groups Projects
Commit 94c5d222 authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

starting work on parameterized gates

parent da1fabb1
No related branches found
No related tags found
No related merge requests found
...@@ -71,7 +71,7 @@ public: ...@@ -71,7 +71,7 @@ public:
tempSrcFile.close(); tempSrcFile.close();
// Execute the scaffold compiler // Execute the scaffold compiler
std::system("scaffcc -fp .tmpSrcFile.scaffold &> /dev/null"); std::system("scaffcc -fRp .tmpSrcFile.scaffold &> /dev/null");
// Remove the temporary source file, we don't need it anymore // Remove the temporary source file, we don't need it anymore
std::remove(".tmpSrcFile.scaffold"); std::remove(".tmpSrcFile.scaffold");
......
...@@ -155,17 +155,17 @@ void ScaffoldCompiler::modifySource() { ...@@ -155,17 +155,17 @@ void ScaffoldCompiler::modifySource() {
functionName); functionName);
std::string fName = (*begin).str(); std::string fName = (*begin).str();
std::string qbitAllocation = "", fargs; std::string varAllocation = "", fargs;
if (this->typeToVarKernelArgs.find("qbit") != this->typeToVarKernelArgs.end()) { for (auto i : orderOfArgs) {
auto varName = this->typeToVarKernelArgs["qbit"]; auto key = i;
qbitAllocation = "qbit " + varName + ";\n "; auto value = typeToVarKernelArgs[i];
} if ("qbit" == key) {
varAllocation += key + " " + value + ";\n ";
for (auto i = typeToVarKernelArgs.begin(); i != typeToVarKernelArgs.end(); ++i) { fargs += value.substr(0, value.find_first_of("[")) + ",";
if ("qbit" == i->first) {
fargs += i->second.substr(0, i->second.find_first_of("[")) + ",";
} else { } else {
fargs += i->second + ",";
varAllocation += key + " " + value + " = 0;\n ";
fargs += value + ",";
} }
} }
...@@ -175,10 +175,10 @@ void ScaffoldCompiler::modifySource() { ...@@ -175,10 +175,10 @@ void ScaffoldCompiler::modifySource() {
} }
// Now wrap in a main function for ScaffCC // Now wrap in a main function for ScaffCC
kernelSource = kernelSource + std::string("\nint main() {\n ") + qbitAllocation + fName kernelSource = kernelSource + std::string("\nint main() {\n ") + varAllocation + fName
+ std::string(");\n}"); + std::string(");\n}");
// std::cout << "\n" << kernelSource << "\n"; std::cout << "\n" << kernelSource << "\n";
} }
std::shared_ptr<IR> ScaffoldCompiler::compile() { std::shared_ptr<IR> ScaffoldCompiler::compile() {
......
...@@ -100,3 +100,24 @@ BOOST_AUTO_TEST_CASE(checkCodeWithMeasurementIf) { ...@@ -100,3 +100,24 @@ BOOST_AUTO_TEST_CASE(checkCodeWithMeasurementIf) {
BOOST_VERIFY(graphir->size() == 23); BOOST_VERIFY(graphir->size() == 23);
} }
BOOST_AUTO_TEST_CASE(checkCodeWithArgument) {
using GraphType = QuantumCircuit;
auto compiler =
qci::common::AbstractFactory::createAndCast<xacc::ICompiler>(
"compiler", "scaffold");
BOOST_VERIFY(compiler);
const std::string src("__qpu__ kernel (qbit qreg[1], double phi) {\n"
// " qbit qreg[1];\n"
" Rz(qreg[0], phi);\n"
"}\n");
auto ir = compiler->compile(src);
BOOST_VERIFY(ir);
auto graphir = std::dynamic_pointer_cast<xacc::GraphIR<GraphType>>(ir);
BOOST_VERIFY(graphir);
graphir->persist(std::cout);
}
...@@ -103,6 +103,7 @@ public: ...@@ -103,6 +103,7 @@ public:
line.end(), spaceDelim, -1 }, last; line.end(), spaceDelim, -1 }, last;
std::vector<std::string> gateCommand = {first, last}; std::vector<std::string> gateCommand = {first, last};
std::cout << "GSTER: " << gateCommand[0] << "\n";
// Set the gate as a lowercase gate name string // Set the gate as a lowercase gate name string
auto g = boost::to_lower_copy(gateCommand[0]); auto g = boost::to_lower_copy(gateCommand[0]);
boost::trim(g); boost::trim(g);
......
...@@ -104,37 +104,19 @@ public: ...@@ -104,37 +104,19 @@ public:
// so derived types can have reference to it // so derived types can have reference to it
kernelSource = src; kernelSource = src;
kernelArgsToMap();
// Set the accelerator
accelerator = acc; accelerator = acc;
// Get the bit variable type string
auto bitTypeStr = getAsDerived().getBitType(); auto bitTypeStr = getAsDerived().getBitType();
auto firstParen = kernelSource.find_first_of('(');
auto secondParen = kernelSource.find_first_of(')', firstParen);
auto functionArguments = kernelSource.substr(firstParen+1, (secondParen-firstParen)-1);
if (!functionArguments.empty()) { // Get the qubit variable name, if it exists
// First search the prototype to see if it has std::string varName;
// and argument that declares the accelerator bit buffer for (auto it = typeToVarKernelArgs.begin(); it != typeToVarKernelArgs.end(); it++) {
// to use in the kernel if (boost::contains(it->first, bitTypeStr)) {
std::vector<std::string> splitArgs, splitTypeVar; varName = it->second;
boost::split(splitArgs, functionArguments, boost::is_any_of(","));
std::string varName;
for (int i = 0; i < splitArgs.size(); i++) {
// split type from var name
auto s = splitArgs[i];
boost::trim(s);
boost::split(splitTypeVar, s, boost::is_any_of(" "));
auto type = splitTypeVar[0];
auto var = splitTypeVar[1];
boost::trim(type);
boost::trim(var);
typeToVarKernelArgs.insert(std::make_pair(type, var));
if (boost::contains(type, bitTypeStr)) {
varName = var;
}
}
if (typeToVarKernelArgs.find(bitTypeStr)
!= typeToVarKernelArgs.end()) {
auto nBits = accelerator->getBufferSize(varName); auto nBits = accelerator->getBufferSize(varName);
boost::replace_first(kernelSource, boost::replace_first(kernelSource,
std::string(bitTypeStr + " " + varName), std::string(bitTypeStr + " " + varName),
...@@ -147,6 +129,7 @@ public: ...@@ -147,6 +129,7 @@ public:
+ std::to_string(nBits) + "]"; + std::to_string(nBits) + "]";
} }
} }
// Xacc requires that clients provide // Xacc requires that clients provide
// only the body code for an attached // only the body code for an attached
// accelerator... Some language compilers // accelerator... Some language compilers
...@@ -164,6 +147,9 @@ public: ...@@ -164,6 +147,9 @@ public:
*/ */
virtual std::shared_ptr<IR> compile(const std::string& src) { virtual std::shared_ptr<IR> compile(const std::string& src) {
kernelSource = src; kernelSource = src;
kernelArgsToMap();
// Xacc requires that clients provide // Xacc requires that clients provide
// only the body code for an attached // only the body code for an attached
// accelerator... Some language compilers // accelerator... Some language compilers
...@@ -196,11 +182,43 @@ protected: ...@@ -196,11 +182,43 @@ protected:
*/ */
std::map<std::string, std::string> typeToVarKernelArgs; std::map<std::string, std::string> typeToVarKernelArgs;
std::vector<std::string> orderOfArgs;
/** /**
* *
*/ */
std::shared_ptr<IAccelerator> accelerator; std::shared_ptr<IAccelerator> accelerator;
void kernelArgsToMap() {
auto firstParen = kernelSource.find_first_of('(');
auto secondParen = kernelSource.find_first_of(')', firstParen);
auto functionArguments = kernelSource.substr(firstParen+1, (secondParen-firstParen)-1);
int counter = 0;
if (!functionArguments.empty()) {
// First search the prototype to see if it has
// and argument that declares the accelerator bit buffer
// to use in the kernel
std::vector<std::string> splitArgs, splitTypeVar;
boost::split(splitArgs, functionArguments, boost::is_any_of(","));
std::string varName;
for (int i = 0; i < splitArgs.size(); i++) {
// split type from var name
auto s = splitArgs[i];
boost::trim(s);
boost::split(splitTypeVar, s, boost::is_any_of(" "));
auto type = splitTypeVar[0];
auto var = splitTypeVar[1];
boost::trim(type);
boost::trim(var);
std::cout << "Adding " << type << ", " << var << "\n";
typeToVarKernelArgs.insert(std::make_pair(type, var));
orderOfArgs.push_back(type);
}
}
}
/** /**
* *
*/ */
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment