Commit 4fab9039 authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

Fix bug in finding position in comp vector, add a few more tests


Signed-off-by: Mccaskey, Alex's avatarAlex McCaskey <mccaskeyaj@ornl.gov>
parent 3c6df472
...@@ -32,11 +32,6 @@ std::vector<std::string> _generate_gray_code(int num_bits) { ...@@ -32,11 +32,6 @@ std::vector<std::string> _generate_gray_code(int num_bits) {
} }
} }
// for (auto r : result) {
// std::cout << r << " ";
// }
// std::cout << "\n";
auto int_to_str = [&](int n) -> std::string { auto int_to_str = [&](int n) -> std::string {
const int size = sizeof(n) * 8; const int size = sizeof(n) * 8;
std::string res; std::string res;
...@@ -57,8 +52,8 @@ std::vector<std::string> _generate_gray_code(int num_bits) { ...@@ -57,8 +52,8 @@ std::vector<std::string> _generate_gray_code(int num_bits) {
std::vector<std::string> ret; std::vector<std::string> ret;
for (auto r : result) { for (auto r : result) {
// std::cout << int_to_str(r) << "\n";
ret.push_back(int_to_str(r)); ret.push_back(int_to_str(r));
// std::cout << "HI: " << ret.back() << "\n";
} }
return ret; return ret;
...@@ -125,12 +120,19 @@ void _apply_mcu_graycode(std::shared_ptr<CompositeInstruction> circuit, ...@@ -125,12 +120,19 @@ void _apply_mcu_graycode(std::shared_ptr<CompositeInstruction> circuit,
comp.push_back(i != j); comp.push_back(i != j);
} }
// std::cout << pattern << " " << last_pattern << " [";
// for (auto c : comp) std::cout << std::boolalpha << c << " ";
// std::cout << "], ";
int pos = -1; int pos = -1;
auto true_itr = std::find(comp.begin(), comp.end(), true); for (int i = 0; i < comp.size(); i++) {
if (true_itr != comp.end()) { if (comp[i]) {
pos = comp[std::distance(comp.begin(), true_itr)]; pos = i;
break;
}
} }
// std::cout << pos << "\n";
if (pos != -1) { if (pos != -1) {
if (pos != lm_pos) { if (pos != lm_pos) {
circuit->addInstruction(provider->createInstruction( circuit->addInstruction(provider->createInstruction(
...@@ -180,7 +182,13 @@ std::shared_ptr<CompositeInstruction> __gray_code_mcu_gen( ...@@ -180,7 +182,13 @@ std::shared_ptr<CompositeInstruction> __gray_code_mcu_gen(
auto __lam = inst->isParameterized() ? inst->getParameter(0).as<double>() auto __lam = inst->isParameterized() ? inst->getParameter(0).as<double>()
: constants::pi; : constants::pi;
lam = __lam * (1 / (std::pow(2, n_c - 1))); lam = __lam * (1 / (std::pow(2, n_c - 1)));
} } else if (name == "Rx" || name == "X") {
auto __theta = inst->isParameterized() ? inst->getParameter(0).as<double>()
: constants::pi;
theta = __theta * (1 / (std::pow(2, n_c - 1)));
phi = -constants::pi / 2.;
lam = constants::pi / 2.;
}
_apply_mcu_graycode(circuit, theta, phi, lam, ctrl_qubits, target_qubit); _apply_mcu_graycode(circuit, theta, phi, lam, ctrl_qubits, target_qubit);
...@@ -292,7 +300,7 @@ bool ControlledU::expand(const xacc::HeterogeneousMap &runtimeOptions) { ...@@ -292,7 +300,7 @@ bool ControlledU::expand(const xacc::HeterogeneousMap &runtimeOptions) {
auto should_run_gray_mcu_synth = [ctrlU, &ctrlIdxs]() { auto should_run_gray_mcu_synth = [ctrlU, &ctrlIdxs]() {
if (ctrlU->nInstructions() == 1) { if (ctrlU->nInstructions() == 1) {
std::vector<std::string> allowed{"Z"}; std::vector<std::string> allowed{"X", "Rx", "Z", "Rz"};
auto inst = ctrlU->getInstruction(0); auto inst = ctrlU->getInstruction(0);
if (xacc::container::contains(allowed, inst->name()) && if (xacc::container::contains(allowed, inst->name()) &&
ctrlIdxs.size() > 2) { ctrlIdxs.size() > 2) {
...@@ -323,9 +331,13 @@ bool ControlledU::expand(const xacc::HeterogeneousMap &runtimeOptions) { ...@@ -323,9 +331,13 @@ bool ControlledU::expand(const xacc::HeterogeneousMap &runtimeOptions) {
} }
} }
for (auto iter = zero_rotation_idxs.rbegin(); for (int instId = 0; instId < ctrlU->nInstructions(); ++instId) {
iter != zero_rotation_idxs.rend(); ++iter) { // Only add instructions that are not NOOPs
ctrlU->removeInstruction(*iter); if (std::find(zero_rotation_idxs.begin(), zero_rotation_idxs.end(),
instId) == zero_rotation_idxs.end()) {
auto inst = ctrlU->getInstruction(instId)->clone();
addInstruction(inst);
}
} }
} else { } else {
...@@ -334,11 +346,11 @@ bool ControlledU::expand(const xacc::HeterogeneousMap &runtimeOptions) { ...@@ -334,11 +346,11 @@ bool ControlledU::expand(const xacc::HeterogeneousMap &runtimeOptions) {
for (const auto &ctrlIdx : ctrlIdxs) { for (const auto &ctrlIdx : ctrlIdxs) {
ctrlU = applyControl(ctrlU, ctrlIdx); ctrlU = applyControl(ctrlU, ctrlIdx);
} }
}
for (int instId = 0; instId < ctrlU->nInstructions(); ++instId) { for (int instId = 0; instId < ctrlU->nInstructions(); ++instId) {
auto inst = ctrlU->getInstruction(instId)->clone(); auto inst = ctrlU->getInstruction(instId)->clone();
addInstruction(inst); addInstruction(inst);
}
} }
return true; return true;
} }
......
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