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
18 changes: 18 additions & 0 deletions src/qibo/hamiltonians/hamiltonians.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,24 @@ 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
"""
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