Skip to content

Commit

Permalink
Fix GSLS optimizer bounds if entries have None (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
woodsp-ibm authored Oct 30, 2023
1 parent 1062753 commit b254504
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
4 changes: 2 additions & 2 deletions qiskit_algorithms/optimizers/gsls.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ def minimize(
var_lb = np.array([-np.inf] * x0.size)
var_ub = np.array([np.inf] * x0.size)
else:
var_lb = np.array([l for (l, _) in bounds])
var_ub = np.array([u for (_, u) in bounds])
var_lb = np.array([l if l is not None else -np.inf for (l, _) in bounds])
var_ub = np.array([u if u is not None else np.inf for (_, u) in bounds])

x, fun_, nfev, _ = self.ls_optimize(x0.size, fun, x0, var_lb, var_ub)

Expand Down
6 changes: 6 additions & 0 deletions releasenotes/notes/fix_gsls_bounds-29d4a7506130cd69.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
fixes:
- |
Fixes :class:`.GSLS` optimizer :meth:`~.GSLS.minimize` so that if the ``bounds`` parameter
is passed with tuples that have entries of ``None`` then the entry is treated
as equivalent to infinity.
7 changes: 7 additions & 0 deletions test/optimizers/test_optimizers.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,13 @@ def test_gsls(self):
self.assertLessEqual(x_value, 0.01)
self.assertLessEqual(n_evals, 10000)

with self.subTest("Bounds (None, None)"):
algorithm_globals.random_seed = 1
res = optimizer.minimize(rosen, x_0, bounds=[(None, None)] * len(x_0))

self.assertLessEqual(res.fun, 0.01)
self.assertLessEqual(res.nfev, 10000)

def test_scipy_optimizer(self):
"""scipy_optimizer test"""
optimizer = SciPyOptimizer("BFGS", options={"maxiter": 1000})
Expand Down

0 comments on commit b254504

Please sign in to comment.