Skip to content

Commit

Permalink
Suppress superfluous warnings from numpy (#6599)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
dstrain115 authored May 15, 2024
1 parent 2a907bf commit cf86dda
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 18 deletions.
8 changes: 5 additions & 3 deletions cirq-core/cirq/linalg/decompositions.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,9 @@ def kron_factor_4x4_to_2x2s(matrix: np.ndarray) -> Tuple[complex, np.ndarray, np
f2[(a & 1) ^ i, (b & 1) ^ j] = matrix[a ^ i, b ^ j]

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

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

# The following steps follow the appendix exactly.
Expand Down
9 changes: 5 additions & 4 deletions cirq-core/cirq/linalg/diagonalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,11 @@ def bidiagonalize_unitary_with_special_orthogonals(
)

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

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

Expand Down
14 changes: 8 additions & 6 deletions cirq-core/cirq/linalg/predicates.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,10 @@ def is_special_orthogonal(matrix: np.ndarray, *, rtol: float = 1e-5, atol: float
Returns:
Whether the matrix is special orthogonal within the given tolerance.
"""
return is_orthogonal(matrix, rtol=rtol, atol=atol) and (
matrix.shape[0] == 0 or np.allclose(np.linalg.det(matrix), 1, rtol=rtol, atol=atol)
)
with np.errstate(divide="ignore", invalid="ignore"):
return is_orthogonal(matrix, rtol=rtol, atol=atol) and (
matrix.shape[0] == 0 or np.allclose(np.linalg.det(matrix), 1, rtol=rtol, atol=atol)
)


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


def is_normal(matrix: np.ndarray, *, rtol: float = 1e-5, atol: float = 1e-8) -> bool:
Expand Down
3 changes: 2 additions & 1 deletion cirq-core/cirq/linalg/transformations.py
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,8 @@ def to_special(u: np.ndarray) -> np.ndarray:
Returns:
the special unitary matrix
"""
return u * (np.linalg.det(u) ** (-1 / len(u)))
with np.errstate(divide="ignore", invalid="ignore"):
return u * (np.linalg.det(u) ** (-1 / len(u)))


def state_vector_kronecker_product(t1: np.ndarray, t2: np.ndarray) -> np.ndarray:
Expand Down
8 changes: 5 additions & 3 deletions cirq-core/cirq/testing/lin_alg_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ def random_special_unitary(
The sampled special unitary.
"""
r = random_unitary(dim, random_state=random_state)
r[0, :] /= np.linalg.det(r)
with np.errstate(divide="ignore", invalid="ignore"):
r[0, :] /= np.linalg.det(r)
return r


Expand All @@ -152,8 +153,9 @@ def random_special_orthogonal(
The sampled special orthogonal matrix.
"""
m = random_orthogonal(dim, random_state=random_state)
if np.linalg.det(m) < 0:
m[0, :] *= -1
with np.errstate(divide="ignore", invalid="ignore"):
if np.linalg.det(m) < 0:
m[0, :] *= -1
return m


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ def _decompose_abc(matrix: np.ndarray) -> Tuple[np.ndarray, np.ndarray, np.ndarr
See [1], chapter 4.
"""
assert matrix.shape == (2, 2)
delta = np.angle(np.linalg.det(matrix)) * 0.5
with np.errstate(divide="ignore", invalid="ignore"):
# On MacOS, np.linalg.det emits superflous warnings
delta = np.angle(np.linalg.det(matrix)) * 0.5
alpha = np.angle(matrix[0, 0]) + np.angle(matrix[0, 1]) - 2 * delta
beta = np.angle(matrix[0, 0]) - np.angle(matrix[0, 1])

Expand Down

0 comments on commit cf86dda

Please sign in to comment.