Commit 8e6f5d59 authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

setup to add includes and libraries links to xacc compiler executable. added...


setup to add includes and libraries links to xacc compiler executable. added deuteronH3 using 3rd party nlopt
Signed-off-by: Mccaskey, Alex's avatarAlex McCaskey <mccaskeyaj@ornl.gov>
parent d79beeb7
Pipeline #61905 passed with stages
in 10 minutes and 22 seconds
......@@ -15,6 +15,10 @@ def parse_args(args):
help="The target quantum accelerator", required=False)
parser.add_argument("-I", '--include', action='append', type=str,
help="Extra include paths to search", required=False)
parser.add_argument("-L", "--link-path",action='append', type=str,
help="Extra link paths to search", required=False)
parser.add_argument("-l", "--link-library",action='append', type=str,
help="Extra libraries to link", required=False)
parser.add_argument("-o", '--output', type=str,
help="Name of output binary", required=False)
parser.add_argument("-v", '--verbose', action='store_true',
......@@ -33,9 +37,11 @@ def main(argv=None):
import time
command = ["@CMAKE_INSTALL_PREFIX@/bin/xacc-driver"]
includes = []
laterIncludes = []
if opts.include:
for include in opts.include:
includes.append('-I'+include)
includes += ['--include',include]
laterIncludes += ['-I', include]
command += includes
......@@ -63,9 +69,16 @@ def main(argv=None):
'-I', '@CMAKE_INSTALL_PREFIX@/include/cppmicroservices4']
baseLibs = ['@CMAKE_INSTALL_PREFIX@/lib/libxacc.so', '@CMAKE_INSTALL_PREFIX@/lib/libCppMicroServices.so']
if opts.link_path:
for p in opts.link_path:
baseLibs += ['-L', p]
if opts.link_library:
for l in opts.link_library:
baseLibs += ['-l', l]
command = ['clang++-9', '-Wno-attributes', '-O3', '-DNDEBUG', '-rdynamic', '-std=c++11',
'.' + opts.file.replace('.', '_out.'), '-Wl,-rpath,@CMAKE_INSTALL_PREFIX@/lib']
command += includes + baseIncludes + baseLibs
command += laterIncludes + baseIncludes + baseLibs
if opts.output:
command += ['-o', opts.output]
......@@ -86,8 +99,6 @@ def main(argv=None):
os.remove('.'+opts.file.replace('.','_out.'))
os.remove('.'+opts.file.replace('.','_pragma_out.')) if os.path.exists('.'+opts.file.replace('.','_pragma_out.')) else None
return 0
......
......@@ -56,7 +56,7 @@ bool KernelVisitor::VisitFunctionDecl(FunctionDecl *F) {
visitor.TraverseDecl(F);
auto function = visitor.getFunction();
// std::cout << "XACCIR:\n" << function->toString() << "\n";
std::cout << "XACCIR:\n" << function->toString() << "\n";
if (!xacc::optionExists("accelerator")) {
if (xacc::hasAccelerator("tnqvm")) {
......
......@@ -70,7 +70,7 @@ protected:
private:
std::map<std::string, SourceLocation> &map;
};
std::unique_ptr<clang::ASTConsumer>
CreateASTConsumer(clang::CompilerInstance &Compiler,
llvm::StringRef /* dummy */) override {
......@@ -181,6 +181,11 @@ int main(int argc, char **argv) {
xacc::Initialize(argc, argv);
auto extraPaths = xacc::getIncludePaths();
for (auto& p : extraPaths) {
p = "-I"+p;
}
// Get filename
std::string fileName(argv[argc - 1]);
if (!xacc::fileExists(fileName)) {
......@@ -198,6 +203,9 @@ int main(int argc, char **argv) {
std::vector<std::string> args{
"-std=c++11", "-I@CMAKE_INSTALL_PREFIX@/include/xacc",
"-I@CMAKE_INSTALL_PREFIX@/include/cppmicroservices4"};
args.insert(args.end(), extraPaths.begin(), extraPaths.end());
// for (auto& a : args) std::cout << "Arg: " << a << "\n";
if (!tooling::runToolOnCodeWithArgs(action, src, args)) {
xacc::error("Error running xacc compiler.");
......@@ -221,8 +229,10 @@ int main(int argc, char **argv) {
}
auto action2 = new XACCFrontendAction(Rewrite2, fileName);
if (!tooling::runToolOnCodeWithArgs(action2, src2, args)) {
xacc::error("Error running xacc compiler.");
}
return 0;
}
\ No newline at end of file
}
#include "xacc_runtime.hpp"
#include <iostream>
#include "nlopt.hpp"
__qpu__ void ansatz(qbit &q, double t0, double t1) {
X(0);
Ry(t0, 1);
Ry(t1, 2);
CX(2, 0);
CX(0, 1);
Ry(-t0, 1);
CX(0, 1);
CX(1, 0);
}
double objectiveFunction(const std::vector<double> &x,
std::vector<double> &grad, void *f_data) {
auto hamiltonian = xacc::getObservable(
"pauli", "5.907 - 2.1433 X0X1 - 2.1433 Y0Y1 + .21829 Z0 - 6.125 Z1 + "
"9.625 - 9.625 Z2 - 3.91 X1 X2 - 3.91 Y1 Y2");
qbit q = xacc::qalloc(3);
#pragma xacc observe hamiltonian
ansatz(q, x[0], x[1]);
return mpark::get<double>(q->getInformation("observation"));
}
int main(int argc, char **argv) {
xacc::Initialize(argc, argv);
auto dim = 2;
nlopt::algorithm algo = nlopt::algorithm::LN_COBYLA;
double tol = 1e-8;
int maxeval = 30;
nlopt::opt _opt(algo, dim);
_opt.set_min_objective(objectiveFunction, NULL);
_opt.set_lower_bounds(std::vector<double>(dim, -3.1415926));
_opt.set_upper_bounds(std::vector<double>(dim, 3.1415926));
_opt.set_maxeval(maxeval);
_opt.set_ftol_rel(tol);
double optF;
std::vector<double> x(dim);
try {
auto result = _opt.optimize(x, optF);
} catch (std::exception &e) {
xacc::XACCLogger::instance()->error("NLOpt failed: " +
std::string(e.what()));
}
std::cout << "Energy " << optF << "\n";
xacc::Finalize();
return 0;
}
\ No newline at end of file
......@@ -59,6 +59,10 @@ void ctrl_c_handler(int signal) {
exit(1);
}
std::vector<std::string> getIncludePaths() {
return xaccCLParser->getIncludePaths();
}
void PyInitialize(const std::string rootPath) {
std::vector<std::string> args{"--xacc-root-path", rootPath};
Initialize(args);
......@@ -67,7 +71,7 @@ void PyInitialize(const std::string rootPath) {
void Initialize(int arc, char **arv) {
if (!xaccFrameworkInitialized) {
std::shared_ptr<CLIParser> xaccCLParser = std::make_shared<CLIParser>();
xaccCLParser = std::make_shared<CLIParser>();
argc = arc;
argv = arv;
......@@ -75,7 +79,6 @@ void Initialize(int arc, char **arv) {
// Parse any user-supplied command line options
xaccCLParser->parse(argc, argv);//, serviceRegistry.get());
struct sigaction sigIntHandler;
sigIntHandler.sa_handler = ctrl_c_handler;
sigemptyset(&sigIntHandler.sa_mask);
......
......@@ -54,6 +54,8 @@ int getArgc();
*/
char **getArgv();
std::vector<std::string> getIncludePaths();
/**
* Initialize the framework with a vector of strings as arguments.
*
......
......@@ -68,7 +68,9 @@ TEST(CLIParserTester, checkParse) {
"--test2",
"value2",
"--logger-name",
"xacc"
"xacc",
"-I", "/path/to/include",
"-I", "/path/to/other"
});
char** actual_argv = argv.argv();
......@@ -77,6 +79,7 @@ TEST(CLIParserTester, checkParse) {
parser.addOptions({{"test","description"},{"test2","description"}});
parser.parse(argc, actual_argv);//, nullptr);
std::cout << "HELLO\n";
auto options = *RuntimeOptions::instance();
EXPECT_EQ(options["compiler"],"quil");
......@@ -86,6 +89,10 @@ TEST(CLIParserTester, checkParse) {
EXPECT_EQ(options["logger-name"],"xacc");
auto paths = parser.getIncludePaths();
for (auto& p : paths) {
std::cout << "CLParserTester Path: " << p << "\n";
}
}
int main(int argc, char **argv) {
......
......@@ -45,7 +45,9 @@ void CLIParser::parse(int argc,
"compiler", "Indicate the compiler to be used", value<std::string>())(
"a,accelerator", "Indicate the accelerator to be used.",
value<std::string>())("logger-name", "The name of the spd logger",
value<std::string>())(
value<std::string>())
("I,include","", value<std::vector<std::string>>())
(
"list-compilers", "List all available XACC Compilers")(
"list-accelerators", "List all available XACC Accelerators")(
"no-color",
......@@ -97,6 +99,10 @@ void CLIParser::parse(int argc,
exit(0);
}
if (clArgs.count("include")) {
paths = clArgs["include"].as<std::vector<std::string>>();
}
if (xacc::serviceAPIInitialized) {
auto kvargs = clArgs.arguments();
std::map<std::string, std::string> givenopts;
......@@ -125,6 +131,10 @@ void CLIParser::parse(int argc,
}
}
std::vector<std::string> CLIParser::getIncludePaths() {
return paths;
}
void CLIParser::addStringOption(const std::string key,
const std::string description) {
xaccOptions->add_options()(key, description, value<std::string>());
......
......@@ -39,7 +39,7 @@ class CLIParser {
protected:
std::shared_ptr<options_description> xaccOptions;
std::vector<std::string> paths;
public:
/**
* The constructor
......@@ -52,6 +52,7 @@ public:
const std::string description = "");
void addStringOptions(const std::string &category,
const std::map<std::string, std::string> &options);
std::vector<std::string> getIncludePaths();
};
} // namespace xacc
......
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