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

Merge branch 'master' into tnguyen/pyxasm

parents 1655c44f 132dce5b
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -110,12 +110,12 @@ int main(int argc, char **argv) {
  auto q = qalloc(2);

  // Create the Deuteron Hamiltonian (Observable)
  auto H = qcor::createObservable(
  auto H = createObservable(
      "5.907 - 2.1433 X0X1 - 2.1433 Y0Y1 + .21829 Z0 - 6.125 Z1");

  // Create the ObjectiveFunction, here we want to run VQE
  // need to provide ansatz and the Observable
  auto objective = qcor::createObjectiveFunction("vqe", ansatz, H);
  auto objective = createObjectiveFunction("vqe", ansatz, H);

  // Evaluate the ObjectiveFunction at a specified set of parameters
  auto energy = (*objective)(q, .59);
+1 −2
Original line number Diff line number Diff line
#include <clang/CodeGen/CodeGenAction.h>

#include <filesystem>
#include <fstream>
#include <memory>

@@ -477,7 +476,7 @@ void QJIT::jit_compile(const std::string &code,
  auto hetmap_rawFPtr = hetmap_symbol.getAddress();
  kernel_name_to_f_ptr_hetmap.insert({kernel_name, hetmap_rawFPtr});

  // Get the function pointer for the hetmap kernel invocation
  // Get the function pointer for the hetmap kernel invocation with parent composite
  auto parent_hetmap_symbol = cantFail(jit->lookup(parent_hetmap_mangled_name));
  auto parent_hetmap_rawFPtr = parent_hetmap_symbol.getAddress();
  kernel_name_to_f_ptr_parent_hetmap.insert(
+2 −24
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@ using namespace cppmicroservices;
#include "xacc.hpp"
#include "xacc_internal_compiler.hpp"
#include "xacc_service.hpp"
#include "xacc_plugin.hpp"

namespace {

@@ -118,27 +119,4 @@ public:

} // namespace qcor

namespace {

/**
 */
class US_ABI_LOCAL VQEObjectiveActivator : public BundleActivator {

public:
  VQEObjectiveActivator() {}

  /**
   */
  void Start(BundleContext context) {
    auto xt = std::make_shared<qcor::VQEObjective>();
    context.RegisterService<qcor::ObjectiveFunction>(xt);
  }

  /**
   */
  void Stop(BundleContext /*context*/) {}
};

} // namespace

CPPMICROSERVICES_EXPORT_BUNDLE_ACTIVATOR(VQEObjectiveActivator)
REGISTER_PLUGIN(qcor::VQEObjective, qcor::ObjectiveFunction)
+25 −29
Original line number Diff line number Diff line
@@ -43,10 +43,14 @@ std::string GetExecutablePath(const char *Argv0, void *MainAddr) {
}

std::unique_ptr<clang::CodeGenAction> emit_llvm_ir(const std::string src_code) {
  std::ofstream file("local_output.cpp");

  // Persist the src code to a temporary file
  std::string internal_file_name = ".__qcor_internal_llvm_ir_emitter.cpp";
  std::ofstream file(internal_file_name);
  file << src_code;
  file.close();

  // Define the Clang command line
  std::vector<const char *> argv_vec{"@CLANG_EXECUTABLE@",
                                     "-std=c++17",
                                     "-I@CMAKE_INSTALL_PREFIX@/include/xacc",
@@ -54,12 +58,14 @@ std::unique_ptr<clang::CodeGenAction> emit_llvm_ir(const std::string src_code) {
                                     "-I@CMAKE_INSTALL_PREFIX@/include/quantum/gate",
                                     "-I@CMAKE_INSTALL_PREFIX@/include/eigen",
                                     "-c",
                                     "local_output.cpp"};
                                     internal_file_name.c_str()};

  // Create argc and argv
  const char **argv = &argv_vec[0];
  int argc = argv_vec.size();
  void *MainAddr = (void *)(intptr_t)GetExecutablePath;
  std::string Path =
      "@LLVM_INSTALL_PREFIX@/bin/clang++"; // GetExecutablePath(argv[0], MainAddr);
      "@LLVM_INSTALL_PREFIX@/bin/clang++";
  IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
  TextDiagnosticPrinter *DiagClient =
      new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts);
@@ -72,6 +78,7 @@ std::unique_ptr<clang::CodeGenAction> emit_llvm_ir(const std::string src_code) {

  ExitOnErr.setBanner("clang interpreter");

  // Create the Clang driver
  Driver TheDriver(Path, T.str(), Diags);
  TheDriver.setTitle("clang interpreter");
  TheDriver.setCheckInputsExist(false);
@@ -82,10 +89,10 @@ std::unique_ptr<clang::CodeGenAction> emit_llvm_ir(const std::string src_code) {
  SmallVector<const char *, 16> Args(argv, argv + argc);
  Args.push_back("-fsyntax-only");
  std::unique_ptr<Compilation> C(TheDriver.BuildCompilation(Args));
  if (!C)
    std::cout << "ERROR HERE !C\n";

  // FIXME: This is copied from ASTUnit.cpp; simplify and eliminate.
  if (!C) {
    std::cout << "QCOR internal clang execution error. Could not create the Compilation data structure.\n";
    exit(1);
  }

  // We expect to get back exactly one command job, if we didn't something
  // failed. Extract that job from the compilation.
@@ -95,13 +102,14 @@ std::unique_ptr<clang::CodeGenAction> emit_llvm_ir(const std::string src_code) {
    llvm::raw_svector_ostream OS(Msg);
    Jobs.Print(OS, "; ", true);
    Diags.Report(diag::err_fe_expected_compiler_job) << OS.str();
    std::cout << "ERROR JOBS\n";
    std::cout << "Error in creating the clang JobList.\n";
    exit(1);
  }

  const driver::Command &Cmd = cast<driver::Command>(*Jobs.begin());
  if (llvm::StringRef(Cmd.getCreator().getName()) != "clang") {
    Diags.Report(diag::err_fe_expected_clang_command);
    std::cout << " ERROR CMD\n";
    std::cout << "Error in creating the clang driver command.\n";
  }

  // Initialize a compiler invocation object from the clang (-cc1) arguments.
@@ -109,23 +117,16 @@ std::unique_ptr<clang::CodeGenAction> emit_llvm_ir(const std::string src_code) {
  std::unique_ptr<CompilerInvocation> CI(new CompilerInvocation);
  CompilerInvocation::CreateFromArgs(*CI, CCArgs, Diags);

  // Show the invocation, with -v.
  //   if (CI->getHeaderSearchOpts().Verbose) {
  //     llvm::errs() << "clang invocation:\n";
  //     Jobs.Print(llvm::errs(), "\n", true);
  //     llvm::errs() << "\n";
  //   }

  // FIXME: This is copied from cc1_main.cpp; simplify and eliminate.

  // Create a compiler instance to handle the actual work.
  CompilerInstance Clang;
  Clang.setInvocation(std::move(CI));

  // Create the compilers actual diagnostics engine.
  Clang.createDiagnostics();
  if (!Clang.hasDiagnostics())
    std::cout << "HAS DIAGS\n";
  if (!Clang.hasDiagnostics()) {
    std::cout << "Error - could not create Clang diagnostics.\n";
    exit(1);
  }

  // Infer the builtin include path if unspecified.
  if (Clang.getHeaderSearchOpts().UseBuiltinIncludes &&
@@ -135,18 +136,13 @@ std::unique_ptr<clang::CodeGenAction> emit_llvm_ir(const std::string src_code) {

  // Create and execute the frontend to generate an LLVM bitcode module.
  std::unique_ptr<CodeGenAction> Act(new EmitLLVMOnlyAction());
  if (!Clang.ExecuteAction(*Act))
    std::cout << "CODEGEN FAILED\n";
  if (!Clang.ExecuteAction(*Act)) {
    std::cout << "Error in executing clang codegen.\n";
  }

  llvm::InitializeNativeTarget();
  llvm::InitializeNativeTargetAsmPrinter();

  std::remove("local_output.cpp");
  //   int Res = 255;
  //   std::unique_ptr<llvm::LLVMContext> Ctx(Act->takeLLVMContext());
  //   std::unique_ptr<llvm::Module> Module = Act->takeModule();

  //   Module->dump();
  std::remove(internal_file_name.c_str());

  return Act;
}