diff --git a/qiskit_algorithms/eigensolvers/vqd.py b/qiskit_algorithms/eigensolvers/vqd.py index 897ce1ef..19024e2c 100644 --- a/qiskit_algorithms/eigensolvers/vqd.py +++ b/qiskit_algorithms/eigensolvers/vqd.py @@ -123,8 +123,8 @@ def __init__( optimizer: Optimizer | Minimizer | Sequence[Optimizer | Minimizer], *, k: int = 2, - betas: Sequence[float] | None = None, - initial_point: Sequence[float] | Sequence[Sequence[float]] | None = None, + betas: np.ndarray | None = None, + initial_point: np.ndarray | list[np.ndarray] | None = None, callback: Callable[[int, np.ndarray, float, dict[str, Any], int], None] | None = None, ) -> None: """ @@ -167,13 +167,13 @@ def __init__( self._eval_count = 0 - @property # type: ignore[override] - def initial_point(self) -> Sequence[float] | Sequence[Sequence[float]] | None: + @property + def initial_point(self) -> np.ndarray | list[np.ndarray] | None: """Returns initial point.""" return self._initial_point @initial_point.setter - def initial_point(self, initial_point: Sequence[float] | Sequence[Sequence[float]] | None): + def initial_point(self, initial_point: np.ndarray | list[np.ndarray] | None): """Sets initial point""" self._initial_point = initial_point @@ -241,7 +241,7 @@ def compute_eigenvalues( f"of type {type(operator)}." ) from exc - betas = [upper_bound * 10] * (self.k) + betas = np.asarray([upper_bound * 10] * self.k) logger.info("beta autoevaluated to %s", betas[0]) result = self._build_vqd_result() @@ -287,8 +287,9 @@ def compute_eigenvalues( if callable(optimizer): opt_result = optimizer( # pylint: disable=not-callable - fun=energy_evaluation, # type: ignore[arg-type,call-arg] - x0=initial_point, # type: ignore[arg-type] + fun=energy_evaluation, # type: ignore[arg-type] + x0=initial_point, + jac=None, bounds=bounds, ) else: @@ -346,7 +347,7 @@ def _get_evaluate_energy( self, step: int, operator: BaseOperator, - betas: Sequence[float], + betas: np.ndarray, prev_states: list[QuantumCircuit] | None = None, ) -> Callable[[np.ndarray], float | np.ndarray]: """Returns a function handle to evaluate the ansatz's energy for any given parameters. diff --git a/qiskit_algorithms/minimum_eigensolvers/adapt_vqe.py b/qiskit_algorithms/minimum_eigensolvers/adapt_vqe.py index 9d18b023..13684273 100644 --- a/qiskit_algorithms/minimum_eigensolvers/adapt_vqe.py +++ b/qiskit_algorithms/minimum_eigensolvers/adapt_vqe.py @@ -13,7 +13,6 @@ """An implementation of the AdaptVQE algorithm.""" from __future__ import annotations -from collections.abc import Sequence from enum import Enum import re @@ -130,13 +129,13 @@ def __init__( self._excitation_pool: list[BaseOperator] = [] self._excitation_list: list[BaseOperator] = [] - @property # type: ignore[override] - def initial_point(self) -> Sequence[float] | None: + @property + def initial_point(self) -> np.ndarray | None: """Returns the initial point of the internal :class:`~.VQE` solver.""" return self.solver.initial_point @initial_point.setter - def initial_point(self, value: Sequence[float] | None) -> None: + def initial_point(self, value: np.ndarray | None) -> None: """Sets the initial point of the internal :class:`~.VQE` solver.""" self.solver.initial_point = value @@ -275,7 +274,7 @@ def compute_minimum_eigenvalue( # setting up the ansatz for the VQE iteration self._tmp_ansatz.operators = self._excitation_list self.solver.ansatz = self._tmp_ansatz - self.solver.initial_point = theta + self.solver.initial_point = np.asarray(theta) # evaluating the eigenvalue with the internal VQE prev_raw_vqe_result = raw_vqe_result raw_vqe_result = self.solver.compute_minimum_eigenvalue(operator) @@ -297,7 +296,7 @@ def compute_minimum_eigenvalue( theta.pop() self._tmp_ansatz.operators = self._excitation_list self.solver.ansatz = self._tmp_ansatz - self.solver.initial_point = theta + self.solver.initial_point = np.asarray(theta) raw_vqe_result = prev_raw_vqe_result break # appending the computed eigenvalue to the tracking history diff --git a/qiskit_algorithms/minimum_eigensolvers/qaoa.py b/qiskit_algorithms/minimum_eigensolvers/qaoa.py index 9bbc5fd0..8953b795 100644 --- a/qiskit_algorithms/minimum_eigensolvers/qaoa.py +++ b/qiskit_algorithms/minimum_eigensolvers/qaoa.py @@ -129,7 +129,7 @@ def __init__( sampler=sampler, ansatz=None, optimizer=optimizer, - initial_point=initial_point, # type: ignore[arg-type] + initial_point=initial_point, aggregation=aggregation, callback=callback, ) diff --git a/qiskit_algorithms/minimum_eigensolvers/sampling_vqe.py b/qiskit_algorithms/minimum_eigensolvers/sampling_vqe.py index f6661400..80a9c5d3 100644 --- a/qiskit_algorithms/minimum_eigensolvers/sampling_vqe.py +++ b/qiskit_algorithms/minimum_eigensolvers/sampling_vqe.py @@ -14,7 +14,7 @@ from __future__ import annotations -from collections.abc import Callable, Sequence +from collections.abc import Callable import logging from time import time from typing import Any @@ -120,7 +120,7 @@ def __init__( ansatz: QuantumCircuit, optimizer: Optimizer | Minimizer, *, - initial_point: Sequence[float] | None = None, + initial_point: np.ndarray | None = None, aggregation: float | Callable[[list[float]], float] | None = None, callback: Callable[[int, np.ndarray, float, dict[str, Any]], None] | None = None, ) -> None: @@ -152,13 +152,13 @@ def __init__( # this has to go via getters and setters due to the VariationalAlgorithm interface self._initial_point = initial_point - @property # type: ignore[override] - def initial_point(self) -> Sequence[float] | None: + @property + def initial_point(self) -> np.ndarray | None: """Return the initial point.""" return self._initial_point @initial_point.setter - def initial_point(self, value: Sequence[float] | None) -> None: + def initial_point(self, value: np.ndarray | None) -> None: """Set the initial point.""" self._initial_point = value @@ -212,8 +212,9 @@ def compute_minimum_eigenvalue( if callable(self.optimizer): optimizer_result = self.optimizer( - fun=evaluate_energy, # type: ignore[call-arg,arg-type] - x0=initial_point, # type: ignore[arg-type] + fun=evaluate_energy, # type: ignore[arg-type] + x0=initial_point, + jac=None, bounds=bounds, ) else: @@ -222,7 +223,9 @@ def compute_minimum_eigenvalue( was_updated = _set_default_batchsize(self.optimizer) optimizer_result = self.optimizer.minimize( - fun=evaluate_energy, x0=initial_point, bounds=bounds # type: ignore[arg-type] + fun=evaluate_energy, # type: ignore[arg-type] + x0=initial_point, + bounds=bounds ) # reset to original value diff --git a/qiskit_algorithms/minimum_eigensolvers/vqe.py b/qiskit_algorithms/minimum_eigensolvers/vqe.py index ea67d23e..b0e85a67 100644 --- a/qiskit_algorithms/minimum_eigensolvers/vqe.py +++ b/qiskit_algorithms/minimum_eigensolvers/vqe.py @@ -16,7 +16,7 @@ import logging from time import time -from collections.abc import Callable, Sequence +from collections.abc import Callable from typing import Any import numpy as np @@ -119,7 +119,7 @@ def __init__( optimizer: Optimizer | Minimizer, *, gradient: BaseEstimatorGradient | None = None, - initial_point: Sequence[float] | None = None, + initial_point: np.ndarray | None = None, callback: Callable[[int, np.ndarray, float, dict[str, Any]], None] | None = None, ) -> None: r""" @@ -150,12 +150,12 @@ def __init__( self.initial_point = initial_point self.callback = callback - @property # type: ignore[override] - def initial_point(self) -> Sequence[float] | None: + @property + def initial_point(self) -> np.ndarray | None: return self._initial_point @initial_point.setter - def initial_point(self, value: Sequence[float] | None) -> None: + def initial_point(self, value: np.ndarray | None) -> None: self._initial_point = value def compute_minimum_eigenvalue( @@ -182,7 +182,7 @@ def compute_minimum_eigenvalue( if callable(self.optimizer): optimizer_result = self.optimizer( fun=evaluate_energy, # type: ignore[arg-type] - x0=initial_point, # type: ignore[arg-type] + x0=initial_point, jac=evaluate_gradient, bounds=bounds, ) @@ -193,7 +193,7 @@ def compute_minimum_eigenvalue( optimizer_result = self.optimizer.minimize( fun=evaluate_energy, # type: ignore[arg-type] - x0=initial_point, # type: ignore[arg-type] + x0=initial_point, jac=evaluate_gradient, # type: ignore[arg-type] bounds=bounds, ) diff --git a/qiskit_algorithms/utils/validate_initial_point.py b/qiskit_algorithms/utils/validate_initial_point.py index 44925652..dbc3f816 100644 --- a/qiskit_algorithms/utils/validate_initial_point.py +++ b/qiskit_algorithms/utils/validate_initial_point.py @@ -14,8 +14,6 @@ from __future__ import annotations -from typing import cast, Sequence - import numpy as np from qiskit.circuit import QuantumCircuit @@ -23,8 +21,8 @@ def validate_initial_point( - point: Sequence[float] | None, circuit: QuantumCircuit -) -> Sequence[float]: + point: np.ndarray | None | None, circuit: QuantumCircuit +) -> np.ndarray: r""" Validate a choice of initial point against a choice of circuit. If no point is provided, a random point will be generated within certain parameter bounds. It will first look to the @@ -58,7 +56,7 @@ def validate_initial_point( upper_bounds.append(upper if upper is not None else 2 * np.pi) # sample from within bounds - point = cast(Sequence[float], algorithm_globals.random.uniform(lower_bounds, upper_bounds)) + point = algorithm_globals.random.uniform(lower_bounds, upper_bounds) elif len(point) != expected_size: raise ValueError(