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

XASM to handle qalloc inside kernel body



Signed-off-by: default avatarThien Nguyen <nguyentm@ornl.gov>
parent cf78ba83
Loading
Loading
Loading
Loading
+21 −2
Original line number Diff line number Diff line
@@ -211,11 +211,30 @@ class xasm_single_visitor : public xasm::xasm_singleVisitor {
        replaceAll(origText, "Measure", " quantum::mz");
        ss << origText << " ";
      }
    } else {
      // std::cout << "HOWDY: " << context->getText() << "\n";
      for (const auto &expr : context->exp()) {
        std::cout << expr->getText() << "\n";
      }
      if (context->var_value &&
          context->var_value->getText().find("qalloc") != std::string::npos) {
        // std::cout << "Qalloc encountered\n";
        std::stringstream qalloc_ss;
        for (auto c : context->children) {
          qalloc_ss << c->getText() << " ";
        }
        std::string qalloc_call = qalloc_ss.str();
        std::cout << qalloc_call << "\n";
        const auto close_pos = qalloc_call.find_last_of(")");
        qalloc_call.insert(close_pos, ", quantum::getAncillaQubitAllocator()");
        // std::cout << "After: " << qalloc_call << "\n";
        ss << qalloc_call;
      } else {
        for (auto c : context->children) {
          ss << c->getText() << " ";
        }
      }
    }

    result.first = ss.str() + "\n";
    return 0;
+10 −1
Original line number Diff line number Diff line
@@ -21,10 +21,13 @@ namespace {
class NisqQubitAllocator : public AllocEventListener, public QubitAllocator {
public:
  static inline const std::string ANC_BUFFER_NAME = "nisq_temp_buffer";
  virtual void onAllocate(qubit *in_qubit) override {}
  virtual void onAllocate(qubit *in_qubit) override {
    std::cout << "Allocate: " << (void *)in_qubit << "\n";
  }

  // On deallocate: don't try to deref the qubit since it may have been gone.
  virtual void onDealloc(qubit *in_qubit) override {
    std::cout << "Deallocate: " << (void *)in_qubit << "\n";
    // If this qubit was allocated from this pool:
    if (xacc::container::contains(m_allocatedQubits, in_qubit)) {
      const auto qIndex = std::find(m_allocatedQubits.begin(),
@@ -41,6 +44,7 @@ public:
  }

  virtual qubit allocate() override {
    std::cout << "Allocate\n";
    if (!m_qubitPool.empty()) {
      auto recycled_qubit = m_qubitPool.back();
      m_qubitPool.pop_back();
@@ -135,6 +139,11 @@ class NISQ : public ::quantum::QuantumRuntime,
  void initialize(const std::string kernel_name) override {
    provider = xacc::getIRProvider("quantum");
    program = provider->createComposite(kernel_name);
    setGlobalQubitManager(NisqQubitAllocator::getInstance());
  }

  QubitAllocator *get_anc_qubit_allocator() {
    return NisqQubitAllocator::getInstance();
  }

  void __begin_mark_segment_as_compute() override { mark_as_compute = true; }
+4 −0
Original line number Diff line number Diff line
@@ -337,4 +337,8 @@ void ch(qreg src, qreg tgt) {
    ch(src[i], tgt[i]);
  }
}

QubitAllocator *getAncillaQubitAllocator() {
  return qrt_impl->get_anc_qubit_allocator();
}
}  // namespace quantum
 No newline at end of file
+6 −0
Original line number Diff line number Diff line
@@ -79,6 +79,9 @@ public:
  set_current_program(std::shared_ptr<xacc::CompositeInstruction> p) = 0;
  virtual std::shared_ptr<xacc::CompositeInstruction> get_current_program() = 0;
  virtual void set_current_buffer(xacc::AcceleratorBuffer *buffer) = 0;
  // Ancilla qubit allocator:
  // i.e. handle in kernel allocation.
  virtual QubitAllocator *get_anc_qubit_allocator() { return nullptr; }
};
// This represents the public API for the xacc-enabled
// qcor quantum runtime library. The goal here is to provide
@@ -198,6 +201,9 @@ void set_current_buffer(xacc::AcceleratorBuffer *buffer);

// Persist bit-string result from single-bit measurements (if any)
void persistBitstring(xacc::AcceleratorBuffer *buffer);

// Get the ancilla qubit allocator:
QubitAllocator *getAncillaQubitAllocator();
} // namespace quantum

namespace xacc {