§
    º³Ùg(  ã                   óF   — d Z ddlmZ ddlZddlmZ  G d„ de¦  «        ZdS )zQuasidistribution classé    )ÚsqrtNé   )ÚProbDistributionc                   óz   ‡ — e Zd ZdZ ej        d¦  «        ZdZdˆ fd„	Zdd„Z	dd„Z
d	„ Zed
„ ¦   «         Zd„ Zˆ xZS )ÚQuasiDistributionz7A dict-like class for representing quasi-probabilities.z^[01]+$é   Nc                 ó¶  •— || _         || _        d| _        |�ržt          t	          |                     ¦   «         ¦  «        ¦  «        }t          |t          ¦  «        rEt          t          t          |                     ¦   «         ¦  «        ¦  «        ¦  «        dz
  | _        �nt          |t          ¦  «        rò|                     d¦  «        s|                     d¦  «        rbd„ |                     ¦   «         D ¦   «         }t          t          t          |                     ¦   «         ¦  «        ¦  «        ¦  «        dz
  | _        nu| j                             |¦  «        r=t          d„ |D ¦   «         ¦  «        | _        d„ |                     ¦   «         D ¦   «         }nt!          d¦  «        ‚t#          d	¦  «        ‚t%          ¦   «                              |¦  «         d
S )af  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                 ó6   — i | ]\  }}t          |d ¦  «        |“ŒS )r   ©Úint©Ú.0ÚkeyÚvalues      úW/granary/edev/quantum/lib/python3.11/site-packages/qiskit/result/distributions/quasi.pyú
<dictcomp>z.QuasiDistribution.__init__.<locals>.<dictcomp>F   ó&   € ÐNÐNÐN±:°3¸�C  Q™KœK¨ÐNÐNÐNó    c              3   ó4   K  — | ]}t          |¦  «        V — Œd S ©N)Úlen)r   r   s     r   ú	<genexpr>z-QuasiDistribution.__init__.<locals>.<genexpr>L   s(   è è € Ð(BÐ(B°c­¨S©¬Ð(BÐ(BÐ(BÐ(BÐ(BÐ(Br   c                 ó6   — i | ]\  }}t          |d ¦  «        |“ŒS )r
   r   r   s      r   r   z.QuasiDistribution.__init__.<locals>.<dictcomp>M   r   r   z‰The 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Ú
isinstancer   r   ÚbinÚmaxÚstrÚ
startswithÚitemsÚ_bitstring_regexÚsearchÚ
ValueErrorÚ	TypeErrorÚsuperÚ__init__)ÚselfÚdatar   Ústddev_upper_boundÚ	first_keyÚ	__class__s        €r   r.   zQuasiDistribution.__init__    s§  ø€ ð6 ˆŒ
Ø#5ˆÔ ØˆŒØñ 	]Ý�T $§)¢)¡+¤+Ñ.Ô.Ñ/Ô/ˆIÝ˜)¥SÑ)Ô)ð ]õ "%¥S­¨T¯YªY©[¬[Ñ)9Ô)9Ñ%:Ô%:Ñ!;Ô!;¸aÑ!?�”‘Ý˜I¥sÑ+Ô+ð ]Ø×'Ò'¨Ñ-Ô-ð °×1EÒ1EÀdÑ1KÔ1Kð ØNÐNÀÇÂÁÄÐNÑNÔN�Dõ &)­­S°·²±´Ñ-=Ô-=Ñ)>Ô)>Ñ%?Ô%?À!Ñ%C�D”N�NØÔ*×1Ò1°)Ñ<Ô<ð 	å%(Ð(BÐ(B¸TÐ(BÑ(BÔ(BÑ%BÔ%B�D”NØNÐNÀÇÂÁÄÐNÑNÔN�D�Då$ð6ñô ð õ  Ð [Ñ\Ô\Ð\Ý‰Œ×Ò˜ÑÔÐÐÐr   Fc                 ó¸  — t          t          |                      ¦   «         d„ ¬¦  «        ¦  «        }t          |¦  «        }i }d}d}|                     ¦   «         D ]E\  }}|||z  z   }	|	dk     r||z  }|dz  }|||z  z  }Œ&|||z  ||z  z  z  }||         ||z  z   ||<   ŒF|r$t	          || j        ¦  «        t          |¦  «        fS t	          || j        ¦  «        S )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                 ó   — | d         S )Nr   © )Úitems    r   ú<lambda>zDQuasiDistribution.nearest_probability_distribution.<locals>.<lambda>g   s
   € À$ÀqÄ'€ r   )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_distributionz2QuasiDistribution.nearest_probability_distributionX   s  € õ �F 4§:¢:¡<¤<Ð5IÐ5IÐJÑJÔJÑKÔKˆÝ˜Ñ%Ô%ˆ	Øˆ	ØˆØˆØ$×*Ò*Ñ,Ô,ð 	Fð 	F‰HˆC�Ø˜ 	Ñ)Ñ)ˆDØ�aŠxˆxØ˜‘�Ø˜Q‘�	Ø˜˜c™	Ñ!��à˜ 	Ñ)¨d°YÑ.>Ñ?Ñ?�Ø!-¨cÔ!2°T¸IÑ5EÑ!E�	˜#‘�Øð 	GÝ# I¨t¬zÑ:Ô:½DÀ¹J¼JÐFÐFÝ 	¨4¬:Ñ6Ô6Ð6r   c                 óZ   ‡— |€| j         n|Šˆfd„|                      ¦   «         D ¦   «         S )a¦  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"``
        Nc                 ó^   •— i | ])\  }}t          |d ¦  «                             ‰¦  «        |“Œ*S )Úb)ÚformatÚzfill)r   r   r   Úns      €r   r   z:QuasiDistribution.binary_probabilities.<locals>.<dictcomp>‰   s7   ø€ ÐPÐPÐP±Z°S¸%•�s˜CÑ Ô ×&Ò& qÑ)Ô)¨5ÐPÐPÐPr   )r   r(   )r/   Únum_bitsrI   s     @r   Úbinary_probabilitiesz&QuasiDistribution.binary_probabilitiesy   s7   ø€ ð 'Ð.ˆDŒNˆN°HˆØPÐPÐPÐPÀ4Ç:Â:Á<Ä<ÐPÑPÔPÐPr   c                 ó>   — d„ |                       ¦   «         D ¦   «         S )zÍBuild a quasi-probabilities dictionary with hexadecimal string keys

        Returns:
            dict: A dictionary where the keys are hexadecimal strings in the
                format ``"0x1a"``
        c                 ó4   — i | ]\  }}t          |¦  «        |“ŒS r6   )Úhexr   s      r   r   z7QuasiDistribution.hex_probabilities.<locals>.<dictcomp>’   s$   € Ð?Ð?Ð?¡J C¨•�C‘”˜%Ð?Ð?Ð?r   )r(   ©r/   s    r   Úhex_probabilitiesz#QuasiDistribution.hex_probabilities‹   s!   € ð @Ð?°$·*²*±,´,Ð?Ñ?Ô?Ð?r   c                 ó   — | j         S )z@Return an upper bound on standard deviation of expval estimator.)r   rO   s    r   r1   z$QuasiDistribution.stddev_upper_bound”   s   € ð Ô'Ð'r   c                 ó^   ‡ — t          ˆ fd„‰                      ¦   «         D ¦   «         ¦  «        S )Nc                 óD   •— i | ]\  }}|t          |‰j        ¬ ¦  «        “ŒS ))Úndigits)ÚroundÚ__ndigits__)r   r   r   r/   s      €r   r   z.QuasiDistribution.__repr__.<locals>.<dictcomp>š   s/   ø€ Ð_Ð_Ð_ÁJÀCÈ�C�˜u¨dÔ.>Ð?Ñ?Ô?Ð_Ð_Ð_r   )r&   r(   rO   s   `r   Ú__repr__zQuasiDistribution.__repr__™   s3   ø€ ÝÐ_Ð_Ð_Ð_ÐRV×R\ÒR\ÑR^ÔR^Ð_Ñ_Ô_Ñ`Ô`Ð`r   )NN)Fr   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚreÚcompiler)   rV   r.   rC   rK   rP   Úpropertyr1   rW   Ú__classcell__)r3   s   @r   r   r      sË   ø€ € € € € ØAÐAà!�r”z *Ñ-Ô-ÐØ€Kð6ð 6ð 6ð 6ð 6ð 6ðp7ð 7ð 7ð 7ðBQð Qð Qð Qð$@ð @ð @ð ð(ð (ñ „Xð(ðað að að að að að ar   r   )r[   Úmathr   r\   Úprobabilityr   r9   r   r6   r   r   ú<module>rb      s‚   ðð Ð à Ð Ð Ð Ð Ð Ø 	€	€	€	à )Ð )Ð )Ð )Ð )Ð )ð@að @að @að @að @a˜ñ @aô @að @að @að @ar   