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

Update Q# NISQ examples



Move NISQ kernel util to QIR install header

Signed-off-by: default avatarThien Nguyen <nguyentm@ornl.gov>
parent 376fbfbf
Loading
Loading
Loading
Loading
+6 −1
Original line number Diff line number Diff line
#include <iostream> 
#include <vector>
#include "import_kernel_utils.hpp"
#include "qir_nisq_kernel_utils.hpp"

// Util pre-processor to wrap Q# operation 
// in a QCOR QuantumKernel.
// Compile:
// Note: need to use alpha package since this kernel will take a qubit array.
// qcor -qdk-version 0.17.2106148041-alpha bell.qs bell_driver.cpp -shots 1024
// Note: the first qreg argument is implicit.
qcor_import_qsharp_kernel(QCOR__Bell);

int main() {
  // Allocate 2 qubits
  auto q = qalloc(2);
  // Print kernel
  QCOR__Bell::print_kernel(q);
  
  // Execute:
  QCOR__Bell(q);
  q.print();
  return 0;
+2 −2
Original line number Diff line number Diff line
#include <iostream> 
#include <vector>
#include "import_kernel_utils.hpp"
#include "qir_nisq_kernel_utils.hpp"

// Compile:
// qcor -qdk-version 0.17.2106148041-alpha qft.qs qft_driver.cpp -shots 1024 -print-final-submission
@@ -37,7 +37,7 @@ __qpu__ void oracle(qubit q) { T(q); }

int main(int argc, char **argv) {
  auto q = qalloc(4);
  // qpe::print_kernel(std::cout, q, oracle);
  qpe::print_kernel(q, oracle);
  // Run
  qpe(q, oracle);
  q.print();
+1 −1
Original line number Diff line number Diff line
file(GLOB SRC *.cpp)
file(GLOB HEADERS qir-types.hpp qir-qrt.hpp qir-types-utils.hpp)
file(GLOB HEADERS qir-types.hpp qir-qrt.hpp qir-types-utils.hpp qir_nisq_kernel_utils.hpp)

set(LIBRARY_NAME qir-qrt)

+10 −0
Original line number Diff line number Diff line
@@ -74,6 +74,16 @@ struct Qubit {
    return newQubit;
  }

  // Create a QIR qubit with this ID
  // Rationale: should only be used for interoperability with non-QIR
  // environment.
  // i.e. actual qubits are allocated elsewhere, just need to create an alias
  // in QIR runtime to match up the qubit ID.
  static Qubit *create(uint64_t id) {
    Qubit *newQubit = new Qubit(id);
    return newQubit;
  }

  static void reset_counter() { q_counter = 0; }

private:
+9 −7
Original line number Diff line number Diff line
@@ -53,6 +53,7 @@
  private:                                                                     \
    ::Array *m_qubits = nullptr;                                               \
    friend class qcor::QuantumKernel<class OPERATION_NAME, qreg, __VA_ARGS__>; \
    friend class qcor::KernelSignature<qreg, __VA_ARGS__>;                     \
                                                                               \
  protected:                                                                   \
    void operator()(qreg q ARGS_LIST_FOR_FUNC_SIGNATURE(__VA_ARGS__)) {        \
@@ -67,7 +68,7 @@
      if (!m_qubits) {                                                         \
        m_qubits = new ::Array(q.size());                                      \
        for (int i = 0; i < q.size(); ++i) {                                   \
          auto qubit = Qubit::allocate();                                      \
          auto qubit = Qubit::create(q[i].second);                             \
          int8_t *arrayPtr = (*m_qubits)[i];                                   \
          auto qubitPtr = reinterpret_cast<Qubit **>(arrayPtr);                \
          *qubitPtr = qubit;                                                   \
@@ -110,14 +111,15 @@
  private:                                                                     \
    ::Array *m_qubits = nullptr;                                               \
    friend class qcor::QuantumKernel<class OPERATION_NAME, qreg>;              \
    friend class qcor::KernelSignature<qreg>;                                  \
                                                                               \
  protected:                                                                   \
    void operator()(qreg q) {                                                  \
      if (!parent_kernel) {                                                    \
        std::cout << "Create parent kernel\n"; \
        /* std::cout << "Create parent kernel\n";   */                         \
        parent_kernel = qcor::__internal__::create_composite(kernel_name);     \
      }                                                                        \
      std::cout << "Parent:\n" << parent_kernel->toString() << "\n";           \
      /* std::cout << "Parent:\n" << parent_kernel->toString() << "\n";  */    \
      quantum::set_current_program(parent_kernel);                             \
      if (runtime_env == QrtType::FTQC) {                                      \
        quantum::set_current_buffer(q.results());                              \
@@ -126,13 +128,13 @@
      if (!m_qubits) {                                                         \
        m_qubits = new ::Array(q.size());                                      \
        for (int i = 0; i < q.size(); ++i) {                                   \
          auto qubit = Qubit::allocate();                                      \
          auto qubit = Qubit::create(q[i].second);                             \
          int8_t *arrayPtr = (*m_qubits)[i];                                   \
          auto qubitPtr = reinterpret_cast<Qubit **>(arrayPtr);                \
          *qubitPtr = qubit;                                                   \
        }                                                                      \
      }                                                                        \
      std::cout << "INVOKE:\n" << parent_kernel->toString() << "\n";           \
      /*std::cout << "INVOKE:\n" << parent_kernel->toString() << "\n";  */     \
      OPERATION_NAME##__body(m_qubits); /* std::cout << "INVOKE:\n" <<         \
                            parent_kernel->toString(); */                      \
    }                                                                          \
@@ -148,7 +150,7 @@
        return;                                                                \
      }                                                                        \
      auto [q] = args_tuple;                                                   \
      std::cout << "here\n"; \
      /* std::cout << "here\n";   */                                           \
      operator()(q);                                                           \
      xacc::internal_compiler::execute_pass_manager();                         \
      if (is_callable) {                                                       \