From 540582db4e6b54ad2b1e400e0c577d15be85bdb2 Mon Sep 17 00:00:00 2001 From: Edoardo-Pedicillo Date: Tue, 7 Nov 2023 15:23:40 +0400 Subject: [PATCH 1/8] add off_diag property --- src/qibo/models/double_bracket.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/qibo/models/double_bracket.py b/src/qibo/models/double_bracket.py index ca7cde912a..2ddf5478f0 100644 --- a/src/qibo/models/double_bracket.py +++ b/src/qibo/models/double_bracket.py @@ -82,14 +82,19 @@ def diagonal_h_matrix(self): """Diagonal H matrix.""" return self.backend.cast(np.diag(np.diag(self.backend.to_numpy(self.h.matrix)))) + @property + def off_diag_h(self): + return self.h.matrix - self.diagonal_h_matrix + @property def off_diagonal_norm(self): """Norm of off-diagonal part of H matrix.""" - off_diag_h = self.h.matrix - self.diagonal_h_matrix off_diag_h_dag = self.backend.cast( - np.matrix(self.backend.to_numpy(off_diag_h)).getH() + np.matrix(self.backend.to_numpy(self.off_diag_h)).getH() + ) + return np.real( + np.trace(self.backend.to_numpy(off_diag_h_dag @ self.off_diag_h)) ) - return np.real(np.trace(self.backend.to_numpy(off_diag_h_dag @ off_diag_h))) @property def backend(self): From 0eb869310f2688723126340b636bf161de8805f0 Mon Sep 17 00:00:00 2001 From: Edoardo-Pedicillo Date: Tue, 7 Nov 2023 20:48:43 +0400 Subject: [PATCH 2/8] deploy energy fluctuations --- src/qibo/models/double_bracket.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/qibo/models/double_bracket.py b/src/qibo/models/double_bracket.py index 2ddf5478f0..eb09f98f98 100644 --- a/src/qibo/models/double_bracket.py +++ b/src/qibo/models/double_bracket.py @@ -100,3 +100,11 @@ def off_diagonal_norm(self): def backend(self): """Get Hamiltonian's backend.""" return self.h0.backend + + def energy_fluctuation(self, state): + """Evaluate energy fluctuations""" + energy = self.h.expectation(state) + h = self.h.matrix + h2 = self.backend.to_numpy(np.asarray(h @ h)) + average_h2 = self.backend.calculate_expectation_state(h2, state, normalize=True) + return np.sqrt(average_h2 - energy**2) From 6784d15845477464b720ed0d69c037df377041ff Mon Sep 17 00:00:00 2001 From: Edoardo-Pedicillo Date: Tue, 7 Nov 2023 22:22:43 +0400 Subject: [PATCH 3/8] fix bug to run cupy --- src/qibo/models/double_bracket.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/qibo/models/double_bracket.py b/src/qibo/models/double_bracket.py index eb09f98f98..f9a6ffac49 100644 --- a/src/qibo/models/double_bracket.py +++ b/src/qibo/models/double_bracket.py @@ -2,6 +2,8 @@ import numpy as np +from qibo.backends.numpy import NumpyBackend + from ..config import raise_error from ..hamiltonians import Hamiltonian @@ -105,6 +107,8 @@ def energy_fluctuation(self, state): """Evaluate energy fluctuations""" energy = self.h.expectation(state) h = self.h.matrix - h2 = self.backend.to_numpy(np.asarray(h @ h)) + h2 = h @ h + if self.backend.name == "numpy": + h2 = np.asarray(h2) average_h2 = self.backend.calculate_expectation_state(h2, state, normalize=True) return np.sqrt(average_h2 - energy**2) From 14e0fcac92d6e56494cc7321ae19b31971c443c3 Mon Sep 17 00:00:00 2001 From: Edoardo-Pedicillo Date: Tue, 7 Nov 2023 22:25:16 +0400 Subject: [PATCH 4/8] fix cupy with Andrea's suggestion --- src/qibo/models/double_bracket.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/qibo/models/double_bracket.py b/src/qibo/models/double_bracket.py index f9a6ffac49..8364583c44 100644 --- a/src/qibo/models/double_bracket.py +++ b/src/qibo/models/double_bracket.py @@ -107,8 +107,6 @@ def energy_fluctuation(self, state): """Evaluate energy fluctuations""" energy = self.h.expectation(state) h = self.h.matrix - h2 = h @ h - if self.backend.name == "numpy": - h2 = np.asarray(h2) + h2 = Hamiltonian(nqubits=self.h.nqubits, matrix=h @ h) average_h2 = self.backend.calculate_expectation_state(h2, state, normalize=True) return np.sqrt(average_h2 - energy**2) From 7fd24d2996b3dcb0107b347c1f916f46b7ddb154 Mon Sep 17 00:00:00 2001 From: Edoardo-Pedicillo Date: Wed, 8 Nov 2023 19:02:45 +0400 Subject: [PATCH 5/8] tests --- tests/test_models_dbf.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/test_models_dbf.py b/tests/test_models_dbf.py index 722de9df41..8c770163d3 100644 --- a/tests/test_models_dbf.py +++ b/tests/test_models_dbf.py @@ -2,6 +2,7 @@ import numpy as np import pytest +from qibo.backends import GlobalBackend from qibo.hamiltonians import Hamiltonian from qibo.models.double_bracket import DoubleBracketFlow, FlowGeneratorType from qibo.quantum_info import random_hermitian @@ -51,3 +52,11 @@ def test_double_bracket_flow_single_commutator(backend, nqubits): dbf(mode=FlowGeneratorType.single_commutator, step=0.01, d=d) assert initial_off_diagonal_norm > dbf.off_diagonal_norm + + +def test_energy_fluctuations(backend): + h0 = GlobalBackend().cast(np.array([[1, 0], [0, -1]])) + state = GlobalBackend().cast([1, 0]) + dbf = DoubleBracketFlow(Hamiltonian(1, matrix=h0, backend=backend)) + energy_fluctuation = dbf.energy_fluctuation(state=state) + assert energy_fluctuation == 0 From 2c01fb90eb417bcfa592aacb8393aef599b40b45 Mon Sep 17 00:00:00 2001 From: Edoardo-Pedicillo Date: Wed, 8 Nov 2023 19:51:31 +0400 Subject: [PATCH 6/8] fix test --- tests/test_models_dbf.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_models_dbf.py b/tests/test_models_dbf.py index 8c770163d3..58289509ca 100644 --- a/tests/test_models_dbf.py +++ b/tests/test_models_dbf.py @@ -55,8 +55,8 @@ def test_double_bracket_flow_single_commutator(backend, nqubits): def test_energy_fluctuations(backend): - h0 = GlobalBackend().cast(np.array([[1, 0], [0, -1]])) - state = GlobalBackend().cast([1, 0]) - dbf = DoubleBracketFlow(Hamiltonian(1, matrix=h0, backend=backend)) + h0 = np.array([[1, 0], [0, -1]]) + state = np.array([1, 0]) + dbf = DoubleBracketFlow(Hamiltonian(1, matrix=h0)) energy_fluctuation = dbf.energy_fluctuation(state=state) assert energy_fluctuation == 0 From bfcd960a963a864ebf94fcc871169082575ffff1 Mon Sep 17 00:00:00 2001 From: Edoardo Pedicillo Date: Mon, 13 Nov 2023 09:25:08 +0400 Subject: [PATCH 7/8] Update src/qibo/models/double_bracket.py Co-authored-by: Andrea Pasquale --- src/qibo/models/double_bracket.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/qibo/models/double_bracket.py b/src/qibo/models/double_bracket.py index 8364583c44..674575ae53 100644 --- a/src/qibo/models/double_bracket.py +++ b/src/qibo/models/double_bracket.py @@ -107,6 +107,6 @@ def energy_fluctuation(self, state): """Evaluate energy fluctuations""" energy = self.h.expectation(state) h = self.h.matrix - h2 = Hamiltonian(nqubits=self.h.nqubits, matrix=h @ h) + h2 = Hamiltonian(nqubits=self.h.nqubits, matrix=h @ h, backend=self.backend) average_h2 = self.backend.calculate_expectation_state(h2, state, normalize=True) return np.sqrt(average_h2 - energy**2) From ac5ff4addde662163911c58a967a1e422e35707f Mon Sep 17 00:00:00 2001 From: Edoardo Pedicillo Date: Mon, 13 Nov 2023 09:25:13 +0400 Subject: [PATCH 8/8] Update tests/test_models_dbf.py Co-authored-by: Andrea Pasquale --- tests/test_models_dbf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_models_dbf.py b/tests/test_models_dbf.py index 58289509ca..599ac16335 100644 --- a/tests/test_models_dbf.py +++ b/tests/test_models_dbf.py @@ -57,6 +57,6 @@ def test_double_bracket_flow_single_commutator(backend, nqubits): def test_energy_fluctuations(backend): h0 = np.array([[1, 0], [0, -1]]) state = np.array([1, 0]) - dbf = DoubleBracketFlow(Hamiltonian(1, matrix=h0)) + dbf = DoubleBracketFlow(Hamiltonian(1, matrix=h0, backend=backend)) energy_fluctuation = dbf.energy_fluctuation(state=state) assert energy_fluctuation == 0