From 1ed99fc0e897aa3b4906de5df1b85061f32977bc Mon Sep 17 00:00:00 2001 From: woodsp-ibm Date: Wed, 7 Feb 2024 08:39:19 -0500 Subject: [PATCH 1/2] Make MockFidelity also compatible with Qiskit Algorithms >= 0.3.0 --- test/kernels/test_fidelity_qkernel.py | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/test/kernels/test_fidelity_qkernel.py b/test/kernels/test_fidelity_qkernel.py index 047b623dc..5a134bc07 100644 --- a/test/kernels/test_fidelity_qkernel.py +++ b/test/kernels/test_fidelity_qkernel.py @@ -1,6 +1,6 @@ # This code is part of a Qiskit project. # -# (C) Copyright IBM 2021, 2023. +# (C) Copyright IBM 2021, 2024. # # 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 @@ -26,6 +26,7 @@ from qiskit.circuit import Parameter from qiskit.circuit.library import ZFeatureMap from qiskit.primitives import Sampler +from qiskit_algorithms import AlgorithmJob from qiskit_algorithms.utils import algorithm_globals from qiskit_algorithms.state_fidelities import ( ComputeUncompute, @@ -267,9 +268,26 @@ def _run( values_1: Sequence[float] | Sequence[Sequence[float]] | None = None, values_2: Sequence[float] | Sequence[Sequence[float]] | None = None, **options, - ) -> StateFidelityResult: + ) -> StateFidelityResult | AlgorithmJob: values = np.asarray(values_1) fidelities = np.full(values.shape[0], -0.5) + + # Qiskit algorithms changed the internals of the base state fidelity + # class and what this method returns to avoid a threading issue. See + # https://github.com/qiskit-community/qiskit-algorithms/pull/92 for + # more information. That pull request will land in 0.3.0. I made + # this test work with the current released version 0.2.2, so tests + # pass at present, but in the future this logic can be reduced to + # just that needed for 0.3.0 and above if desired when testing against + # earlier algorithm versions is no longer needed or wanted. + from qiskit_algorithms import __version__ as algs_version + if algs_version < "0.3.0": + return StateFidelityResult(fidelities, [], {}, options) + else: + return AlgorithmJob(MockFidelity._call, fidelities, options) + + @staticmethod + def _call(fidelities, options) -> StateFidelityResult: return StateFidelityResult(fidelities, [], {}, options) with self.subTest("No PSD enforcement"): From 2f2fbf4c3189ec9274b45944fa52235f536cf0c1 Mon Sep 17 00:00:00 2001 From: woodsp-ibm Date: Wed, 7 Feb 2024 09:24:10 -0500 Subject: [PATCH 2/2] Run black on the changed file --- test/kernels/test_fidelity_qkernel.py | 1 + 1 file changed, 1 insertion(+) diff --git a/test/kernels/test_fidelity_qkernel.py b/test/kernels/test_fidelity_qkernel.py index 5a134bc07..ffe0f56a5 100644 --- a/test/kernels/test_fidelity_qkernel.py +++ b/test/kernels/test_fidelity_qkernel.py @@ -281,6 +281,7 @@ def _run( # just that needed for 0.3.0 and above if desired when testing against # earlier algorithm versions is no longer needed or wanted. from qiskit_algorithms import __version__ as algs_version + if algs_version < "0.3.0": return StateFidelityResult(fidelities, [], {}, options) else: