@@ -65,9 +65,19 @@ def test_aer_simulator_import(self):
6565 self .assertIsNotNone (backend )
6666
6767 def test_qiskit_algorithms_optimizers (self ):
68- from qiskit_algorithms .optimizers import COBYLA , SPSA
68+ from qiskit_algorithms .optimizers import COBYLA , SPSA , QNSPSA
6969 self .assertIsNotNone (COBYLA )
7070 self .assertIsNotNone (SPSA )
71+ self .assertIsNotNone (QNSPSA )
72+
73+ def test_sampler_v2_for_qnspsa (self ):
74+ """The SamplerV2 used for QNSPSA fidelity must be a BaseSamplerV2 instance."""
75+ from qiskit .primitives import BaseSamplerV2
76+ try :
77+ from qiskit_aer .primitives import SamplerV2 as _SamplerV2
78+ except ImportError :
79+ from qiskit .primitives import StatevectorSampler as _SamplerV2
80+ self .assertTrue (issubclass (_SamplerV2 , BaseSamplerV2 ))
7181
7282 def test_qaoa_package_import (self ):
7383 import qaoa
@@ -153,6 +163,30 @@ def test_hist_method(self):
153163 total_shots = sum (hist .values ())
154164 self .assertEqual (total_shots , 256 )
155165
166+ def test_qnspsa_optimizer (self ):
167+ """QNSPSA optimizer must work with a BaseSamplerV2-compatible sampler.
168+
169+ This tests the fix for the Qiskit 2.x incompatibility where
170+ QNSPSA.get_fidelity() requires a BaseSamplerV2 instance.
171+ """
172+ from qaoa import QAOA
173+ from qaoa import problems , mixers , initialstates
174+ from qiskit_algorithms .optimizers import QNSPSA
175+
176+ problem = problems .MaxCut (self .G )
177+ qaoa_inst = QAOA (
178+ problem ,
179+ mixers .X (),
180+ initialstates .Plus (),
181+ backend = self .backend ,
182+ optimizer = [QNSPSA , {"maxiter" : 3 }],
183+ shots = 256 ,
184+ )
185+ # Should not raise ValueError about BaseSamplerV2
186+ qaoa_inst .optimize (depth = 1 , angles = self .angles )
187+ best_exp = qaoa_inst .get_Exp (depth = 1 )
188+ self .assertIsInstance (best_exp , float )
189+
156190
157191class TestQAOAComponents (unittest .TestCase ):
158192 """Verify individual QAOA components work with Qiskit 2.x."""
0 commit comments