Commit df801744 authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

Fixing Graph.write output, adding HUBO interface

parent b7f4ee2c
#ifndef QUANTUM_AQC_COMPILER_HUBO_HPP_
#define QUANTUM_AQC_COMPILER_HUBO_HPP_
#include "DWGraph.hpp"
namespace xacc {
namespace quantum {
class HUBO {
public:
virtual std::shared_ptr<xacc::quantum::DWGraph> reduceToQubo(std::vector<InstructionParameter> parameters) = 0;
virtual ~HUBO() {}
};
}
}
#endif
......@@ -49,6 +49,24 @@ public:
return retGraph;
}
std::string toKernelSource() {
std::string src;
for (int i = 0; i < order(); i++) {
src += std::to_string(i) + " " + std::to_string(i) + " "
+ std::to_string(getVertexProperty<0>(i)) + "\n";
}
for (int i = 0; i < order(); i++) {
for (int j = 0; j < order(); j++) {
if (i < j && edgeExists(i, j)) {
src += std::to_string(i) + " " + std::to_string(j) + " "
+ std::to_string(getEdgeWeight(i, j)) + "\n";
}
}
}
return src;
}
virtual ~DWGraph() {}
};
......
......@@ -292,12 +292,20 @@ void GateQIR::read(std::istream& stream) {
std::string content { std::istreambuf_iterator<char>(stream),
std::istreambuf_iterator<char>() };
std::vector<std::string> lines, sections;
boost::split(sections, content, boost::is_any_of("}"));
std::vector<std::string> lines, sectionOne, sectionTwo;
boost::split(lines, content, boost::is_any_of("\n"));
for (auto l : lines) {
if (boost::contains(l, "label") && boost::contains(l, "--")){
sectionTwo.push_back(l);
} else if (boost::contains(l, "label")) {
sectionOne.push_back(l);
}
}
// Sections should be size 2 for a valid dot file
boost::split(lines, sections[0], boost::is_any_of("\n"));
for (auto line : lines) {
// boost::split(lines, sections[0], boost::is_any_of("\n"));
for (auto line : sectionOne) {
if (boost::contains(line, "label")) {
CircuitNode v;
......@@ -344,8 +352,8 @@ void GateQIR::read(std::istream& stream) {
// Now add the edges
lines.clear();
boost::split(lines, sections[1], boost::is_any_of(";\n"));
for (auto line : lines) {
// boost::split(lines, sections[1], boost::is_any_of(";\n"));
for (auto line : sectionTwo) {
boost::trim(line);
if (line == "}" || line.empty())
continue;
......
......@@ -109,7 +109,6 @@ BOOST_AUTO_TEST_CASE(checkReadGraph) {
// Create a graph IR modeling a
// quantum teleportation kernel
std::string irstr = "graph G {\n"
"{\n"
"node [shape=box style=filled]\n"
"0 [label=\"Gate=InitialState,Circuit Layer=0,Gate Vertex Id=0,Gate Acting Qubits=[0 1 2],Enabled=1\"];\n"
"1 [label=\"Gate=x,Circuit Layer=1,Gate Vertex Id=1,Gate Acting Qubits=[0],Enabled=1\"];\n"
......@@ -128,31 +127,30 @@ BOOST_AUTO_TEST_CASE(checkReadGraph) {
"14 [label=\"Gate=InitialState,Circuit Layer=10,Gate Vertex Id=14,Gate Acting Qubits=[0 1 2],Enabled=0\"];\n"
"15 [label=\"Gate=x,Circuit Layer=11,Gate Vertex Id=15,Gate Acting Qubits=[2],Enabled=0\"];\n"
"16 [label=\"Gate=FinalState,Circuit Layer=12,Gate Vertex Id=16,Gate Acting Qubits=[0 1 2],Enabled=0\"];\n"
"}\n"
"0--1 ;\n"
"0--2 ;\n"
"2--3 ;\n"
"0--3 ;\n"
"1--4 ;\n"
"3--4 ;\n"
"4--5 ;\n"
"5--6 ;\n"
"4--7 ;\n"
"6--8 ;\n"
"7--8 ;\n"
"3--8 ;\n"
"8--9 ;\n"
"9--10 ;\n"
"10--11 ;\n"
"10--12 ;\n"
"10--12 ;\n"
"11--12 ;\n"
"8--13 ;\n"
"13--14 ;\n"
"14--15 ;\n"
"14--16 ;\n"
"14--16 ;\n"
"15--16 ;\n"
"0--1 [label=\"weight=0.000000\"];\n"
"0--2 [label=\"weight=0.000000\"];\n"
"2--3 [label=\"weight=0.000000\"];\n"
"0--3 [label=\"weight=0.000000\"];\n"
"1--4 [label=\"weight=0.000000\"];\n"
"3--4 [label=\"weight=0.000000\"];\n"
"4--5 [label=\"weight=0.000000\"];\n"
"5--6 [label=\"weight=0.000000\"];\n"
"4--7 [label=\"weight=0.000000\"];\n"
"6--8 [label=\"weight=0.000000\"];\n"
"7--8 [label=\"weight=0.000000\"];\n"
"3--8 [label=\"weight=0.000000\"];\n"
"8--9 [label=\"weight=0.000000\"];\n"
"9--10 [label=\"weight=0.000000\"];\n"
"10--11 [label=\"weight=0.000000\"];\n"
"10--12 [label=\"weight=0.000000\"];\n"
"10--12 [label=\"weight=0.000000\"];\n"
"11--12 [label=\"weight=0.000000\"];\n"
"8--13 [label=\"weight=0.000000\"];\n"
"13--14 [label=\"weight=0.000000\"];\n"
"14--15 [label=\"weight=0.000000\"];\n"
"14--16 [label=\"weight=0.000000\"];\n"
"14--16 [label=\"weight=0.000000\"];\n"
"15--16 [label=\"weight=0.000000\"];\n"
"}";
std::istringstream iss(irstr);
......@@ -262,7 +260,6 @@ BOOST_AUTO_TEST_CASE(checkGenerateGraph) {
qir->write(ss);
std::string expected = "graph G {\n"
"{\n"
"node [shape=box style=filled]\n"
"0 [label=\"Gate=InitialState,Circuit Layer=0,Gate Vertex Id=0,Gate Acting Qubits=[0 1 2],Enabled=1,RuntimeParameters=[]\"];\n"
"1 [label=\"Gate=h,Circuit Layer=1,Gate Vertex Id=1,Gate Acting Qubits=[1],Enabled=1,RuntimeParameters=[]\"];\n"
......@@ -270,18 +267,18 @@ BOOST_AUTO_TEST_CASE(checkGenerateGraph) {
"3 [label=\"Gate=cnot,Circuit Layer=3,Gate Vertex Id=3,Gate Acting Qubits=[0 1],Enabled=1,RuntimeParameters=[]\"];\n"
"4 [label=\"Gate=h,Circuit Layer=4,Gate Vertex Id=4,Gate Acting Qubits=[0],Enabled=1,RuntimeParameters=[]\"];\n"
"5 [label=\"Gate=FinalState,Circuit Layer=5,Gate Vertex Id=5,Gate Acting Qubits=[0 1 2],Enabled=1,RuntimeParameters=[]\"];\n"
"}\n"
"0--1 ;\n"
"1--2 ;\n"
"0--2 ;\n"
"0--3 ;\n"
"2--3 ;\n"
"3--4 ;\n"
"4--5 ;\n"
"3--5 ;\n"
"2--5 ;\n"
"0--1 [label=\"weight=0.000000\"];\n"
"1--2 [label=\"weight=0.000000\"];\n"
"0--2 [label=\"weight=0.000000\"];\n"
"0--3 [label=\"weight=0.000000\"];\n"
"2--3 [label=\"weight=0.000000\"];\n"
"3--4 [label=\"weight=0.000000\"];\n"
"4--5 [label=\"weight=0.000000\"];\n"
"3--5 [label=\"weight=0.000000\"];\n"
"2--5 [label=\"weight=0.000000\"];\n"
"}";
std::cout << ss.str() << "\n\n" << expected << "\n";
BOOST_VERIFY(expected == ss.str());
}
......@@ -458,12 +458,29 @@ public:
boost::write_graphviz(ss, *_graph.get(), writer);
auto str = ss.str();
// Modify the style...
str = str.insert(9, "\n{\nnode [shape=box style=filled]");
str = str.insert(9, "\nnode [shape=box style=filled]");
std::vector<std::string> splitVec;
boost::split(splitVec, str, boost::is_any_of("\n"));
splitVec.insert(splitVec.begin() + 3 + order(), "}");
splitVec.insert(splitVec.begin() + 2 + order(), "}\n");
// std::cout << "HELLO:\n " << str << "\n";
for (auto s : splitVec) {
if (boost::contains(s, "--")) {
// THis is an edge
std::vector<std::string> splitEdge;
boost::split(splitEdge, s, boost::is_any_of("--"));
auto e1Str = splitEdge[0];
auto e2Str = splitEdge[2].substr(0, splitEdge[2].size() - 2);
auto e1Idx = std::stod(e1Str);
auto e2Idx = std::stod(e2Str);
auto news = e1Str + "--" + e2Str + " [label=\"weight=" + std::to_string(getEdgeWeight(e1Idx, e2Idx))+ "\"];";
boost::replace_all(str, s, news);
}
}
splitVec.clear();
boost::split(splitVec, str, boost::is_any_of("\n"));
std::stringstream combine;
std::for_each(splitVec.begin(), splitVec.end(), [&](const std::string& elem) { combine << elem << "\n"; });
stream << combine.str().substr(0, combine.str().size() - 2);
......
......@@ -185,24 +185,22 @@ BOOST_AUTO_TEST_CASE(checkWrite) {
std::string expected =
"graph G {\n"
"{\n"
"node [shape=box style=filled]\n"
"0 [label=\"bias=1\"];\n"
"1 [label=\"bias=2\"];\n"
"2 [label=\"bias=3\"];\n"
"3 [label=\"bias=4\"];\n"
"4 [label=\"bias=5\"];\n"
"}\n"
"0--1 ;\n"
"0--2 ;\n"
"0--3 ;\n"
"0--4 ;\n"
"1--2 ;\n"
"1--3 ;\n"
"1--4 ;\n"
"2--3 ;\n"
"2--4 ;\n"
"3--4 ;\n"
"0--1 [label=\"weight=0.000000\"];\n"
"0--2 [label=\"weight=0.000000\"];\n"
"0--3 [label=\"weight=0.000000\"];\n"
"0--4 [label=\"weight=0.000000\"];\n"
"1--2 [label=\"weight=0.000000\"];\n"
"1--3 [label=\"weight=0.000000\"];\n"
"1--4 [label=\"weight=0.000000\"];\n"
"2--3 [label=\"weight=0.000000\"];\n"
"2--4 [label=\"weight=0.000000\"];\n"
"3--4 [label=\"weight=0.000000\"];\n"
"}";
complete5.setVertexProperty<0>(0, 1.0);
complete5.setVertexProperty<0>(1, 2.0);
......@@ -229,24 +227,22 @@ BOOST_AUTO_TEST_CASE(checkWrite) {
expected =
"graph G {\n"
"{\n"
"node [shape=box style=filled]\n"
"0 [label=\"prop1=val1,prop2=1,prop3=1,prop4=1\"];\n"
"1 [label=\"prop1=val2,prop2=2,prop3=2,prop4=2\"];\n"
"2 [label=\"prop1=val3,prop2=3,prop3=3,prop4=3\"];\n"
"3 [label=\"prop1=val4,prop2=4,prop3=4,prop4=4\"];\n"
"4 [label=\"prop1=val5,prop2=5,prop3=5,prop4=5\"];\n"
"}\n"
"0--1 ;\n"
"0--2 ;\n"
"0--3 ;\n"
"0--4 ;\n"
"1--2 ;\n"
"1--3 ;\n"
"1--4 ;\n"
"2--3 ;\n"
"2--4 ;\n"
"3--4 ;\n"
"0--1 [label=\"weight=0.000000\"];\n"
"0--2 [label=\"weight=0.000000\"];\n"
"0--3 [label=\"weight=0.000000\"];\n"
"0--4 [label=\"weight=0.000000\"];\n"
"1--2 [label=\"weight=0.000000\"];\n"
"1--3 [label=\"weight=0.000000\"];\n"
"1--4 [label=\"weight=0.000000\"];\n"
"2--3 [label=\"weight=0.000000\"];\n"
"2--4 [label=\"weight=0.000000\"];\n"
"3--4 [label=\"weight=0.000000\"];\n"
"}";
complete5_4props.setVertexProperty<0>(0, "val1");
complete5_4props.setVertexProperty<0>(1, "val2");
......@@ -306,15 +302,13 @@ BOOST_AUTO_TEST_CASE(checkWrite) {
graph.setVertexProperty<1>(2, 3.0);
expected = "graph G {\n"
"{\n"
"node [shape=box style=filled]\n"
"0 [label=\"prop1=val1,prop2=1\"];\n"
"1 [label=\"prop1=val2,prop2=2\"];\n"
"2 [label=\"prop1=val3,prop2=3\"];\n"
"}\n"
"0--1 ;\n"
"1--2 ;\n"
"2--0 ;\n"
"0--1 [label=\"weight=2.000000\"];\n"
"1--2 [label=\"weight=3.000000\"];\n"
"2--0 [label=\"weight=1.000000\"];\n"
"}";
std::stringstream ss3;
......@@ -337,15 +331,13 @@ BOOST_AUTO_TEST_CASE(checkWrite) {
expected = "graph G {\n"
"{\n"
"node [shape=box style=filled]\n"
"0 [label=\"prop1=[1 2]\"];\n"
"1 [label=\"prop1=[1 2]\"];\n"
"2 [label=\"prop1=[1 2]\"];\n"
"}\n"
"0--1 ;\n"
"1--2 ;\n"
"2--0 ;\n"
"0--1 [label=\"weight=2.000000\"];\n"
"1--2 [label=\"weight=3.000000\"];\n"
"2--0 [label=\"weight=1.000000\"];\n"
"}";
std::stringstream ss4;
......
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