def variance(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 only operate on 1 qubit and it will be applied to all qubits in parallel Returns: ResultType: variance as a requested result type Examples: >>> circ = Circuit().variance(observable=Observable.Z(), target=0) """ return ResultType.Variance(observable=observable, target=target)
def _get_variance( self, bkcirc: braket.circuits.Circuit, observable: Observable, target: QubitSet, n_shots: int, **kwargs: KwargTypes, ) -> np.float64: if not self.supports_variance: raise RuntimeError("Backend does not support variance") if n_shots < self._variance_min_shots or n_shots > self._variance_max_shots: raise ValueError( f"n_shots must be between {self._variance_min_shots} and " f"{self._variance_max_shots}") restype = ResultType.Variance(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