This GitLab instance is undergoing maintenance and is operating in read-only mode.

You are on a read-only GitLab instance.
Commit 8048a6cb authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

making getAlgo/Opt more robust, adding initial-parameters to nlopt


Signed-off-by: Mccaskey, Alex's avatarAlex McCaskey <mccaskeyaj@ornl.gov>
parent f8a901a6
Pipeline #65427 passed with stage
in 6 minutes and 20 seconds
......@@ -192,7 +192,6 @@ PYBIND11_MODULE(_pyxacc, m) {
.def("hasNext", &xacc::InstructionIterator::hasNext, "")
.def("next", &xacc::InstructionIterator::next, "");
py::class_<xacc::IRTransformation, std::shared_ptr<xacc::IRTransformation>>(
m, "IRTransformation", "")
.def("transform", &xacc::IRTransformation::transform, "");
......@@ -206,7 +205,7 @@ PYBIND11_MODULE(_pyxacc, m) {
py::return_value_policy::reference, "")
.def("generate",
(std::shared_ptr<xacc::Function>(xacc::IRGenerator::*)(
std::map<std::string, xacc::InstructionParameter>&)) &
std::map<std::string, xacc::InstructionParameter> &)) &
xacc::IRGenerator::generate,
py::return_value_policy::reference, "")
.def("analyzeResults", &xacc::IRGenerator::analyzeResults, "");
......@@ -275,8 +274,7 @@ PYBIND11_MODULE(_pyxacc, m) {
(void (xacc::AcceleratorBuffer::*)(const std::string &)) &
xacc::AcceleratorBuffer::appendMeasurement,
"Append the measurement string")
.def("getMeasurements",
&xacc::AcceleratorBuffer::getMeasurements,
.def("getMeasurements", &xacc::AcceleratorBuffer::getMeasurements,
"Return observed measurement bit strings")
.def("computeMeasurementProbability",
&xacc::AcceleratorBuffer::computeMeasurementProbability,
......@@ -397,7 +395,7 @@ PYBIND11_MODULE(_pyxacc, m) {
xacc::Optimizer::setOptions,
"");
py::class_<xacc::Observable, std::shared_ptr<xacc::Observable>>(
py::class_<xacc::Observable, std::shared_ptr<xacc::Observable>>(
m, "Observable", "The Observable interface.")
.def("observe",
(std::vector<std::shared_ptr<xacc::Function>>(xacc::Observable::*)(
......@@ -418,8 +416,9 @@ py::class_<xacc::Observable, std::shared_ptr<xacc::Observable>>(
xacc::Observable::fromString,
"");
py::class_<Term>(m, "Term").def("coeff", &Term::coeff).def("ops",&Term::ops);
py::class_<PauliOperator, xacc::Observable, std::shared_ptr<PauliOperator>>(m, "PauliOperator")
py::class_<Term>(m, "Term").def("coeff", &Term::coeff).def("ops", &Term::ops);
py::class_<PauliOperator, xacc::Observable, std::shared_ptr<PauliOperator>>(
m, "PauliOperator")
.def(py::init<>())
.def(py::init<std::complex<double>>())
.def(py::init<double>())
......@@ -452,11 +451,12 @@ py::class_<xacc::Observable, std::shared_ptr<xacc::Observable>>(
.def("nQubits", &PauliOperator::nQubits)
.def("computeActionOnKet", &PauliOperator::computeActionOnKet)
.def("computeActionOnBra", &PauliOperator::computeActionOnBra)
.def("__iter__",
[](PauliOperator &op) {
return py::make_iterator(op.begin(), op.end());
},
py::keep_alive<0, 1>());
.def(
"__iter__",
[](PauliOperator &op) {
return py::make_iterator(op.begin(), op.end());
},
py::keep_alive<0, 1>());
// Expose XACC API functions
m.def("Initialize", (void (*)(std::vector<std::string>)) & xacc::Initialize,
......@@ -491,7 +491,7 @@ py::class_<xacc::Observable, std::shared_ptr<xacc::Observable>>(
py::return_value_policy::reference,
"Return the IRGenerator of given name.");
m.def("getConnectivity",
[](const std::string acc) -> std::vector<std::pair<int,int>> {
[](const std::string acc) -> std::vector<std::pair<int, int>> {
auto a = xacc::getAccelerator(acc);
return a->getAcceleratorConnectivity();
});
......@@ -545,11 +545,11 @@ py::class_<xacc::Observable, std::shared_ptr<xacc::Observable>>(
(void (*)(std::shared_ptr<AcceleratorBuffer>)) & xacc::analyzeBuffer,
"Analyze the AcceleratorBuffer to produce problem-specific results.");
m.def("getCache", &xacc::getCache, "");
m.def(
"appendCache",
(void (*)(const std::string, const std::string, InstructionParameter &,const std::string)) &
xacc::appendCache,
"");
m.def("appendCache",
(void (*)(const std::string, const std::string, InstructionParameter &,
const std::string)) &
xacc::appendCache,
"");
m.def("Finalize", &xacc::Finalize, "Finalize the framework");
m.def(
"loadBuffer",
......@@ -560,13 +560,27 @@ py::class_<xacc::Observable, std::shared_ptr<xacc::Observable>>(
return buffer;
},
"");
m.def("getAlgorithm", &xacc::getAlgorithm, "");
m.def("getAlgorithm", [](const std::string name, xacc::AlgorithmParameters& params) {
auto algo = xacc::getAlgorithm(name);
algo->initialize(params);
return algo;
}, "");
m.def("getOptimizer", &xacc::getOptimizer, "");
m.def("getAlgorithm",
(std::shared_ptr<xacc::Algorithm>(*)(const std::string)) &
xacc::getAlgorithm,
"");
m.def("getAlgorithm",
(std::shared_ptr<xacc::Algorithm>(*)(const std::string,
xacc::AlgorithmParameters &)) &
xacc::getAlgorithm,
"");
m.def("getOptimizer",
(std::shared_ptr<xacc::Optimizer>(*)(const std::string)) &
xacc::getOptimizer,
"");
m.def("getOptimizer",
(std::shared_ptr<xacc::Optimizer>(*)(
const std::string,
const std::map<std::string, InstructionParameter> &)) &
xacc::getOptimizer,
"");
m.def(
"compileKernel",
[](std::shared_ptr<Accelerator> acc, const std::string &src,
......@@ -670,21 +684,22 @@ py::class_<xacc::Observable, std::shared_ptr<xacc::Observable>>(
auto a = xacc::getService<xacc::quantum::EmbeddingAlgorithm>(algo);
auto hardwareconnections = acc->getAcceleratorConnectivity();
std::set<int> nUniqueBits;
for (auto& edge : hardwareconnections) {
nUniqueBits.insert(edge.first);
nUniqueBits.insert(edge.second);
for (auto &edge : hardwareconnections) {
nUniqueBits.insert(edge.first);
nUniqueBits.insert(edge.second);
}
int nBits = *std::max_element(nUniqueBits.begin(), nUniqueBits.end()) + 1;
int nBits =
*std::max_element(nUniqueBits.begin(), nUniqueBits.end()) + 1;
auto hardware = xacc::getService<Graph>("boost-ugraph");
for (int i = 0; i < nBits; i++) {
std::map<std::string,InstructionParameter> m{{"bias",1.0}};
hardware->addVertex(m);
std::map<std::string, InstructionParameter> m{{"bias", 1.0}};
hardware->addVertex(m);
}
for (auto& edge : hardwareconnections) {
hardware->addEdge(edge.first, edge.second);
for (auto &edge : hardwareconnections) {
hardware->addEdge(edge.first, edge.second);
}
int maxBitIdx = 0;
......@@ -702,9 +717,9 @@ py::class_<xacc::Observable, std::shared_ptr<xacc::Observable>>(
}
auto problemGraph = xacc::getService<Graph>("boost-ugraph");
for (int i = 0; i < maxBitIdx+1;i++) {
std::map<std::string,InstructionParameter> m{{"bias",1.0}};
problemGraph->addVertex(m);
for (int i = 0; i < maxBitIdx + 1; i++) {
std::map<std::string, InstructionParameter> m{{"bias", 1.0}};
problemGraph->addVertex(m);
}
for (auto inst : f->getInstructions()) {
......@@ -717,7 +732,6 @@ py::class_<xacc::Observable, std::shared_ptr<xacc::Observable>>(
}
}
return a->embed(problemGraph, hardware);
},
"");
......
......@@ -269,13 +269,22 @@ std::shared_ptr<Compiler> getCompiler(const std::string &name) {
}
std::shared_ptr<Algorithm> getAlgorithm(const std::string name) {
if (!xacc::xaccFrameworkInitialized) {
if (!xacc::xaccFrameworkInitialized) {
error("XACC not initialized before use. Please execute "
"xacc::Initialize() before using API.");
}
return xacc::getService<Algorithm>(name);
}
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.");
}
return algo;
}
std::shared_ptr<Optimizer> getOptimizer(const std::string name) {
if (!xacc::xaccFrameworkInitialized) {
error("XACC not initialized before use. Please execute "
......@@ -284,6 +293,14 @@ std::shared_ptr<Optimizer> getOptimizer(const std::string name) {
return xacc::getService<Optimizer>(name);
}
std::shared_ptr<Optimizer>
getOptimizer(const std::string name,
const std::map<std::string, xacc::InstructionParameter> &opts) {
auto opt = getOptimizer(name);
opt->setOptions(opts);
return opt;
}
std::shared_ptr<IRProvider> getIRProvider(const std::string &name) {
if (!xacc::xaccFrameworkInitialized) {
error("XACC not initialized before use. Please execute "
......
......@@ -191,7 +191,7 @@ const std::string getOption(const std::string &optionKey);
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<IRProvider> getIRProvider(const std::string &name);
/**
* Set the Compiler to use.
......@@ -214,7 +214,8 @@ void setAccelerator(const std::string &acceleratorName);
* @return accelerator The Accelerator
*/
std::shared_ptr<Accelerator> getAccelerator(const std::string &name);
std::shared_ptr<Accelerator> getAccelerator(const std::string &name, std::shared_ptr<Client> client);
std::shared_ptr<Accelerator> getAccelerator(const std::string &name,
std::shared_ptr<Client> client);
/**
* Get the Accelerator that is currently specified by the
......@@ -246,8 +247,13 @@ std::shared_ptr<Compiler> getCompiler(const std::string &name);
*/
std::shared_ptr<Compiler> getCompiler();
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,
const std::map<std::string, xacc::InstructionParameter> &opts);
using qbit = std::shared_ptr<xacc::AcceleratorBuffer>;
qbit qalloc(const int n);
......@@ -261,15 +267,18 @@ qbit qalloc(const int n);
*/
bool hasCompiler(const std::string &name);
bool hasCache(const std::string fileName, const std::string subdirectory="");
bool hasCache(const std::string fileName, const std::string subdirectory = "");
std::map<std::string, InstructionParameter>
getCache(const std::string fileName, const std::string subdirectory="");
getCache(const std::string fileName, const std::string subdirectory = "");
void appendCache(const std::string fileName, const std::string key,
InstructionParameter &param, const std::string subdirectory="");
InstructionParameter &param,
const std::string subdirectory = "");
void appendCache(const std::string fileName, const std::string key,
InstructionParameter &&param, const std::string subdirectory="");
InstructionParameter &&param,
const std::string subdirectory = "");
void appendCache(const std::string fileName,
std::map<std::string, InstructionParameter> &params, const std::string subdirectory="");
std::map<std::string, InstructionParameter> &params,
const std::string subdirectory = "");
const std::string getRootDirectory();
// template <class Service>
......@@ -323,7 +332,7 @@ const std::string translateWithVisitor(const std::string &originalSource,
void appendCompiled(std::shared_ptr<Function> function);
std::shared_ptr<Function> getCompiled(const std::string name);
void qasm(const std::string& qasmString);
void qasm(const std::string &qasmString);
/**
* This method should be called by clients to
* clean up and finalize the XACC framework. It should
......
......@@ -32,6 +32,10 @@ OptResult NLOptimizer::optimize(OptFunction &function) {
maxeval = options["nlopt-maxeval"].as<int>();
}
std::vector<double> x(dim);
if (options.count("initial-parameters")) {
x = options["initial-parameters"].as<std::vector<double>>();
}
nlopt::opt _opt(algo, dim);
std::function<double(const std::vector<double> &, std::vector<double> &,
void *)>
......@@ -47,7 +51,6 @@ OptResult NLOptimizer::optimize(OptFunction &function) {
_opt.set_ftol_rel(tol);
double optF;
std::vector<double> x(dim);
try {
auto result = _opt.optimize(x, optF);
} catch (std::exception &e) {
......
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