Commit 18b0636b authored by Nguyen, Thien Minh's avatar Nguyen, Thien Minh
Browse files

Move OptimizerImpl to header and fwd declare xacc types



This will allow downstream code to use -> overload if need be

Signed-off-by: default avatarThien Nguyen <nguyentm@ornl.gov>
parent e48a8085
Loading
Loading
Loading
Loading
+5 −6
Original line number Diff line number Diff line
@@ -44,12 +44,10 @@ QaoaWorkflow::execute(const QuantumSimulationModel &model) {
  assert(nParams > 1);
  const std::vector<double> init_params =
      qcor::random_vector(-1.0, 1.0, nParams);
  // **THIEN-TODO**
  // (*optimizer)->appendOption("initial-parameters", init_params);

  (*optimizer)->xacc_opt->appendOption("initial-parameters", init_params);
  std::shared_ptr<xacc::AlgorithmGradientStrategy> gradient_strategy;
  // **THIEN-TODO**
  // if ((*optimizer)->isGradientBased()) {
  if (false) {
  if ((*optimizer)->xacc_opt->isGradientBased()) {
    if (config_params.stringExists("gradient-strategy")) {
      gradient_strategy = xacc::getService<xacc::AlgorithmGradientStrategy>(
          config_params.getString("gradient-strategy"));
@@ -59,7 +57,8 @@ QaoaWorkflow::execute(const QuantumSimulationModel &model) {
          xacc::getService<xacc::AlgorithmGradientStrategy>("autodiff");
    }
    gradient_strategy->initialize(
        {{"observable", xacc::as_shared_ptr(model.observable)}});
        {{"observable", std::dynamic_pointer_cast<xacc::Observable>(
                            model.observable->get_as_opaque())}});
  }

  auto result = optimizer->optimize(
+4 −12
Original line number Diff line number Diff line
@@ -20,19 +20,11 @@ Optimizer::Optimizer(std::shared_ptr<xacc::Identifiable> generic)
Optimizer::~Optimizer() = default;

// Define the internal implementation, wraps an XACC Optimizer
struct Optimizer::OptimizerImpl {
  std::shared_ptr<xacc::Optimizer> xacc_opt;

  OptimizerImpl() = default;
  OptimizerImpl(std::shared_ptr<xacc::Optimizer> opt) : xacc_opt(opt) {}

  std::pair<double, std::vector<double>> optimize(xacc::OptFunction &opt) {
std::pair<double, std::vector<double>>
Optimizer::OptimizerImpl::optimize(xacc::OptFunction &opt) {
  return xacc_opt->optimize(opt);
}

  xacc::Optimizer *operator->() { return xacc_opt.get(); }
};

std::string Optimizer::name() { return m_internal->xacc_opt->name(); }

std::pair<double, std::vector<double>> Optimizer::optimize(
+19 −5
Original line number Diff line number Diff line
@@ -8,6 +8,11 @@
#include "heterogeneous.hpp"
#include "qcor_pimpl.hpp"

namespace xacc {
class Optimizer;
class OptFunction;
} // namespace xacc

namespace qcor {

class ObjectiveFunction;
@@ -20,7 +25,16 @@ class Optimizer {
private:
  // Delegate the actual implementation to
  // this hiddent opaque type.
  class OptimizerImpl;
  // Define the internal implementation, wraps an XACC Optimizer
  // Need to declare OptimizerImpl in the header so that users can overload
  // operator -> all the way to xacc::Optimizer.
  struct OptimizerImpl {
    std::shared_ptr<xacc::Optimizer> xacc_opt;
    OptimizerImpl() = default;
    OptimizerImpl(std::shared_ptr<xacc::Optimizer> opt) : xacc_opt(opt) {}
    std::pair<double, std::vector<double>> optimize(xacc::OptFunction &opt);
    xacc::Optimizer *operator->() { return xacc_opt.get(); }
  };
  qcor_pimpl<OptimizerImpl> m_internal;

public: