From 5cb721596f0b352a7b17ea6156049fed6d646f80 Mon Sep 17 00:00:00 2001 From: Julen Larrucea Date: Thu, 27 Feb 2025 20:35:49 +0100 Subject: [PATCH 1/7] "sampler" is currently a keyword argument The example provided specifies the sampler option as if it would be positional, but it is actually a keyword argument. I don't understand the "*" or the possibility of the sampler to be None. But in order to make it work without removing those, the sampler hast to be especified as "sampler"=sampler. --- qiskit_algorithms/optimizers/qnspsa.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qiskit_algorithms/optimizers/qnspsa.py b/qiskit_algorithms/optimizers/qnspsa.py index 4c00741d..432007e2 100644 --- a/qiskit_algorithms/optimizers/qnspsa.py +++ b/qiskit_algorithms/optimizers/qnspsa.py @@ -80,7 +80,7 @@ def loss(x): # fidelity for estimation of the geometric tensor sampler = Sampler() - fidelity = QNSPSA.get_fidelity(ansatz, sampler) + fidelity = QNSPSA.get_fidelity(ansatz, "sampler"=sampler) # run QN-SPSA qnspsa = QNSPSA(fidelity, maxiter=300) From 98a0acf17e7bae6893b3832bac2f5894d4545260 Mon Sep 17 00:00:00 2001 From: Julen Larrucea Date: Wed, 5 Mar 2025 11:20:05 +0100 Subject: [PATCH 2/7] Updated the arguments for get_fidelity As suggested by @Cryoris, instead of the workaround on the example text, I updated the arguments of the get_fidelity method to include only the required elements and avoid confusion. --- qiskit_algorithms/optimizers/qnspsa.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/qiskit_algorithms/optimizers/qnspsa.py b/qiskit_algorithms/optimizers/qnspsa.py index 432007e2..d8d27f2d 100644 --- a/qiskit_algorithms/optimizers/qnspsa.py +++ b/qiskit_algorithms/optimizers/qnspsa.py @@ -80,7 +80,7 @@ def loss(x): # fidelity for estimation of the geometric tensor sampler = Sampler() - fidelity = QNSPSA.get_fidelity(ansatz, "sampler"=sampler) + fidelity = QNSPSA.get_fidelity(ansatz, sampler) # run QN-SPSA qnspsa = QNSPSA(fidelity, maxiter=300) @@ -232,8 +232,7 @@ def settings(self) -> dict[str, Any]: @staticmethod def get_fidelity( circuit: QuantumCircuit, - *, - sampler: BaseSampler | None = None, + sampler: BaseSampler, ) -> Callable[[np.ndarray, np.ndarray], float]: r"""Get a function to compute the fidelity of ``circuit`` with itself. From 2cd5e7fe33302c2310afa7bb3c14f764a091242d Mon Sep 17 00:00:00 2001 From: Julen Larrucea Date: Mon, 10 Mar 2025 14:02:21 +0100 Subject: [PATCH 3/7] Added release note for update on QNSPSA.get_fidelity --- ...ional_arg_for_qnspsa-2efca170947f3cd7.yaml | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 releasenotes/notes/sampler_as_positional_arg_for_qnspsa-2efca170947f3cd7.yaml diff --git a/releasenotes/notes/sampler_as_positional_arg_for_qnspsa-2efca170947f3cd7.yaml b/releasenotes/notes/sampler_as_positional_arg_for_qnspsa-2efca170947f3cd7.yaml new file mode 100644 index 00000000..6b2e7fda --- /dev/null +++ b/releasenotes/notes/sampler_as_positional_arg_for_qnspsa-2efca170947f3cd7.yaml @@ -0,0 +1,26 @@ +--- +prelude: > + The QNSPSA.get_fidelity function had some deprecated arguments, which + just got cleaned up. +features: + - | + Allowing to call the QNSPSA.get_fidelity function as + `QNSPSA(ansatz, sampler)` with the `sampler` option as a positional + argument (instead of keyword argument). + - | + Do not allow the `sampler` option to be `None`. +issues: + - | + The original module for QNSPSA allowed the use the `get_fidelity()` + function without a `sampler` (`sampler=None`), which is deprecated in the + current version of the code. +upgrade: + - | + The list of arguments for `QNSPSA.get_fidelity()` has been updated to + include only the `ansatz` and the `sampler`, removing previous deprecated + options, and enforcing a non `None` value for the `sampler`. +deprecations: + - | + The `None` value for the `sampler` on the `QNSPSA.get_fidelity()` function + is no longer valid. A proper sampler must be provided. + From 140cdec40ca8f93050e42ba257cdf4d5e3183b31 Mon Sep 17 00:00:00 2001 From: Julen Larrucea Date: Mon, 10 Mar 2025 22:22:52 +0100 Subject: [PATCH 4/7] Updated release notes according to comments from @woodsp-imb --- ...ional_arg_for_qnspsa-2efca170947f3cd7.yaml | 25 +++---------------- 1 file changed, 3 insertions(+), 22 deletions(-) diff --git a/releasenotes/notes/sampler_as_positional_arg_for_qnspsa-2efca170947f3cd7.yaml b/releasenotes/notes/sampler_as_positional_arg_for_qnspsa-2efca170947f3cd7.yaml index 6b2e7fda..08f5c3aa 100644 --- a/releasenotes/notes/sampler_as_positional_arg_for_qnspsa-2efca170947f3cd7.yaml +++ b/releasenotes/notes/sampler_as_positional_arg_for_qnspsa-2efca170947f3cd7.yaml @@ -1,26 +1,7 @@ --- -prelude: > - The QNSPSA.get_fidelity function had some deprecated arguments, which - just got cleaned up. -features: - - | - Allowing to call the QNSPSA.get_fidelity function as - `QNSPSA(ansatz, sampler)` with the `sampler` option as a positional - argument (instead of keyword argument). - - | - Do not allow the `sampler` option to be `None`. -issues: - - | - The original module for QNSPSA allowed the use the `get_fidelity()` - function without a `sampler` (`sampler=None`), which is deprecated in the - current version of the code. upgrade: - | The list of arguments for `QNSPSA.get_fidelity()` has been updated to - include only the `ansatz` and the `sampler`, removing previous deprecated - options, and enforcing a non `None` value for the `sampler`. -deprecations: - - | - The `None` value for the `sampler` on the `QNSPSA.get_fidelity()` function - is no longer valid. A proper sampler must be provided. - + include only the `ansatz` and the `sampler`, and removing previous + wildcard option. From now on, the sampler option must be explicitly + especified, as a positional or keyword argument, and it cannot be `None`. From 0a90153ffd20451e322e187994b5694b99e2feca Mon Sep 17 00:00:00 2001 From: Julen Larrucea Date: Wed, 19 Mar 2025 11:32:03 +0100 Subject: [PATCH 5/7] Update releasenotes/notes/sampler_as_positional_arg_for_qnspsa-2efca170947f3cd7.yaml Fixed typo Co-authored-by: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com> --- .../sampler_as_positional_arg_for_qnspsa-2efca170947f3cd7.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/releasenotes/notes/sampler_as_positional_arg_for_qnspsa-2efca170947f3cd7.yaml b/releasenotes/notes/sampler_as_positional_arg_for_qnspsa-2efca170947f3cd7.yaml index 08f5c3aa..7c2ba08b 100644 --- a/releasenotes/notes/sampler_as_positional_arg_for_qnspsa-2efca170947f3cd7.yaml +++ b/releasenotes/notes/sampler_as_positional_arg_for_qnspsa-2efca170947f3cd7.yaml @@ -4,4 +4,4 @@ upgrade: The list of arguments for `QNSPSA.get_fidelity()` has been updated to include only the `ansatz` and the `sampler`, and removing previous wildcard option. From now on, the sampler option must be explicitly - especified, as a positional or keyword argument, and it cannot be `None`. + specified, as a positional or keyword argument, and it cannot be `None`. From 406a1799b2ff0f2df66c647f33f075264376486e Mon Sep 17 00:00:00 2001 From: Julen Larrucea Date: Thu, 20 Mar 2025 11:05:46 +0100 Subject: [PATCH 6/7] Updated copyright at qnspsa.py Updated the Copyright year --- qiskit_algorithms/optimizers/qnspsa.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qiskit_algorithms/optimizers/qnspsa.py b/qiskit_algorithms/optimizers/qnspsa.py index d8d27f2d..3a0ea8e0 100644 --- a/qiskit_algorithms/optimizers/qnspsa.py +++ b/qiskit_algorithms/optimizers/qnspsa.py @@ -1,6 +1,6 @@ # This code is part of a Qiskit project. # -# (C) Copyright IBM 2021, 2024. +# (C) Copyright IBM 2021, 2025. # # 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 From 32d97f581caeb0d6e40f8c70368dc0bfd40693cf Mon Sep 17 00:00:00 2001 From: Julen Larrucea Date: Fri, 28 Mar 2025 11:29:57 +0100 Subject: [PATCH 7/7] Update releasenotes/notes/sampler_as_positional_arg_for_qnspsa-2efca170947f3cd7.yaml A more accurate rephrasing of the sentence, avoiding the misleading term "wildcard". Co-authored-by: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com> --- ...mpler_as_positional_arg_for_qnspsa-2efca170947f3cd7.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/releasenotes/notes/sampler_as_positional_arg_for_qnspsa-2efca170947f3cd7.yaml b/releasenotes/notes/sampler_as_positional_arg_for_qnspsa-2efca170947f3cd7.yaml index 7c2ba08b..83155506 100644 --- a/releasenotes/notes/sampler_as_positional_arg_for_qnspsa-2efca170947f3cd7.yaml +++ b/releasenotes/notes/sampler_as_positional_arg_for_qnspsa-2efca170947f3cd7.yaml @@ -2,6 +2,6 @@ upgrade: - | The list of arguments for `QNSPSA.get_fidelity()` has been updated to - include only the `ansatz` and the `sampler`, and removing previous - wildcard option. From now on, the sampler option must be explicitly - specified, as a positional or keyword argument, and it cannot be `None`. + remove the keyword enforcement for `sampler` so also allowing it now + as positional. Additionally the `sampler` must be explicitly specified, as a + valid object, as it no longer supports `None` nor defaults to that.