From 88126eb96f2d82c4ba626eb8db07ef256fd35692 Mon Sep 17 00:00:00 2001 From: woodsp-ibm Date: Thu, 3 Aug 2023 17:30:04 -0400 Subject: [PATCH] Utilize local instance of validation.py --- .../eigensolvers/numpy_eigensolver.py | 2 +- .../minimum_eigensolvers/adapt_vqe.py | 2 +- .../minimum_eigensolvers/qaoa.py | 2 +- qiskit_algorithms/optimizers/aqgd.py | 3 +- qiskit_algorithms/optimizers/p_bfgs.py | 2 +- .../optimizers/scipy_optimizer.py | 3 +- qiskit_algorithms/utils/validation.py | 138 ++++++++++++++++++ test/test_validation.py | 3 +- 8 files changed, 147 insertions(+), 8 deletions(-) create mode 100644 qiskit_algorithms/utils/validation.py diff --git a/qiskit_algorithms/eigensolvers/numpy_eigensolver.py b/qiskit_algorithms/eigensolvers/numpy_eigensolver.py index a7811302..0f089839 100644 --- a/qiskit_algorithms/eigensolvers/numpy_eigensolver.py +++ b/qiskit_algorithms/eigensolvers/numpy_eigensolver.py @@ -21,8 +21,8 @@ from qiskit.quantum_info import SparsePauliOp, Statevector from qiskit.quantum_info.operators.base_operator import BaseOperator -from qiskit.utils.validation import validate_min +from qiskit_algorithms.utils.validation import validate_min from .eigensolver import Eigensolver, EigensolverResult from ..exceptions import AlgorithmError from ..list_or_dict import ListOrDict diff --git a/qiskit_algorithms/minimum_eigensolvers/adapt_vqe.py b/qiskit_algorithms/minimum_eigensolvers/adapt_vqe.py index 26cbd1d6..bfae8f0c 100644 --- a/qiskit_algorithms/minimum_eigensolvers/adapt_vqe.py +++ b/qiskit_algorithms/minimum_eigensolvers/adapt_vqe.py @@ -26,8 +26,8 @@ from qiskit.quantum_info.operators.base_operator import BaseOperator from qiskit.circuit.library import EvolvedOperatorAnsatz from qiskit.utils.deprecation import deprecate_arg, deprecate_func -from qiskit.utils.validation import validate_min +from qiskit_algorithms.utils.validation import validate_min from qiskit_algorithms.list_or_dict import ListOrDict from .minimum_eigensolver import MinimumEigensolver diff --git a/qiskit_algorithms/minimum_eigensolvers/qaoa.py b/qiskit_algorithms/minimum_eigensolvers/qaoa.py index 2a4dabc1..6faeb0d5 100644 --- a/qiskit_algorithms/minimum_eigensolvers/qaoa.py +++ b/qiskit_algorithms/minimum_eigensolvers/qaoa.py @@ -21,8 +21,8 @@ from qiskit.circuit.library.n_local.qaoa_ansatz import QAOAAnsatz from qiskit.quantum_info.operators.base_operator import BaseOperator from qiskit.primitives import BaseSampler -from qiskit.utils.validation import validate_min +from qiskit_algorithms.utils.validation import validate_min from qiskit_algorithms.optimizers import Minimizer, Optimizer from .sampling_vqe import SamplingVQE diff --git a/qiskit_algorithms/optimizers/aqgd.py b/qiskit_algorithms/optimizers/aqgd.py index 060345eb..f74bbda4 100644 --- a/qiskit_algorithms/optimizers/aqgd.py +++ b/qiskit_algorithms/optimizers/aqgd.py @@ -18,7 +18,8 @@ from typing import Any import numpy as np -from qiskit.utils.validation import validate_range_exclusive_max + +from qiskit_algorithms.utils.validation import validate_range_exclusive_max from .optimizer import Optimizer, OptimizerSupportLevel, OptimizerResult, POINT from ..exceptions import AlgorithmError diff --git a/qiskit_algorithms/optimizers/p_bfgs.py b/qiskit_algorithms/optimizers/p_bfgs.py index 93a10a37..14d715dd 100644 --- a/qiskit_algorithms/optimizers/p_bfgs.py +++ b/qiskit_algorithms/optimizers/p_bfgs.py @@ -22,8 +22,8 @@ import numpy as np from qiskit.utils import algorithm_globals -from qiskit.utils.validation import validate_min +from qiskit_algorithms.utils.validation import validate_min from .optimizer import OptimizerResult, POINT from .scipy_optimizer import SciPyOptimizer diff --git a/qiskit_algorithms/optimizers/scipy_optimizer.py b/qiskit_algorithms/optimizers/scipy_optimizer.py index e2de3960..ff0ef2a9 100644 --- a/qiskit_algorithms/optimizers/scipy_optimizer.py +++ b/qiskit_algorithms/optimizers/scipy_optimizer.py @@ -19,8 +19,7 @@ import numpy as np from scipy.optimize import minimize -from qiskit.utils.validation import validate_min - +from qiskit_algorithms.utils.validation import validate_min from .optimizer import Optimizer, OptimizerSupportLevel, OptimizerResult, POINT diff --git a/qiskit_algorithms/utils/validation.py b/qiskit_algorithms/utils/validation.py new file mode 100644 index 00000000..ae838d8d --- /dev/null +++ b/qiskit_algorithms/utils/validation.py @@ -0,0 +1,138 @@ +# This code is part of a Qiskit project. +# +# (C) Copyright IBM 2019, 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 +# 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 + + +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}'.") + + +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}") + + +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}") + + +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}") + + +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}") + + +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}") + + +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}") + + +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}") + + +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/test/test_validation.py b/test/test_validation.py index 23053c94..2d9ae22d 100644 --- a/test/test_validation.py +++ b/test/test_validation.py @@ -15,7 +15,8 @@ import unittest from test import QiskitAlgorithmsTestCase -from qiskit.utils.validation import ( + +from qiskit_algorithms.utils.validation import ( validate_in_set, validate_min, validate_min_exclusive,