Skip to content

Commit

Permalink
Merge pull request #173 from UCL-CCS/expval
Browse files Browse the repository at this point in the history
 When state has few terms compared with operator, faster to do direct…
  • Loading branch information
TimWeaving authored Nov 28, 2023
2 parents f182e2c + f35bac8 commit 7c96dd6
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions symmer/operators/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -829,16 +829,19 @@ def expval(self, psi: "QuantumState") -> complex:
Returns:
complex: The expectation value.
"""
if self.n_terms > 1:
@process.parallelize
def f(P, psi):
return single_term_expval(P, psi)

expvals = np.array(f(self, psi))
if self.n_terms > psi.n_terms:
return (psi.dagger * self * psi).real
else:
expvals = np.array(single_term_expval(self, psi))
if self.n_terms > 1:
@process.parallelize
def f(P, psi):
return single_term_expval(P, psi)

expvals = np.array(f(self, psi))
else:
expvals = np.array(single_term_expval(self, psi))

return np.sum(expvals * self.coeff_vec)
return np.sum(expvals * self.coeff_vec).real

def __mul__(self,
mul_obj: Union["PauliwordOp", "QuantumState", complex],
Expand Down

0 comments on commit 7c96dd6

Please sign in to comment.