Skip to content

Commit

Permalink
setup to add includes and libraries links to xacc compiler executable…
Browse files Browse the repository at this point in the history
…. added deuteronH3 using 3rd party nlopt

Signed-off-by: Alex McCaskey <mccaskeyaj@ornl.gov>
  • Loading branch information
Alex McCaskey committed Jul 2, 2019
1 parent d79beeb commit 8e6f5d5
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 12 deletions.
19 changes: 15 additions & 4 deletions scripts/xacc.in
Expand Up @@ -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',
Expand All @@ -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

Expand Down Expand Up @@ -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]
Expand All @@ -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


Expand Down
2 changes: 1 addition & 1 deletion tools/compiler/clang/KernelVisitor.cpp
Expand Up @@ -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")) {
Expand Down
14 changes: 12 additions & 2 deletions tools/compiler/clang/xacc-driver.in.cpp
Expand Up @@ -70,7 +70,7 @@ class XACCFrontendPragmaAction : public clang::ASTFrontendAction {
private:
std::map<std::string, SourceLocation> &map;
};

std::unique_ptr<clang::ASTConsumer>
CreateASTConsumer(clang::CompilerInstance &Compiler,
llvm::StringRef /* dummy */) override {
Expand Down Expand Up @@ -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)) {
Expand All @@ -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.");
Expand All @@ -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;
}
}
56 changes: 56 additions & 0 deletions tools/compiler/examples/deuteronH3.cpp
@@ -0,0 +1,56 @@
#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;
}
7 changes: 5 additions & 2 deletions xacc/XACC.cpp
Expand Up @@ -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);
Expand All @@ -67,15 +71,14 @@ 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;

xacc::ServiceAPI_Initialize(argc, argv);

// 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);
Expand Down
2 changes: 2 additions & 0 deletions xacc/XACC.hpp
Expand Up @@ -54,6 +54,8 @@ int getArgc();
*/
char **getArgv();

std::vector<std::string> getIncludePaths();

/**
* Initialize the framework with a vector of strings as arguments.
*
Expand Down
9 changes: 8 additions & 1 deletion xacc/tests/CLIParserTester.cpp
Expand Up @@ -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();
Expand All @@ -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");
Expand All @@ -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) {
Expand Down
12 changes: 11 additions & 1 deletion xacc/utils/CLIParser.cpp
Expand Up @@ -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",
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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>());
Expand Down
3 changes: 2 additions & 1 deletion xacc/utils/CLIParser.hpp
Expand Up @@ -39,7 +39,7 @@ class CLIParser {

protected:
std::shared_ptr<options_description> xaccOptions;

std::vector<std::string> paths;
public:
/**
* The constructor
Expand All @@ -52,6 +52,7 @@ class CLIParser {
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
Expand Down

0 comments on commit 8e6f5d5

Please sign in to comment.