Unverified Commit 84a56aba authored by Mccaskey, Alex's avatar Mccaskey, Alex Committed by GitHub
Browse files

Merge pull request #487 from tnguyen-ornl/tnguyen/circuit-layer-id

Adding layer metadata to gate node in the DAG representation
parents 9c7d2f14 abec43a3
Pipeline #164904 passed with stage
in 12 minutes and 28 seconds
...@@ -199,7 +199,7 @@ void Circuit::load(std::istream &inStream) { ...@@ -199,7 +199,7 @@ void Circuit::load(std::istream &inStream) {
} }
} }
const int Circuit::depth() { return toGraph()->depth() - 2; } const int Circuit::depth() { return toGraph()->depth(); }
const std::string Circuit::persistGraph() { const std::string Circuit::persistGraph() {
std::stringstream s; std::stringstream s;
......
...@@ -279,11 +279,11 @@ TEST(GateTester, checkGenerateGraph) { ...@@ -279,11 +279,11 @@ TEST(GateTester, checkGenerateGraph) {
std::string expected = R"expected(digraph G { std::string expected = R"expected(digraph G {
node [shape=box style=filled] node [shape=box style=filled]
0 [label="bits=[0,1,2];id=0;name=InitialState"]; 0 [label="bits=[0,1,2];id=0;name=InitialState"];
1 [label="bits=[1];id=1;name=H"]; 1 [label="bits=[1];id=1;layer=0;name=H"];
2 [label="bits=[1,2];id=2;name=CNOT"]; 2 [label="bits=[1,2];id=2;layer=1;name=CNOT"];
3 [label="bits=[0,1];id=3;name=CNOT"]; 3 [label="bits=[0,1];id=3;layer=2;name=CNOT"];
4 [label="bits=[0];id=4;name=H"]; 4 [label="bits=[0];id=4;layer=3;name=H"];
5 [label="bits=[2];id=5;name=Rz"]; 5 [label="bits=[2];id=5;layer=3;name=Rz"];
6 [label="bits=[0,1,2];id=6;name=FinalState"]; 6 [label="bits=[0,1,2];id=6;name=FinalState"];
0->1 ; 0->1 ;
0->2 ; 0->2 ;
......
...@@ -30,7 +30,10 @@ void IRToGraphVisitor::addSingleQubitGate(Gate &inst) { ...@@ -30,7 +30,10 @@ void IRToGraphVisitor::addSingleQubitGate(Gate &inst) {
std::make_pair("bits", inst.bits())}; std::make_pair("bits", inst.bits())};
graph->addVertex(newNode); graph->addVertex(newNode);
graph->addEdge(lastNode.get<std::size_t>("id"), newNode.get<std::size_t>("id"), 1); graph->addEdge(lastNode.get<std::size_t>("id"),
newNode.get<std::size_t>("id"), 1);
const int layerId = graph->depth();
graph->getVertexProperties(id).insert("layer", layerId);
qubitToLastNode[bit] = newNode; qubitToLastNode[bit] = newNode;
} }
...@@ -49,7 +52,8 @@ void IRToGraphVisitor::addTwoQubitGate(Gate &inst) { ...@@ -49,7 +52,8 @@ void IRToGraphVisitor::addTwoQubitGate(Gate &inst) {
graph->addVertex(newNode); graph->addVertex(newNode);
graph->addEdge(lastsrcnodeid, id, 1); graph->addEdge(lastsrcnodeid, id, 1);
graph->addEdge(lasttgtnodeid, id, 1); graph->addEdge(lasttgtnodeid, id, 1);
const auto layerId = graph->depth();
graph->getVertexProperties(id).insert("layer", layerId);
qubitToLastNode[srcbit] = newNode; qubitToLastNode[srcbit] = newNode;
qubitToLastNode[tgtbit] = newNode; qubitToLastNode[tgtbit] = newNode;
} }
......
...@@ -44,6 +44,24 @@ TEST(IRToGraphTester, checkSimple) { ...@@ -44,6 +44,24 @@ TEST(IRToGraphTester, checkSimple) {
auto graph = vis->getGraph(); auto graph = vis->getGraph();
graph->write(std::cout); graph->write(std::cout);
EXPECT_EQ(graph->order(), f->nInstructions() + 2);
// X and H are on the first layer
EXPECT_EQ(graph->getVertexProperties(1).getString("name"), x->name());
EXPECT_EQ(graph->getVertexProperties(1).get<int>("layer"), 0);
EXPECT_EQ(graph->getVertexProperties(2).getString("name"), h->name());
EXPECT_EQ(graph->getVertexProperties(2).get<int>("layer"), 0);
// CNOT on the second layer
EXPECT_EQ(graph->getVertexProperties(3).getString("name"), "CNOT");
EXPECT_EQ(graph->getVertexProperties(3).get<int>("layer"), 1);
// Rz and Z on the third layer
EXPECT_EQ(graph->getVertexProperties(4).getString("name"), rz->name());
EXPECT_EQ(graph->getVertexProperties(4).get<int>("layer"), 2);
EXPECT_EQ(graph->getVertexProperties(5).getString("name"), z->name());
EXPECT_EQ(graph->getVertexProperties(5).get<int>("layer"), 2);
EXPECT_EQ(f->depth(), 3);
} }
TEST(IRToGraphTester, checkCNOTLadder) { TEST(IRToGraphTester, checkCNOTLadder) {
...@@ -69,6 +87,21 @@ TEST(IRToGraphTester, checkCNOTLadder) { ...@@ -69,6 +87,21 @@ TEST(IRToGraphTester, checkCNOTLadder) {
auto graph = vis->getGraph(); auto graph = vis->getGraph();
graph->write(std::cout); graph->write(std::cout);
// These CNOT gates cannot be on the same layer.
for (int i = 1; i < graph->order() - 2; i++) {
auto node = graph->getVertexProperties(i);
EXPECT_EQ(node.getString("name"), "CNOT");
EXPECT_EQ(node.get<int>("layer"), i - 1);
}
EXPECT_EQ(f->depth(), 4);
}
TEST(IRToGraphTester, checkSingleGate) {
auto f = std::make_shared<Circuit>("foo");
auto x = std::make_shared<X>(0);
f->addInstruction(x);
f->toGraph()->write(std::cout);
EXPECT_EQ(f->depth(), 1);
} }
int main(int argc, char **argv) { int main(int argc, char **argv) {
......
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