Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
renatomello committed Jan 30, 2024
1 parent d004521 commit eaa1405
Showing 1 changed file with 66 additions and 1 deletion.
67 changes: 66 additions & 1 deletion tests/test_quantum_info_entropies.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
import pytest

from qibo.config import PRECISION_TOL
from qibo.quantum_info.entropies import entanglement_entropy, entropy, shannon_entropy
from qibo.quantum_info.entropies import (
classical_relative_entropy,
entanglement_entropy,
entropy,
shannon_entropy,
)
from qibo.quantum_info.random_ensembles import random_statevector, random_unitary


Expand Down Expand Up @@ -168,3 +173,63 @@ def test_entanglement_entropy(backend, bipartition, base, check_hermitian):
)

backend.assert_allclose(entang_entrop, 0.0, atol=PRECISION_TOL)


@pytest.mark.parametrize("kind", [None, list])
@pytest.mark.parametrize("validate", [False, True])
@pytest.mark.parametrize("base", [2, 10, np.e, 5])
def test_classical_relative_entropy(backend, base, validate, kind):
with pytest.raises(TypeError):
prob = np.random.rand(1, 2)
prob_q = np.random.rand(1, 5)
prob = backend.cast(prob, dtype=prob.dtype)
prob_q = backend.cast(prob_q, dtype=prob_q.dtype)
test = classical_relative_entropy(prob, prob_q, backend=backend)
with pytest.raises(TypeError):
prob = np.random.rand(1, 2)[0]
prob_q = np.array([])
prob = backend.cast(prob, dtype=prob.dtype)
prob_q = backend.cast(prob_q, dtype=prob_q.dtype)
test = classical_relative_entropy(prob, prob_q, backend=backend)
with pytest.raises(ValueError):
prob = np.array([-1, 2.0])
prob_q = np.random.rand(1, 5)[0]
prob = backend.cast(prob, dtype=prob.dtype)
prob_q = backend.cast(prob_q, dtype=prob_q.dtype)
test = classical_relative_entropy(prob, prob_q, validate=True, backend=backend)
with pytest.raises(ValueError):
prob = np.random.rand(1, 2)[0]
prob_q = np.array([1.0, 0.0])
prob = backend.cast(prob, dtype=prob.dtype)
prob_q = backend.cast(prob_q, dtype=prob_q.dtype)
test = classical_relative_entropy(prob, prob_q, validate=True, backend=backend)
with pytest.raises(ValueError):
prob = np.array([1.0, 0.0])
prob_q = np.random.rand(1, 2)[0]
prob = backend.cast(prob, dtype=prob.dtype)
prob_q = backend.cast(prob_q, dtype=prob_q.dtype)
test = classical_relative_entropy(prob, prob_q, validate=True, backend=backend)
with pytest.raises(ValueError):
prob = np.array([1.0, 0.0])
prob_q = np.array([0.0, 1.0])
prob = backend.cast(prob, dtype=prob.dtype)
prob_q = backend.cast(prob_q, dtype=prob_q.dtype)
test = classical_relative_entropy(prob, prob_q, base=-2, backend=backend)

prob_p = np.random.rand(10)
prob_q = np.random.rand(10)
prob_p /= np.sum(prob_p)
prob_q /= np.sum(prob_q)

target = np.sum(prob_p * np.log(prob_p) / np.log(base)) - np.sum(
prob_p * np.log(prob_q) / np.log(base)
)

if kind is not None:
prob_p, prob_q = kind(prob_p), kind(prob_q)

divergence = classical_relative_entropy(
prob_p, prob_q, base=base, validate=validate, backend=backend
)

backend.assert_allclose(divergence, target, atol=1e-5)

0 comments on commit eaa1405

Please sign in to comment.