diff --git a/test/python/algorithms/eigensolvers/test_vqd.py b/test/python/algorithms/eigensolvers/test_vqd.py index 295308ca0aff..16652a259ebd 100644 --- a/test/python/algorithms/eigensolvers/test_vqd.py +++ b/test/python/algorithms/eigensolvers/test_vqd.py @@ -10,7 +10,7 @@ # copyright notice, and modified files need to carry a notice indicating # that they have been altered from the originals. -""" Test VQD """ +"""Test VQD""" import unittest from test.python.algorithms import QiskitAlgorithmsTestCase @@ -19,13 +19,9 @@ from ddt import data, ddt from qiskit import QuantumCircuit -from qiskit.algorithms.eigensolvers import VQD +from qiskit.algorithms.eigensolvers import VQD, VQDResult from qiskit.algorithms import AlgorithmError -from qiskit.algorithms.optimizers import ( - COBYLA, - L_BFGS_B, - SLSQP, -) +from qiskit.algorithms.optimizers import COBYLA, L_BFGS_B, SLSQP, SPSA from qiskit.algorithms.state_fidelities import ComputeUncompute from qiskit.circuit.library import TwoLocal, RealAmplitudes from qiskit.opflow import PauliSumOp @@ -207,6 +203,7 @@ def store_intermediate_result(eval_count, parameters, mean, metadata, step): @data(H2_PAULI, H2_OP, H2_SPARSE_PAULI) def test_vqd_optimizer(self, op): """Test running same VQD twice to re-use optimizer, then switch optimizer""" + vqd = VQD( estimator=self.estimator, fidelity=self.fidelity, @@ -231,6 +228,17 @@ def run_check(): vqd.optimizer = L_BFGS_B() run_check() + with self.subTest("Batched optimizer replace"): + vqd.optimizer = SLSQP(maxiter=60, max_evals_grouped=10) + run_check() + + with self.subTest("SPSA replace"): + # SPSA takes too long to converge, so we will + # only check that it runs with no errors. + vqd.optimizer = SPSA(maxiter=5, learning_rate=0.01, perturbation=0.01) + result = vqd.compute_eigenvalues(operator=op) + self.assertIsInstance(result, VQDResult) + @data(H2_PAULI, H2_OP, H2_SPARSE_PAULI) def test_optimizer_list(self, op): """Test sending an optimizer list""" diff --git a/test/python/algorithms/gradients/test_estimator_gradient.py b/test/python/algorithms/gradients/test_estimator_gradient.py index ae6dbfa2930d..c84450313b61 100644 --- a/test/python/algorithms/gradients/test_estimator_gradient.py +++ b/test/python/algorithms/gradients/test_estimator_gradient.py @@ -1,6 +1,6 @@ # This code is part of Qiskit. # -# (C) Copyright IBM 2019, 2023. +# (C) Copyright IBM 2019, 2021. # # 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 @@ -14,7 +14,7 @@ """Test Estimator Gradients""" import unittest -from test.python.algorithms.gradients.logging_primitives import LoggingEstimator + import numpy as np from ddt import ddt, data, unpack @@ -35,6 +35,8 @@ from qiskit.quantum_info.random import random_pauli_list from qiskit.test import QiskitTestCase +from .logging_primitives import LoggingEstimator + gradient_factories = [ lambda estimator: FiniteDiffEstimatorGradient(estimator, epsilon=1e-6, method="central"), lambda estimator: FiniteDiffEstimatorGradient(estimator, epsilon=1e-6, method="forward"), diff --git a/test/python/algorithms/gradients/test_qgt.py b/test/python/algorithms/gradients/test_qgt.py index df8eecdd8478..74f22d462d7c 100644 --- a/test/python/algorithms/gradients/test_qgt.py +++ b/test/python/algorithms/gradients/test_qgt.py @@ -14,7 +14,6 @@ """Test QGT.""" import unittest -from test.python.algorithms.gradients.logging_primitives import LoggingEstimator from ddt import ddt, data import numpy as np @@ -26,6 +25,8 @@ from qiskit.primitives import Estimator from qiskit.test import QiskitTestCase +from .logging_primitives import LoggingEstimator + @ddt class TestQGT(QiskitTestCase): diff --git a/test/python/algorithms/gradients/test_sampler_gradient.py b/test/python/algorithms/gradients/test_sampler_gradient.py index 2c9f93a9f17e..b4caac078502 100644 --- a/test/python/algorithms/gradients/test_sampler_gradient.py +++ b/test/python/algorithms/gradients/test_sampler_gradient.py @@ -1,6 +1,6 @@ # This code is part of Qiskit. # -# (C) Copyright IBM 2019, 2023. +# (C) Copyright IBM 2019, 2021. # # 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 @@ -15,9 +15,10 @@ import unittest from typing import List -from test.python.algorithms.gradients.logging_primitives import LoggingSampler + import numpy as np from ddt import ddt, data + from qiskit import QuantumCircuit from qiskit.algorithms.gradients import ( FiniteDiffSamplerGradient, @@ -32,6 +33,8 @@ from qiskit.result import QuasiDistribution from qiskit.test import QiskitTestCase +from .logging_primitives import LoggingSampler + gradient_factories = [ lambda sampler: FiniteDiffSamplerGradient(sampler, epsilon=1e-6, method="central"), lambda sampler: FiniteDiffSamplerGradient(sampler, epsilon=1e-6, method="forward"), diff --git a/test/python/algorithms/minimum_eigensolvers/test_sampling_vqe.py b/test/python/algorithms/minimum_eigensolvers/test_sampling_vqe.py index 27cf6c46cfb0..8e364e3800c4 100644 --- a/test/python/algorithms/minimum_eigensolvers/test_sampling_vqe.py +++ b/test/python/algorithms/minimum_eigensolvers/test_sampling_vqe.py @@ -1,6 +1,6 @@ # This code is part of Qiskit. # -# (C) Copyright IBM 2018, 2023. +# (C) Copyright IBM 2018, 2021. # # 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 @@ -12,6 +12,7 @@ """Test the Sampler VQE.""" + import unittest from functools import partial from test.python.algorithms import QiskitAlgorithmsTestCase diff --git a/test/python/algorithms/time_evolvers/variational/test_var_qite.py b/test/python/algorithms/time_evolvers/variational/test_var_qite.py index 636798d7947e..9c79a0024885 100644 --- a/test/python/algorithms/time_evolvers/variational/test_var_qite.py +++ b/test/python/algorithms/time_evolvers/variational/test_var_qite.py @@ -99,8 +99,8 @@ def test_run_d_1_with_aux_ops(self): var_qite = VarQITE( ansatz, init_param_values, var_principle, estimator, num_timesteps=25 ) - evolution_result = var_qite.evolve(evolution_problem) + aux_ops = evolution_result.aux_ops_evaluated parameter_values = evolution_result.parameter_values[-1] @@ -128,6 +128,7 @@ def test_run_d_1_with_aux_ops(self): ansatz, init_param_values, var_principle, estimator, num_timesteps=25 ) evolution_result = var_qite.evolve(evolution_problem) + aux_ops = evolution_result.aux_ops_evaluated parameter_values = evolution_result.parameter_values[-1] diff --git a/test/python/algorithms/time_evolvers/variational/test_var_qrte.py b/test/python/algorithms/time_evolvers/variational/test_var_qrte.py index 1682c60b842b..b7dfaf59f12b 100644 --- a/test/python/algorithms/time_evolvers/variational/test_var_qrte.py +++ b/test/python/algorithms/time_evolvers/variational/test_var_qrte.py @@ -131,6 +131,7 @@ def test_run_d_1_with_aux_ops(self): ansatz, init_param_values, var_principle, estimator, num_timesteps=25 ) evolution_result = var_qrte.evolve(evolution_problem) + aux_ops = evolution_result.aux_ops_evaluated parameter_values = evolution_result.parameter_values[-1] @@ -158,6 +159,7 @@ def test_run_d_1_with_aux_ops(self): ansatz, init_param_values, var_principle, estimator, num_timesteps=25 ) evolution_result = var_qrte.evolve(evolution_problem) + aux_ops = evolution_result.aux_ops_evaluated parameter_values = evolution_result.parameter_values[-1] diff --git a/test/python/circuit/library/test_qft.py b/test/python/circuit/library/test_qft.py index 49e34736616d..fa5305aa3631 100644 --- a/test/python/circuit/library/test_qft.py +++ b/test/python/circuit/library/test_qft.py @@ -13,6 +13,7 @@ """Test library of QFT circuits.""" import unittest +import warnings import numpy as np from ddt import ddt, data, unpack @@ -184,11 +185,18 @@ def __init__(self, *_args, **_kwargs): # We don't want to issue a warning on mutation until we know that the values are # finalised; this is because a user might want to mutate the number of qubits and the # approximation degree. In these cases, wait until we try to build the circuit. - with self.assertWarns(DeprecationWarning): + with warnings.catch_warnings(record=True) as caught_warnings: + warnings.filterwarnings( + "always", + category=RuntimeWarning, + module=r"qiskit\..*", + message=r".*precision loss in QFT.*", + ) qft = QFT() # Even with the approximation this will trigger the warning. qft.num_qubits = 1080 qft.approximation_degree = 20 + self.assertFalse(caught_warnings) # Short-circuit the build method so it exits after input validation, but without actually # spinning the CPU to build a huge, useless object. diff --git a/test/python/opflow/opflow_test_case.py b/test/python/opflow/opflow_test_case.py index ebd0d5d6b377..ef8d8c6824c7 100644 --- a/test/python/opflow/opflow_test_case.py +++ b/test/python/opflow/opflow_test_case.py @@ -1,6 +1,6 @@ # This code is part of Qiskit. # -# (C) Copyright IBM 2018, 2023. +# (C) Copyright IBM 2018, 2020. # # 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 @@ -12,7 +12,6 @@ """Opflow Test Case""" -import warnings from qiskit.test import QiskitTestCase diff --git a/test/python/primitives/test_estimator.py b/test/python/primitives/test_estimator.py index 73f20f449d71..4a9209e7fa8a 100644 --- a/test/python/primitives/test_estimator.py +++ b/test/python/primitives/test_estimator.py @@ -1,6 +1,6 @@ # This code is part of Qiskit. # -# (C) Copyright IBM 2022, 2023. +# (C) Copyright IBM 2022. # # 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 @@ -13,6 +13,7 @@ """Tests for Estimator.""" import unittest + import numpy as np from ddt import data, ddt, unpack