Skip to content

Commit

Permalink
Merge branch 'main' into fix_fidelity
Browse files Browse the repository at this point in the history
  • Loading branch information
woodsp-ibm authored Oct 30, 2023
2 parents 65ae15c + 524f969 commit adf2b60
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 14 deletions.
21 changes: 12 additions & 9 deletions .github/workflows/deploy-code.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This code is part of a Qiskit project.
#
# (C) Copyright IBM 2021, 2022.
# (C) Copyright IBM 2021, 2023.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
Expand All @@ -20,6 +20,9 @@ on:
jobs:
code_publish:
runs-on: ubuntu-latest
environment: release
permissions:
id-token: write
strategy:
matrix:
python-version: [3.8]
Expand All @@ -28,12 +31,12 @@ jobs:
- uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install deps
run: pip install -U pip setuptools virtualenv wheel
- name: Build sdist
run: python3 setup.py sdist bdist_wheel
- uses: actions/upload-artifact@v3
with:
path: ./dist/*
- name: Deploy to Pypi
env:
TWINE_PASSWORD: ${{ secrets.TWINE_PASSWORD }}
TWINE_USERNAME: qiskit
run : |
pip install -U twine pip setuptools virtualenv wheel
python3 setup.py sdist bdist_wheel
twine upload dist/qiskit*
shell: bash
uses: pypa/gh-action-pypi-publish@release/v1
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
7 changes: 4 additions & 3 deletions qiskit_algorithms/state_fidelities/base_state_fidelity.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

from qiskit import QuantumCircuit
from qiskit.circuit import ParameterVector
from qiskit.primitives.utils import _circuit_key

from ..algorithm_job import AlgorithmJob

Expand Down Expand Up @@ -172,8 +173,8 @@ def _construct_circuits(
circuits = []
for (circuit_1, circuit_2) in zip(circuits_1, circuits_2):

# TODO: improve caching, what if the circuit is modified without changing the id?
circuit = self._circuit_cache.get((id(circuit_1), id(circuit_2)))
# Use the same key for circuits as qiskit.primitives use.
circuit = self._circuit_cache.get((_circuit_key(circuit_1), _circuit_key(circuit_2)))

if circuit is not None:
circuits.append(circuit)
Expand All @@ -192,7 +193,7 @@ def _construct_circuits(
)
circuits.append(circuit)
# update cache
self._circuit_cache[id(circuit_1), id(circuit_2)] = circuit
self._circuit_cache[_circuit_key(circuit_1), _circuit_key(circuit_2)] = circuit

return circuits

Expand Down
7 changes: 7 additions & 0 deletions releasenotes/notes/fix_fidelity_cache-3a4cc328344716e0.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
fixes:
- |
Fixes internal cache used by state fidelities so that circuits are cached
using the same generated key method as that used by the reference primitives.
This avoids a potential incorrect result that could have occurred with the
key as it was before when id() was used.
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 adf2b60

Please sign in to comment.