From 8be795f857df07bd7c94e19de106cfb98331b5e7 Mon Sep 17 00:00:00 2001 From: Renato Mello Date: Thu, 12 Oct 2023 10:54:35 +0400 Subject: [PATCH 1/5] implement `backend.calculate_overlap_density_matrix` --- src/qibo/backends/numpy.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/qibo/backends/numpy.py b/src/qibo/backends/numpy.py index b0447bd56e..8cf3552562 100644 --- a/src/qibo/backends/numpy.py +++ b/src/qibo/backends/numpy.py @@ -687,7 +687,9 @@ def calculate_overlap(self, state1, state2): return self.np.abs(self.np.sum(self.np.conj(state1) * state2)) def calculate_overlap_density_matrix(self, state1, state2): - raise_error(NotImplementedError) + state1 = self.cast(state1) + state2 = self.cast(state2) + return self.np.trace(self.np.transpose(self.np.conj(state1)) @ state2) def calculate_eigenvalues(self, matrix, k=6): if self.issparse(matrix): From 4e728bbcf5f47e685d0085e5a31bf4679c720f9a Mon Sep 17 00:00:00 2001 From: Renato Mello Date: Thu, 12 Oct 2023 10:55:52 +0400 Subject: [PATCH 2/5] fix docstring --- src/qibo/backends/abstract.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/qibo/backends/abstract.py b/src/qibo/backends/abstract.py index 14d1dc752c..a00613e2e0 100644 --- a/src/qibo/backends/abstract.py +++ b/src/qibo/backends/abstract.py @@ -342,7 +342,7 @@ def calculate_overlap(self, state1, state2): # pragma: no cover @abc.abstractmethod def calculate_overlap_density_matrix(self, state1, state2): # pragma: no cover - """Calculate norm of two density matrices.""" + """Calculate overlap of two density matrices.""" raise_error(NotImplementedError) @abc.abstractmethod From bac770516fdb1b8a09555bb142267d4d1a5ef7b6 Mon Sep 17 00:00:00 2001 From: Renato Mello Date: Thu, 12 Oct 2023 11:11:34 +0400 Subject: [PATCH 3/5] rewritting test --- tests/test_callbacks.py | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/tests/test_callbacks.py b/tests/test_callbacks.py index 76911f3b2e..d86f6e5d31 100644 --- a/tests/test_callbacks.py +++ b/tests/test_callbacks.py @@ -7,6 +7,7 @@ # Absolute testing tolerance for the cases of zero entanglement entropy from qibo.config import PRECISION_TOL from qibo.models import AdiabaticEvolution, Circuit +from qibo.quantum_info.random_ensembles import random_density_matrix, random_statevector def test_abstract_callback_properties(): @@ -341,19 +342,29 @@ def test_norm(backend, density_matrix): backend.assert_allclose(final_norm, target_norm) +@pytest.mark.parametrize("seed", list(range(1, 5 + 1))) @pytest.mark.parametrize("density_matrix", [False, True]) -def test_overlap(backend, density_matrix): - state0 = np.random.random(4) + 1j * np.random.random(4) - state1 = np.random.random(4) + 1j * np.random.random(4) +@pytest.mark.parametrize("nqubits", list(range(2, 6 + 1, 2))) +def test_overlap(backend, nqubits, density_matrix, seed): + dims = 2**nqubits + if density_matrix: + state0 = random_density_matrix(dims, seed=seed, backend=backend) + state1 = random_density_matrix(dims, seed=seed + 1, backend=backend) + else: + state0 = random_statevector(dims, seed=seed, backend=backend) + state1 = random_statevector(dims, seed=seed + 1, backend=backend) + overlap = callbacks.Overlap(state0) - overlap.nqubits = 2 + overlap.nqubits = nqubits + if density_matrix: - with pytest.raises(NotImplementedError): - overlap.apply_density_matrix(backend, state1) + final_overlap = overlap.apply_density_matrix(backend, state1) + target_overlap = np.trace(np.transpose(np.conj(state0)) @ state1) else: final_overlap = overlap.apply(backend, state1) target_overlap = np.abs((state0.conj() * state1).sum()) - backend.assert_allclose(final_overlap, target_overlap) + + backend.assert_allclose(final_overlap, target_overlap) @pytest.mark.parametrize("density_matrix", [False, True]) From 0413e9c1dbb9ff6bca9ba2decd1b9979520de14a Mon Sep 17 00:00:00 2001 From: Renato Mello Date: Thu, 12 Oct 2023 12:40:03 +0400 Subject: [PATCH 4/5] remove pragma no cover --- src/qibo/callbacks.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/qibo/callbacks.py b/src/qibo/callbacks.py index f68e8d2312..2d96f795ba 100644 --- a/src/qibo/callbacks.py +++ b/src/qibo/callbacks.py @@ -206,7 +206,7 @@ def apply(self, backend, state): self.append(overlap) return overlap - def apply_density_matrix(self, backend, state): # pragma: no cover + def apply_density_matrix(self, backend, state): overlap = backend.calculate_overlap_density_matrix(self.state, state) self.append(overlap) return overlap From 79598b17d8aa1ad63680a3356c9bbfcc99073c4e Mon Sep 17 00:00:00 2001 From: Renato Mello Date: Thu, 12 Oct 2023 12:40:14 +0400 Subject: [PATCH 5/5] fix tensorflow test --- tests/test_callbacks.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_callbacks.py b/tests/test_callbacks.py index d86f6e5d31..67e2b2a9d6 100644 --- a/tests/test_callbacks.py +++ b/tests/test_callbacks.py @@ -362,7 +362,7 @@ def test_overlap(backend, nqubits, density_matrix, seed): target_overlap = np.trace(np.transpose(np.conj(state0)) @ state1) else: final_overlap = overlap.apply(backend, state1) - target_overlap = np.abs((state0.conj() * state1).sum()) + target_overlap = np.abs(np.sum(np.conj(state0) * state1)) backend.assert_allclose(final_overlap, target_overlap)