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

Merge branch 'master' into tnguyen/qsim-examples

parents 831ca5a1 99e45b8f
Loading
Loading
Loading
Loading
+55 −24
Original line number Diff line number Diff line
#include "qcor_syntax_handler.hpp"
#include "token_collector_util.hpp"

#include <iostream>
#include <regex>
#include <sstream>
@@ -8,6 +8,7 @@
#include "clang/AST/Type.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendPluginRegistry.h"
#include "token_collector_util.hpp"

using namespace clang;

@@ -59,8 +60,7 @@ void QCORSyntaxHandler::GetReplacement(
    std::vector<std::string> program_arg_types,
    std::vector<std::string> program_parameters,
    std::vector<std::string> bufferNames, CachedTokens &Toks,
    llvm::raw_string_ostream &OS) {

    llvm::raw_string_ostream &OS, bool add_het_map_ctor) {
  // Get the Diagnostics engine and create a few custom
  // error messgaes
  auto &diagnostics = PP.getDiagnostics();
@@ -97,7 +97,8 @@ void QCORSyntaxHandler::GetReplacement(
  //   };

  // Rewrite the original function
  OS << "void " << kernel_name << "(" << program_arg_types[0] << " "  << program_parameters[0];
  OS << "void " << kernel_name << "(" << program_arg_types[0] << " "
     << program_parameters[0];
  for (int i = 1; i < program_arg_types.size(); i++) {
    OS << ", " << program_arg_types[i] << " " << program_parameters[i];
  }
@@ -199,6 +200,25 @@ void QCORSyntaxHandler::GetReplacement(
  }
  OS << ") {}\n";

  if (add_het_map_ctor) {
    // Third constructor, give us a way to provide a HeterogeneousMap of
    // arguments, this is used for Pythonic QJIT...
    // KERNEL_NAME(HeterogeneousMap args);
    OS << kernel_name << "(HeterogeneousMap& args): QuantumKernel<"
       << kernel_name << ", " << program_arg_types[0];
    for (int i = 1; i < program_arg_types.size(); i++) {
      OS << ", " << program_arg_types[i];
    }
    OS << "> (args.get<" << program_arg_types[0] << ">(\""
       << program_parameters[0] << "\")";
    for (int i = 1; i < program_parameters.size(); i++) {
      OS << ", "
         << "args.get<" << program_arg_types[i] << ">(\""
         << program_parameters[i] << "\")";
    }
    OS << ") {}\n";
  }

  // Destructor definition
  OS << "virtual ~" << kernel_name << "() {\n";
  OS << "if (disable_destructor) {return;}\n";
@@ -216,10 +236,13 @@ void QCORSyntaxHandler::GetReplacement(
  // If this is a FTQC kernel, skip runtime optimization passes and submit.
  OS << "if (runtime_env == QrtType::FTQC) {\n";
  OS << "if (is_callable) {\n";
  // If this is the top-level kernel, during DTor we persit the bit value to Buffer.
  // If this is the top-level kernel, during DTor we persit the bit value to
  // Buffer.
  OS << "quantum::persistBitstring(" << bufferNames[0] << ".results());\n";
  // Loop the function calls (at the top level only) if there are multiple shots requested.
  OS << "for (size_t shotCount = 1; shotCount < quantum::get_shots(); ++shotCount) {\n";
  // Loop the function calls (at the top level only) if there are multiple shots
  // requested.
  OS << "for (size_t shotCount = 1; shotCount < quantum::get_shots(); "
        "++shotCount) {\n";
  OS << "operator()(" << program_parameters[0];
  for (int i = 1; i < program_parameters.size(); i++) {
    OS << ", " << program_parameters[i];
@@ -265,7 +288,8 @@ void QCORSyntaxHandler::GetReplacement(
    OS << ", " << program_arg_types[i] << " " << program_parameters[i];
  }
  OS << ") {\n";
  OS << "class " << kernel_name << " __ker__temp__(parent, " << program_parameters[0];
  OS << "class " << kernel_name << " __ker__temp__(parent, "
     << program_parameters[0];
  for (int i = 1; i < program_parameters.size(); i++) {
    OS << ", " << program_parameters[i];
  }
@@ -286,6 +310,13 @@ void QCORSyntaxHandler::GetReplacement(
  OS << ");\n";
  OS << "}\n";

  if (add_het_map_ctor) {
    // Add the HeterogeneousMap args function overload
    OS << "void " << kernel_name
       << "__with_hetmap_args(HeterogeneousMap& args) {\n";
    OS << "class " << kernel_name << " __ker__temp__(args);\n";
    OS << "}\n";
  }
  auto s = OS.str();
  qcor::info("[qcor syntax-handler] Rewriting " + kernel_name + " to\n\n" + s);
}
@@ -346,7 +377,7 @@ public:

}  // namespace qcor

static SyntaxHandlerRegistry::Add<qcor::QCORSyntaxHandler>
    X("qcor", "qcor quantum kernel syntax handler");
static SyntaxHandlerRegistry::Add<qcor::QCORSyntaxHandler> X(
    "qcor", "qcor quantum kernel syntax handler");

static FrontendPluginRegistry::Add<qcor::QCORArgs> XX("qcor-args", "");
+1 −1
Original line number Diff line number Diff line
@@ -19,7 +19,7 @@ public:
                      std::vector<std::string> program_arg_types,
                      std::vector<std::string> program_parameters,
                      std::vector<std::string> bufferNames, CachedTokens &Toks,
                      llvm::raw_string_ostream &OS);
                      llvm::raw_string_ostream &OS, bool add_het_map_ctor = false);

  void AddToPredefines(llvm::raw_string_ostream &OS) override;
};
+1 −0
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ add_subdirectory(xasm)
add_subdirectory(staq)
#add_subdirectory(quil)
add_subdirectory(unitary)
add_subdirectory(pyxasm)

if (QCOR_BUILD_TESTS)
  add_subdirectory(tests)
+64 −0
Original line number Diff line number Diff line
# *******************************************************************************
# Copyright (c) 2019 UT-Battelle, LLC.
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v1.0
# and Eclipse Distribution License v.10 which accompany this distribution.
# The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
# and the Eclipse Distribution License is available at
# https://eclipse.org/org/documents/edl-v10.php
#
# Contributors:
#   Alexander J. McCaskey - initial API and implementation
# *******************************************************************************/
set(LIBRARY_NAME qcor-pyxasm-token)

set(ANTLR_LIB ${XACC_ROOT}/lib/libantlr4-runtime.so)
if (APPLE)
  set(ANTLR_LIB ${XACC_ROOT}/lib/libantlr4-runtime.dylib)
endif()

file(GLOB SRC *.cpp generated/*.cpp)

usfunctiongetresourcesource(TARGET ${LIBRARY_NAME} OUT SRC)
usfunctiongeneratebundleinit(TARGET ${LIBRARY_NAME} OUT SRC)

add_library(${LIBRARY_NAME} SHARED ${SRC})

target_compile_options(${LIBRARY_NAME} PRIVATE "-Wno-attributes")
target_include_directories(
  ${LIBRARY_NAME}
  PUBLIC . .. ${XACC_ROOT}/include/antlr4-runtime generated ${CLANG_INCLUDE_DIRS})

target_link_libraries(${LIBRARY_NAME} PUBLIC ${CLANG_LIBS} ${LLVM_LIBS} ${ANTLR_LIB} qrt xacc::xacc)

set(_bundle_name qcor_pyxasm_token)
set_target_properties(${LIBRARY_NAME}
                      PROPERTIES COMPILE_DEFINITIONS
                                 US_BUNDLE_NAME=${_bundle_name}
                                 US_BUNDLE_NAME
                                 ${_bundle_name})

usfunctionembedresources(TARGET
                         ${LIBRARY_NAME}
                         WORKING_DIRECTORY
                         ${CMAKE_CURRENT_SOURCE_DIR}
                         FILES
                         manifest.json)


if(APPLE)
  set_target_properties(${LIBRARY_NAME}
                        PROPERTIES INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib;${XACC_ROOT}/lib;${LLVM_INSTALL_PREFIX}/lib")
  set_target_properties(${LIBRARY_NAME}
                        PROPERTIES LINK_FLAGS "-undefined dynamic_lookup")
else()
  set_target_properties(${LIBRARY_NAME}
                        PROPERTIES INSTALL_RPATH "$ORIGIN/../lib:${LLVM_INSTALL_PREFIX}/lib")
  set_target_properties(${LIBRARY_NAME} PROPERTIES LINK_FLAGS "-shared")
endif()

install(TARGETS ${LIBRARY_NAME} DESTINATION ${CMAKE_INSTALL_PREFIX}/plugins)

if (QCOR_BUILD_TESTS)
add_subdirectory(tests)
endif()
 No newline at end of file
+3 −0
Original line number Diff line number Diff line
#!/bin/bash
set - o errexit
java -jar antlr-4.8-complete.jar -Dlanguage=Cpp -visitor -o generated/ -package pyxasm pyxasm.g4
Loading