From 6860a4870787fbd6b912a4a7b7667b3e27e74da5 Mon Sep 17 00:00:00 2001
From: Marek Gluza <marekgluza@gmail.com>
Date: Fri, 5 Apr 2024 05:50:46 +0200
Subject: [PATCH 01/16] sign conventions on top of the exposed dbr operator

---
 src/qibo/models/dbi/double_bracket.py | 28 ++++++++++++++++++++-------
 1 file changed, 21 insertions(+), 7 deletions(-)

diff --git a/src/qibo/models/dbi/double_bracket.py b/src/qibo/models/dbi/double_bracket.py
index b0f0704c9d..68b9df3bde 100644
--- a/src/qibo/models/dbi/double_bracket.py
+++ b/src/qibo/models/dbi/double_bracket.py
@@ -57,39 +57,53 @@ def __init__(
     def __call__(
         self, step: float, mode: DoubleBracketGeneratorType = None, d: np.array = None
     ):
+        """ We use convention that $H' = U^\dagger H U$ where $U=e^{-sW}$ with $W=[D,H]$ (or depending on `mode` an approximation, see `eval_dbr_unitary`). If $s>0$ then for $D = \Delta(H)$ the GWW DBR will give a $\sigma$-decrease, see https://arxiv.org/abs/2206.11772."""
+
         operator = self.eval_dbr_unitary(step, mode, d)
         operator_dagger = self.backend.cast(
             np.matrix(self.backend.to_numpy(operator)).getH()
         )
-        self.h.matrix = operator @ self.h.matrix @ operator_dagger
+        self.h.matrix = operator_dagger @ self.h.matrix @ operator
         return operator
 
     def eval_dbr_unitary(
         self, step: float, mode: DoubleBracketGeneratorType = None, d: np.array = None
     ):
+        """ In call we will are working in the convention that $H' = U^\dagger H U$ where $U=e^{-sW}$ with $W=[D,H]$ or an approximation of that by a group commutator. That is handy because if we switch from the DBI in the Heisenberg picture for the Hamiltonian, we get that the transformation of the state is $|\psi'\rangle = U |\psi\rangle$ so that $\langle H\rangle_{\psi'} = \langle H' \rangle_\psi$ (i.e. when writing the unitary acting on the state dagger notation is avoided).
+
+The group commutator must approximate $U=e^{-s[D,H]}$. This is achieved by setting $r = \sqrt{s}$ so that
+$$V = e^{-irH}e^{irD}e^{irH}e^{-irD}$$
+because
+$$e^{-irH}De^{irH} = D+ir[D,H]+O(r^2)$$
+so
+$$V\approx e^{irD +i^2 r^2[D,H] + O(r^2) -irD} \approx U\ .$$
+See the app in https://arxiv.org/abs/2206.11772 for a derivation.
+        """
         if mode is None:
             mode = self.mode
 
         if mode is DoubleBracketGeneratorType.canonical:
-            operator = self.backend.calculate_matrix_exp(
+            operator = self.backend.calculate_matrix_exp(-
                 1.0j * step,
                 self.commutator(self.diagonal_h_matrix, self.h.matrix),
             )
         elif mode is DoubleBracketGeneratorType.single_commutator:
             if d is None:
                 raise_error(ValueError, f"Cannot use group_commutator with matrix {d}")
-            operator = self.backend.calculate_matrix_exp(
+            operator = self.backend.calculate_matrix_exp(-
                 1.0j * step,
                 self.commutator(d, self.h.matrix),
             )
         elif mode is DoubleBracketGeneratorType.group_commutator:
             if d is None:
                 d = self.diagonal_h_matrix
+            
+            sqrt_step = np.sqrt(step)
             operator = (
-                self.h.exp(-step)
-                @ self.backend.calculate_matrix_exp(-step, d)
-                @ self.h.exp(step)
-                @ self.backend.calculate_matrix_exp(step, d)
+                self.h.exp(-sqrt_step)
+                @ self.backend.calculate_matrix_exp(sqrt_step, d)
+                @ self.h.exp(sqrt_step)
+                @ self.backend.calculate_matrix_exp(-sqrt_step, d)
             )
         return operator
 

From 1ebe1c984ad512b852d3aa86fc969bf37ae3eee8 Mon Sep 17 00:00:00 2001
From: Marek Gluza <marekgluza@gmail.com>
Date: Fri, 5 Apr 2024 05:55:47 +0200
Subject: [PATCH 02/16] proposed a test of the exposed dbr unitary function

---
 tests/test_models_dbi.py | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/tests/test_models_dbi.py b/tests/test_models_dbi.py
index a3b22d1bbe..6c26ffa25a 100644
--- a/tests/test_models_dbi.py
+++ b/tests/test_models_dbi.py
@@ -45,6 +45,20 @@ def test_double_bracket_iteration_group_commutator(backend, nqubits):
 
     assert initial_off_diagonal_norm > dbf.off_diagonal_norm
 
+@pytest.mark.parametrize("nqubits", [3])
+def test_double_bracket_iteration_eval_dbr_unitary(backend, nqubits):
+    h0 = random_hermitian(2**nqubits, backend=backend)
+    d = backend.cast(np.diag(np.diag(backend.to_numpy(h0))))
+    dbi = DoubleBracketIteration(
+        Hamiltonian(nqubits, h0, backend=backend),
+        mode=DoubleBracketGeneratorType.group_commutator,
+    )
+
+    for s in np.linspace(0,.01,NSTEPS):
+        u = dbi.eval_dbr_unitary(s,mode = DoubleBracketRotationType.single_commutator)
+        v = dbi.eval_dbr_unitary(s, mode = DoubleBracketRotationType.group_commutator)
+
+        assert np.linalg.norm( u - v ) < 10 * s * np.linalg.norm(h0) * np.linalg.norm(d)
 
 @pytest.mark.parametrize("nqubits", [3, 4, 5])
 def test_double_bracket_iteration_single_commutator(backend, nqubits):

From 2f55e1942f56bf04f41d82ca023c1a29f4f0a9dc Mon Sep 17 00:00:00 2001
From: "pre-commit-ci[bot]"
 <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Date: Fri, 5 Apr 2024 04:03:36 +0000
Subject: [PATCH 03/16] [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci
---
 src/qibo/models/dbi/double_bracket.py | 30 +++++++++++++--------------
 tests/test_models_dbi.py              | 10 +++++----
 2 files changed, 21 insertions(+), 19 deletions(-)

diff --git a/src/qibo/models/dbi/double_bracket.py b/src/qibo/models/dbi/double_bracket.py
index 68b9df3bde..f6b4b7dca0 100644
--- a/src/qibo/models/dbi/double_bracket.py
+++ b/src/qibo/models/dbi/double_bracket.py
@@ -57,7 +57,7 @@ def __init__(
     def __call__(
         self, step: float, mode: DoubleBracketGeneratorType = None, d: np.array = None
     ):
-        """ We use convention that $H' = U^\dagger H U$ where $U=e^{-sW}$ with $W=[D,H]$ (or depending on `mode` an approximation, see `eval_dbr_unitary`). If $s>0$ then for $D = \Delta(H)$ the GWW DBR will give a $\sigma$-decrease, see https://arxiv.org/abs/2206.11772."""
+        r"""We use convention that $H' = U^\dagger H U$ where $U=e^{-sW}$ with $W=[D,H]$ (or depending on `mode` an approximation, see `eval_dbr_unitary`). If $s>0$ then for $D = \Delta(H)$ the GWW DBR will give a $\sigma$-decrease, see https://arxiv.org/abs/2206.11772."""
 
         operator = self.eval_dbr_unitary(step, mode, d)
         operator_dagger = self.backend.cast(
@@ -69,35 +69,35 @@ def __call__(
     def eval_dbr_unitary(
         self, step: float, mode: DoubleBracketGeneratorType = None, d: np.array = None
     ):
-        """ In call we will are working in the convention that $H' = U^\dagger H U$ where $U=e^{-sW}$ with $W=[D,H]$ or an approximation of that by a group commutator. That is handy because if we switch from the DBI in the Heisenberg picture for the Hamiltonian, we get that the transformation of the state is $|\psi'\rangle = U |\psi\rangle$ so that $\langle H\rangle_{\psi'} = \langle H' \rangle_\psi$ (i.e. when writing the unitary acting on the state dagger notation is avoided).
-
-The group commutator must approximate $U=e^{-s[D,H]}$. This is achieved by setting $r = \sqrt{s}$ so that
-$$V = e^{-irH}e^{irD}e^{irH}e^{-irD}$$
-because
-$$e^{-irH}De^{irH} = D+ir[D,H]+O(r^2)$$
-so
-$$V\approx e^{irD +i^2 r^2[D,H] + O(r^2) -irD} \approx U\ .$$
-See the app in https://arxiv.org/abs/2206.11772 for a derivation.
+        """In call we will are working in the convention that $H' = U^\\dagger H U$ where $U=e^{-sW}$ with $W=[D,H]$ or an approximation of that by a group commutator. That is handy because if we switch from the DBI in the Heisenberg picture for the Hamiltonian, we get that the transformation of the state is $|\\psi'\rangle = U |\\psi\rangle$ so that $\\langle H\rangle_{\\psi'} = \\langle H' \rangle_\\psi$ (i.e. when writing the unitary acting on the state dagger notation is avoided).
+
+        The group commutator must approximate $U=e^{-s[D,H]}$. This is achieved by setting $r = \\sqrt{s}$ so that
+        $$V = e^{-irH}e^{irD}e^{irH}e^{-irD}$$
+        because
+        $$e^{-irH}De^{irH} = D+ir[D,H]+O(r^2)$$
+        so
+        $$V\approx e^{irD +i^2 r^2[D,H] + O(r^2) -irD} \approx U\\ .$$
+        See the app in https://arxiv.org/abs/2206.11772 for a derivation.
         """
         if mode is None:
             mode = self.mode
 
         if mode is DoubleBracketGeneratorType.canonical:
-            operator = self.backend.calculate_matrix_exp(-
-                1.0j * step,
+            operator = self.backend.calculate_matrix_exp(
+                -1.0j * step,
                 self.commutator(self.diagonal_h_matrix, self.h.matrix),
             )
         elif mode is DoubleBracketGeneratorType.single_commutator:
             if d is None:
                 raise_error(ValueError, f"Cannot use group_commutator with matrix {d}")
-            operator = self.backend.calculate_matrix_exp(-
-                1.0j * step,
+            operator = self.backend.calculate_matrix_exp(
+                -1.0j * step,
                 self.commutator(d, self.h.matrix),
             )
         elif mode is DoubleBracketGeneratorType.group_commutator:
             if d is None:
                 d = self.diagonal_h_matrix
-            
+
             sqrt_step = np.sqrt(step)
             operator = (
                 self.h.exp(-sqrt_step)
diff --git a/tests/test_models_dbi.py b/tests/test_models_dbi.py
index 6c26ffa25a..b5e781fb37 100644
--- a/tests/test_models_dbi.py
+++ b/tests/test_models_dbi.py
@@ -45,6 +45,7 @@ def test_double_bracket_iteration_group_commutator(backend, nqubits):
 
     assert initial_off_diagonal_norm > dbf.off_diagonal_norm
 
+
 @pytest.mark.parametrize("nqubits", [3])
 def test_double_bracket_iteration_eval_dbr_unitary(backend, nqubits):
     h0 = random_hermitian(2**nqubits, backend=backend)
@@ -54,11 +55,12 @@ def test_double_bracket_iteration_eval_dbr_unitary(backend, nqubits):
         mode=DoubleBracketGeneratorType.group_commutator,
     )
 
-    for s in np.linspace(0,.01,NSTEPS):
-        u = dbi.eval_dbr_unitary(s,mode = DoubleBracketRotationType.single_commutator)
-        v = dbi.eval_dbr_unitary(s, mode = DoubleBracketRotationType.group_commutator)
+    for s in np.linspace(0, 0.01, NSTEPS):
+        u = dbi.eval_dbr_unitary(s, mode=DoubleBracketRotationType.single_commutator)
+        v = dbi.eval_dbr_unitary(s, mode=DoubleBracketRotationType.group_commutator)
+
+        assert np.linalg.norm(u - v) < 10 * s * np.linalg.norm(h0) * np.linalg.norm(d)
 
-        assert np.linalg.norm( u - v ) < 10 * s * np.linalg.norm(h0) * np.linalg.norm(d)
 
 @pytest.mark.parametrize("nqubits", [3, 4, 5])
 def test_double_bracket_iteration_single_commutator(backend, nqubits):

From 3eb7029d671a1b799093646148347b1cf4eda8d1 Mon Sep 17 00:00:00 2001
From: Marek Gluza <marekgluza@gmail.com>
Date: Fri, 5 Apr 2024 08:02:35 +0200
Subject: [PATCH 04/16] improving the bound implementation

---
 tests/test_models_dbi.py | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/tests/test_models_dbi.py b/tests/test_models_dbi.py
index 6c26ffa25a..61bc6aadaa 100644
--- a/tests/test_models_dbi.py
+++ b/tests/test_models_dbi.py
@@ -47,6 +47,7 @@ def test_double_bracket_iteration_group_commutator(backend, nqubits):
 
 @pytest.mark.parametrize("nqubits", [3])
 def test_double_bracket_iteration_eval_dbr_unitary(backend, nqubits):
+    """ The bound is $$||e^{-[D,H]}-GC||\le s^{3/2}(||[H,[D,H]||+||[D,[D,H]]||$$"""
     h0 = random_hermitian(2**nqubits, backend=backend)
     d = backend.cast(np.diag(np.diag(backend.to_numpy(h0))))
     dbi = DoubleBracketIteration(
@@ -58,7 +59,7 @@ def test_double_bracket_iteration_eval_dbr_unitary(backend, nqubits):
         u = dbi.eval_dbr_unitary(s,mode = DoubleBracketRotationType.single_commutator)
         v = dbi.eval_dbr_unitary(s, mode = DoubleBracketRotationType.group_commutator)
 
-        assert np.linalg.norm( u - v ) < 10 * s * np.linalg.norm(h0) * np.linalg.norm(d)
+        assert np.linalg.norm( u - v ) < 10 * s**(1.49) (  np.linalg.norm(h0) + np.linalg.norm(d)) * np.linalg.norm(h0) * np.linalg.norm(d)
 
 @pytest.mark.parametrize("nqubits", [3, 4, 5])
 def test_double_bracket_iteration_single_commutator(backend, nqubits):

From 42c1c929f66711f6e6200bf8b1a5551c74ebedf6 Mon Sep 17 00:00:00 2001
From: "pre-commit-ci[bot]"
 <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Date: Fri, 5 Apr 2024 06:04:50 +0000
Subject: [PATCH 05/16] [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci
---
 tests/test_models_dbi.py | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/tests/test_models_dbi.py b/tests/test_models_dbi.py
index eb5db84dc8..e6f9a9dd4f 100644
--- a/tests/test_models_dbi.py
+++ b/tests/test_models_dbi.py
@@ -48,7 +48,7 @@ def test_double_bracket_iteration_group_commutator(backend, nqubits):
 
 @pytest.mark.parametrize("nqubits", [3])
 def test_double_bracket_iteration_eval_dbr_unitary(backend, nqubits):
-    """ The bound is $$||e^{-[D,H]}-GC||\le s^{3/2}(||[H,[D,H]||+||[D,[D,H]]||$$"""
+    r"""The bound is $$||e^{-[D,H]}-GC||\le s^{3/2}(||[H,[D,H]||+||[D,[D,H]]||$$"""
     h0 = random_hermitian(2**nqubits, backend=backend)
     d = backend.cast(np.diag(np.diag(backend.to_numpy(h0))))
     dbi = DoubleBracketIteration(
@@ -60,7 +60,10 @@ def test_double_bracket_iteration_eval_dbr_unitary(backend, nqubits):
         u = dbi.eval_dbr_unitary(s, mode=DoubleBracketRotationType.single_commutator)
         v = dbi.eval_dbr_unitary(s, mode=DoubleBracketRotationType.group_commutator)
 
-        assert np.linalg.norm( u - v ) < 10 * s**(1.49) (  np.linalg.norm(h0) + np.linalg.norm(d)) * np.linalg.norm(h0) * np.linalg.norm(d)
+        assert np.linalg.norm(u - v) < 10 * s ** (1.49)(
+            np.linalg.norm(h0) + np.linalg.norm(d)
+        ) * np.linalg.norm(h0) * np.linalg.norm(d)
+
 
 @pytest.mark.parametrize("nqubits", [3, 4, 5])
 def test_double_bracket_iteration_single_commutator(backend, nqubits):

From 18517e852041e140614c6bd273823fd26984a366 Mon Sep 17 00:00:00 2001
From: Marek Gluza <marekgluza@gmail.com>
Date: Fri, 5 Apr 2024 09:24:29 +0200
Subject: [PATCH 06/16] Fixing the type which was already set for a future
 convention

---
 tests/test_models_dbi.py | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/tests/test_models_dbi.py b/tests/test_models_dbi.py
index e6f9a9dd4f..8ca7fef077 100644
--- a/tests/test_models_dbi.py
+++ b/tests/test_models_dbi.py
@@ -57,8 +57,8 @@ def test_double_bracket_iteration_eval_dbr_unitary(backend, nqubits):
     )
 
     for s in np.linspace(0, 0.01, NSTEPS):
-        u = dbi.eval_dbr_unitary(s, mode=DoubleBracketRotationType.single_commutator)
-        v = dbi.eval_dbr_unitary(s, mode=DoubleBracketRotationType.group_commutator)
+        u = dbi.eval_dbr_unitary(s, mode=DoubleBracketGeneratorType.single_commutator)
+        v = dbi.eval_dbr_unitary(s, mode=DoubleBracketGeneratorType.group_commutator)
 
         assert np.linalg.norm(u - v) < 10 * s ** (1.49)(
             np.linalg.norm(h0) + np.linalg.norm(d)

From 8cba7ad51773290e8103f16191aaeca5bb78551e Mon Sep 17 00:00:00 2001
From: Marek Gluza <marekgluza@gmail.com>
Date: Fri, 5 Apr 2024 09:39:16 +0200
Subject: [PATCH 07/16] fixing typo in test file

---
 tests/test_models_dbi.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tests/test_models_dbi.py b/tests/test_models_dbi.py
index 8ca7fef077..7b5249566b 100644
--- a/tests/test_models_dbi.py
+++ b/tests/test_models_dbi.py
@@ -60,7 +60,7 @@ def test_double_bracket_iteration_eval_dbr_unitary(backend, nqubits):
         u = dbi.eval_dbr_unitary(s, mode=DoubleBracketGeneratorType.single_commutator)
         v = dbi.eval_dbr_unitary(s, mode=DoubleBracketGeneratorType.group_commutator)
 
-        assert np.linalg.norm(u - v) < 10 * s ** (1.49)(
+        assert np.linalg.norm(u - v) < 10 * s ** 1.49 * (
             np.linalg.norm(h0) + np.linalg.norm(d)
         ) * np.linalg.norm(h0) * np.linalg.norm(d)
 

From 0e281b4972b03e5813aab2995b53c49ec030e2ea Mon Sep 17 00:00:00 2001
From: "pre-commit-ci[bot]"
 <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Date: Fri, 5 Apr 2024 07:39:47 +0000
Subject: [PATCH 08/16] [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci
---
 tests/test_models_dbi.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tests/test_models_dbi.py b/tests/test_models_dbi.py
index 7b5249566b..e700929f3f 100644
--- a/tests/test_models_dbi.py
+++ b/tests/test_models_dbi.py
@@ -60,7 +60,7 @@ def test_double_bracket_iteration_eval_dbr_unitary(backend, nqubits):
         u = dbi.eval_dbr_unitary(s, mode=DoubleBracketGeneratorType.single_commutator)
         v = dbi.eval_dbr_unitary(s, mode=DoubleBracketGeneratorType.group_commutator)
 
-        assert np.linalg.norm(u - v) < 10 * s ** 1.49 * (
+        assert np.linalg.norm(u - v) < 10 * s**1.49 * (
             np.linalg.norm(h0) + np.linalg.norm(d)
         ) * np.linalg.norm(h0) * np.linalg.norm(d)
 

From 6871133093c296c0e92b4d2d1beb94bebc1c4cf2 Mon Sep 17 00:00:00 2001
From: Marek Gluza <marekgluza@gmail.com>
Date: Fri, 5 Apr 2024 09:40:44 +0200
Subject: [PATCH 09/16] now the test should run otherwise it entered into 0<0

---
 tests/test_models_dbi.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tests/test_models_dbi.py b/tests/test_models_dbi.py
index 7b5249566b..5faa22757c 100644
--- a/tests/test_models_dbi.py
+++ b/tests/test_models_dbi.py
@@ -56,7 +56,7 @@ def test_double_bracket_iteration_eval_dbr_unitary(backend, nqubits):
         mode=DoubleBracketGeneratorType.group_commutator,
     )
 
-    for s in np.linspace(0, 0.01, NSTEPS):
+    for s in np.linspace(0.001, 0.01, NSTEPS):
         u = dbi.eval_dbr_unitary(s, mode=DoubleBracketGeneratorType.single_commutator)
         v = dbi.eval_dbr_unitary(s, mode=DoubleBracketGeneratorType.group_commutator)
 

From 34405cb600e0f40886a7cdb8ca5e29c1965a4eea Mon Sep 17 00:00:00 2001
From: Marek Gluza <marekgluza@gmail.com>
Date: Fri, 5 Apr 2024 09:48:40 +0200
Subject: [PATCH 10/16] passing the d argument in the test

---
 tests/test_models_dbi.py | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/tests/test_models_dbi.py b/tests/test_models_dbi.py
index 723dc590a6..3038651fd3 100644
--- a/tests/test_models_dbi.py
+++ b/tests/test_models_dbi.py
@@ -57,8 +57,8 @@ def test_double_bracket_iteration_eval_dbr_unitary(backend, nqubits):
     )
 
     for s in np.linspace(0.001, 0.01, NSTEPS):
-        u = dbi.eval_dbr_unitary(s, mode=DoubleBracketGeneratorType.single_commutator)
-        v = dbi.eval_dbr_unitary(s, mode=DoubleBracketGeneratorType.group_commutator)
+        u = dbi.eval_dbr_unitary(s,d=d, mode=DoubleBracketGeneratorType.single_commutator)
+        v = dbi.eval_dbr_unitary(s,d=d, mode=DoubleBracketGeneratorType.group_commutator)
 
         assert np.linalg.norm(u - v) < 10 * s**1.49 * (
             np.linalg.norm(h0) + np.linalg.norm(d)

From 03ca52ce8f2ae76eca7ace6e167a842a09dd4a98 Mon Sep 17 00:00:00 2001
From: "pre-commit-ci[bot]"
 <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Date: Fri, 5 Apr 2024 07:49:39 +0000
Subject: [PATCH 11/16] [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci
---
 tests/test_models_dbi.py | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/tests/test_models_dbi.py b/tests/test_models_dbi.py
index 3038651fd3..8b4352c80b 100644
--- a/tests/test_models_dbi.py
+++ b/tests/test_models_dbi.py
@@ -57,8 +57,12 @@ def test_double_bracket_iteration_eval_dbr_unitary(backend, nqubits):
     )
 
     for s in np.linspace(0.001, 0.01, NSTEPS):
-        u = dbi.eval_dbr_unitary(s,d=d, mode=DoubleBracketGeneratorType.single_commutator)
-        v = dbi.eval_dbr_unitary(s,d=d, mode=DoubleBracketGeneratorType.group_commutator)
+        u = dbi.eval_dbr_unitary(
+            s, d=d, mode=DoubleBracketGeneratorType.single_commutator
+        )
+        v = dbi.eval_dbr_unitary(
+            s, d=d, mode=DoubleBracketGeneratorType.group_commutator
+        )
 
         assert np.linalg.norm(u - v) < 10 * s**1.49 * (
             np.linalg.norm(h0) + np.linalg.norm(d)

From d924465c1cf40eb619cff3b7059ce36b6b91c380 Mon Sep 17 00:00:00 2001
From: Sam-XiaoyueLi <xiaoyue.li.samantha@gmail.com>
Date: Mon, 8 Apr 2024 11:04:49 +0800
Subject: [PATCH 12/16] Remove raise error in test_models_dbi.py

---
 tests/test_models_dbi.py | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)

diff --git a/tests/test_models_dbi.py b/tests/test_models_dbi.py
index 8b4352c80b..3b0c169345 100644
--- a/tests/test_models_dbi.py
+++ b/tests/test_models_dbi.py
@@ -10,6 +10,7 @@
 from qibo.quantum_info import random_hermitian
 
 NSTEPS = 50
+seed = 10
 """Number of steps for evolution."""
 
 
@@ -27,23 +28,20 @@ def test_double_bracket_iteration_canonical(backend, nqubits):
     assert initial_off_diagonal_norm > dbf.off_diagonal_norm
 
 
-@pytest.mark.parametrize("nqubits", [3, 4, 5])
+@pytest.mark.parametrize("nqubits", [1, 2])
 def test_double_bracket_iteration_group_commutator(backend, nqubits):
-    h0 = random_hermitian(2**nqubits, backend=backend)
+    h0 = random_hermitian(2**nqubits, backend=backend, seed=seed)
     d = backend.cast(np.diag(np.diag(backend.to_numpy(h0))))
-    dbf = DoubleBracketIteration(
+    dbi = DoubleBracketIteration(
         Hamiltonian(nqubits, h0, backend=backend),
         mode=DoubleBracketGeneratorType.group_commutator,
     )
-    initial_off_diagonal_norm = dbf.off_diagonal_norm
-
-    with pytest.raises(ValueError):
-        dbf(mode=DoubleBracketGeneratorType.group_commutator, step=0.01)
+    initial_off_diagonal_norm = dbi.off_diagonal_norm
 
     for _ in range(NSTEPS):
-        dbf(step=0.01, d=d)
+        dbi(step=0.01, d=d)
 
-    assert initial_off_diagonal_norm > dbf.off_diagonal_norm
+    assert initial_off_diagonal_norm > dbi.off_diagonal_norm
 
 
 @pytest.mark.parametrize("nqubits", [3])

From 051240ee1df3d441f33b0cca7e4cf47c356d43b4 Mon Sep 17 00:00:00 2001
From: Sam-XiaoyueLi <xiaoyue.li.samantha@gmail.com>
Date: Mon, 8 Apr 2024 11:32:01 +0800
Subject: [PATCH 13/16] Remove "prints" comment in utils.py

---
 src/qibo/models/dbi/utils.py | 2 --
 1 file changed, 2 deletions(-)

diff --git a/src/qibo/models/dbi/utils.py b/src/qibo/models/dbi/utils.py
index 02e95bbe28..6a38f043b4 100644
--- a/src/qibo/models/dbi/utils.py
+++ b/src/qibo/models/dbi/utils.py
@@ -115,11 +115,9 @@ def select_best_dbr_generator(
         dbi_object(step=step)
         optimal_steps.append(step)
         norms_off_diagonal_restriction.append(dbi_object.off_diagonal_norm)
-        # print(f'canonical step {step}, loss {dbi_object.off_diagonal_norm}')
         dbi_object.h = deepcopy(h_before)
         dbi_object.mode = generator_type
     # find best d
-    # print(norms_off_diagonal_restriction)
     idx_max_loss = norms_off_diagonal_restriction.index(
         min(norms_off_diagonal_restriction)
     )

From 09614bea650b2fb319708bf9624f9ad3d5ff7253 Mon Sep 17 00:00:00 2001
From: Sam-XiaoyueLi <xiaoyue.li.samantha@gmail.com>
Date: Mon, 8 Apr 2024 17:59:23 +0800
Subject: [PATCH 14/16] correct sign in eval_dbr_unitaruy (double_bracket.py) -
 calculate_matrix_exp contains -1j

---
 src/qibo/models/dbi/double_bracket.py | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/src/qibo/models/dbi/double_bracket.py b/src/qibo/models/dbi/double_bracket.py
index f6b4b7dca0..f5e44d535e 100644
--- a/src/qibo/models/dbi/double_bracket.py
+++ b/src/qibo/models/dbi/double_bracket.py
@@ -100,10 +100,10 @@ def eval_dbr_unitary(
 
             sqrt_step = np.sqrt(step)
             operator = (
-                self.h.exp(-sqrt_step)
-                @ self.backend.calculate_matrix_exp(sqrt_step, d)
-                @ self.h.exp(sqrt_step)
+                self.h.exp(sqrt_step)
                 @ self.backend.calculate_matrix_exp(-sqrt_step, d)
+                @ self.h.exp(-sqrt_step)
+                @ self.backend.calculate_matrix_exp(sqrt_step, d)
             )
         return operator
 

From f2236cbac5e301ea52fa23e38ba203c6d8400343 Mon Sep 17 00:00:00 2001
From: Marek Gluza <marekgluza@gmail.com>
Date: Mon, 8 Apr 2024 12:17:10 +0200
Subject: [PATCH 15/16] Update tests/test_models_dbi.py

Co-authored-by: Andrea Pasquale <andreapasquale97@gmail.com>
---
 tests/test_models_dbi.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tests/test_models_dbi.py b/tests/test_models_dbi.py
index 3b0c169345..68bba49cbd 100644
--- a/tests/test_models_dbi.py
+++ b/tests/test_models_dbi.py
@@ -10,7 +10,7 @@
 from qibo.quantum_info import random_hermitian
 
 NSTEPS = 50
-seed = 10
+SEED = 10
 """Number of steps for evolution."""
 
 

From fba400b113fab16c30b2a25b924a5a76313ba296 Mon Sep 17 00:00:00 2001
From: Marek Gluza <marekgluza@gmail.com>
Date: Mon, 8 Apr 2024 12:17:34 +0200
Subject: [PATCH 16/16] Update tests/test_models_dbi.py

Co-authored-by: Andrea Pasquale <andreapasquale97@gmail.com>
---
 tests/test_models_dbi.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tests/test_models_dbi.py b/tests/test_models_dbi.py
index 68bba49cbd..9732e04323 100644
--- a/tests/test_models_dbi.py
+++ b/tests/test_models_dbi.py
@@ -30,7 +30,7 @@ def test_double_bracket_iteration_canonical(backend, nqubits):
 
 @pytest.mark.parametrize("nqubits", [1, 2])
 def test_double_bracket_iteration_group_commutator(backend, nqubits):
-    h0 = random_hermitian(2**nqubits, backend=backend, seed=seed)
+    h0 = random_hermitian(2**nqubits, backend=backend, seed=SEED)
     d = backend.cast(np.diag(np.diag(backend.to_numpy(h0))))
     dbi = DoubleBracketIteration(
         Hamiltonian(nqubits, h0, backend=backend),