Skip to content

Commit cf86dda

Browse files
authored
Suppress superfluous warnings from numpy (#6599)
* Suppress superfluous warnings from numpy - On MacOS, with numpy ~1.26, np.linalg.det gives spurious warnings when passed a complex array. - Suppress these warnings, as they are just annoying and do not signify anything.
1 parent 2a907bf commit cf86dda

File tree

6 files changed

+28
-18
lines changed

6 files changed

+28
-18
lines changed

cirq-core/cirq/linalg/decompositions.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,9 @@ def kron_factor_4x4_to_2x2s(matrix: np.ndarray) -> Tuple[complex, np.ndarray, np
222222
f2[(a & 1) ^ i, (b & 1) ^ j] = matrix[a ^ i, b ^ j]
223223

224224
# Rescale factors to have unit determinants.
225-
f1 /= np.sqrt(np.linalg.det(f1)) or 1
226-
f2 /= np.sqrt(np.linalg.det(f2)) or 1
225+
with np.errstate(divide="ignore", invalid="ignore"):
226+
f1 /= np.sqrt(np.linalg.det(f1)) or 1
227+
f2 /= np.sqrt(np.linalg.det(f2)) or 1
227228

228229
# Determine global phase.
229230
g = matrix[a, b] / (f1[a >> 1, b >> 1] * f2[a & 1, b & 1])
@@ -965,7 +966,8 @@ def kak_vector(
965966
# The algorithm in the appendix mentioned above is slightly incorrect in
966967
# that it only works for elements of SU(4). A phase correction must be
967968
# added to deal with U(4).
968-
phases = np.log(-1j * np.linalg.det(unitary)).imag + np.pi / 2
969+
with np.errstate(divide="ignore", invalid="ignore"):
970+
phases = np.log(-1j * np.linalg.det(unitary)).imag + np.pi / 2
969971
evals *= np.exp(-1j * phases / 2)[..., np.newaxis]
970972

971973
# The following steps follow the appendix exactly.

cirq-core/cirq/linalg/diagonalize.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -255,10 +255,11 @@ def bidiagonalize_unitary_with_special_orthogonals(
255255
)
256256

257257
# Convert to special orthogonal w/o breaking diagonalization.
258-
if np.linalg.det(left) < 0:
259-
left[0, :] *= -1
260-
if np.linalg.det(right) < 0:
261-
right[:, 0] *= -1
258+
with np.errstate(divide="ignore", invalid="ignore"):
259+
if np.linalg.det(left) < 0:
260+
left[0, :] *= -1
261+
if np.linalg.det(right) < 0:
262+
right[:, 0] *= -1
262263

263264
diag = combinators.dot(left, mat, right)
264265

cirq-core/cirq/linalg/predicates.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,10 @@ def is_special_orthogonal(matrix: np.ndarray, *, rtol: float = 1e-5, atol: float
9191
Returns:
9292
Whether the matrix is special orthogonal within the given tolerance.
9393
"""
94-
return is_orthogonal(matrix, rtol=rtol, atol=atol) and (
95-
matrix.shape[0] == 0 or np.allclose(np.linalg.det(matrix), 1, rtol=rtol, atol=atol)
96-
)
94+
with np.errstate(divide="ignore", invalid="ignore"):
95+
return is_orthogonal(matrix, rtol=rtol, atol=atol) and (
96+
matrix.shape[0] == 0 or np.allclose(np.linalg.det(matrix), 1, rtol=rtol, atol=atol)
97+
)
9798

9899

99100
def is_unitary(matrix: np.ndarray, *, rtol: float = 1e-5, atol: float = 1e-8) -> bool:
@@ -128,9 +129,10 @@ def is_special_unitary(matrix: np.ndarray, *, rtol: float = 1e-5, atol: float =
128129
Whether the matrix is unitary with unit determinant within the given
129130
tolerance.
130131
"""
131-
return is_unitary(matrix, rtol=rtol, atol=atol) and (
132-
matrix.shape[0] == 0 or np.allclose(np.linalg.det(matrix), 1, rtol=rtol, atol=atol)
133-
)
132+
with np.errstate(divide="ignore", invalid="ignore"):
133+
return is_unitary(matrix, rtol=rtol, atol=atol) and (
134+
matrix.shape[0] == 0 or np.allclose(np.linalg.det(matrix), 1, rtol=rtol, atol=atol)
135+
)
134136

135137

136138
def is_normal(matrix: np.ndarray, *, rtol: float = 1e-5, atol: float = 1e-8) -> bool:

cirq-core/cirq/linalg/transformations.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,8 @@ def to_special(u: np.ndarray) -> np.ndarray:
592592
Returns:
593593
the special unitary matrix
594594
"""
595-
return u * (np.linalg.det(u) ** (-1 / len(u)))
595+
with np.errstate(divide="ignore", invalid="ignore"):
596+
return u * (np.linalg.det(u) ** (-1 / len(u)))
596597

597598

598599
def state_vector_kronecker_product(t1: np.ndarray, t2: np.ndarray) -> np.ndarray:

cirq-core/cirq/testing/lin_alg_utils.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ def random_special_unitary(
133133
The sampled special unitary.
134134
"""
135135
r = random_unitary(dim, random_state=random_state)
136-
r[0, :] /= np.linalg.det(r)
136+
with np.errstate(divide="ignore", invalid="ignore"):
137+
r[0, :] /= np.linalg.det(r)
137138
return r
138139

139140

@@ -152,8 +153,9 @@ def random_special_orthogonal(
152153
The sampled special orthogonal matrix.
153154
"""
154155
m = random_orthogonal(dim, random_state=random_state)
155-
if np.linalg.det(m) < 0:
156-
m[0, :] *= -1
156+
with np.errstate(divide="ignore", invalid="ignore"):
157+
if np.linalg.det(m) < 0:
158+
m[0, :] *= -1
157159
return m
158160

159161

cirq-core/cirq/transformers/analytical_decompositions/controlled_gate_decomposition.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ def _decompose_abc(matrix: np.ndarray) -> Tuple[np.ndarray, np.ndarray, np.ndarr
4747
See [1], chapter 4.
4848
"""
4949
assert matrix.shape == (2, 2)
50-
delta = np.angle(np.linalg.det(matrix)) * 0.5
50+
with np.errstate(divide="ignore", invalid="ignore"):
51+
# On MacOS, np.linalg.det emits superflous warnings
52+
delta = np.angle(np.linalg.det(matrix)) * 0.5
5153
alpha = np.angle(matrix[0, 0]) + np.angle(matrix[0, 1]) - 2 * delta
5254
beta = np.angle(matrix[0, 0]) - np.angle(matrix[0, 1])
5355

0 commit comments

Comments
 (0)