Skip to content

Commit

Permalink
Utilize local instance of validation.py (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
woodsp-ibm authored Aug 15, 2023
1 parent a953e5c commit 6738c6d
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 8 deletions.
2 changes: 1 addition & 1 deletion qiskit_algorithms/eigensolvers/numpy_eigensolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion qiskit_algorithms/minimum_eigensolvers/adapt_vqe.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@
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.exceptions import AlgorithmError

from qiskit_algorithms.list_or_dict import ListOrDict

from .minimum_eigensolver import MinimumEigensolver
Expand Down
2 changes: 1 addition & 1 deletion qiskit_algorithms/minimum_eigensolvers/qaoa.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion qiskit_algorithms/optimizers/aqgd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion qiskit_algorithms/optimizers/p_bfgs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 1 addition & 2 deletions qiskit_algorithms/optimizers/scipy_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
138 changes: 138 additions & 0 deletions qiskit_algorithms/utils/validation.py
Original file line number Diff line number Diff line change
@@ -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}")
3 changes: 2 additions & 1 deletion test/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 6738c6d

Please sign in to comment.