Skip to content

Commit

Permalink
improve calculate_hamiltonian_state_product method
Browse files Browse the repository at this point in the history
  • Loading branch information
Simone-Bordoni committed Mar 12, 2024
1 parent 573008c commit a7a496d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 23 deletions.
20 changes: 9 additions & 11 deletions src/qibo/backends/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -718,9 +718,9 @@ def calculate_overlap(self, state1, state2):
return self.np.abs(self.np.sum(np.conj(self.cast(state1)) * self.cast(state2)))

def calculate_overlap_density_matrix(self, state1, state2):
state1 = self.cast(state1)
state2 = self.cast(state2)
return self.np.trace(self.np.transpose(np.conj(state1)) @ state2)
return self.np.trace(
self.np.matmul(self.np.conj(self.cast(state1)).T, self.cast(state2))
)

def calculate_eigenvalues(self, matrix, k=6):
if self.issparse(matrix):
Expand Down Expand Up @@ -767,20 +767,18 @@ def calculate_expectation_density_matrix(self, hamiltonian, state, normalize):
ev = ev / norm
return ev

# TODO: remove this method
def calculate_hamiltonian_matrix_product(self, matrix1, matrix2):
return self.np.dot(matrix1, matrix2)
return matrix1 @ matrix2

# TODO: remove this method
def calculate_hamiltonian_state_product(self, matrix, state):
rank = len(tuple(state.shape))
if rank == 1: # vector
return matrix.dot(state[:, np.newaxis])[:, 0]
elif rank == 2: # matrix
return matrix.dot(state)
else:
if len(tuple(state.shape)) > 2:
raise_error(
ValueError,
f"Cannot multiply Hamiltonian with rank-{rank} tensor.",
f"Cannot multiply Hamiltonian with rank-{len(tuple(state.shape))} tensor.",
)
return matrix @ state

def assert_allclose(self, value, target, rtol=1e-7, atol=0.0):
if isinstance(value, CircuitResult) or isinstance(value, QuantumState):
Expand Down
16 changes: 4 additions & 12 deletions src/qibo/backends/pytorch.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,10 @@ def sample_shots(self, probabilities, nshots):
self.cast(probabilities, dtype="float"), nshots, replacement=True
)

def calculate_overlap_density_matrix(self, state1, state2):
return self.np.trace(
self.np.matmul(self.np.conj(self.cast(state1)).T, self.cast(state2))
)
# def calculate_overlap_density_matrix(self, state1, state2):
# return self.np.trace(
# self.np.matmul(self.np.conj(self.cast(state1)).T, self.cast(state2))
# )

def calculate_eigenvalues(self, matrix, k=6):
return self.np.linalg.eigvalsh(matrix) # pylint: disable=not-callable
Expand All @@ -168,14 +168,6 @@ def calculate_matrix_exp(self, a, matrix, eigenvectors=None, eigenvalues=None):
ud = self.np.conj(eigenvectors).T
return self.np.matmul(eigenvectors, self.np.matmul(expd, ud))

def calculate_hamiltonian_matrix_product(self, matrix1, matrix2):
if self.issparse(matrix1) or self.issparse(matrix2): # pragma: no cover
return self.np.sparse.mm(matrix1, matrix2) # pylint: disable=E1102
return self.np.matmul(matrix1, matrix2)

def calculate_hamiltonian_state_product(self, matrix, state):
return self.np.matmul(matrix, state)

def test_regressions(self, name):
if name == "test_measurementresult_apply_bitflips":
return [
Expand Down
5 changes: 5 additions & 0 deletions tests/test_hamiltonians.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,11 @@ def test_hamiltonian_matmul(backend, sparse_type):
backend.assert_allclose((H1 @ H2).matrix, (m1 @ m2))
backend.assert_allclose((H2 @ H1).matrix, (m2 @ m1))

try:
H1 @ np.zeros(3 * (2**nqubits,), dtype=m1.dtype)
except Exception as error:
print(error)

with pytest.raises(ValueError):
H1 @ np.zeros(3 * (2**nqubits,), dtype=m1.dtype)
with pytest.raises(NotImplementedError):
Expand Down

0 comments on commit a7a496d

Please sign in to comment.