Skip to content

Commit

Permalink
Fix Ci for latest mypy
Browse files Browse the repository at this point in the history
  • Loading branch information
woodsp-ibm committed Oct 13, 2023
1 parent 745a892 commit faea666
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 36 deletions.
19 changes: 10 additions & 9 deletions qiskit_algorithms/eigensolvers/vqd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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.
Expand Down
11 changes: 5 additions & 6 deletions qiskit_algorithms/minimum_eigensolvers/adapt_vqe.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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
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 @@ -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,
)
Expand Down
19 changes: 11 additions & 8 deletions qiskit_algorithms/minimum_eigensolvers/sampling_vqe.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand Down
14 changes: 7 additions & 7 deletions qiskit_algorithms/minimum_eigensolvers/vqe.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"""
Expand Down Expand Up @@ -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(
Expand All @@ -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,
)
Expand All @@ -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,
)
Expand Down
8 changes: 3 additions & 5 deletions qiskit_algorithms/utils/validate_initial_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,15 @@

from __future__ import annotations

from typing import cast, Sequence

import numpy as np

from qiskit.circuit import QuantumCircuit
from qiskit_algorithms.utils.algorithm_globals import algorithm_globals


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
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit faea666

Please sign in to comment.