Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Utilize local instance of validation.py #35

Merged
merged 2 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading