This GitLab instance is undergoing maintenance and is operating in read-only mode.

You are on a read-only GitLab instance.
Unverified Commit a71dc6a6 authored by Mccaskey, Alex's avatar Mccaskey, Alex Committed by GitHub
Browse files

Merge pull request #121 from spowers/powers/rotationOpt

adding optimizations for adjacent rotations
parents 8f46bf37 a2230d16
Pipeline #65809 passed with stage
in 6 minutes and 29 seconds
......@@ -59,7 +59,7 @@ std::shared_ptr<IR> CircuitOptimizer::transform(std::shared_ptr<IR> ir) {
}
}
}
// Remove all CNOT(p,q) CNOT(p,q) Pairs
// Remove all CNOT(p,q) CNOT(p,q) pairs
while (true) {
bool modified = false;
gateFunction = std::dynamic_pointer_cast<GateFunction>(
......@@ -83,7 +83,7 @@ std::shared_ptr<IR> CircuitOptimizer::transform(std::shared_ptr<IR> ir) {
if (!modified)
break;
}
// Remove all H(p)H(p) pairs
while (true) {
bool modified = false;
gateFunction = std::dynamic_pointer_cast<GateFunction>(
......@@ -102,7 +102,9 @@ std::shared_ptr<IR> CircuitOptimizer::transform(std::shared_ptr<IR> ir) {
gateFunction->getInstruction(nAsVec[0] - 1)->disable();
modified = true;
break;
} else if (isRotation(node["name"].toString()) && isRotation(nextNode["name"].toString()) &&
}
/*
else if (isRotation(node["name"].toString()) && isRotation(nextNode["name"].toString()) &&
node["name"].toString() == nextNode["name"].toString()) {
auto val1 = ipToDouble(
gateFunction->getInstruction(node["id"].as<int>() - 1)->getParameter(0));
......@@ -117,6 +119,43 @@ std::shared_ptr<IR> CircuitOptimizer::transform(std::shared_ptr<IR> ir) {
break;
}
}
*/
}
}
if (!modified)
break;
}
// Merge adjacent rotation gates Rz()Rz() or Rx()Rx() or Ry()Ry()
while (true) {
bool modified = false;
gateFunction = std::dynamic_pointer_cast<GateFunction>(gateFunction->enabledView());
auto graphView = gateFunction->toGraph();
// start/end nodes get added: start at 1, stop at n-2 b/c last will be n-2 neighbor
for (int i = 1; i < graphView->order() - 2; i++) {
auto node = graphView->getVertexProperties(i);
auto nAsVec = graphView->getNeighborList(node["id"].as<int>());
// if it has more than 1 neighbor, don't consider
if (nAsVec.size() == 1) {
auto nextNode = graphView->getVertexProperties(nAsVec[0]);
if (isRotation(node["name"].toString()) && isRotation(nextNode["name"].toString())
&& node["name"].toString() == nextNode["name"].toString()) {
auto val1 = ipToDouble(gateFunction->getInstruction(node["id"].as<int>() - 1)->getParameter(0));
auto val2 = ipToDouble(gateFunction->getInstruction(nextNode["id"].as<int>() - 1)->getParameter(0));
if (std::fabs(val1+val2) < 1e-12) {
gateFunction->getInstruction(node["id"].as<int>() - 1)->disable();
gateFunction->getInstruction(nextNode["id"].as<int>() - 1)->disable();
}
else {
InstructionParameter tmp(val1+val2);
gateFunction->getInstruction(node["id"].as<int>() - 1)->setParameter(0, tmp);
gateFunction->getInstruction(nextNode["id"].as<int>() - 1)->disable();
}
modified = true;
break;
}
}
}
......
......@@ -216,6 +216,30 @@ TEST(CircuitOptimizerTester, checkOptimize) {
}
}
TEST(CircuitOptimizerTester, checkAdjRotations) {
if (xacc::hasCompiler("xacc-py")) {
auto c = xacc::getService<xacc::Compiler>("xacc-py");
auto f = c->compile("def foo(buffer):\n H(0)\n Rx(1.5708,1)\n Rx(-1.5708, 1)\n H(2)\n Rz(0.5,3)\n Rz(0.5,3)\n")
->getKernels()[0];
auto ir = std::make_shared<GateIR>();
ir->addKernel(f);
CircuitOptimizer opt;
auto newir = opt.transform(ir);
auto optF = newir->getKernels()[0];
optF = std::dynamic_pointer_cast<GateFunction>(optF->enabledView());
CountGatesOfTypeVisitor<Rz> countRz(optF);
CountGatesOfTypeVisitor<Rx> countRx(optF);
EXPECT_EQ(3, optF->nInstructions());
EXPECT_EQ(1, countRz.countGates());
EXPECT_EQ(0, countRx.countGates());
EXPECT_TRUE(mpark::get<double>(optF->getInstruction(2)->getParameter(0))==1.0 );
std::cout << "FINAL CIRCUIT:\n" << optF->toString("q") << "\n";
}
}
TEST(CircuitOptimizerTester, checkSimple) {
if (xacc::hasCompiler("xacc-py")) {
......
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