§
    º³ÙgÄ  ã                   óp   — d Z ddlZddlmZ d„ Zd„ Zd„ Zd„ Zdd„Z	d	„ Z
d
„ Zd„ Zdd„Zdd„Zdd„Zdd„ZdS )zPost-processing of raw result.é    N)ÚQiskitErrorc                 óf   — t          t          t          | d¦  «        ¦  «        ¦  «        dd…         S )z9Convert hexadecimal readouts (memory) to binary readouts.é   é   N)ÚstrÚbinÚint)Ú	hexstrings    úO/granary/edev/quantum/lib/python3.11/site-packages/qiskit/result/postprocess.pyÚ_hex_to_binr      s+   € å�s•3�y "Ñ%Ô%Ñ&Ô&Ñ'Ô'¨¨¨Ô+Ð+ó    c                 ó<   — t          t          | d¦  «        ¦  «        S )z<Convert bitstring readouts (memory) to hexadecimal readouts.r   )Úhexr	   )Ú	bitstrings    r   Ú_bin_to_hexr      s   € å�s�9˜aÑ Ô Ñ!Ô!Ð!r   c                 óF   — t          t          | d¦  «        d|› d�¦  «        S )z[If the bitstring is truncated, pad extra zeros to make its
    length equal to memory_slotsr   Ú0Úb)Úformatr	   )r   Úmemory_slotss     r   Ú
_pad_zerosr      s*   € õ •#�i Ñ#Ô#Ð%8¨Ð%8Ð%8Ð%8Ñ9Ô9Ð9r   c                 ó¨   — g }d}t          |¦  «        D ]*\  }}|                     | |||z   …         ¦  «         ||z  }Œ+d                     |¦  «        S )zMSeparate a bitstring according to the registers defined in the result header.r   ú )ÚreversedÚappendÚjoin)r   Ú
creg_sizesÚ
substringsÚrunning_indexÚ_Úsizes         r   Ú_separate_bitstringr"   $   sh   € à€JØ€MÝ˜JÑ'Ô'ð ð ‰ˆˆ4Ø×Ò˜) M°MÀDÑ4HÐ$HÔIÑJÔJÐJØ˜ÑˆˆØ�8Š8�JÑÔÐr   c                 óö   — |                       d¦  «        rt          | ¦  «        } |rR|                     dd¦  «        }|                     dd¦  «        }|rt          | |¦  «        } |r|rt	          | |¦  «        } | S )aw  
    Format a single bitstring (memory) from a single shot experiment.

    - The hexadecimals are expanded to bitstrings

    - Spaces are inserted at register divisions.

    Args:
        shot_memory (str): result of a single experiment.
        header (dict): the experiment header dictionary containing
            useful information for postprocessing. creg_sizes
            are a nested list where the inner element is a list
            of creg name, creg size pairs. memory_slots is an integers
            specifying the number of total memory_slots in the experiment.

    Returns:
        dict: a formatted memory
    Ú0xr   Nr   )Ú
startswithr   Úgetr   r"   )Úshot_memoryÚheaderr   r   s       r   Úformat_counts_memoryr)   .   s”   € ð& ×Ò˜dÑ#Ô#ð /Ý! +Ñ.Ô.ˆØð GØ—Z’Z ¨dÑ3Ô3ˆ
Ø—z’z .°$Ñ7Ô7ˆØð 	@Ý$ [°,Ñ?Ô?ˆKØð 	G˜,ð 	GÝ-¨k¸:ÑFÔFˆKØÐr   c                 óª   — t          j        | t           j        ¬¦  «        }|j        d         dk    st	          d¦  «        ‚|d         d|d         z  z   S )a   Convert nested list of shape (..., 2) to complex numpy array with shape (...)

    Args:
        complex_list (list): List to convert.

    Returns:
        np.ndarray: Complex numpy array

    Raises:
        QiskitError: If inner most array of input nested list is not of length 2.
    ©Údtypeéÿÿÿÿr   z*Inner most nested list is not of length 2.).r   ù              ð?).é   )ÚnpÚasarrayÚ
complex128Úshaper   )Úcomplex_listÚarrs     r   Ú_list_to_complex_arrayr6   M   sR   € õ Œ*�\­¬Ð
7Ñ
7Ô
7€CØŒ9�RŒ=˜AÒÐÝÐFÑGÔGÐGàˆvŒ;˜˜c &œkÑ)Ñ)Ð)r   c                 ó„   — t          | ¦  «        }dt          |j        ¦  «        cxk    rdk    sn t          d¦  «        ‚|S )a£  Format an experiment result memory object for measurement level 0.

    Args:
        memory (list): Memory from experiment with `meas_level==1`. `avg` or
            `single` will be inferred from shape of result memory.

    Returns:
        np.ndarray: Measurement level 0 complex numpy array

    Raises:
        QiskitError: If the returned numpy array does not have 2 (avg) or 3 (single)
            indices.
    r   é   z*Level zero memory is not of correct shape.©r6   Úlenr3   r   ©ÚmemoryÚformatted_memorys     r   Úformat_level_0_memoryr>   `   sR   € õ .¨fÑ5Ô5Ðà•Ð$Ô*Ñ+Ô+Ð0Ð0Ò0Ð0¨qÒ0Ð0Ð0Ð0ÝÐFÑGÔGÐGØÐr   c                 ó„   — t          | ¦  «        }dt          |j        ¦  «        cxk    rdk    sn t          d¦  «        ‚|S )a£  Format an experiment result memory object for measurement level 1.

    Args:
        memory (list): Memory from experiment with `meas_level==1`. `avg` or
            `single` will be inferred from shape of result memory.

    Returns:
        np.ndarray: Measurement level 1 complex numpy array

    Raises:
        QiskitError: If the returned numpy array does not have 1 (avg) or 2 (single)
            indices.
    r/   r   z)Level one memory is not of correct shape.r9   r;   s     r   Úformat_level_1_memoryr@   u   sR   € õ .¨fÑ5Ô5Ðà•Ð$Ô*Ñ+Ô+Ð0Ð0Ò0Ð0¨qÒ0Ð0Ð0Ð0ÝÐEÑFÔFÐFØÐr   c                 óZ   — g }| D ]%}|                      t          ||¦  «        ¦  «         Œ&|S )aS  Format an experiment result memory object for measurement level 2.

    Args:
        memory (list): Memory from experiment with `meas_level==2` and `memory==True`.
        header (dict): the experiment header dictionary containing
            useful information for postprocessing.

    Returns:
        list[str]: List of bitstrings
    )r   r)   )r<   r(   Úmemory_listr'   s       r   Úformat_level_2_memoryrC   Š   sB   € ð €KØð Fð FˆØ×ÒÕ/°¸VÑDÔDÑEÔEÐEÐEØÐr   c                 óh   — i }|                       ¦   «         D ]\  }}t          ||¦  «        }|||<   Œ|S )aG  Format a single experiment result coming from backend to present
    to the Qiskit user.

    Args:
        counts (dict): counts histogram of multiple shots
        header (dict): the experiment header dictionary containing
            useful information for postprocessing.

    Returns:
        dict: a formatted counts
    )Úitemsr)   )Úcountsr(   Úcounts_dictÚkeyÚvals        r   Úformat_countsrJ   ›   sF   € ð €KØ—L’L‘N”Nð ð ‰ˆˆSÝ" 3¨Ñ/Ô/ˆØˆ�CÑÐØÐr   c                 ór  — ddl m} t          | |¦  «        r<|r8 |t          j        | j        |¬¦  «        |                      ¦   «         ¬¦  «        S | S t          | t          j        ¦  «        r|rt          j        | |¬¦  «        S | S t          | ¦  «        }| r7t          | d         t          ¦  «        rt          j
        | t          ¬¦  «        }nPt          j        |t          ¬¦  «        }t          |¦  «        D ]%}| |         d         d| |         d         z  z   ||<   Œ&|rt          j        ||¬¦  «        }|S )aE  Format statevector coming from the backend to present to the Qiskit user.

    Args:
        vec (list): a list of [re, im] complex numbers.
        decimals (int): the number of decimals in the statevector.
            If None, no rounding is done.

    Returns:
        list[complex]: a list of python complex numbers.
    r   )ÚStatevector©Údecimals)Údimsr+   r.   r/   )Ú&qiskit.quantum_info.states.statevectorrL   Ú
isinstancer0   ÚaroundÚdatarO   Úndarrayr:   ÚcomplexÚarrayÚzerosÚrange)ÚvecrN   rL   Ú	num_basisÚvec_complexÚis         r   Úformat_statevectorr]   ®   sK  € ð CÐBÐBÐBÐBÐBå�#�{Ñ#Ô#ð Øð 	XØ�;�rœy¨¬¸HÐEÑEÔEÈCÏHÊHÉJÌJÐWÑWÔWÐWØˆ
Ý�#•r”zÑ"Ô"ð Øð 	5Ý”9˜S¨8Ð4Ñ4Ô4Ð4Øˆ
Ý�C‘”€IØ
ð 8�z˜#˜aœ&¥'Ñ*Ô*ð 8Ý”h˜s­'Ð2Ñ2Ô2ˆˆå”h˜yµÐ8Ñ8Ô8ˆÝ�yÑ!Ô!ð 	8ð 	8ˆAØ  œV AœY¨¨c°!¬f°Q¬i©Ñ7ˆK˜‰NˆNàð @Ý”i °hÐ?Ñ?Ô?ˆØÐr   c                 óà  — ddl m} t          | |¦  «        rO|rK |t          j        | j        |¬¦  «        |                      ¦   «         |                      ¦   «         ¬¦  «        S | S t          | t          j        ¦  «        r|rt          j        | |¬¦  «        S | S t          | ¦  «        }t          j
        ||ft          ¬¦  «        }t          | ¦  «        D ]\  }}t          ||¦  «        ||<   Œ|S )aN  Format unitary coming from the backend to present to the Qiskit user.

    Args:
        mat (list[list]): a list of list of [re, im] complex numbers
        decimals (int): the number of decimals in the statevector.
            If None, no rounding is done.

    Returns:
        list[list[complex]]: a matrix of complex numbers
    r   )ÚOperatorrM   )Ú
input_dimsÚoutput_dimsr+   )Ú&qiskit.quantum_info.operators.operatorr_   rQ   r0   rR   rS   r`   ra   rT   r:   rW   rU   Ú	enumerater]   )ÚmatrN   r_   rZ   Úmat_complexr\   rY   s          r   Úformat_unitaryrf   Ñ   s  € ð @Ð?Ð?Ð?Ð?Ð?å�#�xÑ Ô ð Øð 	Ø�8Ý”	˜#œ(¨XÐ6Ñ6Ô6ØŸ>š>Ñ+Ô+ØŸOšOÑ-Ô-ðñ ô ð ð
 ˆ
Ý�#•r”zÑ"Ô"ð Øð 	5Ý”9˜S¨8Ð4Ñ4Ô4Ð4Øˆ
Ý�C‘”€IÝ”(˜I yÐ1½ÐAÑAÔA€KÝ˜C‘.”.ð ;ð ;‰ˆˆ3Ý+¨C°Ñ:Ô:ˆ�A‰ˆØÐr   )N)Ú__doc__Únumpyr0   Úqiskit.exceptionsr   r   r   r   r"   r)   r6   r>   r@   rC   rJ   r]   rf   © r   r   ú<module>rk      s  ðð %Ð $à Ð Ð Ð à )Ð )Ð )Ð )Ð )Ð )ð,ð ,ð ,ð
"ð "ð "ð
:ð :ð :ð ð  ð  ðð ð ð ð>*ð *ð *ð&ð ð ð*ð ð ð*ð ð ð ð"ð ð ð ð& ð  ð  ð  ðFð ð ð ð ð r   