code: def get_energy(self, circuit, sampler):
          
          val = 0.0
          for meas in self.hamiltonian:
              c = circuit.copy()
              for op in meas.ops:
                  if op.op == "X":
                      c.h[op.n]
                  elif op.op == "Y":
                      c.rx(-np.pi / 2)[op.n]
              measured = sampler(c, meas.n_iter())
              for bits, prob in measured.items():
                  if sum(bits) % 2:
                      val -= prob * meas.coeff
                  else:
                      val += prob * meas.coeff
          return val.real
docs: Calculate energy from circuit and sampler.