def expectation(observable: Observable, target: QubitSetInput = None) -> ResultType: """Registers this function into the circuit class. Args: observable (Observable): the observable for the result type target (int, Qubit, or iterable of int / Qubit, optional): Target qubits that the result type is requested for. Default is `None`, which means the observable must operate only on 1 qubit and it is applied to all qubits in parallel. Returns: ResultType: expectation as a requested result type Examples: >>> circ = Circuit().expectation(observable=Observable.Z(), target=0) """ return ResultType.Expectation(observable=observable, target=target)
def _get_expectation_value( self, bkcirc: braket.circuits.Circuit, observable: Observable, target: QubitSet, n_shots: int, **kwargs: KwargTypes, ) -> np.float64: if not self.supports_expectation: raise RuntimeError("Backend does not support expectation") if (n_shots < self._expectation_min_shots or n_shots > self._expectation_max_shots): raise ValueError( f"n_shots must be between {self._expectation_min_shots} and " f"{self._expectation_max_shots}") restype = ResultType.Expectation(observable, target=target) bkcirc.add_result_type(restype) task = self._run(bkcirc, n_shots=n_shots, **kwargs) res = task.result() return res.get_value_by_result_type(restype) # type: ignore