Commit de265997 authored by Nguyen, Thien's avatar Nguyen, Thien
Browse files

Work on extended QASM2 generation



XACCToStaqOpenQASM visitor to handle IfStmt (currently empty impl)
Signed-off-by: Nguyen, Thien's avatarThien Nguyen <nguyentm@ornl.gov>
parent 4b894baf
...@@ -432,7 +432,7 @@ StaqCompiler::translate(std::shared_ptr<xacc::CompositeInstruction> function) { ...@@ -432,7 +432,7 @@ StaqCompiler::translate(std::shared_ptr<xacc::CompositeInstruction> function) {
// First search buffer names and see if we have // First search buffer names and see if we have
while (iter.hasNext()) { while (iter.hasNext()) {
auto &next = *iter.next(); auto &next = *iter.next();
if (next.isEnabled()) { if (!next.isComposite() && next.isEnabled()) {
for (int i = 0; i < next.nRequiredBits(); i++) { for (int i = 0; i < next.nRequiredBits(); i++) {
auto bufName = next.getBufferName(i); auto bufName = next.getBufferName(i);
int size = next.bits()[i] + 1; int size = next.bits()[i] + 1;
......
...@@ -97,12 +97,24 @@ void XACCToStaqOpenQasm::visit(CPhase &cphase) { ...@@ -97,12 +97,24 @@ void XACCToStaqOpenQasm::visit(CPhase &cphase) {
<< "[" << cphase.bits()[0] << "], " << (cphase.getBufferNames().empty() ? "q" : cphase.getBufferName(1)) << "[" << cphase.bits()[1] << "];\n"; << "[" << cphase.bits()[0] << "], " << (cphase.getBufferNames().empty() ? "q" : cphase.getBufferName(1)) << "[" << cphase.bits()[1] << "];\n";
} }
void XACCToStaqOpenQasm::visit(Measure &m) { void XACCToStaqOpenQasm::visit(Measure &m) {
ss << "measure " << (m.getBufferNames().empty() ? "q" : m.getBufferName(0)) << m.bits() << " -> " << cregNames[m.getBufferName(0)] << m.bits() << ";\n"; ss << "measure " << (m.getBufferNames().empty() ? "q" : m.getBufferName(0))
<< m.bits() << " -> " << cregNames[m.getBufferName(0)] << m.bits()
<< ";\n";
qubitIdxToMeasCregName[m.bits()[0]] = cregNames[m.getBufferName(0)];
} }
void XACCToStaqOpenQasm::visit(Identity &i) {} void XACCToStaqOpenQasm::visit(Identity &i) {}
void XACCToStaqOpenQasm::visit(U &u) { void XACCToStaqOpenQasm::visit(U &u) {
ss << "u3(" << std::fixed << std::setprecision(16) << xacc::InstructionParameterToDouble(u.getParameter(0)) << "," << xacc::InstructionParameterToDouble(u.getParameter(1)) << "," <<xacc::InstructionParameterToDouble(u.getParameter(2)) << ") " << (u.getBufferNames().empty() ? "q" : u.getBufferName(0)) << u.bits() << ";\n"; ss << "u3(" << std::fixed << std::setprecision(16) << xacc::InstructionParameterToDouble(u.getParameter(0)) << "," << xacc::InstructionParameterToDouble(u.getParameter(1)) << "," <<xacc::InstructionParameterToDouble(u.getParameter(2)) << ") " << (u.getBufferNames().empty() ? "q" : u.getBufferName(0)) << u.bits() << ";\n";
} }
void XACCToStaqOpenQasm::visit(IfStmt &ifStmt) {} void XACCToStaqOpenQasm::visit(IfStmt &ifStmt) {
// Note: QASM2 'if' must be inline (no braces)
// Note: this extended syntax: if (creg[k] == 1) is not supported by IBM.
// (IBM requires comparison to the full register value, not a bit check)
for (auto i : ifStmt.getInstructions()) {
ss << "if (" << qubitIdxToMeasCregName[ifStmt.bits()[0]] << "["
<< ifStmt.bits()[0] << "] == 1) ";
i->accept(this);
}
}
} // namespace internal_staq } // namespace internal_staq
} // namespace xacc } // namespace xacc
\ No newline at end of file
...@@ -266,7 +266,8 @@ class XACCToStaqOpenQasm : public AllGateVisitor { ...@@ -266,7 +266,8 @@ class XACCToStaqOpenQasm : public AllGateVisitor {
public: public:
std::stringstream ss; std::stringstream ss;
std::map<std::string, std::string> cregNames; std::map<std::string, std::string> cregNames;
// Last known creg that stored the measurement result of a qubit
std::map<size_t, std::string> qubitIdxToMeasCregName;
XACCToStaqOpenQasm(std::map<std::string, int> bufNamesToSize); XACCToStaqOpenQasm(std::map<std::string, int> bufNamesToSize);
void visit(Hadamard &h) override; void visit(Hadamard &h) override;
void visit(CNOT &cnot) override; void visit(CNOT &cnot) override;
......
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