Commit 6ec1d4ca authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

Configuring ScaffoldCompiler to better handle multiple function declarations

parent 49300182
......@@ -14,7 +14,7 @@ run cd /projects/xacc && mkdir rpm_build && cd rpm_build && togo configure -n "A
&& sed -i -r "s/(Version: *).*/\11.0/" spec/header && sed -i -r "s/(Summary: *).*/\1XACC - eXtreme-scale ACCelerator programming environment./" spec/header \
&& sed -i -r "s/(Release: *).*/\11.fc25/" spec/header && sed -i -r "s/(License: *).*/\1BSD/" spec/header \
&& sed -i -r "s/(Buildarch: *).*/\1x86_64/" spec/header && sed -i -r "s/(Group: *).*/\1Development\/System/" spec/header \
&& sed -i -r "s/(Requires: *).*/\1gcc-c++ cmake git make libtool spdlog mpich-devel boost-mpich-devel/" spec/header \
&& sed -i -r "s/(Requires: *).*/\1gcc-c++ cmake python-devel git make libtool spdlog mpich-devel boost-mpich-devel/" spec/header \
&& sed -i -r '/Requires/s/^#//g' spec/header \
&& togo build package && cp rpms/*.rpm /projects/ \
&& cat spec/header
......@@ -14,5 +14,5 @@ run apt-get -y update && apt-get -y install wget && wget https://github.com/ORNL
&& cd xacc && mkdir build && cd build && cmake .. -DCMAKE_INSTALL_PREFIX=/usr/local/xacc && make && make install
run cd /xacc && mkdir deb_build && cd deb_build && mkdir -p usr/local/xacc && cp -r /usr/local/xacc/* usr/local/xacc/ \
&& fpm -s dir -t deb --name xacc --version 1.0 --depends libboost-all-dev --depends gcc-6 --depends g++-6 --depends mpich \
&& fpm -s dir -t deb --name xacc --version 1.0 --depends libboost-all-dev --depends gcc-6 --depends g++-6 --depends python-dev --depends mpich \
--depends libspdlog-dev --depends cmake --depends git --depends make --depends libtool --description "XACC - eXtreme-scale ACCelerator programming framework" . && cp *.deb /
......@@ -36,6 +36,7 @@
#include "GateInstruction.hpp"
#include "GateFunction.hpp"
#include "ConditionalFunction.hpp"
#include "GateQIR.hpp"
using namespace clang;
......@@ -49,6 +50,8 @@ protected:
std::string cbitVarName;
std::string qbitVarName;
std::shared_ptr<xacc::quantum::GateQIR> ir;
std::shared_ptr<xacc::quantum::GateFunction> function;
std::shared_ptr<xacc::quantum::ConditionalFunction> currentConditional;
......@@ -58,6 +61,8 @@ protected:
public:
ScaffoldASTConsumer() : ir(std::make_shared<xacc::quantum::GateQIR>()) {}
// Override the method that gets called for each parsed top-level
// declaration.
virtual bool HandleTopLevelDecl(DeclGroupRef DR) {
......@@ -150,6 +155,7 @@ public:
}
function = std::make_shared<xacc::quantum::GateFunction>(
c->getDeclName().getAsString(), parameters);
ir->addKernel(function);
}
return true;
}
......@@ -163,50 +169,62 @@ public:
bool isParameterizedInst = false;
auto fd = c->getDirectCallee();
auto gateName = fd->getNameInfo().getAsString();
std::vector<int> qubits;
std::vector<xacc::InstructionParameter> params;
for (auto i = c->arg_begin(); i != c->arg_end(); ++i) {
std::string arg;
llvm::raw_string_ostream argstream(arg);
i->printPretty(argstream, nullptr, policy);
auto argStr = argstream.str();
auto gateRegistry = xacc::quantum::GateInstructionRegistry::instance();
auto availableGates = gateRegistry->getRegisteredIds();
std::shared_ptr<xacc::Instruction> inst;
if (!std::any_of(availableGates.cbegin(), availableGates.cend(),
[=](std::string i) {return i == gateName;})) {
if (ir->kernelExists(gateName)) {
inst = ir->getKernel(gateName);
}
} else {
std::vector<int> qubits;
std::vector<xacc::InstructionParameter> params;
for (auto i = c->arg_begin(); i != c->arg_end(); ++i) {
std::string arg;
llvm::raw_string_ostream argstream(arg);
i->printPretty(argstream, nullptr, policy);
auto argStr = argstream.str();
// std::cout << "Arg: " << argstream.str() << "\n";
if (boost::contains(argStr, qbitVarName)) {
boost::replace_all(argStr, qbitVarName, "");
boost::replace_all(argStr, "[", "");
boost::replace_all(argStr, "]", "");
qubits.push_back(std::stoi(argStr));
} else {
// This is a gate parameter!!!
isParameterizedInst = true;
// This parameter could just be a hard-coded value
// or it could be a reference to a variable parameter...
try {
double d = boost::lexical_cast<double>(argStr);
params.push_back(d);
} catch (const boost::bad_lexical_cast &) {
params.push_back(argStr);
if (boost::contains(argStr, qbitVarName)) {
boost::replace_all(argStr, qbitVarName, "");
boost::replace_all(argStr, "[", "");
boost::replace_all(argStr, "]", "");
qubits.push_back(std::stoi(argStr));
} else {
// This is a gate parameter!!!
isParameterizedInst = true;
// This parameter could just be a hard-coded value
// or it could be a reference to a variable parameter...
try {
double d = boost::lexical_cast<double>(argStr);
params.push_back(d);
} catch (const boost::bad_lexical_cast &) {
params.push_back(argStr);
}
}
}
}
std::shared_ptr<xacc::quantum::GateInstruction> inst;
if (isParameterizedInst) {
if (isParameterizedInst) {
int idx = 0;
inst = xacc::quantum::GateInstructionRegistry::instance()->create(gateName, qubits);
for (auto p : params) {
inst->setParameter(idx, p);
idx++;
}
int idx = 0;
inst = gateRegistry->create(gateName, qubits);
for (auto p : params) {
inst->setParameter(idx, p);
idx++;
}
} else if (gateName != "MeasZ") {
} else if (gateName != "MeasZ") {
inst = gateRegistry->create(gateName, qubits);
}
inst =
xacc::quantum::GateInstructionRegistry::instance()->create(
gateName, qubits);
}
if (gateName != "MeasZ") {
......@@ -278,8 +296,8 @@ public:
return true;
}
std::shared_ptr<xacc::Function> getFunction() {
return function;
std::shared_ptr<xacc::IR> getIR() {
return ir;
}
const std::string getQubitVariableName() {
......
......@@ -129,16 +129,8 @@ std::shared_ptr<IR> ScaffoldCompiler::compile(const std::string& src,
clang::ParseAST(CI->getPreprocessor(), consumer.get(), CI->getASTContext());
// Get the IR Function representation
auto qirFunction = consumer->getFunction();
return consumer->getIR();
// Create a Quantum IR instance
auto qir = std::make_shared<GateQIR>();
// Give the function to the IR
qir->addKernel(qirFunction);
// Return...
return qir;
}
std::shared_ptr<IR> ScaffoldCompiler::compile(const std::string& src) {
......@@ -165,13 +157,7 @@ std::shared_ptr<IR> ScaffoldCompiler::compile(const std::string& src) {
consumer = std::make_shared<scaffold::ScaffoldASTConsumer>();
clang::ParseAST(CI->getPreprocessor(), consumer.get(), CI->getASTContext());
auto qirFunction = consumer->getFunction();
auto qir = std::make_shared<GateQIR>();
qir->addKernel(qirFunction);
return qir;
return consumer->getIR();
}
}
......
......@@ -148,6 +148,52 @@ BOOST_AUTO_TEST_CASE(checkWithParameter) {
gateqir->persist(std::cout);
}
BOOST_AUTO_TEST_CASE(checkTwoFunctions) {
const std::string src(
"module init(qbit qreg[3], double phi) {\n"
" Rz(qreg[0], phi);\n"
"}\n"
"\n"
"module gateWithParam (qbit qreg[3], double phi) {\n"
" init(qreg, phi);\n"
" H(qreg[1]);\n"
" X(qreg[0]);\n"
"}\n");
auto qir = compiler->compile(src);
qir->persist(std::cout);
}
BOOST_AUTO_TEST_CASE(checkTeleportWithFunctions) {
const std::string src2(
"module init(qbit qreg[3]) {\n"
" X(qreg[0]);\n"
"}\n"
"module createBellPair(qbit qreg[3]) {\n"
" H(qreg[1]);\n"
" CNOT(qreg[1], qreg[2]);\n"
"}\n"
"module teleport (qbit qreg[3]) {\n"
" cbit creg[2];\n"
" // Init qubit 0 to 1\n"
" init(qreg);\n"
" // create bell pair\n"
" createBellPair(qreg);\n"
" // Now teleport...\n"
" CNOT(qreg[0],qreg[1]);\n"
" H(qreg[0]);\n"
" creg[0] = MeasZ(qreg[0]);\n"
" creg[1] = MeasZ(qreg[1]);\n"
" if (creg[0] == 1) Z(qreg[2]);\n"
" if (creg[1] == 1) X(qreg[2]);\n"
"}\n");
auto qir2 = compiler->compile(src2);
qir2->persist(std::cout);
}
/*
BOOST_AUTO_TEST_CASE(checkMultipleFunction) {
const std::string src(
......
......@@ -41,6 +41,10 @@ public:
}
virtual bool kernelExists(const std::string& name) {
return false;
}
virtual std::shared_ptr<Function> getKernel(const std::string& name) {
}
......
......@@ -118,6 +118,11 @@ public:
XACCError("Invalid kernel name.");
}
virtual bool kernelExists(const std::string& name) {
return std::any_of(kernels.cbegin(), kernels.cend(),
[=](std::shared_ptr<Function> i) {return i->getName() == name;});
}
/**
* Return a string representation of this
* intermediate representation
......
......@@ -98,3 +98,32 @@ BOOST_AUTO_TEST_CASE(checkTeleportSerialization) {
std::cout << "HELLO: \n" << json << "\n";
}
BOOST_AUTO_TEST_CASE(checkFunctionWithFunction) {
auto f = std::make_shared<GateFunction>("foo");
auto init = std::make_shared<GateFunction>("init");
xacc::InstructionParameter p = "phi";
auto rz = GateInstructionRegistry::instance()->create("Rz", std::vector<int>{0});
rz->setParameter(0, p);
init->addInstruction(rz);
f->addInstruction(init);
auto x = std::make_shared<X>(0);
auto h = std::make_shared<Hadamard>(1);
f->addInstruction(x);
f->addInstruction(h);
JsonVisitor visitor(f);
auto json = visitor.write();
std::cout << "HELLO: \n" << json << "\n";
}
......@@ -85,6 +85,14 @@ public:
*/
virtual void addKernel(std::shared_ptr<Function> kernel) = 0;
/**
* Return true if the kernel with given name exists in this IR.
*
* @param name The name of the kernel to return.
* @return exists True if kernel exists.
*/
virtual bool kernelExists(const std::string& name) = 0;
/**
* Return the kernel with the given name.
*
......
Markdown is supported
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