Skip to content

Commit

Permalink
Merge pull request #1131 from qiboteam/ene_fluc
Browse files Browse the repository at this point in the history
Moving energy fluctuation to `Hamiltonian`
  • Loading branch information
MatteoRobbiati authored Dec 14, 2023
2 parents 8e725ce + 93e6236 commit 3e2b7b4
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 13 deletions.
21 changes: 21 additions & 0 deletions src/qibo/hamiltonians/hamiltonians.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,27 @@ def eye(self, dim: Optional[int] = None):
dim = int(self.matrix.shape[0])
return self.backend.cast(self.backend.matrices.I(dim), dtype=self.matrix.dtype)

def energy_fluctuation(self, state):
"""
Evaluate energy fluctuation:
.. math::
\\Xi_{k}(\\mu) = \\sqrt{\\langle\\mu|\\hat{H}^2|\\mu\\rangle - \\langle\\mu|\\hat{H}|\\mu\\rangle^2} \\,
for a given state :math:`|\\mu\\rangle`.
Args:
state (np.ndarray): quantum state to be used to compute the energy fluctuation.
Return:
Energy fluctuation value (float).
"""
energy = self.expectation(state)
h = self.matrix
h2 = Hamiltonian(nqubits=self.nqubits, matrix=h @ h, backend=self.backend)
average_h2 = self.backend.calculate_expectation_state(h2, state, normalize=True)
return np.sqrt(np.abs(average_h2 - energy**2))

def __add__(self, o):
if isinstance(o, self.__class__):
if self.nqubits != o.nqubits:
Expand Down
18 changes: 12 additions & 6 deletions src/qibo/models/dbi/double_bracket.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,15 @@ def loss(self, step: float, look_ahead: int = 1):
return loss

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)
"""
Evaluate energy fluctuation
.. math::
\\Xi_{k}(\\mu) = \\sqrt{\\langle\\mu|\\hat{H}^2|\\mu\\rangle - \\langle\\mu|\\hat{H}|\\mu\\rangle^2} \\,
for a given state :math:`|\\mu\\rangle`.
Args:
state (np.ndarray): quantum state to be used to compute the energy fluctuation with H.
"""
return self.h.energy_fluctuation(state)
8 changes: 1 addition & 7 deletions src/qibo/models/variational.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,7 @@ def energy_fluctuation(self, state):
Args:
state (np.ndarray): quantum state to be used to compute the energy fluctuation with H.
"""
energy = self.hamiltonian.expectation(state)
h = self.hamiltonian.matrix
h2 = Hamiltonian(
nqubits=self.hamiltonian.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)
return self.hamiltonian.energy_fluctuation(state)


class AAVQE:
Expand Down
15 changes: 15 additions & 0 deletions tests/test_hamiltonians.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,3 +423,18 @@ def construct_hamiltonian():

backend.assert_allclose(H.exp(0.5), target_matrix)
backend.assert_allclose(H1.exp(0.5), target_matrix)


def test_hamiltonian_energy_fluctuation(backend):
"""Test energy fluctuation."""
# define hamiltonian
ham = hamiltonians.XXZ(nqubits=2, backend=backend)
# take ground state and zero state
ground_state = ham.ground_state()
zero_state = np.ones(2**2) / np.sqrt(2**2)
# collect energy fluctuations
gs_energy_fluctuation = ham.energy_fluctuation(ground_state)
zs_energy_fluctuation = ham.energy_fluctuation(zero_state)

assert np.isclose(gs_energy_fluctuation, 0, atol=1e-5)
assert gs_energy_fluctuation < zs_energy_fluctuation

0 comments on commit 3e2b7b4

Please sign in to comment.