diff --git a/quantum/gate/compilers/scaffold/ScaffCCAPI.hpp b/quantum/gate/compilers/scaffold/ScaffCCAPI.hpp index 0cbecadec63998928b51cc78793f4d2974344717..d551a4efee1df6215e1845ae7d7efcd0512e0886 100644 --- a/quantum/gate/compilers/scaffold/ScaffCCAPI.hpp +++ b/quantum/gate/compilers/scaffold/ScaffCCAPI.hpp @@ -71,7 +71,7 @@ public: tempSrcFile.close(); // 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 std::remove(".tmpSrcFile.scaffold"); diff --git a/quantum/gate/compilers/scaffold/ScaffoldCompiler.cpp b/quantum/gate/compilers/scaffold/ScaffoldCompiler.cpp index 3fed7012ef8b3dafd206572c9b9c15cb0b078e57..79e670b2eabf045bb56a3f2c22dda4b6e7d6ef74 100644 --- a/quantum/gate/compilers/scaffold/ScaffoldCompiler.cpp +++ b/quantum/gate/compilers/scaffold/ScaffoldCompiler.cpp @@ -155,17 +155,17 @@ void ScaffoldCompiler::modifySource() { functionName); std::string fName = (*begin).str(); - std::string qbitAllocation = "", fargs; - if (this->typeToVarKernelArgs.find("qbit") != this->typeToVarKernelArgs.end()) { - auto varName = this->typeToVarKernelArgs["qbit"]; - qbitAllocation = "qbit " + varName + ";\n "; - } - - for (auto i = typeToVarKernelArgs.begin(); i != typeToVarKernelArgs.end(); ++i) { - if ("qbit" == i->first) { - fargs += i->second.substr(0, i->second.find_first_of("[")) + ","; + std::string varAllocation = "", fargs; + for (auto i : orderOfArgs) { + auto key = i; + auto value = typeToVarKernelArgs[i]; + if ("qbit" == key) { + varAllocation += key + " " + value + ";\n "; + fargs += value.substr(0, value.find_first_of("[")) + ","; } else { - fargs += i->second + ","; + + varAllocation += key + " " + value + " = 0;\n "; + fargs += value + ","; } } @@ -175,10 +175,10 @@ void ScaffoldCompiler::modifySource() { } // 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::cout << "\n" << kernelSource << "\n"; + std::cout << "\n" << kernelSource << "\n"; } std::shared_ptr<IR> ScaffoldCompiler::compile() { diff --git a/quantum/gate/compilers/scaffold/tests/ScaffoldCompilerTester.cpp b/quantum/gate/compilers/scaffold/tests/ScaffoldCompilerTester.cpp index d9d2193f994608be8aa869bceaed9ac1b1283137..44ed9a20063dc0906569da2ef21f21c2142b0a02 100644 --- a/quantum/gate/compilers/scaffold/tests/ScaffoldCompilerTester.cpp +++ b/quantum/gate/compilers/scaffold/tests/ScaffoldCompilerTester.cpp @@ -100,3 +100,24 @@ BOOST_AUTO_TEST_CASE(checkCodeWithMeasurementIf) { 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); +} diff --git a/quantum/gate/utils/QasmToGraph.hpp b/quantum/gate/utils/QasmToGraph.hpp index c4cbd8667d89b994093cb70e76d7fc2c5d812cea..0e515f9dbc8d6779d95b04bd91549e102a108c5b 100644 --- a/quantum/gate/utils/QasmToGraph.hpp +++ b/quantum/gate/utils/QasmToGraph.hpp @@ -103,6 +103,7 @@ public: line.end(), spaceDelim, -1 }, last; std::vector<std::string> gateCommand = {first, last}; + std::cout << "GSTER: " << gateCommand[0] << "\n"; // Set the gate as a lowercase gate name string auto g = boost::to_lower_copy(gateCommand[0]); boost::trim(g); diff --git a/xacc/compiler/Compiler.hpp b/xacc/compiler/Compiler.hpp index d68cdbd90b579a70392b32ac4e53ffce0f57ba69..70ea763ff42b1de08b9daeb14cc5b890a6fcbe45 100644 --- a/xacc/compiler/Compiler.hpp +++ b/xacc/compiler/Compiler.hpp @@ -104,37 +104,19 @@ public: // so derived types can have reference to it kernelSource = src; + kernelArgsToMap(); + + // Set the accelerator accelerator = acc; + // Get the bit variable type string 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()) { - // 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); - typeToVarKernelArgs.insert(std::make_pair(type, var)); - if (boost::contains(type, bitTypeStr)) { - varName = var; - } - } - - if (typeToVarKernelArgs.find(bitTypeStr) - != typeToVarKernelArgs.end()) { + // Get the qubit variable name, if it exists + std::string varName; + for (auto it = typeToVarKernelArgs.begin(); it != typeToVarKernelArgs.end(); it++) { + if (boost::contains(it->first, bitTypeStr)) { + varName = it->second; auto nBits = accelerator->getBufferSize(varName); boost::replace_first(kernelSource, std::string(bitTypeStr + " " + varName), @@ -147,6 +129,7 @@ public: + std::to_string(nBits) + "]"; } } + // Xacc requires that clients provide // only the body code for an attached // accelerator... Some language compilers @@ -164,6 +147,9 @@ public: */ virtual std::shared_ptr<IR> compile(const std::string& src) { kernelSource = src; + + kernelArgsToMap(); + // Xacc requires that clients provide // only the body code for an attached // accelerator... Some language compilers @@ -196,11 +182,43 @@ protected: */ std::map<std::string, std::string> typeToVarKernelArgs; + std::vector<std::string> orderOfArgs; + /** * */ 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); + } + + } + } /** * */