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

Fixed an issue with looping Q# kernels



Once all the runtime registers are deallocated, we need to reset the FTQC buffer addressing to make sure the simulator doesn't create a big state vector -> slow.

Signed-off-by: default avatarThien Nguyen <nguyentm@ornl.gov>
parent 7d36f7f1
Loading
Loading
Loading
Loading
+17 −1
Original line number Diff line number Diff line
@@ -379,7 +379,7 @@ void __quantum__rt__qubit_release_array(Array *q) {
  // qubit index numbers (unique) are unused
  // => the backend won't apply any further instructions on these.
  for (std::size_t i = 0; i < allocated_arrays.size(); i++) {
    if (allocated_arrays[i].get() == q) {
    if (allocated_arrays[i] && allocated_arrays[i].get() == q) {
      auto &array_ptr = allocated_arrays[i];
      auto array_size = array_ptr->size();
      if (verbose && mode == QRT_MODE::FTQC)
@@ -391,8 +391,24 @@ void __quantum__rt__qubit_release_array(Array *q) {
        delete qubitPtr;
      }
      array_ptr->clear();
      array_ptr.reset();
    }
  }

  // If all the runtime arrays have been cleared/dealocated, 
  // clean up the entire global register.
  // This is to handle **multiple** calls into an FTQC kernels (Q#/OpenQASM3).
  // At the end of the execution, all registers have been deallocated.
  if (std::all_of(allocated_arrays.begin(), allocated_arrays.end(),
                  [](auto &array_ptr) { return array_ptr == nullptr; })) {
    if (verbose) {
      std::cout << "Reset global buffer.\n";
    }
    allocated_arrays.clear();
    global_qreg.reset();
    allocated_qbits = 0;
    Qubit::reset_counter();
  }
}

void __quantum__rt__finalize() {
+8 −4
Original line number Diff line number Diff line
@@ -65,16 +65,20 @@ struct Qubit {
  operator int() const { return id; }
  // Allocation function:
  // Note: currently, we don't reclaim deallocated qubits.
  // TODO: track qubit deallocations for reuse...
  // until the very end of the quantum execution:
  // i.e. all qubit array are cleaned-up 
  // reset_counter() will be called.
  static Qubit *allocate() {
    static uint64_t counter = 0;
    Qubit *newQubit = new Qubit(counter);
    counter++;
    Qubit *newQubit = new Qubit(q_counter);
    q_counter++;
    return newQubit;
  }

  static void reset_counter() { q_counter = 0; }

private:
  Qubit(uint64_t idVal) : id(idVal) {}
  inline static uint64_t q_counter = 0;
};

using Result = bool;