Commit 3c6df472 authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

add a test to the new gray code m_ctrl gen


Signed-off-by: Mccaskey, Alex's avatarAlex McCaskey <mccaskeyaj@ornl.gov>
parent d570393a
......@@ -290,11 +290,12 @@ bool ControlledU::expand(const xacc::HeterogeneousMap &runtimeOptions) {
auto ctrlU = uComposite;
auto should_run_gray_mcu_synth = [ctrlU,&ctrlIdxs]() {
auto should_run_gray_mcu_synth = [ctrlU, &ctrlIdxs]() {
if (ctrlU->nInstructions() == 1) {
std::vector<std::string> allowed{"Z"};
auto inst = ctrlU->getInstruction(0);
if (xacc::container::contains(allowed, inst->name()) && ctrlIdxs.size() > 2) {
if (xacc::container::contains(allowed, inst->name()) &&
ctrlIdxs.size() > 2) {
return true;
}
}
......@@ -303,6 +304,30 @@ bool ControlledU::expand(const xacc::HeterogeneousMap &runtimeOptions) {
if (should_run_gray_mcu_synth()) {
ctrlU = __gray_code_mcu_gen(ctrlU, ctrlIdxs);
std::vector<int> zero_rotation_idxs;
for (int i = 0; i < ctrlU->nInstructions(); i++) {
auto inst = ctrlU->getInstruction(i);
if (inst->name() == "U") {
auto p0 = inst->getParameter(0);
auto p1 = inst->getParameter(1);
auto p2 = inst->getParameter(2);
if (p0.isNumeric() && p1.isNumeric() && p2.isNumeric()) {
if (std::fabs(xacc::InstructionParameterToDouble(p0)) < 1e-12 &&
std::fabs(xacc::InstructionParameterToDouble(p1)) < 1e-12 &&
std::fabs(xacc::InstructionParameterToDouble(p2)) < 1e-12) {
// std::cout << "Removing " << inst->toString() << "\n";
// inst->disable();
zero_rotation_idxs.push_back(i);
}
}
}
}
for (auto iter = zero_rotation_idxs.rbegin();
iter != zero_rotation_idxs.rend(); ++iter) {
ctrlU->removeInstruction(*iter);
}
} else {
// Recursive application of control bits:
......
......@@ -3,7 +3,7 @@
#include "xacc_service.hpp"
#include "CommonGates.hpp"
#include <Eigen/Dense>
#include "GateFusion.hpp"
#include "CountGatesOfTypeVisitor.hpp"
using namespace xacc;
using namespace xacc::quantum;
......@@ -283,7 +283,7 @@ TEST(ControlledGateTester, checkControlSwap)
TEST(ControlledGateTester, checkEltonBug) {
auto gateRegistry = xacc::getService<xacc::IRProvider>("quantum");
auto z = std::make_shared<Z>(5);
auto z = std::make_shared<Z>(4);
z->setBufferNames(std::vector<std::string>{"q"});
std::shared_ptr<xacc::CompositeInstruction> comp =
......@@ -293,16 +293,24 @@ TEST(ControlledGateTester, checkEltonBug) {
xacc::getService<Instruction>("C-U"));
const std::vector<int> ctrl_idxs{0,1,2,3};
multi_ctrl_z->expand({{"U", comp}, {"control-idx", ctrl_idxs}});
auto opt = xacc::getIRTransformation("circuit-optimizer");
opt->apply(multi_ctrl_z, nullptr);
std::cout << "HOWDY: \n" << multi_ctrl_z->toString() << "\n";
std::cout << multi_ctrl_z->nInstructions() << "\n";
Eigen::MatrixXcd expected = Eigen::MatrixXcd::Identity(32,32);
expected(31,31) = -1;
std::cout << expected << "\n";
// Eigen::MatrixXcd expected = Eigen::MatrixXcd::Identity(32,32);
// expected(31,31) = -1;
// std::cout << expected << "\n";
xacc::quantum::CountGatesOfTypeVisitor<CNOT> vis(multi_ctrl_z);
xacc::quantum::CountGatesOfTypeVisitor<U> visu(multi_ctrl_z);
// QiskitImpl Reports: OrderedDict([('cx', 44), ('p', 30), ('u', 30)])
// all ours are U+CX, and we remove U(0,0,0).
// There are 8 U(0,0,0). So should have 44 CX, 60-8=52 Us
EXPECT_EQ(vis.countGates(), 44);
EXPECT_EQ(visu.countGates(), 52);
// FIXME Need to test
}
......
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