From ac877e8453ac01964a119de817b7a10627154396 Mon Sep 17 00:00:00 2001 From: Alec Edgington <54802828+cqc-alec@users.noreply.github.com> Date: Tue, 4 Jan 2022 17:36:18 +0000 Subject: [PATCH] [bugfix] Fixes for mypy. (#160) --- pytket/pytket/circuit/display/utils.py | 1 - pytket/pytket/utils/outcomearray.py | 8 +++++--- pytket/pytket/utils/spam.py | 9 +++++---- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/pytket/pytket/circuit/display/utils.py b/pytket/pytket/circuit/display/utils.py index 31c8cc5034..4f5ea7d79e 100644 --- a/pytket/pytket/circuit/display/utils.py +++ b/pytket/pytket/circuit/display/utils.py @@ -160,7 +160,6 @@ def format_logic_exp(exp: Union[LogicExp, Bit, BitRegister, int]) -> str: exp.op.value, format_logic_exp(exp.args[1]), ) - return "" def get_gate_colour(op_type: str) -> str: diff --git a/pytket/pytket/utils/outcomearray.py b/pytket/pytket/utils/outcomearray.py index a4e014c07c..0de6766625 100644 --- a/pytket/pytket/utils/outcomearray.py +++ b/pytket/pytket/utils/outcomearray.py @@ -15,9 +15,10 @@ """`OutcomeArray` class and associated methods.""" import operator from functools import reduce -from typing import Counter, List, Sequence, Dict, Tuple, Any, cast +from typing import Counter, List, Sequence, Dict, Tuple, Any, Optional, cast import numpy as np +from numpy.typing import ArrayLike class OutcomeArray(np.ndarray): @@ -57,12 +58,13 @@ def __array_finalize__(self, obj): # type: ignore # see InfoArray.__array_finalize__ for comments if obj is None: return - self._width: int = getattr(obj, "_width", None) + self._width: Optional[int] = getattr(obj, "_width", None) @property def width(self) -> int: """Number of bit entries stored, less than or equal to the bit capacity of the array.""" + assert type(self._width) is int return self._width @property @@ -77,7 +79,7 @@ def __eq__(self, other: "OutcomeArray") -> bool: # type: ignore return bool(np.array_equal(self, other) and self.width == other.width) @classmethod - def from_readouts(cls, readouts: Sequence[Sequence[int]]) -> "OutcomeArray": + def from_readouts(cls, readouts: ArrayLike) -> "OutcomeArray": """Create OutcomeArray from a 2D array like object of read-out integers, e.g. [[1, 1, 0], [0, 1, 1]]""" readouts_ar = np.array(readouts, dtype=int) diff --git a/pytket/pytket/utils/spam.py b/pytket/pytket/utils/spam.py index 2e242a30c3..39d710bcab 100644 --- a/pytket/pytket/utils/spam.py +++ b/pytket/pytket/utils/spam.py @@ -449,13 +449,14 @@ def calculate_matrices(self, results_list: List[BackendResult]) -> None: # produced state measured_state_index = binary_to_int(measured_state) # update characterisation matrix - self.subsets_matrix_map[qb_sub][ - measured_state_index, prepared_state_index - ] += count + M = self.subsets_matrix_map[qb_sub] + assert type(M) is np.ndarray + M[measured_state_index, prepared_state_index] += count # normalise everything self.characterisation_matrices = [ - mat / np.sum(mat, axis=0) for mat in self.subsets_matrix_map.values() + mat / np.sum(cast(np.ndarray, mat), axis=0) + for mat in self.subsets_matrix_map.values() ] def get_parallel_measure(self, circuit: Circuit) -> ParallelMeasures: