Skip to content

Commit

Permalink
test errors
Browse files Browse the repository at this point in the history
  • Loading branch information
renatomello committed Jan 30, 2024
1 parent cb799bd commit e347490
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/qibo/quantum_info/entropies.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Submodule with entropy measures."""
from typing import Union

import numpy as np

Expand Down Expand Up @@ -151,7 +152,9 @@ def classical_relative_entropy(prob_dist_p, prob_dist_q, base: float = 2, backen
return entropy_p - relative


def classical_renyi_entropy(prob_dist, alpha: float, base: float = 2, backend=None):
def classical_renyi_entropy(
prob_dist, alpha: Union[float, int], base: float = 2, backend=None
):
"""Calculates the classical Rényi entropy :math:`H_{\\alpha}` of a discrete probability distribution.
For :math:`\\alpha \\in (0, \\, 1) \\cup (1, \\, \\infty)` and probability distribution
Expand All @@ -171,7 +174,7 @@ def classical_renyi_entropy(prob_dist, alpha: float, base: float = 2, backend=No
Args:
prob_dist (ndarray): discrete probability distribution.
alpha (float): order of the Rényi entropy.
alpha (float or int): order of the Rényi entropy.
If :math:`\\alpha = 1`, defaults to :func:`qibo.quantum_info.entropies.shannon_entropy`.
If :math:`\\alpha = \\infty`, defaults to the
`min-entropy <https://en.wikipedia.org/wiki/Min-entropy>`_.
Expand All @@ -190,7 +193,7 @@ def classical_renyi_entropy(prob_dist, alpha: float, base: float = 2, backend=No
# np.float64 is necessary instead of native float because of tensorflow
prob_dist = backend.cast(prob_dist, dtype=np.float64)

Check warning on line 194 in src/qibo/quantum_info/entropies.py

View check run for this annotation

Codecov / codecov/patch

src/qibo/quantum_info/entropies.py#L194

Added line #L194 was not covered by tests

if not isinstance(alpha, float):
if not isinstance(alpha, (float, int)):
raise_error(
TypeError, f"alpha must be type float, but it is type {type(alpha)}."
)
Expand Down
43 changes: 43 additions & 0 deletions tests/test_quantum_info_entropies.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from qibo.config import PRECISION_TOL
from qibo.quantum_info.entropies import (
classical_relative_entropy,
classical_renyi_entropy,
entanglement_entropy,
entropy,
relative_entropy,
Expand Down Expand Up @@ -113,6 +114,48 @@ def test_classical_relative_entropy(backend, base, kind):
backend.assert_allclose(divergence, target, atol=1e-5)


@pytest.mark.parametrize("kind", [None, list])
@pytest.mark.parametrize("base", [2, 10, np.e, 5])
@pytest.mark.parametrize("alpha", [1, 2, 3, np.inf])
def test_classical_renyi_entropy(backend, alpha, base, kind):
with pytest.raises(TypeError):
prob = np.array([1.0, 0.0])
prob = backend.cast(prob, dtype=prob.dtype)
test = classical_renyi_entropy(prob, alpha="2", backend=backend)
with pytest.raises(ValueError):
prob = np.array([1.0, 0.0])
prob = backend.cast(prob, dtype=prob.dtype)
test = classical_renyi_entropy(prob, alpha=-2, backend=backend)
with pytest.raises(TypeError):
prob = np.array([1.0, 0.0])
prob = backend.cast(prob, dtype=prob.dtype)
test = classical_renyi_entropy(prob, alpha, base="2", backend=backend)
with pytest.raises(ValueError):
prob = np.array([1.0, 0.0])
prob = backend.cast(prob, dtype=prob.dtype)
test = classical_renyi_entropy(prob, alpha, base=-2, backend=backend)
with pytest.raises(TypeError):
prob = np.array([[1.0], [0.0]])
prob = backend.cast(prob, dtype=prob.dtype)
test = classical_renyi_entropy(prob, alpha, backend=backend)
with pytest.raises(TypeError):
prob = np.array([])
prob = backend.cast(prob, dtype=prob.dtype)
test = classical_renyi_entropy(prob, alpha, backend=backend)
with pytest.raises(ValueError):
prob = np.array([1.0, -1.0])
prob = backend.cast(prob, dtype=prob.dtype)
test = classical_renyi_entropy(prob, alpha, backend=backend)
with pytest.raises(ValueError):
prob = np.array([1.1, 0.0])
prob = backend.cast(prob, dtype=prob.dtype)
test = classical_renyi_entropy(prob, alpha, backend=backend)
with pytest.raises(ValueError):
prob = np.array([0.5, 0.4999999])
prob = backend.cast(prob, dtype=prob.dtype)
test = classical_renyi_entropy(prob, alpha, backend=backend)


@pytest.mark.parametrize("check_hermitian", [False, True])
@pytest.mark.parametrize("base", [2, 10, np.e, 5])
def test_entropy(backend, base, check_hermitian):
Expand Down

0 comments on commit e347490

Please sign in to comment.