<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def expectation(self, diag_hermitian: bk.TensorLike, trials: int = None) -> bk.BKTensor: """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. ) """

        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)