Commit 86fafddd authored by Nguyen, Thien Minh's avatar Nguyen, Thien Minh
Browse files

Completed single-qubit gate merge optimization pass


Signed-off-by: Nguyen, Thien Minh's avatarThien Nguyen <nguyentm@ornl.gov>
parent 816d663f
......@@ -32,15 +32,35 @@ void MergeSingleQubitGatesOptimizer::apply(std::shared_ptr<CompositeInstruction>
auto fuser = xacc::getService<xacc::quantum::GateFuser>("default");
fuser->initialize(tmpKernel);
const Eigen::Matrix2cd uMat = fuser->calcFusedGate(1);
std::cout << "Unitary matrix: \n" << uMat << "\n";
auto zyz = std::dynamic_pointer_cast<quantum::Circuit>(xacc::getService<Instruction>("z-y-z"));
const bool expandOk = zyz->expand({
std::make_pair("unitary", uMat)
});
assert(zyz->nInstructions() == 3);
std::cout << "Decomposed: \n" << zyz->toString() << "\n";
// TODO: further optimize this sequence: remove trivial rotation (0, 2*pi, etc.)
// Optimized decomposed sequence:
const auto nbInstructionsAfter = zyz->nInstructions();
// A simplified sequence was found.
if (nbInstructionsAfter < sequence.size())
{
// Rewrite:
const size_t bitIdx = program->getInstruction(sequence[0])->bits()[0];
// Disable to remove:
const auto programLengthBefore = program->nInstructions();
for (const auto& instIdx: sequence)
{
auto instrPtr = program->getInstruction(instIdx);
instrPtr->disable();
}
program->removeDisabled();
const auto locationToInsert = sequence[0];
for (auto& newInst: zyz->getInstructions())
{
newInst->setBits({bitIdx});
program->insertInstruction(locationToInsert, newInst->clone());
}
const auto programLengthAfter = program->nInstructions();
assert(programLengthAfter < programLengthBefore);
}
}
}
}
......
......@@ -8,6 +8,7 @@
TEST(GateMergingTester, checkSingleQubitSimple)
{
auto c = xacc::getService<xacc::Compiler>("xasm");
// This is identity: H-X-H is equal to Z => Z-Z = I
auto f = c->compile(R"(__qpu__ void test1(qbit q) {
Z(q[0]);
H(q[0]);
......@@ -17,6 +18,8 @@ TEST(GateMergingTester, checkSingleQubitSimple)
auto opt = xacc::getService<xacc::IRTransformation>("single-qubit-gate-merging");
opt->apply(f, nullptr);
// No instruction after optimization
EXPECT_EQ(f->nInstructions(), 0);
}
TEST(GateMergingTester, checkSingleQubitStopAtTwoQubitGate)
......@@ -32,6 +35,12 @@ TEST(GateMergingTester, checkSingleQubitStopAtTwoQubitGate)
auto opt = xacc::getService<xacc::IRTransformation>("single-qubit-gate-merging");
opt->apply(f, nullptr);
// Becomes: Z (Rz(pi)) - CNOT - H
EXPECT_EQ(f->nInstructions(), 3);
EXPECT_EQ(f->getInstruction(0)->name(), "Rz");
EXPECT_NEAR(f->getInstruction(0)->getParameter(0).as<double>(), M_PI, 1e-6);
EXPECT_EQ(f->getInstruction(1)->name(), "CNOT");
EXPECT_EQ(f->getInstruction(2)->name(), "H");
}
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