Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Simone-Bordoni committed Mar 7, 2024
1 parent db2414e commit 381bfbe
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/qibo/hamiltonians/hamiltonians.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ def exp(self, a):

def expectation(self, state, normalize=False):
if isinstance(state, self.backend.tensor_types):
state = self.backend.cast(state)
shape = tuple(state.shape)
if len(shape) == 1: # state vector
return self.backend.calculate_expectation_state(self, state, normalize)
Expand Down Expand Up @@ -175,6 +176,7 @@ def energy_fluctuation(self, state):
Return:
Energy fluctuation value (float).
"""
state = self.backend.cast(state)
energy = self.expectation(state)
h = self.matrix
h2 = Hamiltonian(nqubits=self.nqubits, matrix=h @ h, backend=self.backend)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_hamiltonians.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ def test_hamiltonian_energy_fluctuation(backend):
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)
zero_state = backend.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)
Expand Down
1 change: 1 addition & 0 deletions tests/test_hamiltonians_symbolic.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ def test_symbolic_hamiltonian_state_expectation_different_nqubits(

def test_hamiltonian_expectation_from_samples(backend):
"""Test Hamiltonian expectation value calculation."""
backend.set_seed(0)
obs0 = 2 * Z(0) * Z(1) + Z(0) * Z(2)
obs1 = 2 * Z(0) * Z(1) + Z(0) * Z(2) * I(3)
h0 = hamiltonians.SymbolicHamiltonian(obs0, backend=backend)
Expand Down
3 changes: 2 additions & 1 deletion tests/test_measurements_probabilistic.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,9 @@ def test_measurementresult_apply_bitflips(backend, i, p0, p1):

c = models.Circuit(3)
c.add(gates.M(*range(3)))
state = backend.np.zeros(8)
state = np.zeros(8)
state[0] = 1.0
state = backend.cast(state)
result = CircuitResult(state, c.measurements, backend)
result._samples = backend.cast(np.zeros((10, 3)), dtype="int32")
backend.set_seed(123)
Expand Down

0 comments on commit 381bfbe

Please sign in to comment.