Skip to content

Commit

Permalink
Add validation of sizes of arguments of OptimizationResults and unit …
Browse files Browse the repository at this point in the history
…tests (qiskit-community/qiskit-aqua#1167)

* add more unit tests of converters

* fix min_eigen_optimizer
  • Loading branch information
t-imamichi authored Aug 5, 2020
1 parent 57eafdd commit 9ebfd39
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 137 deletions.
6 changes: 3 additions & 3 deletions qiskit/optimization/algorithms/minimum_eigen_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
class MinimumEigenOptimizerResult(OptimizationResult):
""" Minimum Eigen Optimizer Result."""

def __init__(self, x: List[float], fval: float, variables: List[Variable],
def __init__(self, x: Union[List[float], np.ndarray], fval: float, variables: List[Variable],
samples: List[Tuple[str, float, float]],
qubo_converter: QuadraticProgramToQubo) -> None:
"""
Expand Down Expand Up @@ -173,10 +173,10 @@ def solve(self, problem: QuadraticProgram) -> MinimumEigenOptimizerResult:
samples = [(x_str, offset, 1.0)]

# translate result back to integers
base_res = OptimizationResult(x=x, fval=fval, variables=problem.variables)
base_res = OptimizationResult(x=x, fval=fval, variables=problem_.variables)
base_res = self._qubo_converter.interpret(base_res)
opt_res = MinimumEigenOptimizerResult(x=base_res.x, fval=base_res.fval,
variables=base_res.variables,
variables=problem.variables,
samples=samples,
qubo_converter=deepcopy(self._qubo_converter))
return opt_res
Expand Down
8 changes: 8 additions & 0 deletions qiskit/optimization/algorithms/optimization_algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,15 @@ def __init__(self, x: Union[List[float], np.ndarray], fval: float,
variables: the list of variables of the optimization problem.
raw_results: the original results object from the optimization algorithm.
status: the termination status of the optimization algorithm.
Raises:
QiskitOptimizationError: if sizes of ``x`` and ``variables`` do not match.
"""
if len(x) != len(variables):
raise QiskitOptimizationError(
'Inconsistent size of optimal value and variables. x: size {} {}, '
'variables: size {} {}'.format(len(x), x, len(variables),
[v.name for v in variables]))
self._x = x if isinstance(x, np.ndarray) else np.array(x) # pylint: disable=invalid-name
self._fval = fval
self._raw_results = raw_results
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ def find_value(x, replacements, var_values):
fval = result.fval
results = OptimizationResult(x=x_v, fval=fval,
raw_results=(replacements, deepcopy(self._qubo_converter)),
variables=problem.variables)
variables=problem_ref.variables)
results = self._qubo_converter.interpret(results)
return results

Expand Down
2 changes: 1 addition & 1 deletion qiskit/optimization/converters/inequality_to_equality.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ def interpret(self, result: OptimizationResult) -> OptimizationResult:
# convert back the optimization result into that of the original problem
names = [x.name for x in self._dst.variables]
new_x = self._interpret_var(names, result.x)
return OptimizationResult(x=new_x, fval=result.fval, variables=self._dst.variables,
return OptimizationResult(x=new_x, fval=result.fval, variables=self._src.variables,
raw_results=result.raw_results, status=result.status)

def _interpret_var(self, names, vals) -> List[int]:
Expand Down
2 changes: 1 addition & 1 deletion qiskit/optimization/converters/integer_to_binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ def interpret(self, result: OptimizationResult) -> OptimizationResult:
The result of the original problem.
"""
new_x = self._interpret_var(result.x)
return OptimizationResult(x=new_x, fval=result.fval, variables=result.variables,
return OptimizationResult(x=new_x, fval=result.fval, variables=self._src.variables,
raw_results=result.raw_results)

def _interpret_var(self, vals: Union[List[float], np.ndarray]) -> List[float]:
Expand Down
4 changes: 2 additions & 2 deletions qiskit/optimization/converters/linear_equality_to_penalty.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def interpret(self, result: OptimizationResult) -> OptimizationResult:
QiskitOptimizationError: if the number of variables in the result differs from
that of the original problem.
"""
if len(result.x) != len(self._src.variables):
if len(result.x) != self._src.get_num_vars():
raise QiskitOptimizationError(
'The number of variables in the passed result differs from '
'that of the original problem.'
Expand All @@ -192,7 +192,7 @@ def interpret(self, result: OptimizationResult) -> OptimizationResult:
new_status = OptimizationResultStatus.INFEASIBLE

return OptimizationResult(x=result.x, fval=substituted_qp.objective.constant,
variables=result.variables, raw_results=result.raw_results,
variables=self._src.variables, raw_results=result.raw_results,
status=new_status)

@property
Expand Down
Loading

0 comments on commit 9ebfd39

Please sign in to comment.