qiskit-documentation/docs/api/qiskit/0.43/qiskit.algorithms.optimizer...

301 lines
11 KiB
Plaintext
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: UMDA
description: API reference for qiskit.algorithms.optimizers.UMDA
in_page_toc_min_heading_level: 1
python_api_type: class
python_api_name: qiskit.algorithms.optimizers.UMDA
---
# UMDA
<Class id="qiskit.algorithms.optimizers.UMDA" isDedicatedPage={true} github="https://github.com/qiskit/qiskit/tree/stable/0.24/qiskit/algorithms/optimizers/umda.py" signature="UMDA(maxiter=100, size_gen=20, alpha=0.5)" modifiers="class">
Bases: [`Optimizer`](qiskit.algorithms.optimizers.Optimizer "qiskit.algorithms.optimizers.optimizer.Optimizer")
Continuous Univariate Marginal Distribution Algorithm (UMDA).
UMDA \[1] is a specific type of Estimation of Distribution Algorithm (EDA) where new individuals are sampled from univariate normal distributions and are updated in each iteration of the algorithm by the best individuals found in the previous iteration.
<Admonition title="See also" type="note">
This original implementation of the UDMA optimizer for Qiskit was inspired by my (Vicente P. Soloviev) work on the EDAspy Python package \[2].
</Admonition>
EDAs are stochastic search algorithms and belong to the family of the evolutionary algorithms. The main difference is that EDAs have a probabilistic model which is updated in each iteration from the best individuals of previous generations (elite selection). Depending on the complexity of the probabilistic model, EDAs can be classified in different ways. In this case, UMDA is a univariate EDA as the embedded probabilistic model is univariate.
UMDA has been compared to some of the already implemented algorithms in Qiskit library to optimize the parameters of variational algorithms such as QAOA or VQE and competitive results have been obtained \[1]. UMDA seems to provide very good solutions for those circuits in which the number of layers is not big.
The optimization process can be personalized depending on the paremeters chosen in the initialization. The main parameter is the population size. The bigger it is, the final result will be better. However, this increases the complexity of the algorithm and the runtime will be much heavier. In the work \[1] different experiments have been performed where population size has been set to 20 - 30.
<Admonition title="Note" type="note">
The UMDA implementation has more parameters but these have default values for the initialization for better understanding of the user. For example, `lpha` parameter has been set to 0.5 and is the percentage of the population which is selected in each iteration to update the probabilistic model.
</Admonition>
**Example**
This short example runs UMDA to optimize the parameters of a variational algorithm. Here we will use the same operator as used in the algorithms introduction, which was originally computed by Qiskit Nature for an H2 molecule. The minimum energy of the H2 Hamiltonian can be found quite easily so we are able to set maxiters to a small value.
```python
from qiskit.opflow import X, Z, I
from qiskit import Aer
from qiskit.algorithms.optimizers import UMDA
from qiskit.algorithms import QAOA
from qiskit.utils import QuantumInstance
H2_op = (-1.052373245772859 * I ^ I) + (0.39793742484318045 * I ^ Z) + (-0.39793742484318045 * Z ^ I) + (-0.01128010425623538 * Z ^ Z) + (0.18093119978423156 * X ^ X)
p = 2 # Toy example: 2 layers with 2 parameters in each layer: 4 variables
opt = UMDA(maxiter=100, size_gen=20)
backend = Aer.get_backend('statevector_simulator')
vqe = QAOA(opt,
quantum_instance=QuantumInstance(backend=backend),
reps=p)
result = vqe.compute_minimum_eigenvalue(operator=H2_op)
```
If it is desired to modify the percentage of individuals considered to update the probabilistic model, then this code can be used. Here for example we set the 60% instead of the 50% predefined.
```python
opt = UMDA(maxiter=100, size_gen=20, alpha = 0.6)
backend = Aer.get_backend('statevector_simulator')
vqe = QAOA(opt,
quantum_instance=QuantumInstance(backend=backend),
reps=p)
result = vqe.compute_minimum_eigenvalue(operator=qubit_op)
```
**References**
\[1]: Vicente P. Soloviev, Pedro Larrañaga and Concha Bielza (2022, July). Quantum Parametric Circuit Optimization with Estimation of Distribution Algorithms. In 2022 The Genetic and Evolutionary Computation Conference (GECCO). DOI: [https://doi.org/10.1145/3520304.3533963](https://doi.org/10.1145/3520304.3533963)
\[2]: Vicente P. Soloviev. Python package EDAspy. [https://github.com/VicentePerezSoloviev/EDAspy](https://github.com/VicentePerezSoloviev/EDAspy).
**Parameters**
* **maxiter** (*int*) Maximum number of iterations.
* **size\_gen** (*int*) Population size of each generation.
* **alpha** (*float*) Percentage (0, 1] of the population to be selected as elite selection.
## Methods
<span id="qiskit-algorithms-optimizers-umda-get-support-level" />
### get\_support\_level
<Function id="qiskit.algorithms.optimizers.UMDA.get_support_level" signature="UMDA.get_support_level()">
Get the support level dictionary.
</Function>
<span id="qiskit-algorithms-optimizers-umda-gradient-num-diff" />
### gradient\_num\_diff
<Function id="qiskit.algorithms.optimizers.UMDA.gradient_num_diff" signature="UMDA.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*) the epsilon used in the numeric differentiation.
* **max\_evals\_grouped** (*int*) max evals grouped, defaults to 1 (i.e. no batching).
**Returns**
the gradient computed
**Return type**
grad
</Function>
<span id="qiskit-algorithms-optimizers-umda-minimize" />
### minimize
<Function id="qiskit.algorithms.optimizers.UMDA.minimize" signature="UMDA.minimize(fun, x0, jac=None, bounds=None)">
Minimize the scalar function.
**Parameters**
* **fun** (*Callable\[\[POINT], float]*) The scalar function to minimize.
* **x0** (*POINT*) The initial point for the minimization.
* **jac** (*Callable\[\[POINT], POINT] | None*) The gradient of the scalar function `fun`.
* **bounds** (*list\[tuple\[float, float]] | None*) Bounds for the variables of `fun`. This argument might be ignored if the optimizer does not support bounds.
**Returns**
The result of the optimization, containing e.g. the result as attribute `x`.
**Return type**
[OptimizerResult](qiskit.algorithms.optimizers.OptimizerResult "qiskit.algorithms.optimizers.OptimizerResult")
</Function>
<span id="qiskit-algorithms-optimizers-umda-print-options" />
### print\_options
<Function id="qiskit.algorithms.optimizers.UMDA.print_options" signature="UMDA.print_options()">
Print algorithm-specific options.
</Function>
<span id="qiskit-algorithms-optimizers-umda-set-max-evals-grouped" />
### set\_max\_evals\_grouped
<Function id="qiskit.algorithms.optimizers.UMDA.set_max_evals_grouped" signature="UMDA.set_max_evals_grouped(limit)">
Set max evals grouped
</Function>
<span id="qiskit-algorithms-optimizers-umda-set-options" />
### set\_options
<Function id="qiskit.algorithms.optimizers.UMDA.set_options" signature="UMDA.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*) options, given as name=value.
</Function>
<span id="qiskit-algorithms-optimizers-umda-wrap-function" />
### wrap\_function
<Function id="qiskit.algorithms.optimizers.UMDA.wrap_function" signature="UMDA.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*) the args to be injected
**Returns**
wrapper
**Return type**
function\_wrapper
</Function>
## Attributes
### ELITE\_FACTOR
<Attribute id="qiskit.algorithms.optimizers.UMDA.ELITE_FACTOR" attributeValue="0.4" />
### STD\_BOUND
<Attribute id="qiskit.algorithms.optimizers.UMDA.STD_BOUND" attributeValue="0.3" />
### alpha
<Attribute id="qiskit.algorithms.optimizers.UMDA.alpha">
Returns the alpha parameter value (percentage of population selected to update probabilistic model)
</Attribute>
### bounds\_support\_level
<Attribute id="qiskit.algorithms.optimizers.UMDA.bounds_support_level">
Returns bounds support level
</Attribute>
### gradient\_support\_level
<Attribute id="qiskit.algorithms.optimizers.UMDA.gradient_support_level">
Returns gradient support level
</Attribute>
### initial\_point\_support\_level
<Attribute id="qiskit.algorithms.optimizers.UMDA.initial_point_support_level">
Returns initial point support level
</Attribute>
### is\_bounds\_ignored
<Attribute id="qiskit.algorithms.optimizers.UMDA.is_bounds_ignored">
Returns is bounds ignored
</Attribute>
### is\_bounds\_required
<Attribute id="qiskit.algorithms.optimizers.UMDA.is_bounds_required">
Returns is bounds required
</Attribute>
### is\_bounds\_supported
<Attribute id="qiskit.algorithms.optimizers.UMDA.is_bounds_supported">
Returns is bounds supported
</Attribute>
### is\_gradient\_ignored
<Attribute id="qiskit.algorithms.optimizers.UMDA.is_gradient_ignored">
Returns is gradient ignored
</Attribute>
### is\_gradient\_required
<Attribute id="qiskit.algorithms.optimizers.UMDA.is_gradient_required">
Returns is gradient required
</Attribute>
### is\_gradient\_supported
<Attribute id="qiskit.algorithms.optimizers.UMDA.is_gradient_supported">
Returns is gradient supported
</Attribute>
### is\_initial\_point\_ignored
<Attribute id="qiskit.algorithms.optimizers.UMDA.is_initial_point_ignored">
Returns is initial point ignored
</Attribute>
### is\_initial\_point\_required
<Attribute id="qiskit.algorithms.optimizers.UMDA.is_initial_point_required">
Returns is initial point required
</Attribute>
### is\_initial\_point\_supported
<Attribute id="qiskit.algorithms.optimizers.UMDA.is_initial_point_supported">
Returns is initial point supported
</Attribute>
### maxiter
<Attribute id="qiskit.algorithms.optimizers.UMDA.maxiter">
Returns the maximum number of iterations
</Attribute>
### setting
<Attribute id="qiskit.algorithms.optimizers.UMDA.setting">
Return setting
</Attribute>
### settings
<Attribute id="qiskit.algorithms.optimizers.UMDA.settings" />
### size\_gen
<Attribute id="qiskit.algorithms.optimizers.UMDA.size_gen">
Returns the size of the generations (number of individuals per generation)
</Attribute>
</Class>