From 4aa9130c8cfbf246d724d2a0670aab23d62a3afa Mon Sep 17 00:00:00 2001 From: woodsp-ibm Date: Tue, 17 Oct 2023 16:36:53 -0400 Subject: [PATCH] Fix State Fidelity cache key --- qiskit_algorithms/state_fidelities/base_state_fidelity.py | 7 ++++--- .../notes/fix_fidelity_cache-3a4cc328344716e0.yaml | 7 +++++++ 2 files changed, 11 insertions(+), 3 deletions(-) create mode 100644 releasenotes/notes/fix_fidelity_cache-3a4cc328344716e0.yaml diff --git a/qiskit_algorithms/state_fidelities/base_state_fidelity.py b/qiskit_algorithms/state_fidelities/base_state_fidelity.py index 1909e63f..3a293107 100644 --- a/qiskit_algorithms/state_fidelities/base_state_fidelity.py +++ b/qiskit_algorithms/state_fidelities/base_state_fidelity.py @@ -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 from .state_fidelity_result import StateFidelityResult @@ -173,8 +174,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) @@ -193,7 +194,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 diff --git a/releasenotes/notes/fix_fidelity_cache-3a4cc328344716e0.yaml b/releasenotes/notes/fix_fidelity_cache-3a4cc328344716e0.yaml new file mode 100644 index 00000000..fe6443cc --- /dev/null +++ b/releasenotes/notes/fix_fidelity_cache-3a4cc328344716e0.yaml @@ -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.