Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DBF Energy fluctuations #1080

Merged
merged 8 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/qibo/models/double_bracket.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import numpy as np

from qibo.backends.numpy import NumpyBackend

from ..config import raise_error
from ..hamiltonians import Hamiltonian

Expand Down Expand Up @@ -82,16 +84,29 @@ 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):
"""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 = 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)
9 changes: 9 additions & 0 deletions tests/test_models_dbf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = np.array([[1, 0], [0, -1]])
state = np.array([1, 0])
dbf = DoubleBracketFlow(Hamiltonian(1, matrix=h0, backend=backend))
energy_fluctuation = dbf.energy_fluctuation(state=state)
assert energy_fluctuation == 0