From 5f8208f40c9548c88dbbd6a85ef520fc36a56415 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elena=20Pe=C3=B1a=20Tapia?= Date: Wed, 15 Nov 2023 17:15:53 +0100 Subject: [PATCH] Remove alg_globals and validation --- qiskit/utils/__init__.py | 6 +- qiskit/utils/algorithm_globals.py | 170 -------------- qiskit/utils/validation.py | 211 ------------------ ...move-algorithm-utils-707648b69af439dc.yaml | 14 ++ 4 files changed, 15 insertions(+), 386 deletions(-) delete mode 100644 qiskit/utils/algorithm_globals.py delete mode 100644 qiskit/utils/validation.py create mode 100644 releasenotes/notes/remove-algorithm-utils-707648b69af439dc.yaml diff --git a/qiskit/utils/__init__.py b/qiskit/utils/__init__.py index 45200413ba2a..08f1fe8414dd 100644 --- a/qiskit/utils/__init__.py +++ b/qiskit/utils/__init__.py @@ -1,6 +1,6 @@ # This code is part of Qiskit. # -# (C) Copyright IBM 2018, 2021. +# (C) Copyright IBM 2018, 2023. # # 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 @@ -38,7 +38,6 @@ .. autofunction:: has_ibmq .. autofunction:: has_aer .. autofunction:: name_args -.. autodata:: algorithm_globals .. autosummary:: :toctree: ../stubs/ @@ -78,8 +77,6 @@ from .entangler_map import get_entangler_map, validate_entangler_map from .backend_utils import has_ibmq, has_aer from .name_unnamed_args import name_args -from .algorithm_globals import algorithm_globals - __all__ = [ "LazyDependencyManager", @@ -92,7 +89,6 @@ "has_ibmq", "has_aer", "name_args", - "algorithm_globals", "add_deprecation_to_docstring", "deprecate_arg", "deprecate_arguments", diff --git a/qiskit/utils/algorithm_globals.py b/qiskit/utils/algorithm_globals.py deleted file mode 100644 index eb1dd4f492e0..000000000000 --- a/qiskit/utils/algorithm_globals.py +++ /dev/null @@ -1,170 +0,0 @@ -# This code is part of Qiskit. -# -# (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 -# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. -# -# Any modifications or derivative works of this code must retain this -# copyright notice, and modified files need to carry a notice indicating -# that they have been altered from the originals. - -"""Algorithm Globals""" - -from typing import Optional -import logging - -import numpy as np - -from qiskit.tools import parallel -from qiskit.utils.deprecation import deprecate_func -from ..user_config import get_config -from ..exceptions import QiskitError - - -logger = logging.getLogger(__name__) - - -class QiskitAlgorithmGlobals: - """Class for global properties.""" - - CPU_COUNT = parallel.local_hardware_info()["cpus"] - - @deprecate_func( - additional_msg=( - "This algorithm utility has been migrated to an independent package: " - "https://github.com/qiskit-community/qiskit-algorithms. You can run " - "``pip install qiskit_algorithms`` and import ``from qiskit_algorithms.utils`` instead. " - ), - since="0.45.0", - ) - def __init__(self) -> None: - self._random_seed = None # type: Optional[int] - self._num_processes = QiskitAlgorithmGlobals.CPU_COUNT - self._random = None - self._massive = False - try: - settings = get_config() - self.num_processes = settings.get("num_processes", QiskitAlgorithmGlobals.CPU_COUNT) - except Exception as ex: # pylint: disable=broad-except - logger.debug("User Config read error %s", str(ex)) - - @property - @deprecate_func( - additional_msg=( - "This algorithm utility has been migrated to an independent package: " - "https://github.com/qiskit-community/qiskit-algorithms. You can run " - "``pip install qiskit_algorithms`` and import ``from qiskit_algorithms.utils`` instead. " - ), - since="0.45.0", - is_property=True, - ) - def random_seed(self) -> Optional[int]: - """Return random seed.""" - return self._random_seed - - @random_seed.setter - @deprecate_func( - additional_msg=( - "This algorithm utility has been migrated to an independent package: " - "https://github.com/qiskit-community/qiskit-algorithms. You can run " - "``pip install qiskit_algorithms`` and import ``from qiskit_algorithms.utils`` instead. " - ), - since="0.45.0", - is_property=True, - ) - def random_seed(self, seed: Optional[int]) -> None: - """Set random seed.""" - self._random_seed = seed - self._random = None - - @property - @deprecate_func( - additional_msg=( - "This algorithm utility belongs to a legacy workflow and has no replacement." - ), - since="0.45.0", - is_property=True, - ) - def num_processes(self) -> int: - """Return num processes.""" - return self._num_processes - - @num_processes.setter - @deprecate_func( - additional_msg=( - "This algorithm utility belongs to a legacy workflow and has no replacement." - ), - since="0.45.0", - is_property=True, - ) - def num_processes(self, num_processes: Optional[int]) -> None: - """Set num processes. - If 'None' is passed, it resets to QiskitAlgorithmGlobals.CPU_COUNT - """ - if num_processes is None: - num_processes = QiskitAlgorithmGlobals.CPU_COUNT - elif num_processes < 1: - raise QiskitError(f"Invalid Number of Processes {num_processes}.") - elif num_processes > QiskitAlgorithmGlobals.CPU_COUNT: - raise QiskitError( - "Number of Processes {} cannot be greater than cpu count {}.".format( - num_processes, QiskitAlgorithmGlobals.CPU_COUNT - ) - ) - self._num_processes = num_processes - # TODO: change Terra CPU_COUNT until issue - # gets resolved: https://github.com/Qiskit/qiskit-terra/issues/1963 - try: - parallel.CPU_COUNT = self.num_processes - except Exception as ex: # pylint: disable=broad-except - logger.warning( - "Failed to set qiskit.tools.parallel.CPU_COUNT to value: '%s': Error: '%s'", - self.num_processes, - str(ex), - ) - - @property - @deprecate_func( - additional_msg=( - "This algorithm utility has been migrated to an independent package: " - "https://github.com/qiskit-community/qiskit-algorithms. You can run " - "``pip install qiskit_algorithms`` and import ``from qiskit_algorithms.utils`` instead. " - ), - since="0.45.0", - is_property=True, - ) - def random(self) -> np.random.Generator: - """Return a numpy np.random.Generator (default_rng).""" - if self._random is None: - self._random = np.random.default_rng(self._random_seed) - return self._random - - @property - @deprecate_func( - additional_msg=( - "This algorithm utility belongs to a legacy workflow and has no replacement." - ), - since="0.45.0", - is_property=True, - ) - def massive(self) -> bool: - """Return massive to allow processing of large matrices or vectors.""" - return self._massive - - @massive.setter - @deprecate_func( - additional_msg=( - "This algorithm utility belongs to a legacy workflow and has no replacement." - ), - since="0.45.0", - is_property=True, - ) - def massive(self, massive: bool) -> None: - """Set massive to allow processing of large matrices or vectors.""" - self._massive = massive - - -# Global instance to be used as the entry point for globals. -algorithm_globals = QiskitAlgorithmGlobals() diff --git a/qiskit/utils/validation.py b/qiskit/utils/validation.py deleted file mode 100644 index 0d3d59340aae..000000000000 --- a/qiskit/utils/validation.py +++ /dev/null @@ -1,211 +0,0 @@ -# This code is part of Qiskit. -# -# (C) Copyright IBM 2019, 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 -# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. -# -# Any modifications or derivative works of this code must retain this -# copyright notice, and modified files need to carry a notice indicating -# that they have been altered from the originals. - -""" -Validation module -""" - -from typing import Set -from qiskit.utils.deprecation import deprecate_func - - -@deprecate_func( - additional_msg=( - "This algorithm utility has been migrated to an independent package: " - "https://github.com/qiskit-community/qiskit-algorithms. You can run " - "``pip install qiskit_algorithms`` and import ``from qiskit_algorithms.utils`` instead. " - ), - since="0.45.0", -) -def validate_in_set(name: str, value: object, values: Set[object]) -> None: - """ - Args: - name: value name. - value: value to check. - values: set that should contain value. - Raises: - ValueError: invalid value - """ - if value not in values: - raise ValueError(f"{name} must be one of '{values}', was '{value}'.") - - -@deprecate_func( - additional_msg=( - "This algorithm utility has been migrated to an independent package: " - "https://github.com/qiskit-community/qiskit-algorithms. You can run " - "``pip install qiskit_algorithms`` and import ``from qiskit_algorithms.utils`` instead. " - ), - since="0.45.0", -) -def validate_min(name: str, value: float, minimum: float) -> None: - """ - Args: - name: value name. - value: value to check. - minimum: minimum value allowed. - Raises: - ValueError: invalid value - """ - if value < minimum: - raise ValueError(f"{name} must have value >= {minimum}, was {value}") - - -@deprecate_func( - additional_msg=( - "This algorithm utility has been migrated to an independent package: " - "https://github.com/qiskit-community/qiskit-algorithms. You can run " - "``pip install qiskit_algorithms`` and import ``from qiskit_algorithms.utils`` instead. " - ), - since="0.45.0", -) -def validate_min_exclusive(name: str, value: float, minimum: float) -> None: - """ - Args: - name: value name. - value: value to check. - minimum: minimum value allowed. - Raises: - ValueError: invalid value - """ - if value <= minimum: - raise ValueError(f"{name} must have value > {minimum}, was {value}") - - -@deprecate_func( - additional_msg=( - "This algorithm utility has been migrated to an independent package: " - "https://github.com/qiskit-community/qiskit-algorithms. You can run " - "``pip install qiskit_algorithms`` and import ``from qiskit_algorithms.utils`` instead. " - ), - since="0.45.0", -) -def validate_max(name: str, value: float, maximum: float) -> None: - """ - Args: - name: value name. - value: value to check. - maximum: maximum value allowed. - Raises: - ValueError: invalid value - """ - if value > maximum: - raise ValueError(f"{name} must have value <= {maximum}, was {value}") - - -@deprecate_func( - additional_msg=( - "This algorithm utility has been migrated to an independent package: " - "https://github.com/qiskit-community/qiskit-algorithms. You can run " - "``pip install qiskit_algorithms`` and import ``from qiskit_algorithms.utils`` instead. " - ), - since="0.45.0", -) -def validate_max_exclusive(name: str, value: float, maximum: float) -> None: - """ - Args: - name: value name. - value: value to check. - maximum: maximum value allowed. - Raises: - ValueError: invalid value - """ - if value >= maximum: - raise ValueError(f"{name} must have value < {maximum}, was {value}") - - -@deprecate_func( - additional_msg=( - "This algorithm utility has been migrated to an independent package: " - "https://github.com/qiskit-community/qiskit-algorithms. You can run " - "``pip install qiskit_algorithms`` and import ``from qiskit_algorithms.utils`` instead. " - ), - since="0.45.0", -) -def validate_range(name: str, value: float, minimum: float, maximum: float) -> None: - """ - Args: - name: value name. - value: value to check. - minimum: minimum value allowed. - maximum: maximum value allowed. - Raises: - ValueError: invalid value - """ - if value < minimum or value > maximum: - raise ValueError(f"{name} must have value >= {minimum} and <= {maximum}, was {value}") - - -@deprecate_func( - additional_msg=( - "This algorithm utility has been migrated to an independent package: " - "https://github.com/qiskit-community/qiskit-algorithms. You can run " - "``pip install qiskit_algorithms`` and import ``from qiskit_algorithms.utils`` instead. " - ), - since="0.45.0", -) -def validate_range_exclusive(name: str, value: float, minimum: float, maximum: float) -> None: - """ - Args: - name: value name. - value: value to check. - minimum: minimum value allowed. - maximum: maximum value allowed. - Raises: - ValueError: invalid value - """ - if value <= minimum or value >= maximum: - raise ValueError(f"{name} must have value > {minimum} and < {maximum}, was {value}") - - -@deprecate_func( - additional_msg=( - "This algorithm utility has been migrated to an independent package: " - "https://github.com/qiskit-community/qiskit-algorithms. You can run " - "``pip install qiskit_algorithms`` and import ``from qiskit_algorithms.utils`` instead. " - ), - since="0.45.0", -) -def validate_range_exclusive_min(name: str, value: float, minimum: float, maximum: float) -> None: - """ - Args: - name: value name. - value: value to check. - minimum: minimum value allowed. - maximum: maximum value allowed. - Raises: - ValueError: invalid value - """ - if value <= minimum or value > maximum: - raise ValueError(f"{name} must have value > {minimum} and <= {maximum}, was {value}") - - -@deprecate_func( - additional_msg=( - "This algorithm utility has been migrated to an independent package: " - "https://github.com/qiskit-community/qiskit-algorithms. You can run " - "``pip install qiskit_algorithms`` and import ``from qiskit_algorithms.utils`` instead. " - ), - since="0.45.0", -) -def validate_range_exclusive_max(name: str, value: float, minimum: float, maximum: float) -> None: - """ - Args: - name: value name. - value: value to check. - minimum: minimum value allowed. - maximum: maximum value allowed. - Raises: - ValueError: invalid value - """ - if value < minimum or value >= maximum: - raise ValueError(f"{name} must have value >= {minimum} and < {maximum}, was {value}") diff --git a/releasenotes/notes/remove-algorithm-utils-707648b69af439dc.yaml b/releasenotes/notes/remove-algorithm-utils-707648b69af439dc.yaml new file mode 100644 index 000000000000..3e0dd0863026 --- /dev/null +++ b/releasenotes/notes/remove-algorithm-utils-707648b69af439dc.yaml @@ -0,0 +1,14 @@ +--- +upgrade: + - | + The following algorithm utilities in :mod:`qiskit.utils` have been removed + from the codebase: + + * ``algorithm_globals`` + * ``qiskit.utils.validation`` + + They were deprecated in Qiskit 0.45 as a consequence of the migration + of ``qiskit.algorithms`` to a standalone + `package `_, where + these utils have also been migrated. The can be found in the new package + under ``qiskit_algorithms.utils``.