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

Added a Q# example testing ref. counting mechanism



Signed-off-by: default avatarThien Nguyen <nguyentm@ornl.gov>
parent f313f1bd
Loading
Loading
Loading
Loading
+38 −0
Original line number Diff line number Diff line
#include <iostream> 
#include <vector>
#include "qir-types-utils.hpp"

// Include the external QSharp function.
qcor_include_qsharp(QCOR__TestKernel__body, ::Array*);
qcor_include_qsharp(QCOR__TestClean__body, void);

// Compile with:
// Include both the qsharp source and this driver file
// in the command line.
// $ qcor -qrt ftqc kernel.qs driver.cpp
// Run with:
// $ ./a.out
int main() {
  // Kernel that clean-up all allocated objects.
  QCOR__TestClean__body();
  // No leak expected.
  assert(!qcor::internal::AllocationTracker::get().checkLeak());
  // This kernel returns an Array,
  // (allocated in the kernel body)
  auto test = QCOR__TestKernel__body();
  const auto resultVec = qcor::qir::fromArray<double>(test);
  // Should detect a leak.
  assert(qcor::internal::AllocationTracker::get().checkLeak());

  for (const auto &el : resultVec) {
    std::cout << el << "\n";
  }

  // Release the ref-count of the returned array.
  // This should dealloc the Array
  test->release_ref();
  // No leak expected.
  assert(!qcor::internal::AllocationTracker::get().checkLeak());

  return 0;
}
 No newline at end of file
+20 −0
Original line number Diff line number Diff line
namespace QCOR 
{ 
operation TestClean() : Unit {
    // Arrays
    let test1 = [1.0, 2.0];
    let test2 = [1, 2, 3, 4];
    // Tuples
    let test4 = (1.0, [2, 3, 4]);
}

operation TestKernel() : Double[] {
    // Arrays
    let test1 = [1.0, 2.0];
    let test2 = [1, 2, 3, 4];
    // Tuples
    let test4 = (1.0, [2, 3, 4]);
    // Return an array
    return [5.0, 6.0, 7.0];
}
}
 No newline at end of file