Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix State Fidelity cache key #93

Merged
merged 2 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
from .state_fidelity_result import StateFidelityResult
Expand Down Expand Up @@ -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)
Expand All @@ -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

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.