Skip to content

Commit

Permalink
Replace raising QiskitError by AlgorithmError (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
woodsp-ibm authored Aug 15, 2023
1 parent 1863c6f commit 9964449
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 12 deletions.
8 changes: 4 additions & 4 deletions qiskit_algorithms/minimum_eigensolvers/adapt_vqe.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@

import numpy as np

from qiskit import QiskitError
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.exceptions import AlgorithmError
from qiskit_algorithms.list_or_dict import ListOrDict

from .minimum_eigensolver import MinimumEigensolver
Expand Down Expand Up @@ -239,8 +239,8 @@ def compute_minimum_eigenvalue(
Raises:
TypeError: If an ansatz other than :class:`~.EvolvedOperatorAnsatz` is provided.
QiskitError: If all evaluated gradients lie below the convergence threshold in the first
iteration of the algorithm.
AlgorithmError: If all evaluated gradients lie below the convergence threshold in
the first iteration of the algorithm.
Returns:
An :class:`~.AdaptVQEResult` which is a :class:`~.VQEResult` but also but also
Expand Down Expand Up @@ -281,7 +281,7 @@ def compute_minimum_eigenvalue(
# log gradients
if np.abs(max_grad[0]) < self.gradient_threshold:
if iteration == 1:
raise QiskitError(
raise AlgorithmError(
"All gradients have been evaluated to lie below the convergence threshold "
"during the first iteration of the algorithm. Try to either tighten the "
"convergence threshold or pick a different ansatz."
Expand Down
6 changes: 3 additions & 3 deletions qiskit_algorithms/optimizers/snobfit.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from typing import Any

import numpy as np
from qiskit.exceptions import QiskitError
from qiskit_algorithms.exceptions import AlgorithmError
from qiskit_algorithms.utils import optionals as _optionals
from .optimizer import Optimizer, OptimizerSupportLevel, OptimizerResult, POINT

Expand Down Expand Up @@ -53,13 +53,13 @@ def __init__(
Raises:
MissingOptionalLibraryError: scikit-quant or SQSnobFit not installed
QiskitError: If NumPy 1.24.0 or above is installed.
AlgorithmError: If NumPy 1.24.0 or above is installed.
See https://github.com/scikit-quant/scikit-quant/issues/24 for more details.
"""
# check version
version = tuple(map(int, np.__version__.split(".")))
if version >= (1, 24, 0):
raise QiskitError(
raise AlgorithmError(
"SnobFit is incompatible with NumPy 1.24.0 or above, please "
"install a previous version. See also scikit-quant/scikit-quant#24."
)
Expand Down
6 changes: 3 additions & 3 deletions qiskit_algorithms/time_evolvers/pvqd/pvqd.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from qiskit.synthesis import EvolutionSynthesis, LieTrotter
from qiskit.utils import algorithm_globals

from ...exceptions import AlgorithmError, QiskitError
from ...exceptions import AlgorithmError
from ...optimizers import Minimizer, Optimizer
from ...state_fidelities.base_state_fidelity import BaseStateFidelity
from ..real_time_evolver import RealTimeEvolver
Expand Down Expand Up @@ -420,12 +420,12 @@ def _validate_setup(self, skip=None):
)

if self.ansatz.num_parameters == 0:
raise QiskitError(
raise AlgorithmError(
"The ansatz cannot have 0 parameters, otherwise it cannot be trained."
)

if len(self.initial_parameters) != self.ansatz.num_parameters:
raise QiskitError(
raise AlgorithmError(
f"Mismatching number of parameters in the ansatz ({self.ansatz.num_parameters}) "
f"and the initial parameters ({len(self.initial_parameters)})."
)
12 changes: 12 additions & 0 deletions releasenotes/notes/change_exception-85f9f8d86ac16720.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
upgrade:
- |
A couple of algorithms here, :class:`.PVQD`, :class:`.AdaptVQE` and optimizer
:class:`.SNOBFIT`, directly raised a :class:`~qiskit.exceptions.QiskitError`.
These have been changed to raise an :class:`.AlgorithmError` instead. Algorithms
have now been moved out of ``Qiskit`` and this better distinguishes the exception to the
algorithms when raised. Now ``AlgorithmError`` was already raised elsewhere by the algorithms
here so this makes things more consistent too. Note, that as ``AlgorithmError``
internally extends ``QiskitError``, any code that might have caught that specifically
will continue to work. However we do recommend you update your code accordingly for
``AlgorithmError``.
4 changes: 2 additions & 2 deletions test/time_evolvers/test_pvqd.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
import numpy as np
from ddt import data, ddt, unpack

from qiskit import QiskitError
from qiskit.circuit import Gate, Parameter, QuantumCircuit
from qiskit.circuit.library import EfficientSU2
from qiskit.primitives import Estimator, Sampler
from qiskit.quantum_info import Pauli, SparsePauliOp
from qiskit.test import QiskitTestCase
from qiskit.utils import algorithm_globals

from qiskit_algorithms import AlgorithmError
from qiskit_algorithms.time_evolvers import TimeEvolutionProblem
from qiskit_algorithms.optimizers import L_BFGS_B, SPSA, GradientDescent, OptimizerResult
from qiskit_algorithms.state_fidelities import ComputeUncompute
Expand Down Expand Up @@ -222,7 +222,7 @@ def test_zero_parameters(self):
optimizer=SPSA(maxiter=10, learning_rate=0.1, perturbation=0.01),
)

with self.assertRaises(QiskitError):
with self.assertRaises(AlgorithmError):
_ = pvqd.evolve(problem)

def test_initial_state_raises(self):
Expand Down

0 comments on commit 9964449

Please sign in to comment.