Commit 44c40c7f authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

fixing licenses in source files throughout



Signed-off-by: Mccaskey, Alex's avatarAlex McCaskey <mccaskeyaj@ornl.gov>
parent 20e9bc57
Pipeline #71494 passed with stage
in 4 minutes and 41 seconds
/*******************************************************************************
* Copyright (c) 2019 UT-Battelle, LLC.
* All rights reserved. This program and the accompanying materials
* 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
*
* Contributors:
* Alexander J. McCaskey - initial API and implementation
*******************************************************************************/
#include "IRProvider.hpp"
#include "xacc.hpp"
#include "xacc_service.hpp"
......
/*******************************************************************************
* Copyright (c) 2019 UT-Battelle, LLC.
* All rights reserved. This program and the accompanying materials
* 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
*
* Contributors:
* Alexander J. McCaskey - initial API and implementation
*******************************************************************************/
#ifndef XACC_XASMCOMPILER_HPP
#define XACC_XASMCOMPILER_HPP
......
/*******************************************************************************
* Copyright (c) 2019 UT-Battelle, LLC.
* All rights reserved. This program and the accompanying materials
* 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
*
* Contributors:
* Alexander J. McCaskey - initial API and implementation
*******************************************************************************/
#ifndef XACC_XASMERRORLISTENER_HPP
#define XACC_XASMERRORLISTENER_HPP
......
/*******************************************************************************
* Copyright (c) 2019 UT-Battelle, LLC.
* All rights reserved. This program and the accompanying materials
* 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
*
* Contributors:
* Alexander J. McCaskey - initial API and implementation
*******************************************************************************/
#include "IR.hpp"
#include "IRProvider.hpp"
#include "xacc.hpp"
......
/***********************************************************************************
/*******************************************************************************
* Copyright (c) 2019 UT-Battelle, LLC.
* All rights reserved. This program and the accompanying materials
* 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
*
* Contributors:
* Initial implementation - Amrut Nadgir
*
**********************************************************************************/
* Alexander J. McCaskey - initial API and implementation
*******************************************************************************/
#ifndef XACC_XASMLISTENER_H
#define XACC_XASMLISTENER_H
......
/*******************************************************************************
* Copyright (c) 2019 UT-Battelle, LLC.
* All rights reserved. This program and the accompanying materials
* 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
*
* Contributors:
* Alexander J. McCaskey - initial API and implementation
*******************************************************************************/
#include "QuantumIRProvider.hpp"
#include "GateIR.hpp"
......
/*******************************************************************************
* Copyright (c) 2019 UT-Battelle, LLC.
* All rights reserved. This program and the accompanying materials
* 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
*
* Contributors:
* Alexander J. McCaskey - initial API and implementation
*******************************************************************************/
#ifndef QUANTUM_IRPROVIDER_HPP_
#define QUANTUM_IRPROVIDER_HPP_
......
......@@ -18,6 +18,8 @@
#include "heterogeneous.hpp"
#include "Identifiable.hpp"
#include "CompositeInstruction.hpp"
#include "AcceleratorBuffer.hpp"
namespace xacc {
......@@ -25,16 +27,28 @@ using OptResult = std::pair<double, std::vector<double>>;
class OptFunction {
protected:
std::function<double(const std::vector<double> &)> _function;
std::function<double(const std::vector<double> &, std::vector<double> &)>
_function;
int _dim = 0;
public:
OptFunction() :_function([](const std::vector<double>& x) {return 0.0;}) {}
OptFunction(std::function<double(const std::vector<double> &)> f, const int d)
// Nullary Constructor, make dummy function
OptFunction()
: _function([](const std::vector<double> &x, std::vector<double> &dx) {
return 0.0;
}) {}
// Standard constructor, takes function that takes params as
// first arg and gradient as second arg
OptFunction(
std::function<double(const std::vector<double> &, std::vector<double> &)>
f,
const int d)
: _function(f), _dim(d) {}
virtual const int dimensions() const { return _dim; }
virtual double operator()(const std::vector<double> &params) {
return _function(params);
virtual double operator()(const std::vector<double> &x,
std::vector<double> &dx) {
return _function(x, dx);
}
};
......@@ -43,15 +57,12 @@ protected:
HeterogeneousMap options;
public:
template<typename T>
void appendOption(const std::string key, T& value) {
template <typename T> void appendOption(const std::string key, T &value) {
options.insert(key, value);
}
void setOptions(const HeterogeneousMap &opts) { options = opts; }
virtual OptResult optimize(OptFunction &function) = 0;
void
setOptions(const HeterogeneousMap &opts) {
options = opts;
}
};
} // namespace xacc
#endif
......@@ -24,15 +24,17 @@ OptResult NLOptimizer::optimize(OptFunction &function) {
double tol = 1e-8;
int maxeval = 1000;
// std::stringstream ss;
// options.print<int,std::size_t, std::string,double>(ss);
// std::cout << "OPTS:\n" << ss.str() << "\n";
// std::stringstream ss;
// options.print<int,std::size_t, std::string,double>(ss);
// std::cout << "OPTS:\n" << ss.str() << "\n";
if (options.stringExists("nlopt-optimizer")) {
auto optimizerAlgo = options.getString("nlopt-optimizer");
if (optimizerAlgo == "cobyla") {
algo = nlopt::algorithm::LN_COBYLA;
} else if (optimizerAlgo == "nelder-mead") {
algo = nlopt::algorithm::LN_NELDERMEAD;
} else if (optimizerAlgo == "l-bfgs") {
algo = nlopt::algorithm::LD_LBFGS;
} else {
xacc::XACCLogger::instance()->error("Invalid optimizer at this time: " +
optimizerAlgo);
......@@ -49,7 +51,7 @@ OptResult NLOptimizer::optimize(OptFunction &function) {
std::vector<double> x(dim);
if (options.keyExists<std::vector<double>>("initial-parameters")) {
x = options.get_with_throw<std::vector<double>>("initial-parameters");
x = options.get_with_throw<std::vector<double>>("initial-parameters");
} else if (options.keyExists<std::vector<int>>("initial-parameters")) {
auto tmpx = options.get<std::vector<int>>("initial-parameters");
x = std::vector<double>(tmpx.begin(), tmpx.end());
......@@ -58,7 +60,7 @@ OptResult NLOptimizer::optimize(OptFunction &function) {
std::function<double(const std::vector<double> &, std::vector<double> &,
void *)>
f = [&](const std::vector<double> &x, std::vector<double> &grad,
void *f_data) -> double { return function(x); };
void *f_data) -> double { return function(x, grad); };
auto fptr = LambdaToVFunc::ptr(f);
......@@ -72,10 +74,10 @@ OptResult NLOptimizer::optimize(OptFunction &function) {
try {
auto result = _opt.optimize(x, optF);
} catch (std::exception &e) {
std::cout << "[NLOpt Dimensions (dim,x.size()) = (" << dim << ", " << x.size() << ")\n";
std::cout << "[NLOpt Dimensions (dim,x.size()) = (" << dim << ", "
<< x.size() << ")\n";
xacc::XACCLogger::instance()->error("NLOpt failed: " +
std::string(e.what()));
}
return OptResult{optF, x};
}
......
......@@ -21,37 +21,36 @@
namespace xacc {
struct LambdaToVFunc {
template<typename Tret, typename T>
static Tret lambda_ptr_exec(const std::vector<double>& x, std::vector<double>& grad, void* fd) {
return (Tret) (*(T*)fn<T>())(x,grad,fd);
}
template<typename Tret = double, typename Tfp = Tret(*)(const std::vector<double>&,std::vector<double>&,void*), typename T>
static Tfp ptr(T& t) {
fn<T>(&t);
return (Tfp) lambda_ptr_exec<Tret, T>;
}
template<typename T>
static void* fn(void* new_fn = nullptr) {
static void* fn;
if (new_fn != nullptr)
fn = new_fn;
return fn;
}
template <typename Tret, typename T>
static Tret lambda_ptr_exec(const std::vector<double> &x,
std::vector<double> &grad, void *fd) {
return (Tret)(*(T *)fn<T>())(x, grad, fd);
}
template <typename Tret = double,
typename Tfp = Tret (*)(const std::vector<double> &,
std::vector<double> &, void *),
typename T>
static Tfp ptr(T &t) {
fn<T>(&t);
return (Tfp)lambda_ptr_exec<Tret, T>;
}
template <typename T> static void *fn(void *new_fn = nullptr) {
static void *fn;
if (new_fn != nullptr)
fn = new_fn;
return fn;
}
};
class NLOptimizer : public Optimizer {
public:
OptResult optimize(OptFunction &function) override;
const std::string name() const override {
return "nlopt";
}
const std::string name() const override { return "nlopt"; }
const std::string description() const override {
return "";
}
const std::string description() const override { return ""; }
};
} // namespace xacc
#endif
......@@ -7,23 +7,50 @@ using namespace xacc;
TEST(NLOptimizerTester, checkSimple) {
auto optimizer = xacc::getService<Optimizer>("nlopt");//NLOptimizer optimizer;
auto optimizer =
xacc::getService<Optimizer>("nlopt"); // NLOptimizer optimizer;
OptFunction f([](const std::vector<double>& x) {return x[0]*x[0]+5;},1);
OptFunction f([](const std::vector<double> &x, std::vector<double>& g) { return x[0] * x[0] + 5; },
1);
EXPECT_EQ(1,f.dimensions());
EXPECT_EQ(1, f.dimensions());
optimizer->setOptions(HeterogeneousMap{std::make_pair("nlopt-maxeval", 20)});
auto result = optimizer->optimize(f);
EXPECT_EQ(result.first, 5.0);
EXPECT_EQ(5.0, result.first);
EXPECT_EQ(result.second, std::vector<double>{0.0});
}
TEST(NLOptimizerTester, checkGradient) {
auto optimizer =
xacc::getService<Optimizer>("nlopt"); // NLOptimizer optimizer;
OptFunction f(
[](const std::vector<double> &x, std::vector<double> &grad) {
if (!grad.empty()) {
std::cout << "GRAD\n";
grad[0] = 2. * x[0];
}
return x[0] * x[0] + 5;
},
1);
EXPECT_EQ(1, f.dimensions());
optimizer->setOptions(
HeterogeneousMap{std::make_pair("nlopt-maxeval", 20),
std::make_pair("nlopt-optimizer", "l-bfgs")});
auto result = optimizer->optimize(f);
EXPECT_EQ(result.first, 5.0);
EXPECT_EQ(result.second, std::vector<double>{0.0});
}
int main(int argc, char **argv) {
xacc::Initialize(argc,argv);
xacc::Initialize(argc, argv);
::testing::InitGoogleTest(&argc, argv);
auto ret = RUN_ALL_TESTS();
xacc::Finalize();
......
......@@ -75,5 +75,4 @@ void ServiceRegistry::initialize(const std::string rootPath) {
initialized = true;
}
}
}
} // namespace xacc
......@@ -22,7 +22,7 @@
#include "Compiler.hpp"
// #include "Algorithm.hpp"
// #include "Optimizer.hpp"
#include "Optimizer.hpp"
// #include "IRProvider.hpp"
#include <cppmicroservices/FrameworkFactory.h>
......@@ -39,7 +39,8 @@ using namespace cppmicroservices;
namespace xacc {
using ContributableService =
Variant<std::shared_ptr<Instruction>,std::shared_ptr<Accelerator>,std::shared_ptr<Compiler>>;
Variant<std::shared_ptr<Instruction>, std::shared_ptr<Accelerator>,
std::shared_ptr<Compiler>, std::shared_ptr<Optimizer>>;
class ServiceRegistry {
......@@ -128,13 +129,14 @@ public:
return ret;
}
template<typename Service>
template <typename Service>
bool hasContributedService(const std::string name) {
if (runtimeContributed.count(name)) {
try {
auto tmp = runtimeContributed[name].as_no_error<std::shared_ptr<Service>>();
} catch (std::exception& e) {
return false;
auto tmp =
runtimeContributed[name].as_no_error<std::shared_ptr<Service>>();
} catch (std::exception &e) {
return false;
}
return true;
}
......
/*******************************************************************************
* Copyright (c) 2017 UT-Battelle, LLC.
* All rights reserved. This program and the accompanying materials
* 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
*
* Contributors:
* Alexander J. McCaskey - initial API and implementation
*******************************************************************************/
#include <string>
#include "CLIParser.hpp"
......
Supports Markdown
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