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)
include(CTest)
find_package(Clang 8.0.0 REQUIRED)
find_package(Clang 9.0.0 REQUIRED)
find_package(XACC REQUIRED)
if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
......@@ -35,7 +35,7 @@ include_directories(${XACC_INCLUDE_DIRS})
link_directories(${XACC_LIBRARY_DIR})
message(STATUS "${CLANG_INCLUDE_DIRS}")
#message(STATUS "${CLANG_INCLUDE_DIRS}")
macro(qcor_enable_rpath LIBNAME)
if(APPLE)
......@@ -48,5 +48,6 @@ macro(qcor_enable_rpath LIBNAME)
endif()
endmacro()
add_subdirectory(runtime)
add_subdirectory(compiler)
add_subdirectory(instructions)
......@@ -13,9 +13,30 @@ std::shared_ptr<IR> QCORCompiler::compile(const std::string &src) {
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
QCORCompiler::translate(const std::string &bufferVariable,
std::shared_ptr<Function> function) {
xacc::error("QCORCompiler::translate() not implemented.");
return "";
}
......
......@@ -13,7 +13,6 @@
#ifndef COMPILER_QCORCOMPILER_HPP_
#define COMPILER_QCORCOMPILER_HPP_
#include "XACC.hpp"
#include "antlr4-runtime.h"
using namespace xacc;
......@@ -51,6 +50,9 @@ public:
*/
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
*
......
set(LIBRARY_NAME qcor-ast-plugin)
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 ${CMAKE_SOURCE_DIR}/runtime)
target_include_directories(${LIBRARY_NAME} PUBLIC ${CLANG_INCLUDE_DIRS})
target_include_directories(${LIBRARY_NAME} PUBLIC ${LLVM_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 "/home/project/qcor/runtime/qcor.hpp"
#include "IRProvider.hpp"
#include "XACC.hpp"
#include "qcor.hpp"
using namespace clang;
using namespace xacc;
namespace qcor {
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)
: context(c) {
provider = xacc::getService<IRProvider>("gate");
......@@ -73,40 +95,68 @@ std::shared_ptr<Function> LambdaVisitor::CppToXACCIRVisitor::getFunction() {
LambdaVisitor::LambdaVisitor(CompilerInstance &c) : ci(c) {}
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";
// xacc::info(xaccKernelLambdaStr);
CppToXACCIRVisitor visitor(ci.getASTContext());
// Get the Lambda Function Body
Stmt *q_kernel_body = LE->getBody();
// LE->getType().dump();
// Double check... Is this a Quantum Kernel Lambda?
IsQuantumKernelVisitor isqk(ci.getASTContext());
isqk.TraverseStmt(LE->getBody());
// 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));
// If it is, then map it to XACC IR
if (isqk.isQuantumKernel()) {
Stmt *q_kernel_body = LE->getBody();
// For debugging for now
std::cout << "\n\n";
q_kernel_body->dump();
CppToXACCIRVisitor visitor(ci.getASTContext());
visitor.TraverseStmt(LE->getBody());
auto function = visitor.getFunction();
std::cout << "\n\nXACC IR:\n" << function->toString() << "\n";
// Kick off quantum compilation
auto qcor = xacc::getCompiler("qcor");
std::shared_ptr<Accelerator> targetAccelerator;
if (xacc::optionExists("accelerator")) {
targetAccelerator = xacc::getAccelerator();
}
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;
}
} // namespace compiler
} // 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 {
class LambdaVisitor : public RecursiveASTVisitor<LambdaVisitor> {
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> {
protected:
ASTContext &context;
......
......@@ -13,17 +13,48 @@ QCORPluginAction::CreateASTConsumer(CompilerInstance &ci, llvm::StringRef) {
bool QCORPluginAction::ParseArgs(const CompilerInstance &ci,
const std::vector<std::string> &args) {
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);
}
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;
}
PluginASTAction::ActionType QCORPluginAction::getActionType() {
return PluginASTAction::AddBeforeMainAction;
}
}
} // namespace compiler
} // namespace qcor
static FrontendPluginRegistry::Add<qcor::compiler::QCORPluginAction>
......
......@@ -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
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
#include <stdio.h>
#include "qcor.hpp"
void foo(int* a, int *b) {
if (a[0] > 1) {
b[0] = 2;
}
void foo() {
printf("hi\n");
auto l = [&]() {
int i = 1;
printf("%d \n", 2);
// X(0);
// analog("ibm-hamiltonian-evolve");
// autogen("uccsd",2);
};
l();
qcor::submit([&](qcor::qpu_handler& qh){
qh.vqe([&](double t0){
X(0);
Ry(t0,0);
CX(1,0);
}, 1, 1);
});
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 a = 1;
int b = 2;
foo(&a, &b);
foo();
}
\ 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
Supports Markdown
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