qiskit_addon_opt_mapper.solvers.SolverResult¶
- class SolverResult(x, fval, variables, status, raw_results=None, samples=None)[source]¶
 Bases:
objectA base class for optimization results.
The optimization algorithms return an object of the type
SolverResultwith the information about the solution obtained.SolverResultallows users to get the value of a variable by specifying an index or a name as follows.Examples
>>> from qiskit_addon_opt_mapper import OptimizationProblem >>> from qiskit_addon_opt_mapper.solvers import CplexSolver >>> problem = OptimizationProblem() >>> _ = 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 = CplexSolver() >>> 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}
Note
The order of variables should be equal to that of the problem solved by optimization algorithms. Optimization algorithms and converters of
OptimizationProblemshould maintain the order when generating a newSolverResultobject.Init method.
- Parameters:
 x (list[float] | np.ndarray | None) – the variable values found in the optimization, or possibly None in case of FAILURE.
fval (float) – the objective function value.
variables (list[Variable]) – the list of variables of the optimization problem.
raw_results (Any | None) – the original results object from the optimization algorithm.
status (SolverResultStatus) – the termination status of the optimization algorithm.
samples (list[SolutionSample] | None) – the solution samples.
- Raises:
 OptimizationError – if sizes of
xandvariablesdo not match or one of (fval, samples) is not provided.
- __init__(x, fval, variables, status, raw_results=None, samples=None)[source]¶
 Init method.
- Parameters:
 x (list[float] | ndarray | None) – the variable values found in the optimization, or possibly None in case of FAILURE.
fval (float) – the objective function value.
variables (list[Variable]) – the list of variables of the optimization problem.
raw_results (Any | None) – the original results object from the optimization algorithm.
status (SolverResultStatus) – the termination status of the optimization algorithm.
samples (list[SolutionSample] | None) – the solution samples.
- Raises:
 OptimizationError – if sizes of
xandvariablesdo not match or one of (fval, samples) is not provided.- Return type:
 None
Methods
__init__(x, fval, variables, status[, ...])Init method.
Get <Zi x Zj> correlation matrix from the samples.
Returns a pretty printed string of this optimization result.
Attributes
Returns the objective function value.
Return the original results object from the optimization algorithm.
Returns the list of solution samples.
Returns the termination status of the optimization algorithm.
Returns the list of variable names of the optimization problem.
Returns the list of variables of the optimization problem.
Returns the variable values as a dictionary of the variable name and corresponding value.
Returns the variable values found in the optimization or None in case of FAILURE.
- property fval: float | None¶
 Returns the objective function value.
- Returns:
 The function value corresponding to the objective function value found in the optimization.
- get_correlations()[source]¶
 Get <Zi x Zj> correlation matrix from the samples.
- Returns:
 A correlation matrix.
- Return type:
 
- prettyprint()[source]¶
 Returns a pretty printed string of this optimization result.
- Returns:
 A pretty printed string representing the result.
- Return type:
 
- property raw_results: Any¶
 Return the original results object from the optimization algorithm.
Currently a dump for any leftovers.
- Returns:
 Additional result information of the optimization algorithm.
- property samples: list[SolutionSample]¶
 Returns the list of solution samples.
- Returns:
 The list of solution samples.
- property status: SolverResultStatus¶
 Returns the termination status of the optimization algorithm.
- Returns:
 The termination status of the algorithm.
- property variable_names: list[str]¶
 Returns the list of variable names of the optimization problem.
- Returns:
 The list of variable names of the optimization problem.
- property variables: list[Variable]¶
 Returns the list of variables of the optimization problem.
- Returns:
 The list of variables.