gradient_function.hpp 2.69 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
/*******************************************************************************
 * Copyright (c) 2018-, UT-Battelle, LLC.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the BSD 3-Clause License 
 * which accompanies this distribution. 
 *
 * Contributors:
 *   Alexander J. McCaskey - initial API and implementation
 *   Thien Nguyen - implementation
 *******************************************************************************/
Nguyen, Thien Minh's avatar
Nguyen, Thien Minh committed
11
#pragma once
12
#include "Identifiable.hpp"
13
#include "heterogeneous.hpp"
14
15
#include "qcor_ir.hpp"
#include "qcor_observable.hpp"
16
#include <functional>
Nguyen, Thien Minh's avatar
Nguyen, Thien Minh committed
17
18
#include <memory>
#include <vector>
19

Nguyen, Thien Minh's avatar
Nguyen, Thien Minh committed
20
namespace qcor {
21
class ObjectiveFunction;
Nguyen, Thien Minh's avatar
Nguyen, Thien Minh committed
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Gradient function type:
// Input: set of current parameters (std::vector<double>) and the current
// objective (cost) function value. Output: gradients (std::vector<double>)
// Requirements: size(parameters) == size (gradients)
using GradientFunctionType =
    std::function<std::vector<double>(const std::vector<double> &, double)>;
class GradientFunction {
protected:
  GradientFunctionType gradient_func;

public:
  GradientFunction() {}
  GradientFunction(GradientFunctionType func) : gradient_func(func) {}
  std::vector<double> operator()(const std::vector<double> &x,
                                 double current_val) {
    return gradient_func(x, current_val);
  }
};

41
42
43
namespace __internal__ {
std::shared_ptr<GradientFunction>
get_gradient_method(const std::string &type,
44
45
                    std::shared_ptr<ObjectiveFunction> obj_func,
                    xacc::HeterogeneousMap options = {});
46
47
48

std::shared_ptr<GradientFunction>
get_gradient_method(const std::string &type,
49
                    std::function<std::shared_ptr<CompositeInstruction>(
50
51
                        std::vector<double>)>
                        kernel_eval,
52
                    Operator &obs);
53
} // namespace __internal__
Nguyen, Thien Minh's avatar
Nguyen, Thien Minh committed
54

55
56
57
58
59
// Interface for gradient calculation services.
// Note: we keep the base GradientFunction API as simple as possible (just a
// thin wrapper around std::function, i.e. C++ lambda) so that users can define
// it in-place if need be. We also provide a set of registered gradient
// services implementing this interface.
60
61
class KernelGradientService : public GradientFunction,
                              public xacc::Identifiable {
Nguyen, Thien Minh's avatar
Nguyen, Thien Minh committed
62
public:
63
64
  virtual void initialize(std::shared_ptr<ObjectiveFunction> obj_func,
                          xacc::HeterogeneousMap &&options = {}) = 0;
65
  virtual void
66
  initialize(std::function<std::shared_ptr<CompositeInstruction>(
67
68
                 std::vector<double>)>
                 kernel_eval,
69
             Operator &obs, xacc::HeterogeneousMap &&options = {}) = 0;
Nguyen, Thien Minh's avatar
Nguyen, Thien Minh committed
70
71
};
} // namespace qcor