
    z	i%                      >    S r SSKrSSKJr  SSKJr   " S S\5      rg)z6A container class for counts from a circuit execution.    N)postprocess)
exceptionsc                   |   ^  \ rS rSrSr\R                  " S5      rSU 4S jjrS r	S r
S r\S 5       rS	 rS
rU =r$ )Counts   z:A class to store a counts result from a circuit execution.z	^[01\s]+$c                   > Sn[        U5      nU(       d  0 U l        0 U l        0 nGO&[        [	        UR                  5       5      5      n[        U[        5      (       aF  Xl        U R                  R                  5        VVs0 s H  u  px[        U5      U_M     snnU l        GO[        U[        5      (       Ga  UR                  S5      (       aG  Xl        U R                  R                  5        VVs0 s H  u  px[        US5      U_M     snnU l        GO6UR                  S5      (       at  UR                  5        VVs0 s H  u  px[        US5      U_M     snnU l        U R                  R                  5        VVs0 s H  u  px[        U5      U_M     snnU l        OU(       d  U(       d  SU l        SU l        UnO0 n	0 n
UR                  5        H]  u  pU R                  R                  U5      (       d  [        R                  " S5      eU R!                  U5      nXU'   X[        U5      '   M_     Xl        Xl        O[#        S5      e0 nX0l        U R$                  (       a  U R$                  US'   X@l        U R&                  (       a  U R&                  US'   U(       d  [(        R*                  " U R                  US	9n[,        TU ]]  U5        X l        gs  snnf s  snnf s  snnf s  snnf )
aW  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``.
N0xr   0bztCounts 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
isinstanceintitemshexstr
startswithbitstring_regexsearchr   QiskitError_remove_space_underscore	TypeErrorr   r   r   format_countssuper__init__
time_taken)selfdatar"   r   r   bin_data	first_keykeyvaluehex_dictint_dict	bitstringint_keyr   	__class__s                 N/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/result/counts.pyr!   Counts.__init__    se   > DzDLDLHT$))+./I)S))#BF,,BTBTBVWBVJCC%BVWIs++''--#'LIMI[I[I]#^I]:3CQK$6I]#^DL))$//IM#V:3CQK$6#VDLFJllFXFXFZ#[FZ
CHeOFZ#[DL%l'+'+#'#%#%04

,I#'#7#7#>#>y#I#I&0&<&<%A'" !"
 '+&C&CI&NG05W-5:S\2 1= (0'/M  $??#'??F< (%)%6%6F>""00fMH"$W  X $_#V#[s   K4KK$ K*c                 J   U (       d  [         R                  " S5      e[        U R                  5       5      nU R	                  5        Vs/ s H  o"S   U:X  d  M  US   PM     nn[        U5      S:w  a(  [         R                  " SSR                  U5       35      eUS   $ s  snf )zReturn 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 object   r   z.Multiple values have the same maximum counts: ,)r   r   maxvaluesr   lenjoin)r#   	max_valuexmax_values_countss       r.   most_frequentCounts.most_frequentv   s     (()bcc&	+/::<M<aQ49;LTQqT<M !Q&((@J[A\@]^  !## Ns   	B 	B c                    U R                   (       a>  U R                   R                  5        VVs0 s H  u  pUR                  5       U_M     snn$ 0 nU R                  5        HY  u  pBU R                  R	                  U5      (       d  [
        R                  " S5      eU R                  U5      nX#[        U5      '   M[     U$ s  snnf )zReturn 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
zRCounts objects with dit strings do not currently support conversion to hexadecimal)	r   r   lowerr   r   r   r   r   r   )r#   r'   r(   out_dictr+   r,   s         r.   hex_outcomesCounts.hex_outcomes   s     <<9=9K9K9MN9M:3CIIK&9MNNH$(JJL 	++229==$00F  77	B).W& %1 O Os   Cc                    U R                   (       a  U R                   $ 0 nU R                  5        HP  u  p#U R                  R                  U5      (       d  [        R
                  " S5      eU R                  U5      nX1U'   MR     U$ )zBuild 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   )r#   r>   r+   r(   r,   s        r.   int_outcomesCounts.int_outcomes   s~     <<<<H$(JJL 	++229==$00B  77	B$)! %1 O    c                 Z    [        U R                  SS5      R                  SS5      S5      $ )z1Removes all spaces and underscores from bitstring  _   )r   replace)r+   s    r.   r   Counts._remove_space_underscore   s+     9$$S"-55c2>BBrD   c                 4    [        U R                  5       5      $ )zReturn the number of shots)sumr4   )r#   s    r.   shotsCounts.shots   s    4;;=!!rD   )r   r   r   r   r"   )NNN)__name__
__module____qualname____firstlineno____doc__recompiler   r!   r:   r?   rB   staticmethodr   rN   __static_attributes____classcell__)r-   s   @r.   r   r      sJ    Djj.OT%l$&., C C" "rD   r   )rT   rU   qiskit.resultr   qiskitr   r   r    rD   r.   <module>r]      s!    = 	 % b"T b"rD   