From 76da497a5d59c6535900baf3e05c48dc74833fbe Mon Sep 17 00:00:00 2001 From: Manoel Marques Date: Tue, 27 Sep 2022 16:44:09 -0400 Subject: [PATCH] Pending deprecate algorithms (#8703) * Pending deprecate algorithms * Update releasenotes/notes/pending-deprecate-min-eigen-solvers-fa4341e1014e4df0.yaml Co-authored-by: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com> Co-authored-by: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> --- .../minimum_eigen_solver.py | 36 +++++++++++++++-- .../numpy_minimum_eigen_solver.py | 22 +++++++++- .../algorithms/minimum_eigen_solvers/qaoa.py | 40 +++++++++++++------ .../algorithms/minimum_eigen_solvers/vqe.py | 40 +++++++++++++++++-- 4 files changed, 117 insertions(+), 21 deletions(-) diff --git a/qiskit/algorithms/minimum_eigen_solvers/minimum_eigen_solver.py b/qiskit/algorithms/minimum_eigen_solvers/minimum_eigen_solver.py index 7cdd245b7076..373876a9e046 100644 --- a/qiskit/algorithms/minimum_eigen_solvers/minimum_eigen_solver.py +++ b/qiskit/algorithms/minimum_eigen_solvers/minimum_eigen_solver.py @@ -1,6 +1,6 @@ # This code is part of Qiskit. # -# (C) Copyright IBM 2020. +# (C) Copyright IBM 2020, 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 @@ -18,18 +18,34 @@ import numpy as np from qiskit.opflow import OperatorBase +from qiskit.utils.deprecation import deprecate_function from ..algorithm_result import AlgorithmResult from ..list_or_dict import ListOrDict class MinimumEigensolver(ABC): - """The Minimum Eigensolver Interface. + """Pending deprecation: Minimum Eigensolver Interface. + + The Minimum Eigensolver interface has been superseded by the + :class:`qiskit.algorithms.minimum_eigensolvers.MinimumEigensolver` interface. + This interface will be deprecated in a future release and subsequently + removed after that. Algorithms that can compute a minimum eigenvalue for an operator may implement this interface to allow different algorithms to be used interchangeably. """ + @deprecate_function( + "The Minimum Eigensolver interface has been superseded by the " + "qiskit.algorithms.minimum_eigensolvers.MinimumEigensolver interface. " + "This interface will be deprecated in a future release and subsequently " + "removed after that.", + category=PendingDeprecationWarning, + ) + def __init__(self) -> None: + pass + @abstractmethod def compute_minimum_eigenvalue( self, operator: OperatorBase, aux_operators: Optional[ListOrDict[OperatorBase]] = None @@ -67,8 +83,22 @@ def supports_aux_operators(cls) -> bool: class MinimumEigensolverResult(AlgorithmResult): - """Minimum Eigensolver Result.""" + """Pending deprecation: Minimum Eigensolver Result. + + The MinimumEigensolverResult class has been superseded by the + :class:`qiskit.algorithms.minimum_eigensolvers.MinimumEigensolverResult` class. + This class will be deprecated in a future release and subsequently + removed after that. + + """ + @deprecate_function( + "The MinimumEigensolverResult class has been superseded by the " + "qiskit.algorithms.minimum_eigensolvers.MinimumEigensolverResult class. " + "This class will be deprecated in a future release and subsequently " + "removed after that.", + category=PendingDeprecationWarning, + ) def __init__(self) -> None: super().__init__() self._eigenvalue = None diff --git a/qiskit/algorithms/minimum_eigen_solvers/numpy_minimum_eigen_solver.py b/qiskit/algorithms/minimum_eigen_solvers/numpy_minimum_eigen_solver.py index 5515ba92d745..191a130a0163 100644 --- a/qiskit/algorithms/minimum_eigen_solvers/numpy_minimum_eigen_solver.py +++ b/qiskit/algorithms/minimum_eigen_solvers/numpy_minimum_eigen_solver.py @@ -1,6 +1,6 @@ # This code is part of Qiskit. # -# (C) Copyright IBM 2020. +# (C) Copyright IBM 2020, 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 @@ -14,9 +14,11 @@ from typing import List, Optional, Union, Callable import logging +import warnings import numpy as np from qiskit.opflow import OperatorBase +from qiskit.utils.deprecation import deprecate_function from ..eigen_solvers.numpy_eigen_solver import NumPyEigensolver from .minimum_eigen_solver import MinimumEigensolver, MinimumEigensolverResult from ..list_or_dict import ListOrDict @@ -26,9 +28,22 @@ class NumPyMinimumEigensolver(MinimumEigensolver): """ - The Numpy Minimum Eigensolver algorithm. + Pending deprecation: Numpy Minimum Eigensolver algorithm. + + The NumPyMinimumEigensolver class has been superseded by the + :class:`qiskit.algorithms.minimum_eigensolvers.NumPyMinimumEigensolver` class. + This class will be deprecated in a future release and subsequently + removed after that. + """ + @deprecate_function( + "The NumPyMinimumEigensolver class has been superseded by the " + "qiskit.algorithms.minimum_eigensolvers.NumPyMinimumEigensolver class. " + "This class will be deprecated in a future release and subsequently " + "removed after that.", + category=PendingDeprecationWarning, + ) def __init__( self, filter_criterion: Callable[ @@ -44,6 +59,9 @@ def __init__( whether to consider this value or not. If there is no feasible element, the result can even be empty. """ + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + super().__init__() self._ces = NumPyEigensolver(filter_criterion=filter_criterion) self._ret = MinimumEigensolverResult() diff --git a/qiskit/algorithms/minimum_eigen_solvers/qaoa.py b/qiskit/algorithms/minimum_eigen_solvers/qaoa.py index d537f5a892e7..e650ee349cb1 100644 --- a/qiskit/algorithms/minimum_eigen_solvers/qaoa.py +++ b/qiskit/algorithms/minimum_eigen_solvers/qaoa.py @@ -13,6 +13,7 @@ """ The Quantum Approximate Optimization Algorithm. """ from typing import List, Callable, Optional, Union +import warnings import numpy as np from qiskit.algorithms.optimizers import Minimizer, Optimizer @@ -22,13 +23,19 @@ from qiskit.providers import Backend from qiskit.utils.quantum_instance import QuantumInstance from qiskit.utils.validation import validate_min +from qiskit.utils.deprecation import deprecate_function from qiskit.circuit.library.n_local.qaoa_ansatz import QAOAAnsatz from qiskit.algorithms.minimum_eigen_solvers.vqe import VQE class QAOA(VQE): """ - The Quantum Approximate Optimization Algorithm. + Pending deprecation: Quantum Approximate Optimization Algorithm. + + The QAOA class has been superseded by the + :class:`qiskit.algorithms.minimum_eigensolvers.QAOA` class. + This class will be deprecated in a future release and subsequently + removed after that. `QAOA `__ is a well-known algorithm for finding approximate solutions to combinatorial-optimization problems. @@ -52,6 +59,13 @@ class QAOA(VQE): the evolution to a feasible subspace of the full Hilbert space. """ + @deprecate_function( + "The QAOA class has been superseded by the " + "qiskit.algorithms.minimum_eigensolvers.QAOA class. " + "This class will be deprecated in a future release and subsequently " + "removed after that.", + category=PendingDeprecationWarning, + ) def __init__( self, optimizer: Optional[Union[Optimizer, Minimizer]] = None, @@ -114,17 +128,19 @@ def __init__( self._initial_state = initial_state self._cost_operator = None - super().__init__( - ansatz=None, - optimizer=optimizer, - initial_point=initial_point, - gradient=gradient, - expectation=expectation, - include_custom=include_custom, - max_evals_grouped=max_evals_grouped, - callback=callback, - quantum_instance=quantum_instance, - ) + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + super().__init__( + ansatz=None, + optimizer=optimizer, + initial_point=initial_point, + gradient=gradient, + expectation=expectation, + include_custom=include_custom, + max_evals_grouped=max_evals_grouped, + callback=callback, + quantum_instance=quantum_instance, + ) def _check_operator_ansatz(self, operator: OperatorBase) -> OperatorBase: # Recreates a circuit based on operator parameter. diff --git a/qiskit/algorithms/minimum_eigen_solvers/vqe.py b/qiskit/algorithms/minimum_eigen_solvers/vqe.py index 87ab8707b0f1..286298620420 100755 --- a/qiskit/algorithms/minimum_eigen_solvers/vqe.py +++ b/qiskit/algorithms/minimum_eigen_solvers/vqe.py @@ -18,6 +18,7 @@ from __future__ import annotations import logging +import warnings from time import time from typing import Callable, Dict, List, Optional, Tuple, Union @@ -40,6 +41,7 @@ from qiskit.utils import QuantumInstance, algorithm_globals from qiskit.utils.backend_utils import is_aer_provider from qiskit.utils.validation import validate_min +from qiskit.utils.deprecation import deprecate_function from ..aux_ops_evaluator import eval_observables from ..exceptions import AlgorithmError @@ -52,7 +54,12 @@ class VQE(VariationalAlgorithm, MinimumEigensolver): - r"""The Variational Quantum Eigensolver algorithm. + r"""Pending deprecation: Variational Quantum Eigensolver algorithm. + + The VQE class has been superseded by the + :class:`qiskit.algorithms.minimum_eigensolvers.VQE` class. + This class will be deprecated in a future release and subsequently + removed after that. `VQE `__ is a quantum algorithm that uses a variational technique to find @@ -120,6 +127,13 @@ def my_minimizer(fun, x0, jac=None, bounds=None) -> OptimizerResult: """ + @deprecate_function( + "The VQE class has been superseded by the " + "qiskit.algorithms.minimum_eigensolvers.VQE class. " + "This class will be deprecated in a future release and subsequently " + "removed after that.", + category=PendingDeprecationWarning, + ) def __init__( self, ansatz: Optional[QuantumCircuit] = None, @@ -171,7 +185,9 @@ def __init__( """ validate_min("max_evals_grouped", max_evals_grouped, 1) - super().__init__() + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + super().__init__() self._max_evals_grouped = max_evals_grouped self._circuit_sampler = None # type: Optional[CircuitSampler] @@ -641,10 +657,26 @@ def _get_eigenstate(self, optimal_parameters) -> Union[List[float], Dict[str, in class VQEResult(VariationalResult, MinimumEigensolverResult): - """VQE Result.""" + """Pending deprecation: VQE Result. + + The VQEResult class has been superseded by the + :class:`qiskit.algorithms.minimum_eigensolvers.VQEResult` class. + This class will be deprecated in a future release and subsequently + removed after that. + + """ + @deprecate_function( + "The VQEResult class has been superseded by the " + "qiskit.algorithms.minimum_eigensolvers.VQEResult class. " + "This class will be deprecated in a future release and subsequently " + "removed after that.", + category=PendingDeprecationWarning, + ) def __init__(self) -> None: - super().__init__() + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + super().__init__() self._cost_function_evals = None @property