Skip to content

Commit

Permalink
Avoid np._bool (#5368)
Browse files Browse the repository at this point in the history
`np.bool` is being deprecated by numpy for just `bool`, and while one can use `np._bool`, I think in many cases it is better to just use the native boolean.  

This was tested against current and next mypy.
  • Loading branch information
dabacon committed May 16, 2022
1 parent 1e181f0 commit b1a5a39
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion cirq-core/cirq/linalg/predicates.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from cirq import value


def is_diagonal(matrix: np.ndarray, *, atol: float = 1e-8) -> np.bool_:
def is_diagonal(matrix: np.ndarray, *, atol: float = 1e-8) -> bool:
"""Determines if a matrix is a approximately diagonal.
A matrix is diagonal if i!=j implies m[i,j]==0.
Expand Down
8 changes: 4 additions & 4 deletions cirq-core/cirq/linalg/tolerance.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@
from numpy.typing import ArrayLike


def all_near_zero(a: 'ArrayLike', *, atol: float = 1e-8) -> np.bool_:
def all_near_zero(a: 'ArrayLike', *, atol: float = 1e-8) -> bool:
"""Checks if the tensor's elements are all near zero.
Args:
a: Tensor of elements that could all be near zero.
atol: Absolute tolerance.
"""
return np.all(np.less_equal(np.abs(a), atol))
return bool(np.all(np.less_equal(np.abs(a), atol)))


def all_near_zero_mod(
a: Union[float, complex, Iterable[float], np.ndarray], period: float, *, atol: float = 1e-8
) -> np.bool_:
) -> bool:
"""Checks if the tensor's elements are all near multiples of the period.
Args:
Expand All @@ -43,7 +43,7 @@ def all_near_zero_mod(
atol: Absolute tolerance.
"""
b = (np.asarray(a) + period / 2) % period - period / 2
return np.all(np.less_equal(np.abs(b), atol))
return bool(np.all(np.less_equal(np.abs(b), atol)))


def near_zero(a: float, *, atol: float = 1e-8) -> bool:
Expand Down
2 changes: 1 addition & 1 deletion docs/tutorials/hidden_linear_function.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@
" ans = []\n",
" while np.max(A) != 0:\n",
" edges_group = []\n",
" used = np.zeros(n, dtype=np.bool)\n",
" used = np.zeros(n, dtype=bool)\n",
" for i in range(n):\n",
" for j in range(n):\n",
" if A[i][j] == 1 and not used[i] and not used[j]:\n",
Expand Down

0 comments on commit b1a5a39

Please sign in to comment.