Skip to content

Commit b1a5a39

Browse files
authoredMay 16, 2022
Avoid np._bool (#5368)
`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.
1 parent 1e181f0 commit b1a5a39

File tree

3 files changed

+6
-6
lines changed

3 files changed

+6
-6
lines changed
 

‎cirq-core/cirq/linalg/predicates.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from cirq import value
2121

2222

23-
def is_diagonal(matrix: np.ndarray, *, atol: float = 1e-8) -> np.bool_:
23+
def is_diagonal(matrix: np.ndarray, *, atol: float = 1e-8) -> bool:
2424
"""Determines if a matrix is a approximately diagonal.
2525
2626
A matrix is diagonal if i!=j implies m[i,j]==0.

‎cirq-core/cirq/linalg/tolerance.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,19 @@
2222
from numpy.typing import ArrayLike
2323

2424

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

3434

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

4848

4949
def near_zero(a: float, *, atol: float = 1e-8) -> bool:

‎docs/tutorials/hidden_linear_function.ipynb

+1-1
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@
417417
" ans = []\n",
418418
" while np.max(A) != 0:\n",
419419
" edges_group = []\n",
420-
" used = np.zeros(n, dtype=np.bool)\n",
420+
" used = np.zeros(n, dtype=bool)\n",
421421
" for i in range(n):\n",
422422
" for j in range(n):\n",
423423
" if A[i][j] == 1 and not used[i] and not used[j]:\n",

0 commit comments

Comments
 (0)
Please sign in to comment.