
    z	i                     x    S r SSKrSSKJr  S rS rS rS rSS jr	S	 r
S
 rS rSS jrSS jrSS jrSS jrg)zPost-processing of raw result.    N)QiskitErrorc                 D    [        [        [        U S5      5      5      SS $ )z9Convert hexadecimal readouts (memory) to binary readouts.      N)strbinint)	hexstrings    S/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/result/postprocess.py_hex_to_binr      s     s3y"%&'++    c                 ,    [        [        U S5      5      $ )z<Convert bitstring readouts (memory) to hexadecimal readouts.r   )hexr	   )	bitstrings    r   _bin_to_hexr      s    s9a !!r   c                 6    [        [        U S5      SU S35      $ )zWIf 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#qa%899r   c                     / nSn[        U5       H  u  pEUR                  XX5-    5        X5-  nM      SR                  U5      $ )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"   $   sL    JMJ')M4HIJ ( 88Jr   c                     U R                  S5      (       a  [        U 5      n U(       aO  UR                  SS5      nUR                  SS5      nU(       a  [        X5      n U(       a  U(       a  [	        X5      n U $ )aC  
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)   .   sd    & d##!+.ZZd3
zz.$7$[?K,-kFKr   c                     [         R                  " U [         R                  S9nUR                  S   S:X  d  [	        S5      eUS   SU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.
dtyper   z*Inner most nested list is not of length 2.).r                 ?).   )npasarray
complex128shaper   )complex_listarrs     r   _list_to_complex_arrayr6   M   sL     **\
7C99R=AFGGv;c&k)))r   c                 v    [        U 5      nS[        UR                  5      s=::  a  S::  d  O  [        S5      eU$ )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>   `   s:     .f5$**+0q0FGGr   c                 v    [        U 5      nS[        UR                  5      s=::  a  S::  d  O  [        S5      eU$ )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   s:     .f5$**+0q0EFFr   c                 P    / nU  H  nUR                  [        X15      5        M     U$ )a7  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      s-     K/DE r   c                 Z    0 nU R                  5        H  u  p4[        X15      nXBU'   M     U$ )a'  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      s5     KLLN"3/C # r   c                 \   SSK Jn  [        X5      (       a;  U(       a2  U" [        R                  " U R
                  US9U R                  5       S9$ U $ [        U [        R                  5      (       a  U(       a  [        R                  " XS9$ U $ [        U 5      nU (       a2  [        U S   [        5      (       a  [        R                  " U [        S9nO?[        R                  " U[        S9n[        U5       H  nX   S   SX   S   -  -   XE'   M     U(       a  [        R                  " XAS9nU$ )a)  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]      s     C###ryyHECHHJWW
#rzz""99S44
CI
z#a&'**hhs'2hhy8y!A VAYcfQi7KN " ii?r   c                    SSK Jn  [        X5      (       aJ  U(       aA  U" [        R                  " U R
                  US9U R                  5       U R                  5       S9$ U $ [        U [        R                  5      (       a  U(       a  [        R                  " XS9$ U $ [        U 5      n[        R                  " X34[        S9n[        U 5       H  u  pV[        Xa5      XE'   M     U$ )a2  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6>>+OO- 
 
#rzz""99S44
CI((I1AKC.+C: !r   )N)__doc__numpyr0   qiskit.exceptionsr   r   r   r   r"   r)   r6   r>   r@   rC   rJ   r]   rf    r   r   <module>rk      sM    %  ),
"
: >*&**"& Fr   