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

Moving energy fluctuation to Hamiltonian #1131

Merged
merged 10 commits into from
Dec 14, 2023
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.
MatteoRobbiati marked this conversation as resolved.
Show resolved Hide resolved

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