199 lines
8.6 KiB
Plaintext
199 lines
8.6 KiB
Plaintext
---
|
||
title: OptimizationResult
|
||
description: API reference for qiskit.optimization.algorithms.OptimizationResult
|
||
in_page_toc_min_heading_level: 1
|
||
python_api_type: class
|
||
python_api_name: qiskit.optimization.algorithms.OptimizationResult
|
||
---
|
||
|
||
<span id="qiskit-optimization-algorithms-optimizationresult" />
|
||
|
||
# qiskit.optimization.algorithms.OptimizationResult
|
||
|
||
<Class id="qiskit.optimization.algorithms.OptimizationResult" isDedicatedPage={true} github="https://github.com/qiskit-community/qiskit-aqua/tree/stable/0.8/qiskit/optimization/algorithms/optimization_algorithm.py" signature="OptimizationResult(x, fval, variables, status, raw_results=None)" modifiers="class">
|
||
A base class for optimization results.
|
||
|
||
The optimization algorithms return an object of the type `OptimizationResult` with the information about the solution obtained.
|
||
|
||
`OptimizationResult` allows users to get the value of a variable by specifying an index or a name as follows.
|
||
|
||
**Examples**
|
||
|
||
```python
|
||
>>> from qiskit.optimization import QuadraticProgram
|
||
>>> from qiskit.optimization.algorithms import CplexOptimizer
|
||
>>> problem = QuadraticProgram()
|
||
>>> _ = problem.binary_var('x1')
|
||
>>> _ = problem.binary_var('x2')
|
||
>>> _ = problem.binary_var('x3')
|
||
>>> problem.minimize(linear={'x1': 1, 'x2': -2, 'x3': 3})
|
||
>>> print([var.name for var in problem.variables])
|
||
['x1', 'x2', 'x3']
|
||
>>> optimizer = CplexOptimizer()
|
||
>>> result = optimizer.solve(problem)
|
||
>>> print(result.variable_names)
|
||
['x1', 'x2', 'x3']
|
||
>>> print(result.x)
|
||
[0. 1. 0.]
|
||
>>> print(result[1])
|
||
1.0
|
||
>>> print(result['x1'])
|
||
0.0
|
||
>>> print(result.fval)
|
||
-2.0
|
||
>>> print(result.variables_dict)
|
||
{'x1': 0.0, 'x2': 1.0, 'x3': 0.0}
|
||
```
|
||
|
||
<Admonition title="Note" type="note">
|
||
The order of variables should be equal to that of the problem solved by optimization algorithms. Optimization algorithms and converters of `QuadraticProgram` should maintain the order when generating a new `OptimizationResult` object.
|
||
</Admonition>
|
||
|
||
**Parameters**
|
||
|
||
* **x** (`Union`\[`List`\[`float`], `ndarray`, `None`]) – the optimal value found in the optimization, or possibly None in case of FAILURE.
|
||
* **fval** (`float`) – the optimal function value.
|
||
* **variables** (`List`\[`Variable`]) – the list of variables of the optimization problem.
|
||
* **raw\_results** (`Optional`\[`Any`]) – the original results object from the optimization algorithm.
|
||
* **status** (`OptimizationResultStatus`) – the termination status of the optimization algorithm.
|
||
|
||
**Raises**
|
||
|
||
[**QiskitOptimizationError**](qiskit.optimization.QiskitOptimizationError "qiskit.optimization.QiskitOptimizationError") – if sizes of `x` and `variables` do not match.
|
||
|
||
### \_\_init\_\_
|
||
|
||
<Function id="qiskit.optimization.algorithms.OptimizationResult.__init__" signature="__init__(x, fval, variables, status, raw_results=None)">
|
||
**Parameters**
|
||
|
||
* **x** (`Union`\[`List`\[`float`], `ndarray`, `None`]) – the optimal value found in the optimization, or possibly None in case of FAILURE.
|
||
* **fval** (`float`) – the optimal function value.
|
||
* **variables** (`List`\[`Variable`]) – the list of variables of the optimization problem.
|
||
* **raw\_results** (`Optional`\[`Any`]) – the original results object from the optimization algorithm.
|
||
* **status** (`OptimizationResultStatus`) – the termination status of the optimization algorithm.
|
||
|
||
**Raises**
|
||
|
||
[**QiskitOptimizationError**](qiskit.optimization.QiskitOptimizationError "qiskit.optimization.QiskitOptimizationError") – if sizes of `x` and `variables` do not match.
|
||
</Function>
|
||
|
||
## Methods
|
||
|
||
| | |
|
||
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------- |
|
||
| [`__init__`](#qiskit.optimization.algorithms.OptimizationResult.__init__ "qiskit.optimization.algorithms.OptimizationResult.__init__")(x, fval, variables, status\[, …]) | **type x**`Union`\[`List`\[`float`], `ndarray`, `None`] |
|
||
|
||
## Attributes
|
||
|
||
| | |
|
||
| -------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------- |
|
||
| [`fval`](#qiskit.optimization.algorithms.OptimizationResult.fval "qiskit.optimization.algorithms.OptimizationResult.fval") | Returns the optimal function value. |
|
||
| [`raw_results`](#qiskit.optimization.algorithms.OptimizationResult.raw_results "qiskit.optimization.algorithms.OptimizationResult.raw_results") | Return the original results object from the optimization algorithm. |
|
||
| [`status`](#qiskit.optimization.algorithms.OptimizationResult.status "qiskit.optimization.algorithms.OptimizationResult.status") | Returns the termination status of the optimization algorithm. |
|
||
| [`variable_names`](#qiskit.optimization.algorithms.OptimizationResult.variable_names "qiskit.optimization.algorithms.OptimizationResult.variable_names") | Returns the list of variable names of the optimization problem. |
|
||
| [`variables`](#qiskit.optimization.algorithms.OptimizationResult.variables "qiskit.optimization.algorithms.OptimizationResult.variables") | Returns the list of variables of the optimization problem. |
|
||
| [`variables_dict`](#qiskit.optimization.algorithms.OptimizationResult.variables_dict "qiskit.optimization.algorithms.OptimizationResult.variables_dict") | Returns the optimal value as a dictionary of the variable name and corresponding value. |
|
||
| [`x`](#qiskit.optimization.algorithms.OptimizationResult.x "qiskit.optimization.algorithms.OptimizationResult.x") | Returns the optimal value found in the optimization or None in case of FAILURE. |
|
||
|
||
### fval
|
||
|
||
<Attribute id="qiskit.optimization.algorithms.OptimizationResult.fval">
|
||
Returns the optimal function value.
|
||
|
||
**Return type**
|
||
|
||
`float`
|
||
|
||
**Returns**
|
||
|
||
The function value corresponding to the optimal value found in the optimization.
|
||
</Attribute>
|
||
|
||
### raw\_results
|
||
|
||
<Attribute id="qiskit.optimization.algorithms.OptimizationResult.raw_results">
|
||
Return the original results object from the optimization algorithm.
|
||
|
||
Currently a dump for any leftovers.
|
||
|
||
**Return type**
|
||
|
||
`Any`
|
||
|
||
**Returns**
|
||
|
||
Additional result information of the optimization algorithm.
|
||
</Attribute>
|
||
|
||
### status
|
||
|
||
<Attribute id="qiskit.optimization.algorithms.OptimizationResult.status">
|
||
Returns the termination status of the optimization algorithm.
|
||
|
||
**Return type**
|
||
|
||
`OptimizationResultStatus`
|
||
|
||
**Returns**
|
||
|
||
The termination status of the algorithm.
|
||
</Attribute>
|
||
|
||
### variable\_names
|
||
|
||
<Attribute id="qiskit.optimization.algorithms.OptimizationResult.variable_names">
|
||
Returns the list of variable names of the optimization problem.
|
||
|
||
**Return type**
|
||
|
||
`List`\[`str`]
|
||
|
||
**Returns**
|
||
|
||
The list of variable names of the optimization problem.
|
||
</Attribute>
|
||
|
||
### variables
|
||
|
||
<Attribute id="qiskit.optimization.algorithms.OptimizationResult.variables">
|
||
Returns the list of variables of the optimization problem.
|
||
|
||
**Return type**
|
||
|
||
`List`\[`Variable`]
|
||
|
||
**Returns**
|
||
|
||
The list of variables.
|
||
</Attribute>
|
||
|
||
### variables\_dict
|
||
|
||
<Attribute id="qiskit.optimization.algorithms.OptimizationResult.variables_dict">
|
||
Returns the optimal value as a dictionary of the variable name and corresponding value.
|
||
|
||
**Return type**
|
||
|
||
`Dict`\[`str`, `float`]
|
||
|
||
**Returns**
|
||
|
||
The optimal value as a dictionary of the variable name and corresponding value.
|
||
</Attribute>
|
||
|
||
### x
|
||
|
||
<Attribute id="qiskit.optimization.algorithms.OptimizationResult.x">
|
||
Returns the optimal value found in the optimization or None in case of FAILURE.
|
||
|
||
**Return type**
|
||
|
||
`Optional`\[`ndarray`]
|
||
|
||
**Returns**
|
||
|
||
The optimal value found in the optimization.
|
||
</Attribute>
|
||
</Class>
|
||
|