367 lines
17 KiB
Plaintext
367 lines
17 KiB
Plaintext
---
|
||
title: SteppableOptimizer
|
||
description: API reference for qiskit.algorithms.optimizers.SteppableOptimizer
|
||
in_page_toc_min_heading_level: 1
|
||
python_api_type: class
|
||
python_api_name: qiskit.algorithms.optimizers.SteppableOptimizer
|
||
---
|
||
|
||
# SteppableOptimizer
|
||
|
||
<Class id="qiskit.algorithms.optimizers.SteppableOptimizer" isDedicatedPage={true} github="https://github.com/qiskit/qiskit/tree/stable/0.46/qiskit/algorithms/optimizers/steppable_optimizer.py" signature="qiskit.algorithms.optimizers.SteppableOptimizer(maxiter=100)" modifiers="class">
|
||
Bases: [`Optimizer`](qiskit.algorithms.optimizers.Optimizer "qiskit.algorithms.optimizers.optimizer.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 [`ask()`](#qiskit.algorithms.optimizers.SteppableOptimizer.ask "qiskit.algorithms.optimizers.SteppableOptimizer.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 `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.
|
||
|
||
```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
|
||
|
||
optmizer.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 [`step()`](#qiskit.algorithms.optimizers.SteppableOptimizer.step "qiskit.algorithms.optimizers.SteppableOptimizer.step") method which wraps the [`ask()`](#qiskit.algorithms.optimizers.SteppableOptimizer.ask "qiskit.algorithms.optimizers.SteppableOptimizer.ask") and [`tell()`](#qiskit.algorithms.optimizers.SteppableOptimizer.tell "qiskit.algorithms.optimizers.SteppableOptimizer.tell") methods. In the same spirit the method [`minimize()`](#qiskit.algorithms.optimizers.SteppableOptimizer.minimize "qiskit.algorithms.optimizers.SteppableOptimizer.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](https://optuna.readthedocs.io/en/stable/tutorial/20_recipes/009_ask_and_tell.html)
|
||
|
||
**Parameters**
|
||
|
||
**maxiter** ([*int*](https://docs.python.org/3/library/functions.html#int "(in Python v3.12)")) – Number of steps in the optimization process before ending the loop.
|
||
|
||
## Attributes
|
||
|
||
### bounds\_support\_level
|
||
|
||
<Attribute id="qiskit.algorithms.optimizers.SteppableOptimizer.bounds_support_level">
|
||
Returns bounds support level
|
||
</Attribute>
|
||
|
||
### gradient\_support\_level
|
||
|
||
<Attribute id="qiskit.algorithms.optimizers.SteppableOptimizer.gradient_support_level">
|
||
Returns gradient support level
|
||
</Attribute>
|
||
|
||
### initial\_point\_support\_level
|
||
|
||
<Attribute id="qiskit.algorithms.optimizers.SteppableOptimizer.initial_point_support_level">
|
||
Returns initial point support level
|
||
</Attribute>
|
||
|
||
### is\_bounds\_ignored
|
||
|
||
<Attribute id="qiskit.algorithms.optimizers.SteppableOptimizer.is_bounds_ignored">
|
||
Returns is bounds ignored
|
||
</Attribute>
|
||
|
||
### is\_bounds\_required
|
||
|
||
<Attribute id="qiskit.algorithms.optimizers.SteppableOptimizer.is_bounds_required">
|
||
Returns is bounds required
|
||
</Attribute>
|
||
|
||
### is\_bounds\_supported
|
||
|
||
<Attribute id="qiskit.algorithms.optimizers.SteppableOptimizer.is_bounds_supported">
|
||
Returns is bounds supported
|
||
</Attribute>
|
||
|
||
### is\_gradient\_ignored
|
||
|
||
<Attribute id="qiskit.algorithms.optimizers.SteppableOptimizer.is_gradient_ignored">
|
||
Returns is gradient ignored
|
||
</Attribute>
|
||
|
||
### is\_gradient\_required
|
||
|
||
<Attribute id="qiskit.algorithms.optimizers.SteppableOptimizer.is_gradient_required">
|
||
Returns is gradient required
|
||
</Attribute>
|
||
|
||
### is\_gradient\_supported
|
||
|
||
<Attribute id="qiskit.algorithms.optimizers.SteppableOptimizer.is_gradient_supported">
|
||
Returns is gradient supported
|
||
</Attribute>
|
||
|
||
### is\_initial\_point\_ignored
|
||
|
||
<Attribute id="qiskit.algorithms.optimizers.SteppableOptimizer.is_initial_point_ignored">
|
||
Returns is initial point ignored
|
||
</Attribute>
|
||
|
||
### is\_initial\_point\_required
|
||
|
||
<Attribute id="qiskit.algorithms.optimizers.SteppableOptimizer.is_initial_point_required">
|
||
Returns is initial point required
|
||
</Attribute>
|
||
|
||
### is\_initial\_point\_supported
|
||
|
||
<Attribute id="qiskit.algorithms.optimizers.SteppableOptimizer.is_initial_point_supported">
|
||
Returns is initial point supported
|
||
</Attribute>
|
||
|
||
### setting
|
||
|
||
<Attribute id="qiskit.algorithms.optimizers.SteppableOptimizer.setting">
|
||
Return setting
|
||
</Attribute>
|
||
|
||
### settings
|
||
|
||
<Attribute id="qiskit.algorithms.optimizers.SteppableOptimizer.settings">
|
||
The optimizer settings in a dictionary format.
|
||
|
||
The settings can for instance be used for JSON-serialization (if all settings are serializable, which e.g. doesn’t hold per default for callables), such that the optimizer object can be reconstructed as
|
||
|
||
```python
|
||
settings = optimizer.settings
|
||
# JSON serialize and send to another server
|
||
optimizer = OptimizerClass(**settings)
|
||
```
|
||
</Attribute>
|
||
|
||
### state
|
||
|
||
<Attribute id="qiskit.algorithms.optimizers.SteppableOptimizer.state">
|
||
Return the current state of the optimizer.
|
||
</Attribute>
|
||
|
||
## Methods
|
||
|
||
### ask
|
||
|
||
<Function id="qiskit.algorithms.optimizers.SteppableOptimizer.ask" signature="ask()">
|
||
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 [`step()`](#qiskit.algorithms.optimizers.SteppableOptimizer.step "qiskit.algorithms.optimizers.SteppableOptimizer.step") in the optimization process.
|
||
|
||
**Returns**
|
||
|
||
An object containing the data needed to make the function evaluation to advance the optimization process.
|
||
|
||
**Return type**
|
||
|
||
[*AskData*](qiskit.algorithms.optimizers.AskData "qiskit.algorithms.optimizers.steppable_optimizer.AskData")
|
||
</Function>
|
||
|
||
### continue\_condition
|
||
|
||
<Function id="qiskit.algorithms.optimizers.SteppableOptimizer.continue_condition" signature="continue_condition()">
|
||
Condition that indicates the optimization process should continue.
|
||
|
||
**Returns**
|
||
|
||
`True` if the optimization process should continue, `False` otherwise.
|
||
|
||
**Return type**
|
||
|
||
[bool](https://docs.python.org/3/library/functions.html#bool "(in Python v3.12)")
|
||
</Function>
|
||
|
||
### create\_result
|
||
|
||
<Function id="qiskit.algorithms.optimizers.SteppableOptimizer.create_result" signature="create_result()" modifiers="abstract">
|
||
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.
|
||
|
||
**Return type**
|
||
|
||
[*OptimizerResult*](qiskit.algorithms.optimizers.OptimizerResult "qiskit.algorithms.optimizers.optimizer.OptimizerResult")
|
||
</Function>
|
||
|
||
### evaluate
|
||
|
||
<Function id="qiskit.algorithms.optimizers.SteppableOptimizer.evaluate" signature="evaluate(ask_data)" modifiers="abstract">
|
||
Evaluates the function according to the instructions contained in `ask_data`.
|
||
|
||
If the user decides to use [`step()`](#qiskit.algorithms.optimizers.SteppableOptimizer.step "qiskit.algorithms.optimizers.SteppableOptimizer.step") instead of [`ask()`](#qiskit.algorithms.optimizers.SteppableOptimizer.ask "qiskit.algorithms.optimizers.SteppableOptimizer.ask") and [`tell()`](#qiskit.algorithms.optimizers.SteppableOptimizer.tell "qiskit.algorithms.optimizers.SteppableOptimizer.tell") this function will contain the logic on how to evaluate the function.
|
||
|
||
**Parameters**
|
||
|
||
**ask\_data** ([*AskData*](qiskit.algorithms.optimizers.AskData "qiskit.algorithms.optimizers.steppable_optimizer.AskData")) – Contains the information on how to do the evaluation.
|
||
|
||
**Returns**
|
||
|
||
Data of all relevant information about the function evaluation.
|
||
|
||
**Return type**
|
||
|
||
[*TellData*](qiskit.algorithms.optimizers.TellData "qiskit.algorithms.optimizers.steppable_optimizer.TellData")
|
||
</Function>
|
||
|
||
### get\_support\_level
|
||
|
||
<Function id="qiskit.algorithms.optimizers.SteppableOptimizer.get_support_level" signature="get_support_level()" modifiers="abstract">
|
||
Return support level dictionary
|
||
</Function>
|
||
|
||
### gradient\_num\_diff
|
||
|
||
<Function id="qiskit.algorithms.optimizers.SteppableOptimizer.gradient_num_diff" signature="gradient_num_diff(x_center, f, epsilon, max_evals_grouped=None)" modifiers="static">
|
||
We compute the gradient with the numeric differentiation in the parallel way, around the point x\_center.
|
||
|
||
**Parameters**
|
||
|
||
* **x\_center** (*ndarray*) – point around which we compute the gradient
|
||
* **f** (*func*) – the function of which the gradient is to be computed.
|
||
* **epsilon** ([*float*](https://docs.python.org/3/library/functions.html#float "(in Python v3.12)")) – the epsilon used in the numeric differentiation.
|
||
* **max\_evals\_grouped** ([*int*](https://docs.python.org/3/library/functions.html#int "(in Python v3.12)")) – max evals grouped, defaults to 1 (i.e. no batching).
|
||
|
||
**Returns**
|
||
|
||
the gradient computed
|
||
|
||
**Return type**
|
||
|
||
grad
|
||
</Function>
|
||
|
||
### minimize
|
||
|
||
<Function id="qiskit.algorithms.optimizers.SteppableOptimizer.minimize" signature="minimize(fun, x0, jac=None, bounds=None)">
|
||
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 [`ask()`](#qiskit.algorithms.optimizers.SteppableOptimizer.ask "qiskit.algorithms.optimizers.SteppableOptimizer.ask") and [`tell()`](#qiskit.algorithms.optimizers.SteppableOptimizer.tell "qiskit.algorithms.optimizers.SteppableOptimizer.tell") and evaluating the function manually.
|
||
|
||
**Parameters**
|
||
|
||
* **fun** (*Callable\[\[POINT],* [*float*](https://docs.python.org/3/library/functions.html#float "(in Python v3.12)")*]*) – Function to minimize.
|
||
* **x0** (*POINT*) – Initial point.
|
||
* **jac** (*Callable\[\[POINT], POINT] | None*) – Function to compute the gradient.
|
||
* **bounds** ([*list*](https://docs.python.org/3/library/stdtypes.html#list "(in Python v3.12)")*\[*[*tuple*](https://docs.python.org/3/library/stdtypes.html#tuple "(in Python v3.12)")*\[*[*float*](https://docs.python.org/3/library/functions.html#float "(in Python v3.12)")*,* [*float*](https://docs.python.org/3/library/functions.html#float "(in Python v3.12)")*]] | None*) – Bounds of the search space.
|
||
|
||
**Returns**
|
||
|
||
Object containing the result of the optimization.
|
||
|
||
**Return type**
|
||
|
||
[OptimizerResult](qiskit.algorithms.optimizers.OptimizerResult "qiskit.algorithms.optimizers.OptimizerResult")
|
||
</Function>
|
||
|
||
### print\_options
|
||
|
||
<Function id="qiskit.algorithms.optimizers.SteppableOptimizer.print_options" signature="print_options()">
|
||
Print algorithm-specific options.
|
||
</Function>
|
||
|
||
### set\_max\_evals\_grouped
|
||
|
||
<Function id="qiskit.algorithms.optimizers.SteppableOptimizer.set_max_evals_grouped" signature="set_max_evals_grouped(limit)">
|
||
Set max evals grouped
|
||
</Function>
|
||
|
||
### set\_options
|
||
|
||
<Function id="qiskit.algorithms.optimizers.SteppableOptimizer.set_options" signature="set_options(**kwargs)">
|
||
Sets or updates values in the options dictionary.
|
||
|
||
The options dictionary may be used internally by a given optimizer to pass additional optional values for the underlying optimizer/optimization function used. The options dictionary may be initially populated with a set of key/values when the given optimizer is constructed.
|
||
|
||
**Parameters**
|
||
|
||
**kwargs** ([*dict*](https://docs.python.org/3/library/stdtypes.html#dict "(in Python v3.12)")) – options, given as name=value.
|
||
</Function>
|
||
|
||
### start
|
||
|
||
<Function id="qiskit.algorithms.optimizers.SteppableOptimizer.start" signature="start(fun, x0, jac=None, bounds=None)" modifiers="abstract">
|
||
Populates the state of the optimizer with the data provided and sets all the counters to 0.
|
||
|
||
**Parameters**
|
||
|
||
* **fun** (*Callable\[\[POINT],* [*float*](https://docs.python.org/3/library/functions.html#float "(in Python v3.12)")*]*) – Function to minimize.
|
||
* **x0** (*POINT*) – Initial point.
|
||
* **jac** (*Callable\[\[POINT], POINT] | None*) – Function to compute the gradient.
|
||
* **bounds** ([*list*](https://docs.python.org/3/library/stdtypes.html#list "(in Python v3.12)")*\[*[*tuple*](https://docs.python.org/3/library/stdtypes.html#tuple "(in Python v3.12)")*\[*[*float*](https://docs.python.org/3/library/functions.html#float "(in Python v3.12)")*,* [*float*](https://docs.python.org/3/library/functions.html#float "(in Python v3.12)")*]] | None*) – Bounds of the search space.
|
||
</Function>
|
||
|
||
### step
|
||
|
||
<Function id="qiskit.algorithms.optimizers.SteppableOptimizer.step" signature="step()">
|
||
Performs one step in the optimization process.
|
||
|
||
This method composes [`ask()`](#qiskit.algorithms.optimizers.SteppableOptimizer.ask "qiskit.algorithms.optimizers.SteppableOptimizer.ask"), [`evaluate()`](#qiskit.algorithms.optimizers.SteppableOptimizer.evaluate "qiskit.algorithms.optimizers.SteppableOptimizer.evaluate"), and [`tell()`](#qiskit.algorithms.optimizers.SteppableOptimizer.tell "qiskit.algorithms.optimizers.SteppableOptimizer.tell") to make a “step” in the optimization process.
|
||
</Function>
|
||
|
||
### tell
|
||
|
||
<Function id="qiskit.algorithms.optimizers.SteppableOptimizer.tell" signature="tell(ask_data, tell_data)">
|
||
Updates the optimization state using the results of the function evaluation.
|
||
|
||
A canonical optimization example using [`ask()`](#qiskit.algorithms.optimizers.SteppableOptimizer.ask "qiskit.algorithms.optimizers.SteppableOptimizer.ask") and [`tell()`](#qiskit.algorithms.optimizers.SteppableOptimizer.tell "qiskit.algorithms.optimizers.SteppableOptimizer.tell") can be seen in [`step()`](#qiskit.algorithms.optimizers.SteppableOptimizer.step "qiskit.algorithms.optimizers.SteppableOptimizer.step").
|
||
|
||
**Parameters**
|
||
|
||
* **ask\_data** ([*AskData*](qiskit.algorithms.optimizers.AskData "qiskit.algorithms.optimizers.steppable_optimizer.AskData")) – Contains the information on how the evaluation was done.
|
||
* **tell\_data** ([*TellData*](qiskit.algorithms.optimizers.TellData "qiskit.algorithms.optimizers.steppable_optimizer.TellData")) – Contains all relevant information about the evaluation of the objective function.
|
||
</Function>
|
||
|
||
### wrap\_function
|
||
|
||
<Function id="qiskit.algorithms.optimizers.SteppableOptimizer.wrap_function" signature="wrap_function(function, args)" modifiers="static">
|
||
Wrap the function to implicitly inject the args at the call of the function.
|
||
|
||
**Parameters**
|
||
|
||
* **function** (*func*) – the target function
|
||
* **args** ([*tuple*](https://docs.python.org/3/library/stdtypes.html#tuple "(in Python v3.12)")) – the args to be injected
|
||
|
||
**Returns**
|
||
|
||
wrapper
|
||
|
||
**Return type**
|
||
|
||
function\_wrapper
|
||
</Function>
|
||
</Class>
|
||
|