Commit 1549cc28 authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

ran clang format, added optimizeFunction api call



Signed-off-by: Mccaskey, Alex's avatarAlex McCaskey <mccaskeyaj@ornl.gov>
parent af7c9d5f
......@@ -4,8 +4,8 @@
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompanies this
* distribution. The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html and the Eclipse Distribution License
* is available at https://eclipse.org/org/documents/edl-v10.php
* http://www.eclipse.org/legal/epl-v10.html and the Eclipse Distribution
*License is available at https://eclipse.org/org/documents/edl-v10.php
*
* Contributors:
* Alexander J. McCaskey - initial API and implementation
......@@ -24,50 +24,51 @@ namespace xacc {
namespace quantum {
std::shared_ptr<IR> PyXACCCompiler::compile(const std::string& src,
std::shared_ptr<Accelerator> acc) {
std::shared_ptr<IR> PyXACCCompiler::compile(const std::string &src,
std::shared_ptr<Accelerator> acc) {
// Setup the Antlr Parser
ANTLRInputStream input(src);
PyXACCIRLexer lexer(&input);
lexer.removeErrorListeners();
lexer.addErrorListener(new PyXACCErrorListener());
CommonTokenStream tokens(&lexer);
PyXACCIRParser parser(&tokens);
parser.removeErrorListeners();
parser.addErrorListener(new PyXACCErrorListener());
// Walk the Abstract Syntax Tree
tree::ParseTree *tree = parser.xaccsrc();
PyXACCListener listener;
tree::ParseTreeWalker::DEFAULT.walk(&listener, tree);
// Setup the Antlr Parser
ANTLRInputStream input(src);
PyXACCIRLexer lexer(&input);
lexer.removeErrorListeners();
lexer.addErrorListener(new PyXACCErrorListener());
// Create and return the IR
auto ir = std::make_shared<GateIR>();
ir->addKernel(listener.getKernel());
return ir;
CommonTokenStream tokens(&lexer);
PyXACCIRParser parser(&tokens);
parser.removeErrorListeners();
parser.addErrorListener(new PyXACCErrorListener());
// Walk the Abstract Syntax Tree
tree::ParseTree *tree = parser.xaccsrc();
PyXACCListener listener;
tree::ParseTreeWalker::DEFAULT.walk(&listener, tree);
// Create and return the IR
auto ir = std::make_shared<GateIR>();
ir->addKernel(listener.getKernel());
return ir;
}
std::shared_ptr<IR> PyXACCCompiler::compile(const std::string& src) {
return compile(src, nullptr);
std::shared_ptr<IR> PyXACCCompiler::compile(const std::string &src) {
return compile(src, nullptr);
}
const std::string PyXACCCompiler::translate(const std::string &bufferVariable,
std::shared_ptr<Function> function) {
auto visitor = std::make_shared<PyXACCVisitor>(function->getParameters());
xacc::InstructionIterator it(function);
while (it.hasNext()) {
// Get the next node in the tree
auto nextInst = it.next();
if (nextInst->isEnabled()) {
nextInst->accept(visitor);
}
const std::string
PyXACCCompiler::translate(const std::string &bufferVariable,
std::shared_ptr<Function> function) {
auto visitor = std::make_shared<PyXACCVisitor>(function->getParameters());
xacc::InstructionIterator it(function);
while (it.hasNext()) {
// Get the next node in the tree
auto nextInst = it.next();
if (nextInst->isEnabled()) {
nextInst->accept(visitor);
}
}
return visitor->getPyXACCString();
}
return visitor->getPyXACCString();
}
} // namespace quantum
}
} // namespace xacc
......@@ -4,8 +4,8 @@
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompanies this
* distribution. The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html and the Eclipse Distribution License
* is available at https://eclipse.org/org/documents/edl-v10.php
* http://www.eclipse.org/legal/epl-v10.html and the Eclipse Distribution
*License is available at https://eclipse.org/org/documents/edl-v10.php
*
* Contributors:
* Alexander J. McCaskey - initial API and implementation
......@@ -23,89 +23,84 @@ namespace quantum {
* An Antlr error listener for handling parsing errors.
*/
class PyXACCErrorListener : public antlr4::BaseErrorListener {
public:
void syntaxError(antlr4::Recognizer *recognizer, antlr4::Token * offendingSymbol, size_t line, size_t charPositionInLine, const std::string &msg, std::exception_ptr e) override {
std::ostringstream output;
output << "Invalid PyXACC source: ";
output << "line " << line << ":" << charPositionInLine << " " << msg;
xacc::error(output.str());
}
public:
void syntaxError(antlr4::Recognizer *recognizer,
antlr4::Token *offendingSymbol, size_t line,
size_t charPositionInLine, const std::string &msg,
std::exception_ptr e) override {
std::ostringstream output;
output << "Invalid PyXACC source: ";
output << "line " << line << ":" << charPositionInLine << " " << msg;
xacc::error(output.str());
}
};
/**
* The PyXACCCompiler is an XACC Compiler that compiles
* python-like gate instruction source code to produce a
* python-like gate instruction source code to produce a
* XACC IR.
*/
class PyXACCCompiler: public xacc::Compiler {
class PyXACCCompiler : public xacc::Compiler {
public:
/**
* The Compiler.
*/
PyXACCCompiler() {}
/**
* Compile the given kernel code for the
* given Accelerator.
*
* @param src The source code
* @param acc Reference to the D-Wave Accelerator
* @return
*/
virtual std::shared_ptr<xacc::IR> compile(const std::string& src,
std::shared_ptr<Accelerator> acc);
/**
* Compile the given kernel code.
*
* @param src The source code
* @return
*/
virtual std::shared_ptr<xacc::IR> compile(const std::string& src);
/**
* Return the command line options for this compiler
*
* @return options Description of command line options.
*/
virtual std::shared_ptr<options_description> getOptions() {
auto desc = std::make_shared<options_description>(
"PyXACC Compiler Options");
return desc;
}
virtual bool handleOptions(variables_map& map) {
return false;
}
/**
* We don't allow translations for the PyXACC Compiler.
* @param bufferVariable
* @param function
* @return
*/
virtual const std::string translate(const std::string& bufferVariable,
std::shared_ptr<Function> function);
virtual const std::string name() const {
return "xacc-py";
}
virtual const std::string description() const {
return "";
}
/**
* The destructor
*/
virtual ~PyXACCCompiler() {}
/**
* The Compiler.
*/
PyXACCCompiler() {}
/**
* Compile the given kernel code for the
* given Accelerator.
*
* @param src The source code
* @param acc Reference to the D-Wave Accelerator
* @return
*/
virtual std::shared_ptr<xacc::IR> compile(const std::string &src,
std::shared_ptr<Accelerator> acc);
/**
* Compile the given kernel code.
*
* @param src The source code
* @return
*/
virtual std::shared_ptr<xacc::IR> compile(const std::string &src);
/**
* Return the command line options for this compiler
*
* @return options Description of command line options.
*/
virtual std::shared_ptr<options_description> getOptions() {
auto desc =
std::make_shared<options_description>("PyXACC Compiler Options");
return desc;
}
virtual bool handleOptions(variables_map &map) { return false; }
/**
* We don't allow translations for the PyXACC Compiler.
* @param bufferVariable
* @param function
* @return
*/
virtual const std::string translate(const std::string &bufferVariable,
std::shared_ptr<Function> function);
virtual const std::string name() const { return "xacc-py"; }
virtual const std::string description() const { return ""; }
/**
* The destructor
*/
virtual ~PyXACCCompiler() {}
};
}
} // namespace quantum
}
} // namespace xacc
#endif
......@@ -4,8 +4,8 @@
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompanies this
* distribution. The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html and the Eclipse Distribution License
* is available at https://eclipse.org/org/documents/edl-v10.php
* http://www.eclipse.org/legal/epl-v10.html and the Eclipse Distribution
*License is available at https://eclipse.org/org/documents/edl-v10.php
*
* Contributors:
* Alexander J. McCaskey - initial API and implementation
......@@ -25,29 +25,24 @@ namespace {
/**
*/
class US_ABI_LOCAL PyXACCCompilerActivator: public BundleActivator {
class US_ABI_LOCAL PyXACCCompilerActivator : public BundleActivator {
public:
PyXACCCompilerActivator() {
}
/**
*/
void Start(BundleContext context) {
auto c = std::make_shared<xacc::quantum::PyXACCCompiler>();
context.RegisterService<xacc::Compiler>(c);
context.RegisterService<xacc::OptionsProvider>(c);
}
/**
*/
void Stop(BundleContext /*context*/) {
}
PyXACCCompilerActivator() {}
/**
*/
void Start(BundleContext context) {
auto c = std::make_shared<xacc::quantum::PyXACCCompiler>();
context.RegisterService<xacc::Compiler>(c);
context.RegisterService<xacc::OptionsProvider>(c);
}
/**
*/
void Stop(BundleContext /*context*/) {}
};
}
} // namespace
CPPMICROSERVICES_EXPORT_BUNDLE_ACTIVATOR(PyXACCCompilerActivator)
......@@ -4,8 +4,8 @@
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompanies this
* distribution. The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html and the Eclipse Distribution License
* is available at https://eclipse.org/org/documents/edl-v10.php
* http://www.eclipse.org/legal/epl-v10.html and the Eclipse Distribution
*License is available at https://eclipse.org/org/documents/edl-v10.php
*
* Contributors:
* Alexander J. McCaskey - initial API and implementation
......@@ -23,124 +23,126 @@ namespace xacc {
namespace quantum {
PyXACCListener::PyXACCListener() {
provider = xacc::getService<IRProvider>("gate");
provider = xacc::getService<IRProvider>("gate");
}
std::shared_ptr<Function> PyXACCListener::getKernel() {
return f;
std::shared_ptr<Function> PyXACCListener::getKernel() { return f; }
void PyXACCListener::enterXacckernel(PyXACCIRParser::XacckernelContext *ctx) {
std::vector<InstructionParameter> params;
for (int i = 0; i < ctx->param().size(); i++) {
params.push_back(
InstructionParameter(ctx->param(static_cast<size_t>(i))->getText()));
functionVariableNames.push_back(
ctx->param(static_cast<size_t>(i))->getText());
}
f = provider->createFunction(ctx->kernelname->getText(), {}, params);
}
void PyXACCListener::enterXacckernel(PyXACCIRParser::XacckernelContext * ctx) {
std::vector<InstructionParameter> params;
for (int i = 0; i < ctx->param().size(); i++) {
params.push_back(InstructionParameter(ctx->param(static_cast<size_t>(i))->getText()));
functionVariableNames.push_back(ctx->param(static_cast<size_t>(i))->getText());
void PyXACCListener::enterUop(PyXACCIRParser::UopContext *ctx) {
auto is_double = [](const std::string &s) -> bool {
try {
std::stod(s);
} catch (std::exception &e) {
return false;
}
f = provider->createFunction(ctx->kernelname->getText(), {}, params);
}
return true;
};
auto is_int = [](const std::string &s) -> bool {
try {
std::stoi(s);
} catch (std::exception &e) {
return false;
}
return true;
};
void PyXACCListener::enterUop(PyXACCIRParser::UopContext * ctx) {
auto is_double = [](const std::string& s) -> bool
{
try {
std::stod(s);
} catch(std::exception& e) {
return false;
}
return true;
};
auto is_int = [](const std::string& s) -> bool
{
try {
std::stoi(s);
} catch(std::exception& e) {
return false;
}
return true;
};
auto gateName = ctx->gatename->getText();
boost::trim(gateName);
if (gateName == "CX") {gateName = "CNOT";}
else if (gateName == "MEASURE") {gateName = "Measure";}
if (gateName == "xacc") {
auto generatorName = ctx->explist()->exp(0)->getText();
std::map<std::string, InstructionParameter> params;
for (int i = 1; i < ctx->explist()->exp().size(); i++) {
auto key = ctx->explist()->exp(i)->id()->getText();
auto valStr = ctx->explist()->exp(i)->exp(0)->getText();
if (is_int(valStr)) {
params.insert({key, InstructionParameter(std::stoi(valStr))});
} else if (is_double(valStr)) {
params.insert({key, InstructionParameter(std::stod(valStr))});
} else {
params.insert({key, InstructionParameter(valStr)});
}
}
auto gateName = ctx->gatename->getText();
boost::trim(gateName);
if (gateName == "CX") {
gateName = "CNOT";
} else if (gateName == "MEASURE") {
gateName = "Measure";
}
// Add the Function's InstructionParameters
for (int i = 0; i < f->nParameters(); i++) {
params.insert({"param_"+std::to_string(i),f->getParameter(i)});
}
auto generator = xacc::getService<xacc::IRGenerator>(generatorName);
auto genF = generator->generate(params);
for (auto i : genF->getInstructions()) {
f->addInstruction(i);
}
} else if (ctx->explist()->exp().size() > 0) {
auto paramStr = ctx->explist()->exp(0);
std::vector<int> qubits;
std::vector<InstructionParameter> params;
for (int i = 0; i < ctx->explist()->exp().size(); i++) {
// First arg could be a real value (3.14), a string (id), or a general expression (0.5*t0)
if (i == 0 && (ctx->explist()->exp(i)->real() != nullptr ||
ctx->explist()->exp(i)->id() != nullptr ||
!ctx->explist()->exp(i)->exp().empty())) {
// we have a parameter for a parameterized gate
auto tmp = ctx->explist()->exp(i);
if (tmp->real() != nullptr || tmp->id() != nullptr) {
auto str = ctx->explist()->exp(i)->getText();
auto param = is_double(str) ? InstructionParameter(std::stod(str)) : InstructionParameter(str);
params.push_back(param);
} else if (tmp->exp().size() == 1 && tmp->exp(0)->real() != nullptr) {
// this is a neg double
params.push_back(InstructionParameter(std::stod(tmp->getText())));
} else {
params.push_back(InstructionParameter(tmp->getText()));
}
} else {
qubits.push_back(std::stoi(ctx->explist()->exp(i)->INT()->getText()));
}
}
if (gateName == "xacc") {
std::shared_ptr<Instruction> gate;
if (gateName == "Measure") {
std::vector<int> measurebit {qubits[0]};
InstructionParameter p1(qubits[1]);
gate = provider->createInstruction(gateName, measurebit);
gate->setParameter(0, p1);
} else {
gate = provider->createInstruction(gateName, qubits);
int count = 0;
for (auto p : params) {
gate->setParameter(count, p);
count++;
}
}
auto generatorName = ctx->explist()->exp(0)->getText();
std::map<std::string, InstructionParameter> params;
for (int i = 1; i < ctx->explist()->exp().size(); i++) {
auto key = ctx->explist()->exp(i)->id()->getText();
auto valStr = ctx->explist()->exp(i)->exp(0)->getText();
if (is_int(valStr)) {
params.insert({key, InstructionParameter(std::stoi(valStr))});
} else if (is_double(valStr)) {
params.insert({key, InstructionParameter(std::stod(valStr))});
} else {
params.insert({key, InstructionParameter(valStr)});
}
}
// Add the Function's InstructionParameters
for (int i = 0; i < f->nParameters(); i++) {
params.insert({"param_" + std::to_string(i), f->getParameter(i)});
}
f->addInstruction(gate);
auto generator = xacc::getService<xacc::IRGenerator>(generatorName);
auto genF = generator->generate(params);
for (auto i : genF->getInstructions()) {
f->addInstruction(i);
}
} else if (ctx->explist()->exp().size() > 0) {
auto paramStr = ctx->explist()->exp(0);
std::vector<int> qubits;
std::vector<InstructionParameter> params;
for (int i = 0; i < ctx->explist()->exp().size(); i++) {
// First arg could be a real value (3.14), a string (id), or a general
// expression (0.5*t0)
if (i == 0 && (ctx->explist()->exp(i)->real() != nullptr ||
ctx->explist()->exp(i)->id() != nullptr ||
!ctx->explist()->exp(i)->exp().empty())) {
// we have a parameter for a parameterized gate
auto tmp = ctx->explist()->exp(i);
if (tmp->real() != nullptr || tmp->id() != nullptr) {
auto str = ctx->explist()->exp(i)->getText();
auto param = is_double(str) ? InstructionParameter(std::stod(str))
: InstructionParameter(str);
params.push_back(param);
} else if (tmp->exp().size() == 1 && tmp->exp(0)->real() != nullptr) {
// this is a neg double
params.push_back(InstructionParameter(std::stod(tmp->getText())));
} else {
params.push_back(InstructionParameter(tmp->getText()));
}
} else {
qubits.push_back(std::stoi(ctx->explist()->exp(i)->INT()->getText()));
}
}
std::shared_ptr<Instruction> gate;
if (gateName == "Measure") {
std::vector<int> measurebit{qubits[0]};
InstructionParameter p1(qubits[1]);
gate = provider->createInstruction(gateName, measurebit);
gate->setParameter(0, p1);
} else {
xacc::error("Only permitting gates with 1 parameter for now.");
gate = provider->createInstruction(gateName, qubits);
int count = 0;
for (auto p : params) {
gate->setParameter(count, p);
count++;
}
}
}
f->addInstruction(gate);
} else {
xacc::error("Only permitting gates with 1 parameter for now.");
}
}
}
} // namespace quantum
} // namespace xacc
......@@ -4,8 +4,8 @@
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompanies this
* distribution. The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html and the Eclipse Distribution License
* is available at https://eclipse.org/org/documents/edl-v10.php
* http://www.eclipse.org/legal/epl-v10.html and the Eclipse Distribution
*License is available at https://eclipse.org/org/documents/edl-v10.php
*
* Contributors:
* Alexander J. McCaskey - initial API and implementation
......@@ -23,26 +23,28 @@ namespace xacc {
namespace quantum {
/**
* The PyXACCListener implements the Antlr-generated
* The PyXACCListener implements the Antlr-generated
* PyXACCIRBaseListener and maps the Antlr AST to XACC IR.
*/
class PyXACCListener : public PyXACCIRBaseListener {
protected:
std::shared_ptr<Function> f;
std::shared_ptr<IRProvider> provider;
std::vector<std::string> functionVariableNames;
public:
std::shared_ptr<Function> getKernel();
protected:
std::shared_ptr<Function> f;
std::shared_ptr<