Skip to content

Commit

Permalink
Make MockFidelity also compatible with Qiskit Algorithms >= 0.3.0 (#761)
Browse files Browse the repository at this point in the history
* Make MockFidelity also compatible with Qiskit Algorithms >= 0.3.0

* Run black on the changed file

(cherry picked from commit ce24989)
  • Loading branch information
woodsp-ibm authored and mergify[bot] committed Feb 7, 2024
1 parent 23d2b24 commit 81bc834
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions test/kernels/test_fidelity_qkernel.py
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, 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
Expand All @@ -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,
Expand Down Expand Up @@ -267,9 +268,27 @@ 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"):
Expand Down

0 comments on commit 81bc834

Please sign in to comment.