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

Added an example to test kernel util with more arguments



the syntax handler doesn't like argument names start with '__', hence adding an alpha character

Signed-off-by: default avatarThien Nguyen <nguyentm@ornl.gov>
parent 93ee947c
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
namespace QCOR 
{
open Microsoft.Quantum.Intrinsic;
// Deuteron ansatz (to be used a QCOR NISQ kernel)
operation ansatz(qubits : Qubit[], theta : Double) : Unit {
  X(qubits[0]);
  Ry(theta, qubits[1]);
  CNOT(qubits[1], qubits[0]);          
}
}
 No newline at end of file
+33 −0
Original line number Diff line number Diff line
#include <iostream> 
#include <vector>
#include "qir_nisq_kernel_utils.hpp"
#include "qcor_qsim.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 deuteron.qs vqe_driver.cpp 
// Note: the first qreg argument is implicit.
qcor_import_qsharp_kernel(QCOR__ansatz, double);

int main() {
  // Allocate 2 qubits
  auto q = qalloc(2);
  // Hamiltonian
  auto H = 5.907 - 2.1433 * X(0) * X(1) - 2.1433 * Y(0) * Y(1) + .21829 * Z(0) -
           6.125 * Z(1);

  auto problemModel = QuaSiMo::ModelFactory::createModel(QCOR__ansatz, H, 2, 1);
  auto optimizer = createOptimizer("nlopt");
  // Instantiate a VQE workflow with the nlopt optimizer
  auto workflow = QuaSiMo::getWorkflow("vqe", {{"optimizer", optimizer}});

  // Result should contain the ground-state energy along with the optimal
  // parameters.
  auto result = workflow->execute(problemModel);

  const auto energy = result.get<double>("energy");
  std::cout << "Ground-state energy = " << energy << "\n";
  return 0;
}
 No newline at end of file
+2 −2
Original line number Diff line number Diff line
@@ -31,8 +31,8 @@

#define CONSTRUCT_ARGS_LIST_(type_name, var_name) , type_name var_name
#define CONSTRUCT_ARGS_LIST(type_name, counter)                                \
  CONSTRUCT_ARGS_LIST_(type_name, __internal__var__##counter)
#define CONSTRUCT_VAR_NAME_LIST(type_name, counter) , __internal__var__##counter
  CONSTRUCT_ARGS_LIST_(type_name, m__internal__var__##counter)
#define CONSTRUCT_VAR_NAME_LIST(type_name, counter) , m__internal__var__##counter

// Macro to use to construct list of types and var names.
#define ARGS_LIST_FOR_FUNC_SIGNATURE(...)                                      \