class GradientDescent(maxiter=100, learning_rate=0.01, tol=1e-07, callback=None, perturbation=None)[source]

Bases : SteppableOptimizer

For a function $$f$$ and an initial point $$\vec\theta_0$$, the standard (or « vanilla ») gradient descent method is an iterative scheme to find the minimum $$\vec\theta^*$$ of $$f$$ by updating the parameters in the direction of the negative gradient of $$f$$

$\vec\theta_{n+1} = \vec\theta_{n} - \eta_n \vec\nabla f(\vec\theta_{n}),$

for a small learning rate $$\eta_n > 0$$.

You can either provide the analytic gradient $$\vec\nabla f$$ as jac in the minimize() method, or, if you do not provide it, use a finite difference approximation of the gradient. To adapt the size of the perturbation in the finite difference gradients, set the perturbation property in the initializer.

This optimizer supports a callback function. If provided in the initializer, the optimizer will call the callback in each iteration with the following information in this order: current number of function values, current parameters, current function value, norm of current gradient.

Exemples

A minimum example that will use finite difference gradients with a default perturbation of 0.01 and a default learning rate of 0.01.

from qiskit.algorithms.optimizers import GradientDescent

def f(x):
return (np.linalg.norm(x) - 1) ** 2

initial_point = np.array([1, 0.5, -0.2])

result = optimizer.minimize(fun=fun, x0=initial_point)

print(f"Found minimum {result.x} at a value"
"of {result.fun} using {result.nfev} evaluations.")


An example where the learning rate is an iterator and we supply the analytic gradient. Note how much faster this convergences (i.e. less nfev) compared to the previous example.

from qiskit.algorithms.optimizers import GradientDescent

def learning_rate():
power = 0.6
constant_coeff = 0.1
def powerlaw():
n = 0
while True:
yield constant_coeff * (n ** power)
n += 1

return powerlaw()

def f(x):
return (np.linalg.norm(x) - 1) ** 2

return 2 * (np.linalg.norm(x) - 1) * x / np.linalg.norm(x)

initial_point = np.array([1, 0.5, -0.2])

print(f"Found minimum {result.x} at a value"
"of {result.fun} using {result.nfev} evaluations.")


An other 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

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

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,))

while optimizer.continue_condition():

optimizer.state.njev += 1

optmizer.state.nit += 1

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

Paramètres
• maxiter (int) – The maximum number of iterations.

• learning_rate (Union[float, List[float], ndarray, Callable[[], Iterator]]) – A constant, list, array or factory of generators yielding learning rates for the parameter updates. See the docstring for an example.

• tol (float) – If the norm of the parameter update is smaller than this threshold, the optimizer has converged.

• perturbation (Optional[float]) – If no gradient is passed to minimize() the gradient is approximated with a forward finite difference scheme with perturbation perturbation in both directions (defaults to 1e-2 if required). Ignored when we have an explicit function for the gradient.

Lève

ValueError – If learning_rate is an array and its lenght is less than maxiter.

Methods

 ask Returns an object with the data needed to evaluate the gradient. continue_condition Condition that indicates the optimization process should come to an end. create_result Creates a result of the optimization process. evaluate Evaluates the gradient. get_support_level Get the 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 x by an ammount proportional to the learning rate and value of the gradient at that point. wrap_function Wrap the function to implicitly inject the args at the call of the function.

Attributes

bounds_support_level

Returns bounds 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_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

perturbation

Returns the perturbation.

This is the perturbation used in the finite difference gradient approximation.

Type renvoyé

Optional[float]

setting

Return setting

settings
Type renvoyé

Dict[str, Any]

state

Return the current state of the optimizer.

Type renvoyé

GradientDescentState

tol

Returns the tolerance of the optimizer.

Any step with smaller stepsize than this value will stop the optimization.

Type renvoyé

float