§
    º³Ùg%   ã                   óF   — d Z ddlZddlmZ ddlmZ  G d„ de¦  «        ZdS )z6A container class for counts from a circuit execution.é    N)Úpostprocess)Ú
exceptionsc                   ór   ‡ — e Zd ZdZ ej        d¦  «        Zd
ˆ fd„	Zd„ Zd„ Z	d„ Z
ed„ ¦   «         Zd	„ Zˆ xZS )ÚCountsz:A class to store a counts result from a circuit execution.z	^[01\s]+$Nc                 óø  •— d}t          |¦  «        }|si | _        i | _        i }�nÜt          t	          |                     ¦   «         ¦  «        ¦  «        }t          |t          ¦  «        r1|| _        d„ | j                             ¦   «         D ¦   «         | _        �nht          |t          ¦  «        �rC| 
                    d¦  «        r1|| _        d„ | j                             ¦   «         D ¦   «         | _        �n| 
                    d¦  «        rLd„ |                     ¦   «         D ¦   «         | _        d„ | j                             ¦   «         D ¦   «         | _        n«|s|sd| _        d| _        |}n–i }i }|                     ¦   «         D ]_\  }	}
| j                             |	¦  «        st          j        d¦  «        ‚|                      |	¦  «        }|
||<   |
|t!          |¦  «        <   Œ`|| _        || _        nt#          d	¦  «        ‚i }|| _        | j        r
| j        |d
<   || _        | j        r
| j        |d<   |st)          j        | j        |¬¦  «        }t-          ¦   «                              |¦  «         || _        dS )a'  Build a counts object

        Args:
            data (dict): The dictionary input for the counts. Where the keys
                represent a measured classical value and the value is an
                integer the number of shots with that result.
                The keys can be one of several formats:

                     * A hexadecimal string of the form ``'0x4a'``
                     * A bit string prefixed with ``0b`` for example ``'0b1011'``
                     * A bit string formatted across register and memory slots.
                       For example, ``'00 10'``.
                     * A dit string, for example ``'02'``. Note for objects created
                       with dit strings the ``creg_sizes`` and ``memory_slots``
                       kwargs don't work and :meth:`hex_outcomes` and
                       :meth:`int_outcomes` also do not work.

            time_taken (float): The duration of the experiment that generated
                the counts in seconds.
            creg_sizes (list): a nested list where the inner element is a list
                of tuples containing both the classical register name and
                classical register size. For example,
                ``[('c_reg', 2), ('my_creg', 4)]``.
            memory_slots (int): The number of total ``memory_slots`` in the
                experiment.
        Raises:
            TypeError: If the input key type is not an ``int`` or ``str``.
            QiskitError: If a dit string key is input with ``creg_sizes`` and/or
                ``memory_slots``.
        Nc                 ó4   — i | ]\  }}t          |¦  «        |“ŒS © ©Úhex©Ú.0ÚkeyÚvalues      úJ/granary/edev/quantum/lib/python3.11/site-packages/qiskit/result/counts.pyú
<dictcomp>z#Counts.__init__.<locals>.<dictcomp>I   s$   € ÐWÐWÐW±J°C¸¥ C¡¤¨%ÐWÐWÐWó    Ú0xc                 ó6   — i | ]\  }}t          |d ¦  «        |“ŒS ©r   ©Úintr   s      r   r   z#Counts.__init__.<locals>.<dictcomp>M   s&   € Ð#^Ð#^Ð#^¹:¸3À¥C¨¨Q¡K¤K°Ð#^Ð#^Ð#^r   Ú0bc                 ó6   — i | ]\  }}t          |d ¦  «        |“ŒS r   r   r   s      r   r   z#Counts.__init__.<locals>.<dictcomp>O   s&   € Ð#VÐ#VÐ#V¹:¸3À¥C¨¨Q¡K¤K°Ð#VÐ#VÐ#Vr   c                 ó4   — i | ]\  }}t          |¦  «        |“ŒS r	   r
   r   s      r   r   z#Counts.__init__.<locals>.<dictcomp>P   s$   € Ð#[Ð#[Ð#[¹
¸¸U¥C¨¡H¤H¨eÐ#[Ð#[Ð#[r   ztCounts objects with dit strings do not currently support dit string formatting parameters creg_sizes or memory_slotszgInvalid input key type %s, must be either an int key or string key with hexadecimal value or bit stringÚ
creg_sizesÚmemory_slots)Úheader)ÚdictÚint_rawÚhex_rawÚnextÚiterÚkeysÚ
isinstancer   ÚitemsÚstrÚ
startswithÚbitstring_regexÚsearchr   ÚQiskitErrorÚ_remove_space_underscorer   Ú	TypeErrorr   r   r   Úformat_countsÚsuperÚ__init__Ú
time_taken)ÚselfÚdatar0   r   r   Úbin_dataÚ	first_keyÚhex_dictÚint_dictÚ	bitstringr   Úint_keyr   Ú	__class__s                €r   r/   zCounts.__init__    s˜  ø€ ð> ˆÝ�D‰zŒzˆØð (	ØˆDŒLØˆDŒLØˆH‰Hå�T $§)¢)¡+¤+Ñ.Ô.Ñ/Ô/ˆIÝ˜)¥SÑ)Ô)ð "Ø#�”ØWÐWÀ$Ä,×BTÒBTÑBVÔBVÐWÑWÔW�”‘Ý˜I¥sÑ+Ô+ñ Ø×'Ò'¨Ñ-Ô-ð 0Ø#'�D”LØ#^Ð#^ÈÌ×I[ÒI[ÑI]ÔI]Ð#^Ñ#^Ô#^�D”L‘LØ×)Ò)¨$Ñ/Ô/ð 0Ø#VÐ#VÈÏÊÉÌÐ#VÑ#VÔ#V�D”LØ#[Ð#[ÀdÄl×FXÒFXÑFZÔFZÐ#[Ñ#[Ô#[�D”L�Là%ð 0¨lð 0Ø'+˜œØ'+˜œØ#'˜˜à#%˜Ø#%˜Ø04·
²
±´ð 	;ð 	;Ñ,˜I uØ#'Ô#7×#>Ò#>¸yÑ#IÔ#Ið "Ý&0Ô&<ð%Añ'"ô '"ð !"ð
 '+×&CÒ&CÀIÑ&NÔ&N˜GØ05˜H WÑ-Ø5:˜H¥S¨¡\¤\Ñ2Ð2Ø'/˜œØ'/˜œ˜åðMñô ð ð ˆØ$ˆŒØŒ?ð 	3Ø#'¤?ˆF�<Ñ Ø(ˆÔØÔð 	7Ø%)Ô%6ˆF�>Ñ"Øð 	NÝ"Ô0°´ÀfÐMÑMÔMˆHÝ‰Œ×Ò˜Ñ"Ô"Ð"Ø$ˆŒˆˆr   c                 ó<  ‡— | st          j        d¦  «        ‚t          |                      ¦   «         ¦  «        Šˆfd„|                      ¦   «         D ¦   «         }t          |¦  «        dk    r*t          j        dd                     |¦  «        › �¦  «        ‚|d         S )zôReturn the most frequent count

        Returns:
            str: The bit string for the most frequent result
        Raises:
            QiskitError: when there is >1 count with the same max counts, or
                an empty object.
        z7Can not return a most frequent count on an empty objectc                 ó8   •— g | ]}|d          ‰k    ¯|d         ‘ŒS )é   r   r	   )r   ÚxÚ	max_values     €r   ú
<listcomp>z(Counts.most_frequent.<locals>.<listcomp>‚   s+   ø€ ÐMÐMÐM a¸1¸Q¼4À9Ò;LÐ;L˜Q˜qœTÐ;LÐ;LÐ;Lr   r<   z.Multiple values have the same maximum counts: ú,r   )r   r*   ÚmaxÚvaluesr%   ÚlenÚjoin)r1   Úmax_values_countsr>   s     @r   Úmost_frequentzCounts.most_frequentv   s¥   ø€ ð ð 	dÝÔ(Ð)bÑcÔcÐcÝ˜Ÿš™œÑ&Ô&ˆ	ØMÐMÐMÐM¨4¯:ª:©<¬<ÐMÑMÔMÐÝÐ Ñ!Ô! QÒ&Ð&ÝÔ(Ø^ÀÇÂÐJ[ÑA\ÔA\Ð^Ð^ñô ð ð ! Ô#Ð#r   c                 ó<  — | j         r#d„ | j                              ¦   «         D ¦   «         S i }|                      ¦   «         D ]Z\  }}| j                             |¦  «        st	          j        d¦  «        ‚|                      |¦  «        }||t          |¦  «        <   Œ[|S )a  Return a counts dictionary with hexadecimal string keys

        Returns:
            dict: A dictionary with the keys as hexadecimal strings instead of
                bitstrings
        Raises:
            QiskitError: If the Counts object contains counts for dit strings
        c                 ó>   — i | ]\  }}|                      ¦   «         |“ŒS r	   )Úlowerr   s      r   r   z'Counts.hex_outcomes.<locals>.<dictcomp>“   s&   € ÐNÐNÐN©:¨3°�C—I’I‘K”K ÐNÐNÐNr   zRCounts objects with dit strings do not currently support conversion to hexadecimal)r    r%   r(   r)   r   r*   r+   r   ©r1   Úout_dictr7   r   r8   s        r   Úhex_outcomeszCounts.hex_outcomes‰   s²   € ð Œ<ð 	ØNÐN¸¼×9KÒ9KÑ9MÔ9MÐNÑNÔNÐNàˆHØ$(§J¢J¡L¤Lð /ð /Ñ �	˜5ØÔ+×2Ò2°9Ñ=Ô=ð Ý$Ô0ðFñô ð ð ×7Ò7¸	ÑBÔB�Ø).��˜W™œÑ&Ð&ØˆOr   c                 óê   — | j         r| j         S i }|                      ¦   «         D ]M\  }}| j                             |¦  «        st	          j        d¦  «        ‚|                      |¦  «        }|||<   ŒN|S )a  Build a counts dictionary with integer keys instead of count strings

        Returns:
            dict: A dictionary with the keys as integers instead of bitstrings
        Raises:
            QiskitError: If the Counts object contains counts for dit strings
        zNCounts objects with dit strings do not currently support conversion to integer)r   r%   r(   r)   r   r*   r+   rJ   s        r   Úint_outcomeszCounts.int_outcomes    s’   € ð Œ<ð 	Ø”<ÐàˆHØ$(§J¢J¡L¤Lð *ð *Ñ �	˜5ØÔ+×2Ò2°9Ñ=Ô=ð Ý$Ô0ðBñô ð ð ×7Ò7¸	ÑBÔB�Ø$)�˜Ñ!Ð!ØˆOr   c                 ór   — t          |                      dd¦  «                             dd¦  «        d¦  «        S )z1Removes all spaces and underscores from bitstringú Ú Ú_é   )r   Úreplace)r7   s    r   r+   zCounts._remove_space_underscore¶   s4   € õ �9×$Ò$ S¨"Ñ-Ô-×5Ò5°c¸2Ñ>Ô>ÀÑBÔBÐBr   c                 óD   — t          |                      ¦   «         ¦  «        S )zReturn the number of shots)ÚsumrB   )r1   s    r   ÚshotszCounts.shots»   s   € å�4—;’;‘=”=Ñ!Ô!Ð!r   )NNN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚreÚcompiler(   r/   rF   rL   rN   Ústaticmethodr+   rW   Ú__classcell__)r9   s   @r   r   r      sµ   ø€ € € € € ØDÐDà �b”j Ñ.Ô.€OðT%ð T%ð T%ð T%ð T%ð T%ðl$ð $ð $ð&ð ð ð.ð ð ð, ðCð Cñ „\ðCð"ð "ð "ð "ð "ð "ð "r   r   )r[   r\   Úqiskit.resultr   Úqiskitr   r   r   r	   r   r   ú<module>rb      sx   ðð =Ð <à 	€	€	€	à %Ð %Ð %Ð %Ð %Ð %Ø Ð Ð Ð Ð Ð ðb"ð b"ð b"ð b"ð b"ˆTñ b"ô b"ð b"ð b"ð b"r   