Commit 98ce4318 authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

adding getOptimizer, adding as_no_error to Variant, adding Observable* to...


adding getOptimizer, adding as_no_error to Variant, adding Observable* to AlgorithmParameter, updated vqe
Signed-off-by: Mccaskey, Alex's avatarAlex McCaskey <mccaskeyaj@ornl.gov>
parent ad7dd4c7
......@@ -73,7 +73,7 @@ CNOT 1 0
ansatz->nParameters());
// Run Optimization routine, default is NLOPT COBYLA
auto optimizer = xacc::getService<xacc::Optimizer>("nlopt");
auto optimizer = xacc::getOptimizer("nlopt");
auto results = optimizer->optimize(vqeOptFunc);
// Print the result
......
......@@ -16,7 +16,7 @@ int main(int argc, char **argv) {
"- 2.1433 Y0Y1"
"+ .21829 Z0 - 6.125 Z1");
auto optimizer = xacc::getService<xacc::Optimizer>("nlopt");
auto optimizer = xacc::getOptimizer("nlopt");
// JIT map Quil QASM Ansatz to IR
xacc::qasm(R"(
......
......@@ -37,9 +37,9 @@ TEST(VQETester, checkSimple) {
auto ir = compiler->compile(rucc, nullptr);
auto ruccsd = ir->getKernel("f");
auto optimizer = xacc::getService<Optimizer>("nlopt");
auto observable = std::make_shared<xacc::quantum::PauliOperator>();
observable->fromString(
auto optimizer = xacc::getOptimizer("nlopt");
xacc::quantum::PauliOperator observable;//= std::make_shared<xacc::quantum::PauliOperator>();
observable.fromString(
"(0.174073,0) Z2 Z3 + (0.1202,0) Z1 Z3 + (0.165607,0) Z1 Z2 + "
"(0.165607,0) Z0 Z3 + (0.1202,0) Z0 Z2 + (-0.0454063,0) Y0 Y1 X2 X3 + "
"(-0.220041,0) Z3 + (-0.106477,0) + (0.17028,0) Z0 + (-0.220041,0) Z2 "
......@@ -47,7 +47,7 @@ TEST(VQETester, checkSimple) {
"Y2 X3 + (0.168336,0) Z0 Z1 + (0.0454063,0) Y0 X1 X2 Y3");
auto vqe = xacc::getService<Algorithm>("vqe");
EXPECT_TRUE(vqe->initialize({{"ansatz",ruccsd}, {"accelerator",acc}, {"observable", observable},{"optimizer",optimizer}}));//
EXPECT_TRUE(vqe->initialize({{"ansatz",ruccsd}, {"accelerator",acc}, {"observable", &observable},{"optimizer",optimizer}}));//
vqe->execute(buffer);
EXPECT_NEAR(-1.13717, mpark::get<double>(buffer->getInformation("opt-val")), 1e-4);
}
......
......@@ -19,7 +19,12 @@ bool VQE::initialize(const AlgorithmParameters &parameters) {
} else if (!parameters.count("accelerator")) {
return false;
}
observable = parameters.at("observable").as<std::shared_ptr<Observable>>();
try {
observable = parameters.at("observable").as_no_error<std::shared_ptr<Observable>>();
} catch (std::exception &e) {
observable = std::shared_ptr<Observable>(parameters.at("observable").as<Observable*>());
}
optimizer = parameters.at("optimizer").as<std::shared_ptr<Optimizer>>();
kernel = parameters.at("ansatz").as<std::shared_ptr<Function>>();
accelerator = parameters.at("accelerator").as<std::shared_ptr<Accelerator>>();
......
......@@ -276,6 +276,14 @@ std::shared_ptr<Algorithm> getAlgorithm(const std::string name) {
return xacc::getService<Algorithm>(name);
}
std::shared_ptr<Optimizer> getOptimizer(const std::string name) {
if (!xacc::xaccFrameworkInitialized) {
error("XACC not initialized before use. Please execute "
"xacc::Initialize() before using API.");
}
return xacc::getService<Optimizer>(name);
}
std::shared_ptr<IRProvider> getIRProvider(const std::string &name) {
if (!xacc::xaccFrameworkInitialized) {
error("XACC not initialized before use. Please execute "
......
......@@ -17,6 +17,7 @@
#include "RemoteAccelerator.hpp"
#include "IRProvider.hpp"
#include "Algorithm.hpp"
#include "Optimizer.hpp"
namespace xacc {
......@@ -246,6 +247,7 @@ std::shared_ptr<Compiler> getCompiler(const std::string &name);
std::shared_ptr<Compiler> getCompiler();
std::shared_ptr<Algorithm> getAlgorithm(const std::string name);
std::shared_ptr<Optimizer> getOptimizer(const std::string name);
using qbit = std::shared_ptr<xacc::AcceleratorBuffer>;
qbit qalloc(const int n);
......
......@@ -13,7 +13,7 @@ namespace xacc {
using AlgorithmParameter =
Variant<int, double, std::string, std::vector<int>, std::vector<double>,
std::vector<std::string>, std::shared_ptr<Observable>,
std::vector<std::string>, std::shared_ptr<Observable>, Observable*,
std::shared_ptr<Accelerator>, std::shared_ptr<Function>,
std::shared_ptr<Optimizer>>;
using AlgorithmParameters = std::map<std::string, AlgorithmParameter>;
......
......@@ -73,6 +73,12 @@ public:
}
return T();
}
template <typename T> T as_no_error() const {
// First off just try to get it
return mpark::get<T>(*this);
}
int which() const {
return this->index();
}
......
......@@ -36,7 +36,7 @@ class TestAlgorithm : public Algorithm {
class TestObservable : public Observable {
public:
std::vector<std::shared_ptr<Function>>
observe(std::shared_ptr<Function> function) override {}
observe(std::shared_ptr<Function> function) override {return {};}
const std::string toString() override { return "";}
void fromString(const std::string str) override {}
const int nBits() override {return 0;}
......
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