Skip to content

Commit

Permalink
Fix code trying to iterate NoneType (qiskit-community/qiskit-aqua#1414)
Browse files Browse the repository at this point in the history
  • Loading branch information
woodsp-ibm authored Nov 6, 2020
1 parent e8350f4 commit 82077a3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
4 changes: 3 additions & 1 deletion qiskit/aqua/components/optimizers/nlopts/nloptimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ def optimize(self, num_vars, objective_function, gradient_function=None,
variable_bounds=None, initial_point=None):
super().optimize(num_vars, objective_function,
gradient_function, variable_bounds, initial_point)
if variable_bounds is None:
variable_bounds = [(None, None)] * num_vars
return self._minimize(self._optimizer_names[self.get_nlopt_optimizer()],
objective_function,
variable_bounds,
Expand All @@ -101,7 +103,7 @@ def optimize(self, num_vars, objective_function, gradient_function=None,
def _minimize(self,
name: str,
objective_function: Callable,
variable_bounds: Optional[List[Tuple[float, float]]] = None,
variable_bounds: Optional[List[Tuple[float, float]]],
initial_point: Optional[np.ndarray] = None,
max_evals: int = 1000) -> Tuple[float, float, int]:
"""Minimize using objective function
Expand Down
17 changes: 10 additions & 7 deletions test/aqua/test_nlopt_optimizers.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,29 @@
class TestNLOptOptimizers(QiskitAquaTestCase):
""" Test NLOpt Optimizers """

def _optimize(self, optimizer):
def _optimize(self, optimizer, use_bound):
x_0 = [1.3, 0.7, 0.8, 1.9, 1.2]
bounds = [(-6, 6)] * len(x_0)
bounds = [(-6, 6)] * len(x_0) if use_bound else None
res = optimizer.optimize(len(x_0), rosen, initial_point=x_0, variable_bounds=bounds)
np.testing.assert_array_almost_equal(res[0], [1.0] * len(x_0), decimal=2)
return res

# ESCH and ISRES do not do well with rosen
@idata([
[CRS],
[DIRECT_L],
[DIRECT_L_RAND],
[CRS, True],
[DIRECT_L, True],
[DIRECT_L_RAND, True],
[CRS, False],
[DIRECT_L, False],
[DIRECT_L_RAND, False],
])
@unpack
def test_nlopt(self, optimizer_cls):
def test_nlopt(self, optimizer_cls, use_bound):
""" NLopt test """
try:
optimizer = optimizer_cls()
optimizer.set_options(**{'max_evals': 50000})
res = self._optimize(optimizer)
res = self._optimize(optimizer, use_bound)
self.assertLessEqual(res[2], 50000)
except MissingOptionalLibraryError as ex:
self.skipTest(str(ex))
Expand Down

0 comments on commit 82077a3

Please sign in to comment.