Commit 40a5dd66 authored by Nguyen, Thien's avatar Nguyen, Thien
Browse files

OpenQASM2 if statements need to be inline when generating native code


Signed-off-by: Nguyen, Thien's avatarThien Nguyen <nguyentm@ornl.gov>
parent 93039092
......@@ -60,4 +60,4 @@ Measure(q[0], c[3]);
f = xacc.getCompiled('iterative_qpe')
qpu = xacc.getAccelerator('ibm:ibmq_manhattan')
print('HOWDY:\n', qpu.getNativeCode(f))
\ No newline at end of file
print('HOWDY:\n', qpu.getNativeCode(f, {'format': 'qasm'}))
\ No newline at end of file
......@@ -118,6 +118,18 @@ void bind_accelerator(py::module &m) {
return qpu.getNativeCode(f);
},
"")
.def(
"getNativeCode",
[](xacc::Accelerator &qpu, std::shared_ptr<CompositeInstruction> f,
PyHeterogeneousMap &options) {
HeterogeneousMap m;
for (auto &item : options) {
PyHeterogeneousMap2HeterogeneousMap vis(m, item.first);
mpark::visit(vis, item.second);
}
return qpu.getNativeCode(f, m);
},
"")
.def("configurationKeys", &xacc::Accelerator::configurationKeys, "")
.def("contributeInstructions", &xacc::Accelerator::contributeInstructions,
"");
......
......@@ -1288,7 +1288,8 @@ IBMAccelerator::getNativeCode(std::shared_ptr<CompositeInstruction> program,
} else if (nextInst->name() == "ifstmt") {
auto ifStmt =
std::dynamic_pointer_cast<xacc::CompositeInstruction>(nextInst);
ss << "if (c[" << nextInst->bits()[0] << "] == 1){\n";
// !! NOTE!! OpenQASM2 doesn't allow multi-statement if block
// so need to wrap each statement individually.
if (ifStmt) {
for (auto &i : ifStmt->getInstructions()) {
auto visitor = std::make_shared<QObjectExperimentVisitor>(
......@@ -1297,16 +1298,20 @@ IBMAccelerator::getNativeCode(std::shared_ptr<CompositeInstruction> program,
auto experiment = visitor->getExperiment();
for (auto &inst : experiment.get_instructions()) {
// std::cout << "HOWDY: " << inst.toString() << "\n";
ss << "if (c[" << nextInst->bits()[0] << "] == 1) ";
ss << inst.toString() << "\n";
}
}
}
ss << "}\n";
}
}
return ss.str();
const std::string preAmple = R"(OPENQASM 2.0;
include "qelib1.inc";
qreg q[)" + std::to_string(program->nLogicalBits()) +
"];\ncreg c[" + std::to_string(memSlots) +
"];\n";
return preAmple + ss.str();
}
xacc::error("Unknown native code format '" + format + "'");
return "";
......
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