Commit 0262a1d4 authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

Fixing bugs throughout, adding all kernels to IR json persistence

parent 17cded3f
......@@ -322,8 +322,7 @@ void RigettiAccelerator::execute(std::shared_ptr<AcceleratorBuffer> buffer,
csvFile.close();
} else if (type == "multishot" || type == "ping" || type == "version") {
if (type == "multishot"){
} else if (type == "multishot") {
std::stringstream ss, oss;
......@@ -360,8 +359,9 @@ void RigettiAccelerator::execute(std::shared_ptr<AcceleratorBuffer> buffer,
oss<<tmp<<std::endl;
}
XACCInfo(oss.str());
}
} else if (type == "ping" || type == "version") {
std::stringstream ss;
ss << postResponse.content.rdbuf();
XACCInfo("Rigetti QVM Response:\n\t" + ss.str());
......
......@@ -54,6 +54,8 @@ if (CLANG_FOUND AND LLVM_FOUND)
include_directories(${CLANG_INCLUDE_DIRS}/extra-tools)
include_directories(${CMAKE_SOURCE_DIR}/quantum/gate)
include_directories(${CMAKE_SOURCE_DIR}/quantum/gate/ir)
include_directories(${CMAKE_SOURCE_DIR}/quantum/gate/utils)
include_directories(${CMAKE_SOURCE_DIR}/tpls/rapidjson/include)
include_directories(${CMAKE_SOURCE_DIR}/quantum/gate/ir/instructions)
add_library(${LIBRARY_NAME} SHARED ${SRC})
......
......@@ -61,13 +61,9 @@ std::shared_ptr<IR> ScaffoldCompiler::compile(const std::string& src,
// Replace the __qpu__ attribute with module
if (boost::contains(kernelSource, "__qpu__")) {
boost::replace_all(kernelSource, "__qpu__", "module");
// kernelSource.erase(kernelSource.find("__qpu__"), 7);
// kernelSource = std::string("module") + kernelSource;
}
std::cout << "HELLO WORLD: \n" << kernelSource << "\n";
// Make a copy, and then delete the body
// of the kernel in that copy, leaving
// just the functionDeclaration(qbit qvar) {}
......
......@@ -34,6 +34,7 @@
#include <boost/test/included/unit_test.hpp>
#include "ScaffoldCompiler.hpp"
#include "GateQIR.hpp"
#include "JsonVisitor.hpp"
using namespace xacc::quantum;
......@@ -190,6 +191,60 @@ BOOST_AUTO_TEST_CASE(checkTeleportWithFunctions) {
qir2->persist(std::cout);
}
BOOST_AUTO_TEST_CASE(checkVQEExample) {
const std::string src(""
"module initializeState(qbit qreg[2], float theta) {\n"
" Rx(qreg[0], 1.57079);\n"
" Ry(qreg[1], 1.57079);\n"
" Rx(qreg[0], -1.57079);\n"
" CNOT(qreg[0], qreg[1]);\n"
" Rz(qreg[0], theta);\n"
" CNOT(qreg[0], qreg[1]);\n"
" Ry(qreg[1], -1.57079);\n"
" Rx(qreg[0], 1.57079);\n"
"}\n"
""
"module g1Term (qbit qreg[2], float theta) {\n"
" initializeState(qreg, theta);\n"
" cbit creg[2];\n"
" creg[0] = MeasZ(qreg[0]);\n"
" creg[1] = MeasZ(qreg[1]);\n"
"}\n"
""
"module g4Term(qbit qreg[2], float theta) {\n"
" initializeState(qreg, theta);\n"
" cbit creg[2];\n"
" Rx(qreg[0], -1.57079);\n"
" Rx(qreg[1], -1.57079);\n"
" creg[0] = MeasZ(qreg[0]);\n"
" creg[1] = MeasZ(qreg[1]);\n"
"}\n"
""
"module g5Term(qbit qreg[2], float theta) {\n"
" initializeState(qreg, theta);\n"
" cbit creg[2];\n"
" H(qreg[0]);\n"
" H(qreg[1]);\n"
" creg[0] = MeasZ(qreg[0]);\n"
" creg[1] = MeasZ(qreg[1]);\n"
"}\n"
"");
ScaffoldCompiler compiler;
auto ir = compiler.compile(src);
// for (auto k : ir->getKernels()) {
// JsonVisitor v(k);
// std::cout << v.write() << "\n";
// }
JsonVisitor visitor(ir->getKernels());
std::cout << "HI:\n" << visitor.write() << "\n";
}
/*
BOOST_AUTO_TEST_CASE(checkMultipleFunction) {
const std::string src(
......
......@@ -48,7 +48,9 @@ public:
virtual std::shared_ptr<Function> getKernel(const std::string& name) {
}
virtual std::vector<std::shared_ptr<Function>> getKernels() {
}
};
}
......
......@@ -266,7 +266,7 @@ std::string GateQIR::toAssemblyString(const std::string& kernelName, const std::
void GateQIR::persist(std::ostream& outStream) {
JsonVisitor visitor(kernels[0]);
JsonVisitor visitor(kernels);
outStream << visitor.write();
// StringBuffer sb;
......
......@@ -156,6 +156,10 @@ public:
*/
virtual void read(std::istream& stream);
virtual std::vector<std::shared_ptr<Function>> getKernels() {
return kernels;
}
/**
* The destructor
*/
......
......@@ -57,38 +57,84 @@ protected:
std::shared_ptr<Function> function;
std::shared_ptr<InstructionIterator> topLevelInstructionIterator;
std::vector<std::shared_ptr<Function>> functions;
public:
JsonVisitor(std::shared_ptr<xacc::Function> f) : buffer(std::make_shared<StringBuffer>()),
writer(std::make_shared<PrettyWriter<StringBuffer>>(*buffer.get())), function(f) {
JsonVisitor(std::shared_ptr<xacc::Function> f) :
buffer(std::make_shared<StringBuffer>()), writer(
std::make_shared<PrettyWriter<StringBuffer>>(
*buffer.get())), function(f) {
}
JsonVisitor(std::vector<std::shared_ptr<xacc::Function>> fs) :
buffer(std::make_shared<StringBuffer>()), writer(
std::make_shared<PrettyWriter<StringBuffer>>(
*buffer.get())), functions(fs) {
}
std::string write() {
// This is a Function, start it as an Object
writer->StartObject();
writer->String("function");
writer->String(function->getName());
// All functions have instructions, start
// that array here.
writer->String("instructions");
writer->String("kernels");
writer->StartArray();
for (auto f : functions) {
// This is a Function, start it as an Object
writer->StartObject();
writer->String("function");
writer->String(f->getName());
// All functions have instructions, start
// that array here.
writer->String("instructions");
writer->StartArray();
topLevelInstructionIterator = std::make_shared<
xacc::InstructionIterator>(f);
while (topLevelInstructionIterator->hasNext()) {
// Get the next node in the tree
auto nextInst = topLevelInstructionIterator->next();
nextInst->accept(this);
}
topLevelInstructionIterator = std::make_shared<xacc::InstructionIterator>(function);
while (topLevelInstructionIterator->hasNext()) {
// Get the next node in the tree
auto nextInst = topLevelInstructionIterator->next();
nextInst->accept(this);
}
// End Instructions
writer->EndArray();
// End Instructions
// End Function
writer->EndObject();
}
writer->EndArray();
// End Function
writer->EndObject();
return buffer->GetString();
}
// std::string write() {
// // This is a Function, start it as an Object
// writer->StartObject();
// writer->String("function");
// writer->String(function->getName());
//
// // All functions have instructions, start
// // that array here.
// writer->String("instructions");
// writer->StartArray();
//
// topLevelInstructionIterator = std::make_shared<xacc::InstructionIterator>(function);
// while (topLevelInstructionIterator->hasNext()) {
// // Get the next node in the tree
// auto nextInst = topLevelInstructionIterator->next();
// nextInst->accept(this);
// }
//
// // End Instructions
// writer->EndArray();
//
// // End Function
// writer->EndObject();
// return buffer->GetString();
// }
void visit(Hadamard& h) {
baseGateInst(dynamic_cast<GateInstruction&>(h));
......@@ -115,9 +161,12 @@ public:
baseGateInst(dynamic_cast<GateInstruction&>(rx), false);
writer->String("angle");
auto p = rx.getParameter(0);
p.which() == 1 ? // Rz's parameter can only be of type double or string
writer->Double(boost::get<double>(p)) :
writer->String(boost::get<std::string>(p));
switch(p.which()) {
case 0: writer->Int(boost::get<int>(p));break;
case 1: writer->Double(boost::get<double>(p));break;
case 2: writer->Double((double)boost::get<float>(p));break;
case 3: writer->String(boost::get<std::string>(p));break;
}
writer->EndObject();
}
......
......@@ -124,6 +124,4 @@ BOOST_AUTO_TEST_CASE(checkFunctionWithFunction) {
std::cout << "HELLO: \n" << json << "\n";
}
......@@ -97,8 +97,13 @@ public:
*/
virtual bool isComposite() { return true; }
/**
*
*/
virtual void evaluateVariableParameters(std::vector<InstructionParameter> parameters) = 0;
// virtual void persist(std::ostream& stream) = 0;
/**
* The destructor
*/
......
......@@ -101,6 +101,8 @@ public:
*/
virtual std::shared_ptr<Function> getKernel(const std::string& name) = 0;
virtual std::vector<std::shared_ptr<Function>> getKernels() = 0;
/**
* The destructor
*/
......
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