
    z	i                     J    S r SSKrSSKJrJr  SSKJr  SSKJ	r	J
r
  1 SkrS rg)	zDRoutines for computing expectation values from sampled distributions    N)sampled_expval_floatsampled_expval_complex)QiskitError   )QuasiDistributionProbDistribution>   01IZc                   ^ SSK Jn  SSKJnJn  [        U [        [        45      (       a  U R                  5       n [        X[        45      (       d  [        S5      e[        U[        5      (       a)  UR                  5       /n[        R                  " S/5      nO[        X5      (       a)  UR                  5       /n[        R                  " S/5      nOV[        X5      (       a;  UR                   R#                  5       n[        R                  " UR$                  5      nO[        S5      e['        [)        [+        U 5      5      5      m[-        U4S jU 5       5      (       a  [        S	T S
35      eU H5  n[/        U5      R1                  [2        5      (       d  M(  [        SU S35      e   UR4                  [        R4                  " [6        5      R8                  :X  a  [;        XVU 5      $ [=        XVU 5      $ )a  Computes expectation value from a sampled distribution

Note that passing a raw dict requires bit-string keys.

Parameters:
    dist (Counts or QuasiDistribution or ProbDistribution or dict): Input sampled distribution
    oper (str or :class:`~.quantum_info.Pauli` or SparsePauliOp): The operator for the
        observable

Returns:
    float: The expectation value
Raises:
    QiskitError: if the input distribution or operator is an invalid type
r   )Countsr   )PauliSparsePauliOpzInvalid input distribution typeg      ?zInvalid operator typec              3   @   >#    U  H  n[        U5      T:g  v   M     g 7f)N)len).0opbitstring_lens     V/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/result/sampled_expval.py	<genexpr>,sampled_expectation_value.<locals>.<genexpr>A   s     
8i3r7m#is   z'One or more operators not same length (z) as input bitstringszInput operator z is not diagonal)countsr   qiskit.quantum_infor   r   
isinstancer   r   binary_probabilitiesdictr   struppernpasarrayto_labelpaulis	to_labelscoeffsr   nextiteranyset
differenceOPERSdtypecomplextyper   r   )	distoperr   r   r   	oper_strsr%   r   r   s	           @r   sampled_expectation_valuer2      s    8 $*,<=>>((*dTN++;<<$ZZ\N	SE"	D	 	 ]]_%	SE"	D	(	(KK))+	DKK(122 T$Z()M

8i
8885m_DYZ
 	
 r7e$$t3CDEE  ||rxx(---%i>>#It<<    )__doc__numpyr    "qiskit._accelerate.sampled_exp_valr   r   qiskit.exceptionsr   distributionsr   r   r+   r2    r3   r   <module>r:      s#    K  [ ) > 	1=r3   