Skip to content

Commit

Permalink
Fix mypy errors (primitives) (#8263)
Browse files Browse the repository at this point in the history
* Fix primitives mypy errors

* Remove casts, fix types

* Apply suggestions from code review

Co-authored-by: Ikko Hamamura <ikkoham@users.noreply.github.com>

---------

Co-authored-by: Ikko Hamamura <ikkoham@users.noreply.github.com>
  • Loading branch information
Randl and ikkoham committed Mar 29, 2023
1 parent 43a9dce commit f0e90f2
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
6 changes: 3 additions & 3 deletions qiskit/primitives/backend_estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def _run_circuits(
circuits: QuantumCircuit | list[QuantumCircuit],
backend: BackendV1 | BackendV2,
**run_options,
) -> tuple[Result, list[dict]]:
) -> tuple[list[Result], list[dict]]:
"""Remove metadata of circuits and run the circuits on a backend.
Args:
circuits: The circuits
Expand Down Expand Up @@ -70,7 +70,7 @@ def _run_circuits(
return result, metadata


def _prepare_counts(results):
def _prepare_counts(results: list[Result]):
counts = []
for res in results:
count = res.get_counts()
Expand Down Expand Up @@ -351,7 +351,7 @@ def _preprocessing(self) -> list[tuple[QuantumCircuit, list[QuantumCircuit]]]:
return preprocessed_circuits

def _postprocessing(
self, result: Result, accum: list[int], metadata: list[dict]
self, result: list[Result], accum: list[int], metadata: list[dict]
) -> EstimatorResult:
"""
Postprocessing for evaluation of expectation value using pauli rotation gates.
Expand Down
4 changes: 3 additions & 1 deletion qiskit/primitives/backend_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,9 @@ def _call(
result, _metadata = _run_circuits(bound_circuits, self._backend, **run_options)
return self._postprocessing(result, bound_circuits)

def _postprocessing(self, result: Result, circuits: list[QuantumCircuit]) -> SamplerResult:
def _postprocessing(
self, result: list[Result], circuits: list[QuantumCircuit]
) -> SamplerResult:
counts = _prepare_counts(result)
shots = sum(counts[0].values())

Expand Down
5 changes: 2 additions & 3 deletions qiskit/primitives/base/base_primitive.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

from abc import ABC
from collections.abc import Sequence
from typing import Any

import numpy as np

Expand Down Expand Up @@ -126,13 +125,13 @@ def _cross_validate_circuits_parameter_values(
)


def _isint(obj: Any) -> bool:
def _isint(obj: Sequence[Sequence[float]] | Sequence[float] | float) -> bool:
"""Check if object is int."""
int_types = (int, np.integer)
return isinstance(obj, int_types) and not isinstance(obj, bool)


def _isreal(obj: Any) -> bool:
def _isreal(obj: Sequence[Sequence[float]] | Sequence[float] | float) -> bool:
"""Check if object is a real number: int or float except ``±Inf`` and ``NaN``."""
float_types = (float, np.floating)
return _isint(obj) or isinstance(obj, float_types) and float("-Inf") < obj < float("Inf")

0 comments on commit f0e90f2

Please sign in to comment.