Loading handlers/qcor_syntax_handler.cpp +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> Loading @@ -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; Loading Loading @@ -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(); Loading Loading @@ -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]; } Loading Loading @@ -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"; Loading @@ -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]; Loading Loading @@ -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]; } Loading @@ -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); } Loading Loading @@ -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", ""); handlers/qcor_syntax_handler.hpp +1 −1 Original line number Diff line number Diff line Loading @@ -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; }; Loading handlers/token_collector/CMakeLists.txt +1 −0 Original line number Diff line number Diff line Loading @@ -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) Loading handlers/token_collector/pyxasm/CMakeLists.txt 0 → 100644 +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 handlers/token_collector/pyxasm/generate.sh 0 → 100755 +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
handlers/qcor_syntax_handler.cpp +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> Loading @@ -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; Loading Loading @@ -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(); Loading Loading @@ -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]; } Loading Loading @@ -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"; Loading @@ -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]; Loading Loading @@ -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]; } Loading @@ -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); } Loading Loading @@ -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", "");
handlers/qcor_syntax_handler.hpp +1 −1 Original line number Diff line number Diff line Loading @@ -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; }; Loading
handlers/token_collector/CMakeLists.txt +1 −0 Original line number Diff line number Diff line Loading @@ -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) Loading
handlers/token_collector/pyxasm/CMakeLists.txt 0 → 100644 +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
handlers/token_collector/pyxasm/generate.sh 0 → 100755 +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