QCOR
qcor_optimizer.hpp
1 #pragma once
2 
3 #include <functional>
4 #include <memory>
5 #include <vector>
6 
7 #include "Identifiable.hpp"
8 #include "heterogeneous.hpp"
9 #include "qcor_pimpl.hpp"
10 
11 namespace xacc {
12 class Optimizer;
13 class OptFunction;
14 } // namespace xacc
15 
16 namespace qcor {
17 
18 class ObjectiveFunction;
19 
20 // This class provides the QCOR specification Optimizer data type.
21 // It's role is to take an ObjectiveFunction as input and execute
22 // an implementation specific, user-specified, optimization workflow to
23 // compute the optimal value and parameters for the provided function.
24 class Optimizer {
25 private:
26  // Delegate the actual implementation to
27  // this hiddent opaque type.
28  // Define the internal implementation, wraps an XACC Optimizer
29  // Need to declare OptimizerImpl in the header so that users can overload
30  // operator -> all the way to xacc::Optimizer.
31  struct OptimizerImpl {
32  std::shared_ptr<xacc::Optimizer> xacc_opt;
33  OptimizerImpl() = default;
34  OptimizerImpl(std::shared_ptr<xacc::Optimizer> opt) : xacc_opt(opt) {}
35  std::pair<double, std::vector<double>> optimize(xacc::OptFunction &opt);
36  xacc::Optimizer *operator->() { return xacc_opt.get(); }
37  };
38  qcor_pimpl<OptimizerImpl> m_internal;
39 
40 public:
41 
42  // Constructors, take the name of the
43  // concrete Optimizer implementation, an
44  // instance of an XACC optimizer, or the
45  // name plus pertinent config options.
46  Optimizer();
47  Optimizer(const std::string& name);
48  Optimizer(const std::string& name, xacc::HeterogeneousMap&& options);
49  Optimizer(std::shared_ptr<xacc::Identifiable> generic_obj);
50 
51  // Delegate to the internal implementation
52  OptimizerImpl *operator->();
53 
54  // Can optimize general <functional> std functions (must provide dimensions)...
55  std::pair<double, std::vector<double>> optimize(
56  std::function<double(const std::vector<double> &)> opt, const int dim);
57  std::pair<double, std::vector<double>> optimize(
58  std::function<double(const std::vector<double> &, std::vector<double> &)>
59  opt,
60  const int dim);
61 
62  // or Objective function pointers, reference, or rvalue reference
63  std::pair<double, std::vector<double>> optimize(
64  std::shared_ptr<ObjectiveFunction> obj);
65  std::pair<double, std::vector<double>> optimize(ObjectiveFunction *obj);
66  std::pair<double, std::vector<double>> optimize(ObjectiveFunction &obj);
67  std::pair<double, std::vector<double>> optimize(ObjectiveFunction &&obj) {
68  return optimize(obj);
69  }
70 
71  // Return the implementation name.
72  std::string name();
73  ~Optimizer();
74 };
75 
76 // Create the desired Optimizer
77 std::shared_ptr<Optimizer> createOptimizer(
78  const std::string &type, xacc::HeterogeneousMap &&options = {});
79 
80 } // namespace qcor
qcor::ObjectiveFunction
Definition: objective_function.hpp:19
qcor::Optimizer
Definition: qcor_optimizer.hpp:24
qcor::qcor_pimpl< OptimizerImpl >
qcor::Optimizer::Optimizer
Optimizer()
————- Optimizer Wrapper —————
qcor
Definition: qcor_syntax_handler.cpp:15