code: def expect(qubits, meas):
      "For the VQE simulation without sampling."
      result = {}
      i = np.arange(len(qubits))
      meas = tuple(meas)
  
      def to_mask(n):
          return reduce(lambda acc, im: acc | (n & (1 << im[0])) << (im[1] - im[0]), enumerate(meas), 0)
  
      def to_key(k):
          return tuple(1 if k & (1 << i) else 0 for i in meas)
  
      mask = reduce(lambda acc, v: acc | (1 << v), meas, 0)
  
      cnt = defaultdict(float)
      for i, v in enumerate(qubits):
          p = v.real ** 2 + v.imag ** 2
          if p != 0.0:
              cnt[i & mask] += p
      return {to_key(k): v for k, v in cnt.items()f expect(qubits, meas):
      "For the VQE simulation without sampling."
      result = {}
      i = np.arange(len(qubits))
      meas = tuple(meas)
  
      def to_mask(n):
          return reduce(lambda acc, im: acc | (n & (1 << im[0])) << (im[1] - im[0]), enumerate(meas), 0)
  
      def to_key(k):
          return tuple(1 if k & (1 << i) else 0 for i in meas)
  
      mask = reduce(lambda acc, v: acc | (1 << v), meas, 0)
  
      cnt = defaultdict(float)
      for i, v in enumerate(qubits):
          p = v.real ** 2 + v.imag ** 2
          if p != 0.0:
              cnt[i & mask] += p
      return {to_key(k): v for k, v in cnt.items()}
docs: For the VQE simulation without sampling.