German
Sprachen
English
Bengali
French
German
Japanese
Korean
Portuguese
Spanish
Tamil

SteppableOptimizer

class SteppableOptimizer(maxiter=100)[Quellcode]

Bases: Optimizer

Base class for a steppable optimizer.

This family of optimizers uses the ask and tell interface. When using this interface the user has to call ask() to get information about how to evaluate the fucntion (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.

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() method which wraps the ask() and tell() methods. In the same spirit the method 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

Parameter

maxiter (int) – Number of steps in the optimization process before ending the loop.

Methods

ask

Ask the optimizer for a set of points to evaluate.

continue_condition

Condition that indicates the optimization process should continue.

create_result

Returns the result of the optimization.

evaluate

Evaluates the function according to the instructions contained in ask_data.

get_support_level

Return support level dictionary

gradient_num_diff

We compute the gradient with the numeric differentiation in the parallel way, around the point x_center.

minimize

Minimizes the function.

print_options

Print algorithm-specific options.

set_max_evals_grouped

Set max evals grouped

set_options

Sets or updates values in the options dictionary.

start

Populates the state of the optimizer with the data provided and sets all the counters to 0.

step

Performs one step in the optimization process.

tell

Updates the optimization state using the results of the function evaluation.

wrap_function

Wrap the function to implicitly inject the args at the call of the function.

Attributes

bounds_support_level

Returns bounds support level

gradient_support_level

Returns gradient support level

initial_point_support_level

Returns initial point support level

is_bounds_ignored

Returns is bounds ignored

is_bounds_required

Returns is bounds required

is_bounds_supported

Returns is bounds supported

is_gradient_ignored

Returns is gradient ignored

is_gradient_required

Returns is gradient required

is_gradient_supported

Returns is gradient supported

is_initial_point_ignored

Returns is initial point ignored

is_initial_point_required

Returns is initial point required

is_initial_point_supported

Returns is initial point supported

setting

Return setting

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

settings = optimizer.settings
# JSON serialize and send to another server
optimizer = OptimizerClass(**settings)
state

Return the current state of the optimizer.