From eaa14050028616962526e8a6f7eb5522772c2e5c Mon Sep 17 00:00:00 2001 From: Renato Mello Date: Tue, 30 Jan 2024 12:16:26 +0400 Subject: [PATCH] tests --- tests/test_quantum_info_entropies.py | 67 +++++++++++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/tests/test_quantum_info_entropies.py b/tests/test_quantum_info_entropies.py index 3cc2c43f2f..d78ceca58a 100644 --- a/tests/test_quantum_info_entropies.py +++ b/tests/test_quantum_info_entropies.py @@ -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 @@ -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)