File size: 10,866 Bytes
b7d9967
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# This code is part of a Qiskit project.
#
# (C) Copyright IBM 2022, 2023.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.

"""SteppableOptimizer interface"""
from __future__ import annotations

from abc import abstractmethod, ABC
from collections.abc import Callable
from dataclasses import dataclass
from .optimizer import Optimizer, POINT, OptimizerResult


@dataclass
class AskData(ABC):
    """Base class for return type of :meth:`~.SteppableOptimizer.ask`.

    Args:
        x_fun: Point or list of points where the function needs to be evaluated to compute the next
        state of the optimizer.
        x_jac: Point or list of points where the gradient/jacobian needs to be evaluated to compute
        the next state of the optimizer.

    """

    x_fun: POINT | list[POINT] | None = None
    x_jac: POINT | list[POINT] | None = None


@dataclass
class TellData(ABC):
    """Base class for argument type of :meth:`~.SteppableOptimizer.tell`.

    Args:
        eval_fun: Image of the function at :attr:`~.ask_data.x_fun`.
        eval_jac: Image of the gradient-jacobian at :attr:`~.ask_data.x_jac`.

    """

    eval_fun: float | list[float] | None = None
    eval_jac: POINT | list[POINT] | None = None


@dataclass
class OptimizerState:
    """Base class representing the state of the optimizer.

    This class stores the current state of the optimizer, given by the current point and
    (optionally) information like the function value, the gradient or the number of
    function evaluations. This dataclass can also store any other individual variables that
    change during the optimization.

    """

    x: POINT
    """Current optimization parameters."""
    fun: Callable[[POINT], float] | None
    """Function being  optimized."""
    jac: Callable[[POINT], POINT] | None
    """Jacobian of the function being optimized."""
    nfev: int | None
    """Number of function evaluations so far in the optimization."""
    njev: int | None
    """Number of jacobian evaluations so far in the optimization."""
    nit: int | None
    """Number of optimization steps performed so far in the optimization."""


class SteppableOptimizer(Optimizer):
    """
    Base class for a steppable optimizer.

    This family of optimizers uses the `ask and tell interface
    <https://optuna.readthedocs.io/en/stable/tutorial/20_recipes/009_ask_and_tell.html>`_.
    When using this interface the user has to call :meth:`~.ask` to get information about
    how to evaluate the function (we are asking the optimizer about how to do the evaluation).
    This information is typically the next points at which the function is evaluated, but depending
    on the optimizer it can also determine whether to evaluate the function or its gradient.
    Once the function has been evaluated, the user calls the method :meth:`~..tell`
    to tell the optimizer what the result of the function evaluation(s) is. The optimizer then
    updates its state accordingly and the user can decide whether to stop the optimization process
    or to repeat a step.

    This interface is more customizable, and allows the user to have full control over the evaluation
    of the function.

    Examples:

        An example where the evaluation of the function has a chance of failing. The user, with
        specific knowledge about his function can catch this errors and handle them before passing
        the result to the optimizer.

        .. code-block:: python

            import random
            import numpy as np
            from qiskit_algorithms.optimizers import GradientDescent

            def objective(x):
                if random.choice([True, False]):
                    return None
                else:
                    return (np.linalg.norm(x) - 1) ** 2

            def grad(x):
                if random.choice([True, False]):
                    return None
                else:
                    return 2 * (np.linalg.norm(x) - 1) * x / np.linalg.norm(x)


            initial_point = np.random.normal(0, 1, size=(100,))

            optimizer = GradientDescent(maxiter=20)
            optimizer.start(x0=initial_point, fun=objective, jac=grad)

            while optimizer.continue_condition():
                ask_data = optimizer.ask()
                evaluated_gradient = None

                while evaluated_gradient is None:
                    evaluated_gradient = grad(ask_data.x_center)
                    optimizer.state.njev += 1

                optimizer.state.nit += 1

                 cf  = TellData(eval_jac=evaluated_gradient)
                optimizer.tell(ask_data=ask_data, tell_data=tell_data)

            result = optimizer.create_result()


    Users that aren't dealing with complicated functions and who are more familiar with step by step
    optimization algorithms can use the :meth:`~.step` method which wraps the :meth:`~.ask`
    and :meth:`~.tell` methods. In the same spirit the method :meth:`~.minimize` will optimize the
    function and return the result.

    To see other libraries that use this interface one can visit:
    https://optuna.readthedocs.io/en/stable/tutorial/20_recipes/009_ask_and_tell.html


    """

    def __init__(
        self,
        maxiter: int = 100,
    ):
        """
        Args:
            maxiter: Number of steps in the optimization process before ending the loop.
        """
        super().__init__()
        self._state: OptimizerState | None = None
        self.maxiter = maxiter

    @property
    def state(self) -> OptimizerState:
        """Return the current state of the optimizer."""
        return self._state

    @state.setter
    def state(self, state: OptimizerState) -> None:
        """Set the current state of the optimizer."""
        self._state = state

    def ask(self) -> AskData:
        """Ask the optimizer for a set of points to evaluate.

        This method asks the optimizer which are the next points to evaluate.
        These points can, e.g., correspond to function values and/or its derivative.
        It may also correspond to variables that let the user infer which points to evaluate.
        It is the first method inside of a :meth:`~.step` in the optimization process.

        Returns:
            An object containing the data needed to make the function evaluation to advance the
            optimization process.

        """
        raise NotImplementedError

    def tell(self, ask_data: AskData, tell_data: TellData) -> None:
        """Updates the optimization state using the results of the function evaluation.

        A canonical optimization example using :meth:`~.ask` and :meth:`~.tell` can be seen
        in :meth:`~.step`.

        Args:
            ask_data: Contains the information on how the evaluation was done.
            tell_data: Contains all relevant information about the evaluation of the objective
                function.
        """
        raise NotImplementedError

    @abstractmethod
    def evaluate(self, ask_data: AskData) -> TellData:
        """Evaluates the function according to the instructions contained in :attr:`~.ask_data`.

        If the user decides to use :meth:`~.step` instead of :meth:`~.ask` and :meth:`~.tell`
        this function will contain the logic on how to evaluate the function.

        Args:
            ask_data: Contains the information on how to do the evaluation.

        Returns:
            Data of all relevant information about the function evaluation.

        """
        raise NotImplementedError

    def _callback_wrapper(self) -> None:
        """
        Wraps the callback function to accommodate each optimizer.
        """
        pass

    def step(self) -> None:
        """Performs one step in the optimization process.

        This method composes :meth:`~.ask`, :meth:`~.evaluate`, and :meth:`~.tell` to make a "step"
        in the optimization process.
        """
        ask_data = self.ask()
        tell_data = self.evaluate(ask_data=ask_data)
        self.tell(ask_data=ask_data, tell_data=tell_data)

    # pylint: disable=invalid-name
    @abstractmethod
    def start(
        self,
        fun: Callable[[POINT], float],
        x0: POINT,
        jac: Callable[[POINT], POINT] | None = None,
        bounds: list[tuple[float, float]] | None = None,
    ) -> None:
        """Populates the state of the optimizer with the data provided and sets all the counters to 0.

        Args:
            fun: Function to minimize.
            x0: Initial point.
            jac: Function to compute the gradient.
            bounds: Bounds of the search space.

        """
        raise NotImplementedError

    def minimize(
        self,
        fun: Callable[[POINT], float],
        x0: POINT,
        jac: Callable[[POINT], POINT] | None = None,
        bounds: list[tuple[float, float]] | None = None,
    ) -> OptimizerResult:
        """Minimizes the function.

        For well behaved functions the user can call this method to minimize a function.
        If the user wants more control on how to evaluate the function a custom loop can be
        created using :meth:`~.ask` and :meth:`~.tell` and evaluating the function manually.

        Args:
            fun: Function to minimize.
            x0: Initial point.
            jac: Function to compute the gradient.
            bounds: Bounds of the search space.

        Returns:
            Object containing the result of the optimization.

        """
        self.start(x0=x0, fun=fun, jac=jac, bounds=bounds)
        while self.continue_condition():
            self.step()
            self._callback_wrapper()
        return self.create_result()

    @abstractmethod
    def create_result(self) -> OptimizerResult:
        """Returns the result of the optimization.

        All the information needed to create such a result should be stored in the optimizer state
        and will typically contain the best point found, the function value and gradient at that point,
        the number of function and gradient evaluation and the number of iterations in the optimization.

        Returns:
            The result of the optimization process.

        """
        raise NotImplementedError

    def continue_condition(self) -> bool:
        """Condition that indicates the optimization process should continue.

        Returns:
            ``True`` if the optimization process should continue, ``False`` otherwise.
        """
        return self.state.nit < self.maxiter