Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Nov 12, 2024
1 parent e4333d2 commit 4e4f0e6
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 59 deletions.
8 changes: 2 additions & 6 deletions src/qibo/quantum_info/entropies.py
Original file line number Diff line number Diff line change
Expand Up @@ -783,9 +783,7 @@ def renyi_entropy(state, alpha: Union[float, int], base: float = 2, backend=None
if alpha == np.inf:
return (
-1
* backend.np.log2(
backend.calculate_matrix_norm(state, order=2)
)
* backend.np.log2(backend.calculate_matrix_norm(state, order=2))
/ np.log2(base)
)

Expand Down Expand Up @@ -892,9 +890,7 @@ def relative_renyi_entropy(
new_target = matrix_power(target, 0.5, backend=backend)

log = backend.np.log2(
backend.calculate_matrix_norm(
new_state @ new_target, order=1
)
backend.calculate_matrix_norm(new_state @ new_target, order=1)
)

return -2 * log / np.log2(base)
Expand Down
4 changes: 1 addition & 3 deletions src/qibo/quantum_info/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -929,9 +929,7 @@ def _check_hermitian(matrix, backend=None):
"""
backend = _check_backend(backend)

norm = backend.calculate_matrix_norm(
backend.np.conj(matrix).T - matrix, order=2
)
norm = backend.calculate_matrix_norm(backend.np.conj(matrix).T - matrix, order=2)

hermitian = bool(float(norm) <= PRECISION_TOL)

Expand Down
4 changes: 1 addition & 3 deletions src/qibo/quantum_info/superoperator_transformations.py
Original file line number Diff line number Diff line change
Expand Up @@ -2132,9 +2132,7 @@ def function(x0, operators):
for prob, oper in zip(x0, operators):
operator = operator + prob * oper

return float(
backend.calculate_matrix_norm(target - operator, order=2)
)
return float(backend.calculate_matrix_norm(target - operator, order=2))

# initial parameters as flat distribution
x0 = [1.0 / (len(kraus_ops) + 1)] * len(kraus_ops)
Expand Down
4 changes: 1 addition & 3 deletions src/qibo/transpiler/unitary_decompositions.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,7 @@ def calculate_single_qubit_unitaries(psi, backend=None):
"""
psi_magic = backend.np.matmul(backend.np.conj(backend.cast(magic_basis)).T, psi)
if (
backend.np.real(
backend.calculate_matrix_norm(backend.np.imag(psi_magic))
)
backend.np.real(backend.calculate_matrix_norm(backend.np.imag(psi_magic)))
> 1e-6
): # pragma: no cover
raise_error(NotImplementedError, "Given state is not real in the magic basis.")
Expand Down
6 changes: 1 addition & 5 deletions tests/test_gates_channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,11 +383,7 @@ def test_thermal_relaxation_channel(backend, t_1, t_2, time, excpop):
target_state = backend.cast(target_state, dtype=target_state.dtype)

backend.assert_allclose(
float(
backend.calculate_matrix_norm(
final_state - target_state, order=2
)
)
float(backend.calculate_matrix_norm(final_state - target_state, order=2))
< PRECISION_TOL,
True,
)
Expand Down
4 changes: 1 addition & 3 deletions tests/test_quantum_info_entropies.py
Original file line number Diff line number Diff line change
Expand Up @@ -687,9 +687,7 @@ def test_relative_renyi_entropy(backend, alpha, base, state_flag, target_flag):
new_target = matrix_power(target_outer, 0.5, backend=backend)

log = backend.np.log2(
backend.calculate_matrix_norm(
new_state @ new_target, order=1
)
backend.calculate_matrix_norm(new_state @ new_target, order=1)
)

log = -2 * log / np.log2(base)
Expand Down
46 changes: 10 additions & 36 deletions tests/test_quantum_info_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,18 +112,14 @@ def test_random_hermitian(backend):
dims = 4
matrix = random_hermitian(dims, backend=backend)
matrix_dagger = backend.np.conj(matrix).T
norm = float(
backend.calculate_matrix_norm(matrix - matrix_dagger, order=2)
)
norm = float(backend.calculate_matrix_norm(matrix - matrix_dagger, order=2))
backend.assert_allclose(norm < PRECISION_TOL, True)

# test if function returns semidefinite Hermitian operator
dims = 4
matrix = random_hermitian(dims, semidefinite=True, backend=backend)
matrix_dagger = backend.np.conj(matrix).T
norm = float(
backend.calculate_matrix_norm(matrix - matrix_dagger, order=2)
)
norm = float(backend.calculate_matrix_norm(matrix - matrix_dagger, order=2))
backend.assert_allclose(norm < PRECISION_TOL, True)

eigenvalues = np.linalg.eigvalsh(backend.to_numpy(matrix))
Expand All @@ -134,9 +130,7 @@ def test_random_hermitian(backend):
dims = 4
matrix = random_hermitian(dims, normalize=True, backend=backend)
matrix_dagger = backend.np.conj(matrix).T
norm = float(
backend.calculate_matrix_norm(matrix - matrix_dagger, order=2)
)
norm = float(backend.calculate_matrix_norm(matrix - matrix_dagger, order=2))
backend.assert_allclose(norm < PRECISION_TOL, True)

eigenvalues = np.linalg.eigvalsh(backend.to_numpy(matrix))
Expand Down Expand Up @@ -183,11 +177,7 @@ def test_random_unitary(backend, measure):
if backend.name == "pytorch"
else np.linalg.inv(matrix)
)
norm = float(
backend.calculate_matrix_norm(
matrix_inv - matrix_dagger, order=2
)
)
norm = float(backend.calculate_matrix_norm(matrix_inv - matrix_dagger, order=2))
backend.assert_allclose(norm < PRECISION_TOL, True)


Expand Down Expand Up @@ -283,9 +273,7 @@ def test_random_density_matrix(backend, dims, pure, metric, basis, normalize):
test = random_density_matrix(dims=dims, normalize=True)
else:
norm_function = (
backend.calculate_matrix_norm
if basis is None
else backend.calculate_norm
backend.calculate_matrix_norm if basis is None else backend.calculate_norm
)
state = random_density_matrix(
dims,
Expand Down Expand Up @@ -423,9 +411,7 @@ def test_pauli_single(backend):

backend.assert_allclose(
np.abs(
backend.to_numpy(
backend.calculate_matrix_norm(matrix - result, order=2)
)
backend.to_numpy(backend.calculate_matrix_norm(matrix - result, order=2))
)
< PRECISION_TOL,
True,
Expand Down Expand Up @@ -465,20 +451,14 @@ def test_random_pauli(
if subset is None:
backend.assert_allclose(
float(
backend.calculate_matrix_norm(
matrix - result_complete_set, order=2
)
backend.calculate_matrix_norm(matrix - result_complete_set, order=2)
)
< PRECISION_TOL,
True,
)
else:
backend.assert_allclose(
float(
backend.calculate_matrix_norm(
matrix - result_subset, order=2
)
)
float(backend.calculate_matrix_norm(matrix - result_subset, order=2))
< PRECISION_TOL,
True,
)
Expand All @@ -490,20 +470,14 @@ def test_random_pauli(
if subset is None:
backend.assert_allclose(
float(
backend.calculate_matrix_norm(
matrix - result_complete_set, order=2
)
backend.calculate_matrix_norm(matrix - result_complete_set, order=2)
)
< PRECISION_TOL,
True,
)
else:
backend.assert_allclose(
float(
backend.calculate_matrix_norm(
matrix - result_subset, order=2
)
)
float(backend.calculate_matrix_norm(matrix - result_subset, order=2))
< PRECISION_TOL,
True,
)
Expand Down

0 comments on commit 4e4f0e6

Please sign in to comment.