Commit d2f9e956 authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

started on runtime package


Signed-off-by: Mccaskey, Alex's avatarAlex McCaskey <mccaskeyaj@ornl.gov>
parent 50a7a891
Pipeline #42593 passed with stages
in 2 minutes and 7 seconds
...@@ -18,7 +18,7 @@ option(QCOR_BUILD_TESTS "Build qcor tests" OFF) ...@@ -18,7 +18,7 @@ option(QCOR_BUILD_TESTS "Build qcor tests" OFF)
include(CTest) include(CTest)
find_package(Clang 8.0.0 REQUIRED) find_package(Clang 9.0.0 REQUIRED)
find_package(XACC REQUIRED) find_package(XACC REQUIRED)
if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
...@@ -35,7 +35,7 @@ include_directories(${XACC_INCLUDE_DIRS}) ...@@ -35,7 +35,7 @@ include_directories(${XACC_INCLUDE_DIRS})
link_directories(${XACC_LIBRARY_DIR}) link_directories(${XACC_LIBRARY_DIR})
message(STATUS "${CLANG_INCLUDE_DIRS}") #message(STATUS "${CLANG_INCLUDE_DIRS}")
macro(qcor_enable_rpath LIBNAME) macro(qcor_enable_rpath LIBNAME)
if(APPLE) if(APPLE)
...@@ -48,5 +48,6 @@ macro(qcor_enable_rpath LIBNAME) ...@@ -48,5 +48,6 @@ macro(qcor_enable_rpath LIBNAME)
endif() endif()
endmacro() endmacro()
add_subdirectory(runtime)
add_subdirectory(compiler) add_subdirectory(compiler)
add_subdirectory(instructions) add_subdirectory(instructions)
...@@ -13,9 +13,30 @@ std::shared_ptr<IR> QCORCompiler::compile(const std::string &src) { ...@@ -13,9 +13,30 @@ std::shared_ptr<IR> QCORCompiler::compile(const std::string &src) {
return compile(src, nullptr); return compile(src, nullptr);
} }
const std::shared_ptr<Function>
QCORCompiler::compile(std::shared_ptr<Function> f, std::shared_ptr<Accelerator> acc) {
if (acc) {
xacc::info("[qcor] Compiling for " + acc->name());
}
// Hardware Independent Transformation
// Hardware Dependent Transformations
if (acc) {
}
// Program Verification???
return f;
}
const std::string const std::string
QCORCompiler::translate(const std::string &bufferVariable, QCORCompiler::translate(const std::string &bufferVariable,
std::shared_ptr<Function> function) { std::shared_ptr<Function> function) {
xacc::error("QCORCompiler::translate() not implemented.");
return ""; return "";
} }
......
...@@ -13,7 +13,6 @@ ...@@ -13,7 +13,6 @@
#ifndef COMPILER_QCORCOMPILER_HPP_ #ifndef COMPILER_QCORCOMPILER_HPP_
#define COMPILER_QCORCOMPILER_HPP_ #define COMPILER_QCORCOMPILER_HPP_
#include "XACC.hpp" #include "XACC.hpp"
#include "antlr4-runtime.h"
using namespace xacc; using namespace xacc;
...@@ -51,6 +50,9 @@ public: ...@@ -51,6 +50,9 @@ public:
*/ */
virtual std::shared_ptr<xacc::IR> compile(const std::string &src); virtual std::shared_ptr<xacc::IR> compile(const std::string &src);
const std::shared_ptr<Function>
compile(std::shared_ptr<Function> f, std::shared_ptr<Accelerator> acc) override;
/** /**
* Return the command line options for this compiler * Return the command line options for this compiler
* *
......
set(LIBRARY_NAME qcor-ast-plugin) set(LIBRARY_NAME qcor-ast-plugin)
add_library(${LIBRARY_NAME} SHARED QCORPluginAction.cpp FuzzyParsingExternalSemaSource.cpp LambdaVisitor.cpp QCORASTConsumer.cpp) add_library(${LIBRARY_NAME} SHARED QCORPluginAction.cpp FuzzyParsingExternalSemaSource.cpp LambdaVisitor.cpp QCORASTConsumer.cpp)
target_include_directories(${LIBRARY_NAME} PUBLIC .) target_include_directories(${LIBRARY_NAME} PUBLIC .)
target_include_directories(${LIBRARY_NAME} PUBLIC ${CMAKE_SOURCE_DIR}/runtime)
target_include_directories(${LIBRARY_NAME} PUBLIC ${CLANG_INCLUDE_DIRS}) target_include_directories(${LIBRARY_NAME} PUBLIC ${CLANG_INCLUDE_DIRS})
target_include_directories(${LIBRARY_NAME} PUBLIC ${LLVM_INCLUDE_DIRS}) target_include_directories(${LIBRARY_NAME} PUBLIC ${LLVM_INCLUDE_DIRS})
target_include_directories(${LIBRARY_NAME} PUBLIC ${XACC_INCLUDE_DIRS}) target_include_directories(${LIBRARY_NAME} PUBLIC ${XACC_INCLUDE_DIRS})
target_link_libraries(${LIBRARY_NAME} PUBLIC ${CLANG_LIBS} ${XACC_LIBRARIES}) target_link_libraries(${LIBRARY_NAME} PUBLIC ${CLANG_LIBS} ${XACC_LIBRARIES} qcor)
#include "LambdaVisitor.hpp" #include "LambdaVisitor.hpp"
#include "/home/project/qcor/runtime/qcor.hpp"
#include "IRProvider.hpp" #include "IRProvider.hpp"
#include "XACC.hpp" #include "XACC.hpp"
#include "qcor.hpp"
using namespace clang; using namespace clang;
using namespace xacc; using namespace xacc;
namespace qcor { namespace qcor {
namespace compiler { namespace compiler {
LambdaVisitor::IsQuantumKernelVisitor::IsQuantumKernelVisitor(ASTContext &c)
: context(c) {
auto irProvider = xacc::getService<xacc::IRProvider>("gate");
validInstructions = irProvider->getInstructions();
validInstructions.push_back("CX");
}
bool LambdaVisitor::IsQuantumKernelVisitor::VisitDeclRefExpr(
DeclRefExpr *expr) {
if (expr->getType() == context.DependentTy) {
auto gateName = expr->getNameInfo().getAsString();
if (std::find(validInstructions.begin(), validInstructions.end(),
gateName) != validInstructions.end()) {
_isQuantumKernel = true;
}
}
}
LambdaVisitor::CppToXACCIRVisitor::CppToXACCIRVisitor(ASTContext &c) LambdaVisitor::CppToXACCIRVisitor::CppToXACCIRVisitor(ASTContext &c)
: context(c) { : context(c) {
provider = xacc::getService<IRProvider>("gate"); provider = xacc::getService<IRProvider>("gate");
...@@ -73,40 +95,68 @@ std::shared_ptr<Function> LambdaVisitor::CppToXACCIRVisitor::getFunction() { ...@@ -73,40 +95,68 @@ std::shared_ptr<Function> LambdaVisitor::CppToXACCIRVisitor::getFunction() {
LambdaVisitor::LambdaVisitor(CompilerInstance &c) : ci(c) {} LambdaVisitor::LambdaVisitor(CompilerInstance &c) : ci(c) {}
bool LambdaVisitor::VisitLambdaExpr(LambdaExpr *LE) { bool LambdaVisitor::VisitLambdaExpr(LambdaExpr *LE) {
// SourceManager &SM = ci.getSourceManager();
// LangOptions &lo = ci.getLangOpts();
// lo.CPlusPlus11 = 1;
// auto xaccKernelLambdaStr =
// Lexer::getSourceText(CharSourceRange(LE->getSourceRange(), true), SM, lo)
// .str();
// std::cout << "Check it out, I got the Lambda as a source string :)\n"; // Get the Lambda Function Body
// xacc::info(xaccKernelLambdaStr); Stmt *q_kernel_body = LE->getBody();
CppToXACCIRVisitor visitor(ci.getASTContext()); // Double check... Is this a Quantum Kernel Lambda?
IsQuantumKernelVisitor isqk(ci.getASTContext());
isqk.TraverseStmt(LE->getBody());
// LE->getType().dump(); // If it is, then map it to XACC IR
if (isqk.isQuantumKernel()) {
// create empty statement, does nothing // For debugging for now
// Stmt *tmp = (Stmt *)new (ci.getASTContext()) NullStmt(nopos); std::cout << "\n\n";
// std::vector<Stmt *> stmts; q_kernel_body->dump();
// stmts.push_back(tmp);
// replace LE's compound statement with that statement
// LE->getBody()->setLastStmt(ReturnStmt::CreateEmpty(
// ci.getASTContext(),
// false));
Stmt *q_kernel_body = LE->getBody(); CppToXACCIRVisitor visitor(ci.getASTContext());
q_kernel_body->dump(); visitor.TraverseStmt(LE->getBody());
auto function = visitor.getFunction();
std::cout << "\n\nXACC IR:\n" << function->toString() << "\n";
visitor.TraverseStmt(LE->getBody()); // Kick off quantum compilation
auto function = visitor.getFunction(); auto qcor = xacc::getCompiler("qcor");
std::cout << "\n\nXACC IR:\n" << function->toString() << "\n";
std::shared_ptr<Accelerator> targetAccelerator;
if (xacc::optionExists("accelerator")) {
targetAccelerator = xacc::getAccelerator();
}
// Kick off quantum compilation function = qcor->compile(function, targetAccelerator);
auto fileName = qcor::persistCompiledCircuit(function);
// write function ir to file
// update LE body to
// [](...) {
// return qcor::loadCompiledCircuit(filename);
// }
}
return true; return true;
} }
} // namespace compiler } // namespace compiler
} // namespace qcor } // namespace qcor
// LE->getType().dump();
// create empty statement, does nothing
// Stmt *tmp = (Stmt *)new (ci.getASTContext()) NullStmt(nopos);
// std::vector<Stmt *> stmts;
// stmts.push_back(tmp);
// replace LE's compound statement with that statement
// LE->getBody()->setLastStmt(ReturnStmt::CreateEmpty(
// ci.getASTContext(),
// false));
// SourceManager &SM = ci.getSourceManager();
// LangOptions &lo = ci.getLangOpts();
// lo.CPlusPlus11 = 1;
// auto xaccKernelLambdaStr =
// Lexer::getSourceText(CharSourceRange(LE->getSourceRange(), true), SM,
// lo)
// .str();
// std::cout << "Check it out, I got the Lambda as a source string :)\n";
// xacc::info(xaccKernelLambdaStr);
...@@ -20,6 +20,19 @@ namespace compiler { ...@@ -20,6 +20,19 @@ namespace compiler {
class LambdaVisitor : public RecursiveASTVisitor<LambdaVisitor> { class LambdaVisitor : public RecursiveASTVisitor<LambdaVisitor> {
protected: protected:
class IsQuantumKernelVisitor
: public RecursiveASTVisitor<IsQuantumKernelVisitor> {
protected:
ASTContext &context;
bool _isQuantumKernel = false;
std::vector<std::string> validInstructions;
public:
IsQuantumKernelVisitor(ASTContext &c);
bool VisitDeclRefExpr(DeclRefExpr *expr);
bool isQuantumKernel() { return _isQuantumKernel; }
};
class CppToXACCIRVisitor : public RecursiveASTVisitor<CppToXACCIRVisitor> { class CppToXACCIRVisitor : public RecursiveASTVisitor<CppToXACCIRVisitor> {
protected: protected:
ASTContext &context; ASTContext &context;
......
...@@ -11,19 +11,50 @@ QCORPluginAction::CreateASTConsumer(CompilerInstance &ci, llvm::StringRef) { ...@@ -11,19 +11,50 @@ QCORPluginAction::CreateASTConsumer(CompilerInstance &ci, llvm::StringRef) {
} }
bool QCORPluginAction::ParseArgs(const CompilerInstance &ci, bool QCORPluginAction::ParseArgs(const CompilerInstance &ci,
const std::vector<std::string> &args) { const std::vector<std::string> &args) {
if (!xacc::isInitialized()) { if (!xacc::isInitialized()) {
xacc::Initialize(args); std::vector<std::string> local;
local.push_back("--logger-name");
local.push_back("qcor");
xacc::Initialize(local);
} }
for (auto a : args) for (auto a : args) {
xacc::info("qcor argument: " + a); xacc::info("qcor argument: " + a);
}
auto it = std::find(args.begin(), args.end(), "accelerator");
if (it != args.end()) {
int index = std::distance(args.begin(), it);
auto acc = args[index + 1];
xacc::setAccelerator(acc);
}
std::vector<std::string> transformations;
it = args.begin();
std::for_each(args.begin(), args.end(), [&](const std::string &value) {
if (value == "transform") {
int index = std::distance(args.begin(), it);
auto transformationName = args[index + 1];
transformations.push_back(transformationName);
}
++it;
});
if (!transformations.empty()) {
std::string transformNames = transformations[0];
for (int i = 1; i < transformations.size(); ++i) {
transformNames += "," + transformations[i];
}
xacc::setOption("qcor-transforms",transformNames);
}
return true; return true;
} }
PluginASTAction::ActionType QCORPluginAction::getActionType() { PluginASTAction::ActionType QCORPluginAction::getActionType() {
return PluginASTAction::AddBeforeMainAction; return PluginASTAction::AddBeforeMainAction;
} }
} } // namespace compiler
} // namespace qcor } // namespace qcor
static FrontendPluginRegistry::Add<qcor::compiler::QCORPluginAction> static FrontendPluginRegistry::Add<qcor::compiler::QCORPluginAction>
......
...@@ -6,29 +6,44 @@ with args ...@@ -6,29 +6,44 @@ with args
clang++-9 -std=c++11 -Xclang -load -Xclang compiler/clang/libqcor-ast-plugin.so -Xclang -add-plugin -Xclang enable-quantum -Xclang -plugin-arg-enable-quantum -Xclang test -Xclang -v test.cpp clang++-9 -std=c++11 -Xclang -load -Xclang compiler/clang/libqcor-ast-plugin.so -Xclang -add-plugin -Xclang enable-quantum -Xclang -plugin-arg-enable-quantum -Xclang test -Xclang -v test.cpp
A better example
clang++-9 -std=c++11 -Xclang -load -Xclang compiler/clang/libqcor-ast-plugin.so
-Xclang -add-plugin -Xclang enable-quantum
-Xclang -plugin-arg-enable-quantum -Xclang accelerator
-Xclang -plugin-arg-enable-quantum -Xclang tnqvm
-Xclang -plugin-arg-enable-quantum -Xclang transform
-Xclang -plugin-arg-enable-quantum -Xclang circuit-optimizer
test.cpp
test.cpp looks like this test.cpp looks like this
#include <stdio.h> #include <stdio.h>
#include "qcor.hpp"
void foo(int* a, int *b) { void foo() {
if (a[0] > 1) {
b[0] = 2;
}
printf("hi\n"); printf("hi\n");
auto l = [&]() { qcor::submit([&](qcor::qpu_handler& qh){
int i = 1; qh.vqe([&](double t0){
printf("%d \n", 2); X(0);
// X(0); Ry(t0,0);
// analog("ibm-hamiltonian-evolve"); CX(1,0);
// autogen("uccsd",2); }, 1, 1);
}; });
l();
auto future = qcor::submit([&](qcor::qpu_handler& qh){
qh.execute([&](double t0){
X(2);
Ry(t0,1);
CX(1,2);
});
});
printf("hi %d\n", future.get());
} }
int main() { int main() {
int a = 1; foo();
int b = 2;
foo(&a, &b);
} }
\ No newline at end of file
set(LIBRARY_NAME qcor)
file(GLOB SRC *.cpp)
add_library(${LIBRARY_NAME} SHARED ${SRC})
target_include_directories(${LIBRARY_NAME}
PUBLIC .
${XACC_INCLUDE_DIRS}
)
target_link_libraries(${LIBRARY_NAME} PUBLIC xacc)
#qcor_enable_rpath(${LIBRARY_NAME})
if(QCOR_BUILD_TESTS)
#add_subdirectory(tests)
endif()
file(GLOB HEADERS *.hpp)
#install(TARGETS ${LIBRARY_NAME} DESTINATION ${CMAKE_INSTALL_PREFIX}/plugins)
#include "qcor.hpp"
#include "IRProvider.hpp"
#include "XACC.hpp"
using namespace xacc;
namespace qcor {
const std::string persistCompiledCircuit(std::shared_ptr<Function> function) {
return "";
}
std::shared_ptr<Function> loadCompiledCircuit() {
auto provider = xacc::getService<IRProvider>("gate");
auto function = provider->createFunction("tmp", {});
return function;
}
std::future<int> submit(HandlerLambda &&totalJob) {
// Create the QPU Handler to pass to the given
// Handler HandlerLambda
qpu_handler handler;
return std::async(std::launch::async, [&] {
totalJob(handler);
return 1;
});
}
} // namespace qcor
\ No newline at end of file
#ifndef RUNTIME_QCOR_HPP_
#define RUNTIME_QCOR_HPP_
#include <future>
#include "qpu_handler.hpp"
namespace xacc {
class Function;
}
using namespace xacc;
namespace qcor {
using HandlerLambda = std::function<void(qpu_handler &)>;
// Persist the given function to file, return
// the file name
const std::string persistCompiledCircuit(std::shared_ptr<Function> function);
// Load the compiled circuit from file
std::shared_ptr<Function> loadCompiledCircuit(const std::string &fileName);
// Submit an asynchronous job to the QPU
std::future<int> submit(HandlerLambda &&);
} // namespace qcor
#endif
#ifndef RUNTIME_QPU_HANDLER_HPP_
#define RUNTIME_QPU_HANDLER_HPP_
#include <string>
#include "XACC.hpp"
namespace qcor {
class qpu_handler {
public:
template <typename QuantumKernel>
void vqe(QuantumKernel &&kernel, double observable, double optimizer) {
xacc::info("qcor executing vqe.");
}
template <typename QuantumKernel> void execute(QuantumKernel &&kernel) {}
template <typename QuantumKernel>
void execute(const std::string &algorithm, QuantumKernel &&kernel) {}
};
} // namespace qcor
#endif
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment