Commit 81051b21 authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

committing cleanup on qbit for use in qcor, and xasm compiler


Signed-off-by: Mccaskey, Alex's avatarAlex McCaskey <mccaskeyaj@ornl.gov>
parent 2c27773a
......@@ -576,7 +576,9 @@ PYBIND11_MODULE(_pyxacc, m) {
});
m.def("qasm", &xacc::qasm, "");
m.def("getCompiled", &xacc::getCompiled, "");
m.def("qalloc", &xacc::qalloc, "");
m.def("qalloc", [](const int n) -> std::shared_ptr<xacc::AcceleratorBuffer> {
return xacc::qalloc(n);
}, "");
m.def(
"info", [](const std::string s) { xacc::info(s); }, "");
m.def(
......
......@@ -10,23 +10,42 @@ using namespace xacc::quantum;
namespace xacc {
namespace generators {
bool UCCSD::validateOptions() {
if (!options.count("nqubits")) {
return false;//xacc::error("UCCSD requires 'nqubits' key.");
}
if (!options.count("nelectrons")) {
return false;//xacc::error("UCCSD requires 'nelectrons' key.");
}
if (options["nqubits"].isVariable()) {
return false;
}
if (options["nelectrons"].isVariable()) {
return false;
}
return true;
}
std::shared_ptr<Function>
UCCSD::generate(std::map<std::string, InstructionParameter> &parameters) {
if (!parameters.count("nelectrons")) {
if (!parameters.count("nelectrons") && parameters["nelectrons"].isVariable()) {
xacc::error("Invalid mapping of parameters for UCCSD generator, missing "
"nelectrons key.");
}
if (!parameters.count("nqubits")) {
if (!parameters.count("nqubits") && parameters["nqubits"].isVariable()) {
xacc::error("Invalid mapping of parameters for UCCSD generator, missing "
"nqubits key.");
}
std::vector<xacc::InstructionParameter> variables;
if (!parameters.empty()) {
if (!validateOptions() && !parameters.empty()) {
options = parameters;
}
......
......@@ -13,6 +13,7 @@ class UCCSD : public xacc::IRGenerator {
public:
std::shared_ptr<Function>
generate(std::map<std::string, InstructionParameter> &parameters) override;
bool validateOptions() override;
const std::string name() const override { return "uccsd"; }
......
......@@ -41,6 +41,24 @@ TEST(XASMCompilerTester, checkApplyAll) {
}
TEST(XASMCompilerTester, checkUnknownParameter) {
auto compiler = xacc::getCompiler("xasm");
auto IR = compiler -> compile(R"([&](qbit q) {
uccsd(q, {{"nqubits",4},{"nelectrons",2}});
})");
EXPECT_EQ(1, IR->getKernels().size());
std::cout << "KERNEL\n" << IR->getKernels()[0]->toString() << "\n";
IR = compiler -> compile(R"([&](qbit q) {
uccsd(q, {{"nqubits",4},{"nelectrons",ne}});
})");
EXPECT_EQ(1, IR->getKernels().size());
auto f = IR->getKernels()[0];
std::cout << "KERNEL2\n" << IR->getKernels()[0]->toString() << "\n";
f->expandIRGenerators({{"nqubits", 4}, {"nelectrons",2}});
std::cout << "KERNEL3\n" << f->toString() << "\n";
}
......
......@@ -80,6 +80,7 @@ void XASMListener::enterInstruction(xasmParser::InstructionContext *ctx) {
auto key = ctx->options->optionsType(i)->key->getText();
key.erase(remove(key.begin(), key.end(), '\"'), key.end());
auto val = ctx->options->optionsType(i)->value->getText();
symbol_table_t symbol_table;
symbol_table.add_constants();
expression_t expr;
......@@ -87,7 +88,12 @@ void XASMListener::enterInstruction(xasmParser::InstructionContext *ctx) {
parser_t parser;
if (parser.compile(val, expr)) {
auto value = expr.value();
options.insert({key, value});
if (ctx->options->optionsType(i)->value->INT() != nullptr) {
int tmp = (int) value;
options.insert({key, tmp});
} else {
options.insert({key, value});
}
} else {
val.erase(remove(val.begin(), val.end(), '\"'), val.end());
options.insert({key, val});
......@@ -108,8 +114,13 @@ void XASMListener::enterInstruction(xasmParser::InstructionContext *ctx) {
xacc::error("Cannot create IRGenerator with name " + instructionName);
}
}
for (auto& kv : options) generator->setOption(kv.first, kv.second);
if (generator->validateOptions()) {
auto genF = generator->generate(options);
function->addInstruction(genF);
} else {
function->addInstruction(generator);
}
} else if (xacc::hasCompiled(instructionName)) {
auto f = xacc::getCompiled(instructionName);
function->addInstruction(f);
......
......@@ -32,7 +32,7 @@ namespace xacc {
bool isPyApi = false;
bool xaccFrameworkInitialized = false;
std::shared_ptr<CLIParser> xaccCLParser; // = std::make_shared<CLIParser>();
std::shared_ptr<CLIParser> xaccCLParser;
int argc = 0;
char **argv = NULL;
std::map<std::string, std::shared_ptr<Function>> compilation_database{};
......@@ -40,7 +40,6 @@ std::map<std::string, std::shared_ptr<Function>> compilation_database{};
int getArgc() { return argc; }
char **getArgv() { return argv; }
void Initialize(std::vector<std::string> argv) {
// XACCLogger::instance()->info("Initializing the XACC Framework");
std::vector<char *> cstrs;
argv.insert(argv.begin(), "appExec");
for (auto &s : argv) {
......@@ -78,7 +77,7 @@ void Initialize(int arc, char **arv) {
xacc::ServiceAPI_Initialize(argc, argv);
// Parse any user-supplied command line options
xaccCLParser->parse(argc, argv); //, serviceRegistry.get());
xaccCLParser->parse(argc, argv);
struct sigaction sigIntHandler;
sigIntHandler.sa_handler = ctrl_c_handler;
sigemptyset(&sigIntHandler.sa_mask);
......@@ -127,7 +126,8 @@ void error(const std::string &msg, MessagePredicate predicate) {
}
qbit qalloc(const int n) {
return std::make_shared<xacc::AcceleratorBuffer>(n);
qbit q(n);
return q;//std::make_shared<xacc::AcceleratorBuffer>(n);
}
void addCommandLineOption(const std::string &optionName,
......@@ -203,7 +203,8 @@ std::shared_ptr<Accelerator> getAccelerator(AcceleratorParameters params) {
return acc;
}
std::shared_ptr<Accelerator> getAccelerator(const std::string &name,
std::shared_ptr<Client> client, AcceleratorParameters params) {
std::shared_ptr<Client> client,
AcceleratorParameters params) {
if (!xacc::xaccFrameworkInitialized) {
error("XACC not initialized before use. Please execute "
"xacc::Initialize() before using API.");
......@@ -228,7 +229,8 @@ std::shared_ptr<Accelerator> getAccelerator(const std::string &name,
return acc;
}
std::shared_ptr<Accelerator> getAccelerator(const std::string &name, AcceleratorParameters params) {
std::shared_ptr<Accelerator> getAccelerator(const std::string &name,
AcceleratorParameters params) {
if (!xacc::xaccFrameworkInitialized) {
error("XACC not initialized before use. Please execute "
"xacc::Initialize() before using API.");
......@@ -245,8 +247,9 @@ std::shared_ptr<Accelerator> getAccelerator(const std::string &name, Accelerator
if (xacc::hasContributedService<Accelerator>(name)) {
acc = xacc::getContributedService<Accelerator>(name);
if (acc) acc->initialize(params);
acc = xacc::getContributedService<Accelerator>(name);
if (acc)
acc->initialize(params);
} else {
error("Invalid Accelerator. Could not find " + name +
......@@ -271,10 +274,11 @@ std::shared_ptr<Compiler> getCompiler(const std::string &name) {
}
auto c = xacc::getService<Compiler>(name, false);
if (!c) {
if (xacc::hasContributedService<Compiler>(name)) {
c = xacc::getContributedService<Compiler>(name);
if (xacc::hasContributedService<Compiler>(name)) {
c = xacc::getContributedService<Compiler>(name);
} else {
error("Invalid Compiler. Could not find " + name + " in Service Registry.");
error("Invalid Compiler. Could not find " + name +
" in Service Registry.");
}
}
return c;
......@@ -292,7 +296,7 @@ std::shared_ptr<Algorithm> getAlgorithm(const std::string name,
xacc::AlgorithmParameters &params) {
auto algo = xacc::getAlgorithm(name);
if (!algo->initialize(params)) {
error("Error initializing " + name + " algorithm.");
error("Error initializing " + name + " algorithm.");
}
return algo;
}
......@@ -337,7 +341,6 @@ std::shared_ptr<IRProvider> getIRProvider(const std::string &name) {
return irp;
}
std::shared_ptr<IRGenerator> getIRGenerator(const std::string &name) {
if (!xacc::xaccFrameworkInitialized) {
error("XACC not initialized before use. Please execute "
......@@ -346,11 +349,11 @@ std::shared_ptr<IRGenerator> getIRGenerator(const std::string &name) {
auto irp = xacc::getService<IRGenerator>(name, false);
if (!irp) {
if(xacc::hasContributedService<IRGenerator>(name)) {
irp = xacc::getContributedService<IRGenerator>(name);
if (xacc::hasContributedService<IRGenerator>(name)) {
irp = xacc::getContributedService<IRGenerator>(name);
} else {
error("Invalid IRProvicer. Could not find " + name +
" in Service Registry.");
error("Invalid IRProvicer. Could not find " + name +
" in Service Registry.");
}
}
return irp;
......@@ -589,12 +592,13 @@ const std::string getRootDirectory() { return xacc::getRootPathString(); }
void appendCompiled(std::shared_ptr<Function> function) {
if (compilation_database.count(function->name())) {
xacc::error("Invalid Function name, already in compilation database: " + function->name() + ".");
xacc::error("Invalid Function name, already in compilation database: " +
function->name() + ".");
}
compilation_database.insert({function->name(), function});
}
bool hasCompiled(const std::string name) {
return compilation_database.count(name);
return compilation_database.count(name);
}
std::shared_ptr<Function> getCompiled(const std::string name) {
......@@ -618,7 +622,7 @@ void qasm(const std::string &qasmString) {
auto lines = split(qasmString, '\n');
std::string currentFunctionName = "";
for (auto &l : lines) {
xacc::trim(l);
xacc::trim(l);
if (l.find(".compiler") == std::string::npos &&
l.find(".function") == std::string::npos && !l.empty()) {
function2code[currentFunctionName] += l + "\n";
......
......@@ -27,13 +27,10 @@ namespace constants {
static constexpr double pi = 3.141592653589793238;
}
class CLIParser;
// Boolean indicating that framework has been initialized
extern bool xaccFrameworkInitialized;
extern bool isPyApi;
// Reference to the command line parser
class CLIParser;
extern std::shared_ptr<CLIParser> xaccCLParser;
extern int argc;
......@@ -41,220 +38,81 @@ extern char **argv;
extern std::map<std::string, std::shared_ptr<Function>> compilation_database;
/**
* This method should be called by
* clients to initialize the XACC framework.
* It should be called before using any of the
* XACC API.
*/
using AcceleratorBufferPtr = std::shared_ptr<xacc::AcceleratorBuffer>;
class qbit : public AcceleratorBufferPtr{
public:
qbit(const int n) : AcceleratorBufferPtr(std::make_shared<xacc::AcceleratorBuffer>(n)) {}
void operator[](const int& i) {}
};
qbit qalloc(const int n);
void Initialize(int argc, char **argv);
void Initialize(std::vector<std::string> argv);
void Initialize();
bool isInitialized();
void PyInitialize(const std::string rootPath);
/**
* Return command line argc integer.
*
* @return argc The number of arguments on the command line
*/
int getArgc();
/**
* Return the command line arguments
*
* @return argv The command line arguments
*/
char **getArgv();
std::vector<std::string> getIncludePaths();
/**
* Initialize the framework with a vector of strings as arguments.
*
* @param argv Vector of string arguments
*/
void Initialize(std::vector<std::string> argv);
/**
* Initialize the framework with no arguments
*/
void Initialize();
/**
* Return if the framework has been initialized.
*
* @return initialized Bool indicating true if initialized
*/
bool isInitialized();
void setIsPyApi();
/**
* Add a valid command line option
*
* @param optionName The name of the string option
* @param optionDescription The description of the option
*/
void addCommandLineOption(const std::string &optionName,
const std::string &optionDescription = "");
/**
* Add a set of options under the provided category.
*
* @param category The name of the category these options fall under
* @param options The options, a map of option names to descriptions
*/
void addCommandLineOptions(const std::string &category,
const std::map<std::string, std::string> &options);
/**
* Add a set of options as a boost options_description object. This method
* gives clients the most flexibility to provide custom options.
*
* @param options The command line options
*/
void addCommandLineOptions(const std::map<std::string, std::string> &options);
/**
* Provide a predicate function that determines
* whether the XACCLogger should print logs.
*
* @param predicate Function with no arguments that returns bool
*/
void setGlobalLoggerPredicate(MessagePredicate predicate);
/**
* Print an info log to the XACC Logger.
*
* @param msg The log to print
* @param predicate Function with no arguments that returns bool
*/
void info(const std::string &msg,
MessagePredicate predicate = std::function<bool(void)>([]() {
return true;
}));
/**
* Print a warning log to the XACC Logger.
*
* @param msg The log to print
* @param predicate Function with no arguments that returns bool
*/
void warning(const std::string &msg,
MessagePredicate predicate = std::function<bool(void)>([]() {
return true;
}));
/**
* Print an debug log to the XACC Logger.
*
* @param msg The log to print
* @param predicate Function with no arguments that returns bool
*/
void debug(const std::string &msg,
MessagePredicate predicate = std::function<bool(void)>([]() {
return true;
}));
/**
* Print an error log to the XACC Logger.
*
* @param msg The log to print
* @param predicate Function with no arguments that returns bool
*/
void error(const std::string &msg,
MessagePredicate predicate = std::function<bool(void)>([]() {
return true;
}));
/**
* Clear the options database
*/
void clearOptions();
/**
* Return true if the option exists.
*
* @param optionKey The name of the option
* @return exists True if it exists
*/
bool optionExists(const std::string &optionKey);
/**
* Return the value of the options with the given name.
* @param optionKey The name of the option
* @return value The value of this option
*/
const std::string getOption(const std::string &optionKey);
/**
* Set the value of the option with the given name.
*
* @param optionKey The name of the option
* @param value The new option value.
*/
void setOption(const std::string &optionKey, const std::string &value);
void unsetOption(const std::string &optionKey);
std::shared_ptr<IRProvider> getIRProvider(const std::string &name);
std::shared_ptr<IRGenerator> getIRGenerator(const std::string &name);
/**
* Set the Compiler to use.
*
* @param compilerName The name of the compiler
*/
void setCompiler(const std::string &compilerName);
std::shared_ptr<IRGenerator> getIRGenerator(const std::string &name);
/**
* Set the Accelerator to use.
*
* @param acceleratorName The name of the accelerator
*/
void setAccelerator(const std::string &acceleratorName);
/**
* Return the Accelerator with given name
*
* @param name The name of the Accelerator
* @return accelerator The Accelerator
*/
std::shared_ptr<Accelerator> getAccelerator(const std::string &name, AcceleratorParameters params = {});
std::shared_ptr<Accelerator> getAccelerator(const std::string &name,
std::shared_ptr<Client> client, AcceleratorParameters params = {});
/**
* Get the Accelerator that is currently specified by the
* 'accelerator' option key.
*
* @return accelerator The Accelerator
*/
AcceleratorParameters params = {});
std::shared_ptr<Accelerator> getAccelerator(const std::string &name,
std::shared_ptr<Client> client,
AcceleratorParameters params = {});
std::shared_ptr<Accelerator> getAccelerator(AcceleratorParameters params = {});
/**
* Return true if the framework has an Accelerator
* with the given name.
*
* @param name Accelerator name
* @return exists Bool that's true if framework has given Accelerator
*/
bool hasAccelerator(const std::string &name);
/**
* Return the Compiler with given name
*/
void setCompiler(const std::string &compilerName);
std::shared_ptr<Compiler> getCompiler(const std::string &name);
/**
* Get the Compiler that is currently specified by the
* 'compiler' option key.
*
* @return compiler The Compiler
*/
std::shared_ptr<Compiler> getCompiler();
bool hasCompiler(const std::string &name);
std::shared_ptr<Algorithm> getAlgorithm(const std::string name,
xacc::AlgorithmParameters &params);
std::shared_ptr<Algorithm> getAlgorithm(const std::string name,
xacc::AlgorithmParameters &&params);
std::shared_ptr<Algorithm> getAlgorithm(const std::string name);
std::shared_ptr<Optimizer> getOptimizer(const std::string name);
std::shared_ptr<Optimizer>
getOptimizer(const std::string name,
......@@ -263,18 +121,6 @@ std::shared_ptr<Optimizer>
getOptimizer(const std::string name,
const std::map<std::string, xacc::InstructionParameter> &&opts);
using qbit = std::shared_ptr<xacc::AcceleratorBuffer>;
qbit qalloc(const int n);
/**
* Return true if the framework has a Compiler
* with the given name.
*
* @param name Compiler name
* @return exists Bool that's true if framework has given Compiler
*/
bool hasCompiler(const std::string &name);
bool hasCache(const std::string fileName, const std::string subdirectory = "");
std::map<std::string, InstructionParameter>
getCache(const std::string fileName, const std::string subdirectory = "");
......@@ -289,38 +135,6 @@ void appendCache(const std::string fileName,
const std::string subdirectory = "");
const std::string getRootDirectory();
// template <class Service>
// std::shared_ptr<Service> getService(const std::string &serviceName) {
// if (!xacc::xaccFrameworkInitialized) {
// error("XACC not initialized before use. Please execute "
// "xacc::Initialize() before using API.");
// }
// auto service = serviceRegistry->getService<Service>(serviceName);
// if (!service) {
// error("Invalid XACC Service. Could not find " + serviceName +
// " in Service Registry.");
// }
// return service;
// }
// template <typename Service> bool hasService(const std::string &serviceName) {
// if (!xacc::xaccFrameworkInitialized) {
// error("XACC not initialized before use. Please execute "
// "xacc::Initialize() before using API.");
// }
// return serviceRegistry->hasService<Service>(serviceName);
// }
// template <typename ServiceInterface>
// std::vector<std::string> getRegisteredIds() {
// return serviceRegistry->getRegisteredIds<ServiceInterface>();
// }
// template <typename ServiceInterface>
// std::vector<std::shared_ptr<ServiceInterface>> getServices() {
// return serviceRegistry->getServices<ServiceInterface>();
// }
std::shared_ptr<Function> optimizeFunction(const std::string optimizer,
std::shared_ptr<Function> function);
......@@ -328,7 +142,6 @@ std::shared_ptr<IRTransformation> getIRTransformation(const std::string &name);
const std::string translate(std::shared_ptr<Function> function,
const std::string toLanguage);
const std::string translateWithVisitor(const std::string &originalSource,
const std::string &originalLanguage,
const std::string &visitorMapping,
......@@ -340,11 +153,7 @@ std::shared_ptr<Function> getCompiled(const std::string name);
bool hasCompiled(const std::string name);
void qasm(const std::string &qasmString);
/**
* This method should be called by clients to
* clean up and finalize the XACC framework. It should
* be called after using the XACC API.
*/
void Finalize();
} // 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