Commit 4144017f authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

made it so open qasm preamble is optional for staq compiler



Signed-off-by: Mccaskey, Alex's avatarAlex McCaskey <mccaskeyaj@ornl.gov>
parent 8acbfe72
Pipeline #85859 passed with stage
in 3 minutes and 58 seconds
# Instructions for building XACC with Staq Support
Simply add the following argument to your cmake call
```
-DSTAQ_DIR=/path/to/staq
```
...@@ -136,6 +136,7 @@ std::shared_ptr<IR> StaqCompiler::compile(const std::string &src, ...@@ -136,6 +136,7 @@ std::shared_ptr<IR> StaqCompiler::compile(const std::string &src,
auto last = _src.find_last_of("}"); auto last = _src.find_last_of("}");
auto sub = _src.substr(first + 1, last - first - 1); auto sub = _src.substr(first + 1, last - first - 1);
auto lines = xacc::split(sub, '\n'); auto lines = xacc::split(sub, '\n');
bool addedNames = false;
for (auto &l : lines) { for (auto &l : lines) {
xacc::trim(l); xacc::trim(l);
tmp += l + "\n"; tmp += l + "\n";
...@@ -144,20 +145,47 @@ std::shared_ptr<IR> StaqCompiler::compile(const std::string &src, ...@@ -144,20 +145,47 @@ std::shared_ptr<IR> StaqCompiler::compile(const std::string &src,
auto size = xacc::getBuffer(b)->size(); auto size = xacc::getBuffer(b)->size();
tmp += "qreg " + b + "[" + std::to_string(size) + "];\n"; tmp += "qreg " + b + "[" + std::to_string(size) + "];\n";
} }
addedNames = true;
} }
} }
_src = tmp; _src = tmp;
if (!addedNames) {
for (auto &b : bufferNames) {
auto size = xacc::getBuffer(b)->size();
_src = "qreg " + b + "[" + std::to_string(size) + "];\n" + _src;
}
}
}
// we allow users to leave out OPENQASM 2.0 and include qelib.inc
// If they did we need to add it for them
std::string tmp = "";
auto lines = xacc::split(_src, '\n');
bool foundOpenQasm = false, foundInclude = false;
for (auto &l : lines) {
if (l.find("OPENQASM") != std::string::npos) {
foundOpenQasm = true;
}
if (l.find("include") != std::string::npos) {
foundInclude = true;
}
} }
// std::cout << "SRC:\n" << _src << "\n"; if (!foundInclude) {
_src = "include \"qelib1.inc\";\n" + _src;
}
if (!foundOpenQasm) {
_src = "OPENQASM 2.0;\n" + _src;
}
// std::cout << " HELLO:\n" << _src << "\n";
using namespace staq; using namespace staq;
auto prog = parser::parse_string(_src); auto prog = parser::parse_string(_src);
transformations::desugar(*prog); transformations::desugar(*prog);
transformations::synthesize_oracles(*prog); transformations::synthesize_oracles(*prog);
optimization::simplify(*prog); optimization::simplify(*prog);
// at this point we have to find out if we have any ancilla // at this point we have to find out if we have any ancilla
// registers // registers
internal_staq::CountAncillas ancillas; internal_staq::CountAncillas ancillas;
...@@ -180,13 +208,13 @@ std::shared_ptr<IR> StaqCompiler::compile(const std::string &src, ...@@ -180,13 +208,13 @@ std::shared_ptr<IR> StaqCompiler::compile(const std::string &src,
// Visit Program to find out how many qreg there are and // Visit Program to find out how many qreg there are and
// use that to build up openqasm xacc function prototype // use that to build up openqasm xacc function prototype
// std::cout << "HELLO:\n"; // std::cout << "HELLO:\n";
// prog->pretty_print(std::cout); // prog->pretty_print(std::cout);
// exit(0); // exit(0);
internal_staq::StaqToXasm translate; internal_staq::StaqToXasm translate;
translate.visit(*prog); translate.visit(*prog);
// std::cout << "XASM:\n" << translate.ss.str() << "\n"; // std::cout << "XASM:\n" << translate.ss.str() << "\n";
std::string kernel; std::string kernel;
if (isXaccKernel) { if (isXaccKernel) {
...@@ -207,16 +235,16 @@ std::shared_ptr<IR> StaqCompiler::compile(const std::string &src, ...@@ -207,16 +235,16 @@ std::shared_ptr<IR> StaqCompiler::compile(const std::string &src,
std::stringstream xx; std::stringstream xx;
std::string name = "tmp"; std::string name = "tmp";
if (xacc::hasCompiled(name)) { if (xacc::hasCompiled(name)) {
int counter = 0; int counter = 0;
while(true) { while (true) {
name = "tmp"+std::to_string(counter); name = "tmp" + std::to_string(counter);
if (!xacc::hasCompiled(name)) { if (!xacc::hasCompiled(name)) {
break; break;
}
counter++;
} }
counter++;
}
} }
xx << "__qpu__ void "<<name<<"("; xx << "__qpu__ void " << name << "(";
xx << "qreg " << c.qregs[0]; xx << "qreg " << c.qregs[0];
for (int i = 1; i < c.qregs.size(); i++) { for (int i = 1; i < c.qregs.size(); i++) {
xx << ", qreg " << c.qregs[i]; xx << ", qreg " << c.qregs[i];
...@@ -227,7 +255,7 @@ std::shared_ptr<IR> StaqCompiler::compile(const std::string &src, ...@@ -227,7 +255,7 @@ std::shared_ptr<IR> StaqCompiler::compile(const std::string &src,
xx << ") {\n" << translate.ss.str() << "}"; xx << ") {\n" << translate.ss.str() << "}";
kernel = xx.str(); kernel = xx.str();
} }
// std::cout << "\n\nFinal:\n" << kernel << "\n"; // std::cout << "\n\nFinal:\n" << kernel << "\n";
return xasm->compile(kernel, acc); return xasm->compile(kernel, acc);
} }
......
...@@ -82,26 +82,21 @@ TEST(InternalCompilerTester, checkStaqAdd) { ...@@ -82,26 +82,21 @@ TEST(InternalCompilerTester, checkStaqAdd) {
c.store(); c.store();
auto src = R"(__qpu__ void add(qreg a, qreg b, qreg c) { auto src = R"(__qpu__ void add(qreg a, qreg b, qreg c) {
OPENQASM 2.0; oracle adder a0,a1,a2,a3,b0,b1,b2,b3,c0,c1,c2,c3 { "@CMAKE_SOURCE_DIR@/quantum/plugins/staq/compiler/tests/adder_4.v" }
include "qelib1.inc"; creg result[4];
// a = 3
x a[0];
x a[1];
oracle adder a0,a1,a2,a3,b0,b1,b2,b3,c0,c1,c2,c3 { "@CMAKE_SOURCE_DIR@/quantum/plugins/staq/compiler/tests/adder_4.v" } // b = 5
x b[0];
x b[2];
creg result[4]; adder a[0],a[1],a[2],a[3],b[0],b[1],b[2],b[3],c[0],c[1],c[2],c[3];
// a = 3 // measure
x a[0]; measure c -> result;
x a[1]; })";
// b = 5
x b[0];
x b[2];
adder a[0],a[1],a[2],a[3],b[0],b[1],b[2],b[3],c[0],c[1],c[2],c[3];
// measure
measure c -> result;
})";
auto circuit = compile("staq", src); auto circuit = compile("staq", src);
xacc::AcceleratorBuffer *bufs[3] = {a.results(), b.results(), c.results()}; xacc::AcceleratorBuffer *bufs[3] = {a.results(), b.results(), c.results()};
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment