Skip to content

Commit

Permalink
Implement double bracket flow
Browse files Browse the repository at this point in the history
  • Loading branch information
Edoardo-Pedicillo committed Oct 16, 2023
1 parent 51233c2 commit ca896b5
Showing 1 changed file with 138 additions and 0 deletions.
138 changes: 138 additions & 0 deletions src/qibo/hamiltonians/double_bracket.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import matplotlib.pyplot as plt
import numpy as np
import numpy.typing as npt

Check warning on line 3 in src/qibo/hamiltonians/double_bracket.py

View check run for this annotation

Codecov / codecov/patch

src/qibo/hamiltonians/double_bracket.py#L1-L3

Added lines #L1 - L3 were not covered by tests

import qibo
from qibo.hamiltonians.hamiltonians import Hamiltonian

Check warning on line 6 in src/qibo/hamiltonians/double_bracket.py

View check run for this annotation

Codecov / codecov/patch

src/qibo/hamiltonians/double_bracket.py#L5-L6

Added lines #L5 - L6 were not covered by tests

FIGSIZE = (5, 5)

Check warning on line 8 in src/qibo/hamiltonians/double_bracket.py

View check run for this annotation

Codecov / codecov/patch

src/qibo/hamiltonians/double_bracket.py#L8

Added line #L8 was not covered by tests


class DoubleBracket(Hamiltonian):
def __init__(self, nqubits: int, matrix: npt.DTypeLike = None, backend=None):

Check warning on line 12 in src/qibo/hamiltonians/double_bracket.py

View check run for this annotation

Codecov / codecov/patch

src/qibo/hamiltonians/double_bracket.py#L11-L12

Added lines #L11 - L12 were not covered by tests
"""
Class implementing the Double Bracket flow algorithm.
For more details, see https://arxiv.org/pdf/2206.11772.pdf
Args:
nqubits(int): Number of qubits.
matrix(npt.DTypeLike, Optional): Hamiltonian matrix.
backend(Optional): Qibo backend.
Example:
.. code-block:: python
import numpy as np
from qibo.hamiltonians.double_bracket import DoubleBracket
from qibo.hamiltonians.hamiltonians import Hamiltonian
nqubits = 3
example_matrix = [[ 3., 0., 0., 1., 0., 0., 1., 0.],
[ 0., 1., 1., 0., 0., 0., 0., 1.],
[ 0., 1., 1., 0., 1., 0., 0., 0.],
[ 1., 0., 0., -1., 0., 1., 0., 0.],
[ 0., 0., 1., 0., 1., 0., 0., 1.],
[ 0., 0., 0., 1., 0., -1., 1., 0.],
[ 1., 0., 0., 0., 0., 1., -1., 0.],
[ 0., 1., 0., 0., 1., 0., 0., -3.]]
example_matrix = np.array(example_matrix)
ham = Hamiltonian(nqubits, example_matrix) # implement an hamiltonian in hamiltonians
db = DoubleBracket(nqubits, matrix = ham.matrix)
for _ in range(1000):
db.evolve( db.group_commutator( 1, Hamiltonian(nqubits,db.delta)))
db.plot_abs_matrix()
db.plot_norm()
"""
super().__init__(nqubits, matrix, backend)
self.history = []
self.norm = []

Check warning on line 53 in src/qibo/hamiltonians/double_bracket.py

View check run for this annotation

Codecov / codecov/patch

src/qibo/hamiltonians/double_bracket.py#L51-L53

Added lines #L51 - L53 were not covered by tests

@property
def delta(self):

Check warning on line 56 in src/qibo/hamiltonians/double_bracket.py

View check run for this annotation

Codecov / codecov/patch

src/qibo/hamiltonians/double_bracket.py#L55-L56

Added lines #L55 - L56 were not covered by tests
"""
Hamiltonian diagonal restriction.
"""
return self.backend.cast(np.diag(self.matrix.diagonal()))

Check warning on line 60 in src/qibo/hamiltonians/double_bracket.py

View check run for this annotation

Codecov / codecov/patch

src/qibo/hamiltonians/double_bracket.py#L60

Added line #L60 was not covered by tests

@property
def sigma(self):

Check warning on line 63 in src/qibo/hamiltonians/double_bracket.py

View check run for this annotation

Codecov / codecov/patch

src/qibo/hamiltonians/double_bracket.py#L62-L63

Added lines #L62 - L63 were not covered by tests
"""
Hamiltonian off-diagonal restriction.
"""
return self.matrix - self.delta

Check warning on line 67 in src/qibo/hamiltonians/double_bracket.py

View check run for this annotation

Codecov / codecov/patch

src/qibo/hamiltonians/double_bracket.py#L67

Added line #L67 was not covered by tests

def db_generator(self, step):

Check warning on line 69 in src/qibo/hamiltonians/double_bracket.py

View check run for this annotation

Codecov / codecov/patch

src/qibo/hamiltonians/double_bracket.py#L69

Added line #L69 was not covered by tests
"""Generator of the GWW flow."""
w = Hamiltonian(

Check warning on line 71 in src/qibo/hamiltonians/double_bracket.py

View check run for this annotation

Codecov / codecov/patch

src/qibo/hamiltonians/double_bracket.py#L71

Added line #L71 was not covered by tests
nqubits=self.nqubits,
matrix=self.delta @ self.sigma - self.sigma @ self.delta,
)
return w.exp(step)

Check warning on line 75 in src/qibo/hamiltonians/double_bracket.py

View check run for this annotation

Codecov / codecov/patch

src/qibo/hamiltonians/double_bracket.py#L75

Added line #L75 was not covered by tests

def group_commutator(self, step: float, gen: Hamiltonian):

Check warning on line 77 in src/qibo/hamiltonians/double_bracket.py

View check run for this annotation

Codecov / codecov/patch

src/qibo/hamiltonians/double_bracket.py#L77

Added line #L77 was not covered by tests
"""
Group commutator.
Args:
step (float)
gen (Hamiltonian): Flow generator.
"""
return gen.exp(-1 * step) @ self.exp(-1 * step) @ gen.exp(step) @ self.exp(step)

Check warning on line 85 in src/qibo/hamiltonians/double_bracket.py

View check run for this annotation

Codecov / codecov/patch

src/qibo/hamiltonians/double_bracket.py#L85

Added line #L85 was not covered by tests

def evolve(self, evolution_op: Hamiltonian):

Check warning on line 87 in src/qibo/hamiltonians/double_bracket.py

View check run for this annotation

Codecov / codecov/patch

src/qibo/hamiltonians/double_bracket.py#L87

Added line #L87 was not covered by tests
"""
Evolution of the Hamiltonian according to the group commutator generated by `gen`.
Args:
gen (Hamiltonian): generator of the group commutator.
"""
self.history.append(

Check warning on line 94 in src/qibo/hamiltonians/double_bracket.py

View check run for this annotation

Codecov / codecov/patch

src/qibo/hamiltonians/double_bracket.py#L94

Added line #L94 was not covered by tests
self.matrix
) # TODO: Delete it, if we don't need to save the matrix evolution
self.matrix = (

Check warning on line 97 in src/qibo/hamiltonians/double_bracket.py

View check run for this annotation

Codecov / codecov/patch

src/qibo/hamiltonians/double_bracket.py#L97

Added line #L97 was not covered by tests
evolution_op
@ self.matrix
@ self.backend.cast(np.transpose(np.conjugate(evolution_op)))
)
self.norm.append(np.linalg.norm(self.sigma))

Check warning on line 102 in src/qibo/hamiltonians/double_bracket.py

View check run for this annotation

Codecov / codecov/patch

src/qibo/hamiltonians/double_bracket.py#L102

Added line #L102 was not covered by tests

def plot_abs_matrix(self, path: str = None):

Check warning on line 104 in src/qibo/hamiltonians/double_bracket.py

View check run for this annotation

Codecov / codecov/patch

src/qibo/hamiltonians/double_bracket.py#L104

Added line #L104 was not covered by tests
"""
Plot the Hamiltonian absolute values.
Args:
path (str, Optional): if a path is provided, the plot is dumped.
"""
fig, ax = plt.subplots(figsize=FIGSIZE)
plot = plt.imshow(np.absolute(self.matrix), cmap="RdBu")
plt.xticks([])
plt.yticks([])
fig.colorbar(plot, ax=ax, extend="both")
if path:
plt.savefig(path)

Check warning on line 118 in src/qibo/hamiltonians/double_bracket.py

View check run for this annotation

Codecov / codecov/patch

src/qibo/hamiltonians/double_bracket.py#L112-L118

Added lines #L112 - L118 were not covered by tests
else:
plt.show()

Check warning on line 120 in src/qibo/hamiltonians/double_bracket.py

View check run for this annotation

Codecov / codecov/patch

src/qibo/hamiltonians/double_bracket.py#L120

Added line #L120 was not covered by tests

def plot_norm(self, path: str = None):

Check warning on line 122 in src/qibo/hamiltonians/double_bracket.py

View check run for this annotation

Codecov / codecov/patch

src/qibo/hamiltonians/double_bracket.py#L122

Added line #L122 was not covered by tests
"""
Plot the evolution of the Hamiltonian off diagonal norm.
Args:
path (str, Optional): if a path is provided, the plot is dumped.
"""

plt.plot(self.norm, "-o")
plt.title(r"Norm off-diagonal $\vert\vert\sigma(H_k)\vert\vert$")
plt.xlabel("epochs")
if path:
plt.savefig(path)

Check warning on line 136 in src/qibo/hamiltonians/double_bracket.py

View check run for this annotation

Codecov / codecov/patch

src/qibo/hamiltonians/double_bracket.py#L132-L136

Added lines #L132 - L136 were not covered by tests
else:
plt.show()

Check warning on line 138 in src/qibo/hamiltonians/double_bracket.py

View check run for this annotation

Codecov / codecov/patch

src/qibo/hamiltonians/double_bracket.py#L138

Added line #L138 was not covered by tests

0 comments on commit ca896b5

Please sign in to comment.