From 55e6ca693136290bdd297053d0af11d4bf4b3ab9 Mon Sep 17 00:00:00 2001 From: Declan Millar Date: Mon, 26 Sep 2022 22:48:04 +0100 Subject: [PATCH] dict -> dict[str, Any] --- qiskit/algorithms/minimum_eigensolvers/vqe.py | 21 ++++++++++--------- qiskit/algorithms/observables_evaluator.py | 5 +++-- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/qiskit/algorithms/minimum_eigensolvers/vqe.py b/qiskit/algorithms/minimum_eigensolvers/vqe.py index 1cc7312f3926..26195b704072 100644 --- a/qiskit/algorithms/minimum_eigensolvers/vqe.py +++ b/qiskit/algorithms/minimum_eigensolvers/vqe.py @@ -17,6 +17,7 @@ import logging from time import time from collections.abc import Callable, Sequence +from typing import Any import numpy as np @@ -95,10 +96,10 @@ def my_minimizer(fun, x0, jac=None, bounds=None) -> OptimizerResult: :class:`.Minimizer` protocol. gradient (BaseEstimatorGradient | None): An optional estimator gradient to be used with the optimizer. - callback (Callable[[int, np.ndarray, float, dict], None] | None): A callback that can access - the intermediate data at each optimization step. These data are: the evaluation count, - the optimizer parameters for the ansatz, the evaluated mean, and the metadata - dictionary. + callback (Callable[[int, np.ndarray, float, dict[str, Any]], None] | None): A callback that + can access the intermediate data at each optimization step. These data are: the + evaluation count, the optimizer parameters for the ansatz, the evaluated mean, and the + metadata dictionary. References: [1] Peruzzo et al, "A variational eigenvalue solver on a quantum processor" @@ -113,7 +114,7 @@ def __init__( *, gradient: BaseEstimatorGradient | None = None, initial_point: Sequence[float] | None = None, - callback: Callable[[int, np.ndarray, float, dict], None] | None = None, + callback: Callable[[int, np.ndarray, float, dict[str, Any]], None] | None = None, ) -> None: r""" Args: @@ -124,11 +125,11 @@ def __init__( can either be a Qiskit :class:`.Optimizer` or a callable implementing the :class:`.Minimizer` protocol. gradient: An optional estimator gradient to be used with the optimizer. - initial_point: An optional initial point (i.e. initial parameter values) for the optimizer. - The length of the initial point must match the number of :attr:`ansatz` parameters. - If ``None``, a random point will be generated within certain parameter bounds. - ``VQE`` will look to the ansatz for these bounds. If the ansatz does not specify - bounds, bounds of :math:`-2\pi`, :math:`2\pi` will be used. + initial_point: An optional initial point (i.e. initial parameter values) for the + optimizer. The length of the initial point must match the number of :attr:`ansatz` + parameters. If ``None``, a random point will be generated within certain parameter + bounds. ``VQE`` will look to the ansatz for these bounds. If the ansatz does not + specify bounds, bounds of :math:`-2\pi`, :math:`2\pi` will be used. callback: A callback that can access the intermediate data at each optimization step. These data are: the evaluation count, the optimizer parameters for the ansatz, the estimated value, and the metadata dictionary. diff --git a/qiskit/algorithms/observables_evaluator.py b/qiskit/algorithms/observables_evaluator.py index 546e1562ddce..f1ca00c4b5d2 100644 --- a/qiskit/algorithms/observables_evaluator.py +++ b/qiskit/algorithms/observables_evaluator.py @@ -14,6 +14,7 @@ from __future__ import annotations from collections.abc import Sequence +from typing import Any import numpy as np @@ -31,7 +32,7 @@ def estimate_observables( observables: ListOrDict[BaseOperator | PauliSumOp], parameter_values: Sequence[float] | None = None, threshold: float = 1e-12, -) -> ListOrDict[tuple[complex, dict]]: +) -> ListOrDict[tuple[complex, dict[str, Any]]]: """ Accepts a sequence of operators and calculates their expectation values - means and metadata. They are calculated with respect to a quantum state provided. A user @@ -94,7 +95,7 @@ def _handle_zero_ops( def _prepare_result( observables_results: list[tuple[complex, dict]], observables: ListOrDict[BaseOperator | PauliSumOp], -) -> ListOrDict[tuple[complex, dict]]: +) -> ListOrDict[tuple[complex, dict[str, Any]]]: """ Prepares a list of tuples of eigenvalues and metadata tuples from ``observables_results`` and ``observables``.