
    z	i(                     >    S r SSKJr  SSKrSSKJr   " S S\5      rg)zQuasidistribution class    )sqrtN   )ProbDistributionc                      ^  \ rS rSrSr\R                  " S5      rSrSU 4S jjr	SS jr
SS jrS r\S	 5       rS
 rSrU =r$ )QuasiDistribution   z7A dict-like class for representing quasi-probabilities.z^[01]+$   c                   > X l         X0l        SU l        U(       Ga  [        [	        UR                  5       5      5      n[        U[        5      (       a5  [        [        [        UR                  5       5      5      5      S-
  U l        GO"[        U[        5      (       Ga  UR                  S5      (       d  UR                  S5      (       ac  UR                  5        VVs0 s H  u  pV[        US5      U_M     nnn[        [        [        UR                  5       5      5      5      S-
  U l        O}U R                  R                  U5      (       aG  [        S U 5       5      U l        UR                  5        VVs0 s H  u  pV[        US5      U_M     nnnO[!        S5      e[#        S5      e[$        TU ]M  U5        gs  snnf s  snnf )	a  Builds a quasiprobability distribution object.

.. note::

    The quasiprobability values might include floating-point errors.
    ``QuasiDistribution.__repr__`` rounds using :meth:`numpy.round`
    and the parameter ``ndigits`` can be manipulated with the
    class attribute ``__ndigits__``. The default is ``15``.

Parameters:
    data (dict): Input quasiprobability data. Where the keys
        represent a measured classical value and the value is a
        float for the quasiprobability of that result.
        The keys can be one of several formats:

            * A hexadecimal string of the form ``"0x4a"``
            * A bit string e.g. ``'0b1011'`` or ``"01011"``
            * An integer

    shots (int): Number of shots the distribution was derived from.
    stddev_upper_bound (float): An upper bound for the standard deviation

Raises:
    TypeError: If the input keys are not a string or int
    ValueError: If the string format of the keys is incorrect
r      0x0bc              3   8   #    U  H  n[        U5      v   M     g 7fN)len).0keys     [/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/result/distributions/quasi.py	<genexpr>-QuasiDistribution.__init__.<locals>.<genexpr>L   s     (BTcSTs   zThe input keys are not a valid string format, must either be a hex string prefixed by '0x' or a binary string optionally prefixed with 0bz9Input data's keys are of invalid type, must be str or intN)shots_stddev_upper_bound	_num_bitsnextiterkeys
isinstanceintr   binmaxstr
startswithitems_bitstring_regexsearch
ValueError	TypeErrorsuper__init__)selfdatar   stddev_upper_bound	first_keyr   value	__class__s          r   r(   QuasiDistribution.__init__    sb   6 
#5 T$))+./I)S)) "%STYY[)9%:!;a!?Is++''--1E1Ed1K1KAEN:3CQK.DN &)S-=)>%?!%CDN**11)<<%((BT(B%BDNAEN:3CQK.DND$6    [\\! O Os   F;8Gc                 ~   [        [        U R                  5       S S95      n[        U5      n0 nSnSnUR                  5        H;  u  pxXU-  -   n	U	S:  a  XX-  nUS-  nXhU-  -  nM$  XeU-  XS-  -  -  nX'   XS-  -   XG'   M=     U(       a   [	        X@R
                  5      [        U5      4$ [	        X@R
                  5      $ )a  Takes a quasiprobability distribution and maps
it to the closest probability distribution as defined by
the L2-norm.

Parameters:
    return_distance (bool): Return the L2 distance between distributions.

Returns:
    ProbDistribution: Nearest probability distribution.
    float: Euclidean (L2) distance of distributions.

Notes:
    Method from Smolin et al., Phys. Rev. Lett. 108, 070502 (2012).
c                     U S   $ )Nr    )items    r   <lambda>DQuasiDistribution.nearest_probability_distribution.<locals>.<lambda>g   s    $q'    )r   r   r   )dictsortedr"   r   r   r   r   )
r)   return_distancesorted_probs	num_elems	new_probsbetadiffr   valtemps
             r    nearest_probability_distribution2QuasiDistribution.nearest_probability_distributionX   s     F4::<5IJK%		$**,HC	))DaxQ	c	!	)d.>??!-!2T5E!E	 - #Izz:DJFF	::66r6   c                     Uc  U R                   OUnU R                  5        VVs0 s H!  u  p4[        US5      R                  U5      U_M#     snn$ s  snnf )aN  Build a quasi-probabilities dictionary with binary string keys

Parameters:
    num_bits (int): number of bits in the binary bitstrings (leading
        zeros will be padded). If None, a default value will be used.
        If keys are given as integers or strings with binary or hex prefix,
        the default value will be derived from the largest key present.
        If keys are given as bitstrings without prefix,
        the default value will be derived from the largest key length.

Returns:
    dict: A dictionary where the keys are binary strings in the format
        ``"0110"``
b)r   r"   formatzfill)r)   num_bitsnr   r-   s        r   binary_probabilities&QuasiDistribution.binary_probabilitiesy   sK     '.DNNHCG::<P<ZSsC &&q)50<PPPs   (Ac                 l    U R                  5        VVs0 s H  u  p[        U5      U_M     snn$ s  snnf )zBuild a quasi-probabilities dictionary with hexadecimal string keys

Returns:
    dict: A dictionary where the keys are hexadecimal strings in the
        format ``"0x1a"``
)r"   hexr)   r   r-   s      r   hex_probabilities#QuasiDistribution.hex_probabilities   s,     37**,?,JCC%,???s   0c                     U R                   $ )z@Return an upper bound on standard deviation of expval estimator.)r   )r)   s    r   r+   $QuasiDistribution.stddev_upper_bound   s     '''r6   c                     [        U R                  5        VVs0 s H  u  pU[        X R                  S9_M     snn5      $ s  snnf )N)ndigits)r    r"   round__ndigits__rM   s      r   __repr__QuasiDistribution.__repr__   s=    RVR\R\R^_R^JCCu.>.>??R^_``_s    A
)r   r   r   )NN)Fr   )__name__
__module____qualname____firstlineno____doc__recompiler#   rU   r(   rA   rI   rN   propertyr+   rV   __static_attributes____classcell__)r.   s   @r   r   r      sR    Azz*-K6p7BQ$@ ( (a ar6   r   )r\   mathr   r]   probabilityr   r7   r   r2   r6   r   <module>rd      s#      	 )@a @ar6   