code: def expectation(self, diag_hermitian: bk.TensorLike,
                      trials: int = None) -> bk.BKTensor:
          
          if trials is None:
              probs = self.probabilities()
          else:
              probs = bk.real(bk.astensorproduct(self.sample(trials) / trials))
  
          diag_hermitian = bk.astensorproduct(diag_hermitian)
          return bk.sum(bk.real(diag_hermitian) * probs)
docs: Return the expectation of a measurement. Since we can only measure
          our computer in the computational basis, we only require the diagonal
          of the Hermitian in that basis.
  
          If the number of trials is specified, we sample the given number of
          times. Else we return the exact expectation (as if we'd performed an
          infinite number of trials. )