
    z	i1                         S r SSKrSSKrSSKrSSKJr  SSKJrJr  Sr	Sr
SrS rS	 rS
 rS rS rS rS rS rS rS rS rS rS rS r\R4                  " S5      S\4S j5       rg)zL
Common functions across several serialization and deserialization modules.
    N)HAS_SYMENGINE)formats
exceptions      utf8c                    [         R                  R                  [        R                  " [         R
                  U R                  [         R                  5      5      5      nUR                  U R                  UR                  5      4$ )zRead a single data chunk from the file like object.

Args:
    file_obj (File): A file like object that contains the QPY binary data.

Returns:
    tuple: Tuple of type key binary and the bytes object of the single data.
)
r   INSTRUCTION_PARAM_makestructunpackINSTRUCTION_PARAM_PACKreadINSTRUCTION_PARAM_SIZEtypesize)file_objdatas     K/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/qpy/common.pyread_generic_typed_datar      s[     $$**g44hmmGDbDb6cdD 99hmmDII...    c                 J   / n[         R                  R                  [        R                  " [         R
                  U R                  [         R                  5      5      5      n[        UR                  5       H(  n[        U 5      u  pgUR                  U" Xg40 UD65        M*     U$ )a  Read a sequence of data from the file like object.

Args:
    file_obj (File): A file like object that contains the QPY binary data.
    deserializer (Callable): Deserializer callback that can handle input object type.
        This must take type key and binary data of the element and return object.
    kwargs: Options set to the deserializer.

Returns:
    list: Deserialized object.
)r   SEQUENCEr   r   r   SEQUENCE_PACKr   SEQUENCE_SIZErangenum_elementsr   append)r   deserializerkwargssequencer   _type_keydatum_bytess           r   read_sequencer%   /   s     H!!g++X]]7;P;P-QRD 4$$% 7 AXEfEF & Or   c           	         0 n[         R                  R                  [        R                  " [         R
                  U R                  [         R                  5      5      5      n[        UR                  5       GH  n[         R                  R                  [        R                  " [         R                  U R                  [         R                  5      5      5      nUR                  SS5      S:  a/  U R                  UR                  5      R                  [         5      nO-["        R$                  " U R                  UR                  5      S9nU" UR&                  U R                  UR(                  5      40 UD6nXU'   GM	     U$ )a  Read a mapping from the file like object.

.. note::

    This function must be used to make a binary data of mapping
    which include QPY serialized values.
    It's easier to use JSON serializer followed by encoding for standard data formats.
    This only supports flat dictionary and key must be string.

Args:
    file_obj (File): A file like object that contains the QPY binary data.
    deserializer (Callable): Deserializer callback that can handle mapping item.
        This must take type key and binary data of the mapping value and return object.
    kwargs: Options set to the deserializer.

Returns:
    dict: Deserialized object.
version   )bytes)r   r   r   r   r   r   r   r   r   r   MAP_ITEMMAP_ITEM_PACKMAP_ITEM_SIZEgetkey_sizedecodeENCODEuuidUUIDr   r   )	r   r   r    mappingr   r"   
map_headerkeydatums	            r   read_mappingr7   G   s   & G!!g++X]]7;P;P-QRD 4$$%%%++MM'//w?T?T1UV

 ::i$r)--
 3 34;;FCC))(--
0C0C"DECZ__hmmJOO.LWPVW & Nr   c                     [         R                  R                  [        R                  " [         R
                  U R                  [         R                  5      5      5      R                  $ )zRead a type key from the file like object.

Args:
    file_obj (File): A file like object that contains the QPY binary data.

Returns:
    bytes: Type key.
)	r   TYPE_KEYr   r   r   TYPE_KEY_PACKr   TYPE_KEY_SIZEr5   )r   s    r   read_type_keyr<   n   sE     !!g++X]]7;P;P-QR	c
r   c                     [         R                  " [        R                  U[	        U5      5      nU R                  U5        U R                  U5        g)zWrite statically typed binary data to the file like object.

Args:
    file_obj (File): A file like object to write data.
    type_key (Enum): Object type of the data.
    data_binary (bytes): Binary data to write.
N)r   packr   r   lenwrite)r   r#   data_binarydata_headers       r   write_generic_typed_datarC   |   s9     ++g<<hKHXYKNN;NN;r   c                     [        U5      nU R                  [        R                  " [        R
                  U5      5        U H  nU" U40 UD6u  pg[        XU5        M     g)a^  Write a sequence of data in the file like object.

Args:
    file_obj (File): A file like object to write data.
    sequence (Sequence): Object to serialize.
    serializer (Callable): Serializer callback that can handle input object type.
        This must return type key and binary data of each element.
    kwargs: Options set to the serializer.
N)r?   r@   r   r>   r   r   rC   )r   r!   
serializerr    r   r6   r#   r$   s           r   write_sequencerF      sP     x=LNN6;;w44lCD *5 ;F ; [A r   c           	         [        U5      nU R                  [        R                  " [        R
                  U5      5        UR                  5        H  u  pVUR                  SS5      S:  a  UR                  [        5      nOUnU" U40 UD6u  p[        R                  " [        R                  [        U5      U[        U	5      5      n
U R                  U
5        U R                  U5        U R                  U	5        M     g)a^  Write a mapping in the file like object.

.. note::

    This function must be used to make a binary data of mapping
    which include QPY serialized values.
    It's easier to use JSON serializer followed by encoding for standard data formats.
    This only supports flat dictionary and key must be string.

Args:
    file_obj (File): A file like object to write data.
    mapping (Mapping): Object to serialize.
    serializer (Callable): Serializer callback that can handle mapping item.
        This must return type key and binary data of the mapping value.
    kwargs: Options set to the serializer.
r'   r(   N)r?   r@   r   r>   r   r   itemsr-   encoder0   r+   )r   r3   rE   r    r   r5   r6   	key_bytesr#   r$   item_headers              r   write_mappingrL      s    " w<LNN6;;w44lCDmmo
::i$r)

6*II *5 ;F ;kk'"7"7YSVWbScd{#y!{# &r   c                 l    U R                  [        R                  " [        R                  U5      5        g)zWrite a type key in the file like object.

Args:
    file_obj (File): A file like object that contains the QPY binary data.
    type_key (bytes): Type key to write.
N)r@   r   r>   r   r:   )r   r#   s     r   write_type_keyrN      s!     NN6;;w44h?@r   c                     [         R                  " 5        nU" X040 UD6  UR                  5       nSSS5        U$ ! , (       d  f       W$ = f)a  Convert object into binary data with specified serializer.

Args:
    obj (any): Object to serialize.
    serializer (Callable): Serializer callback that can handle input object type.
    kwargs: Options set to the serializer.

Returns:
    bytes: Binary data.
N)ioBytesIOgetvalue)objrE   r    	containerbinary_datas        r   data_to_binaryrV      sG     
9,V,((* 
 	 
 s	   :
A	c                     [         R                  " 5        n[        X0U40 UD6  UR                  5       nSSS5        U$ ! , (       d  f       W$ = f)aT  Convert sequence into binary data with specified serializer.

Args:
    sequence (Sequence): Object to serialize.
    serializer (Callable): Serializer callback that can handle input object type.
        This must return type key and binary data of each element.
    kwargs: Options set to the serializer.

Returns:
    bytes: Binary data.
N)rP   rQ   rF   rR   )r!   rE   r    rT   rU   s        r   sequence_to_binaryrX      sI     
yJA&A((* 
 	 
 	   >
Ac                     [         R                  " 5        n[        X0U40 UD6  UR                  5       nSSS5        U$ ! , (       d  f       W$ = f)a\  Convert mapping into binary data with specified serializer.

.. note::

    This function must be used to make a binary data of mapping
    which include QPY serialized values.
    It's easier to use JSON serializer followed by encoding for standard data formats.
    This only supports flat dictionary and key must be string.

Args:
    mapping (Mapping): Object to serialize.
    serializer (Callable): Serializer callback that can handle mapping item.
        This must return type key and binary data of the mapping value.
    kwargs: Options set to the serializer.

Returns:
    bytes: Binary data.
N)rP   rQ   rL   rR   )r3   rE   r    rT   rU   s        r   mapping_to_binaryr[      sI    & 
i*??((* 
 	 
 rY   c                     [         R                  " U 5       nUR                  S5        U" U40 UD6nSSS5        U$ ! , (       d  f       W$ = f)a!  Load object from binary data with specified deserializer.

Args:
    binary_data (bytes): Binary data to deserialize.
    deserializer (Callable): Deserializer callback that can handle input object type.
    kwargs: Options set to the deserializer.

Returns:
    any: Deserialized object.
r   N)rP   rQ   seek)rU   r   r    rT   rS   s        r   data_from_binaryr^     sI     
K	 Iq9// 
! J 
!	  Js	   <
Ac                     [         R                  " U 5       n[        X140 UD6nSSS5        U$ ! , (       d  f       W$ = f)ay  Load object from binary sequence with specified deserializer.

Args:
    binary_data (bytes): Binary data to deserialize.
    deserializer (Callable): Deserializer callback that can handle input object type.
        This must take type key and binary data of the element and return object.
    kwargs: Options set to the deserializer.

Returns:
    any: Deserialized sequence.
N)rP   rQ   r%   )rU   r   r    rT   r!   s        r   sequence_from_binaryr`     s=     
K	 I CFC 
! O 
!	  O   .
=c                     [         R                  " U 5       n[        X140 UD6nSSS5        U$ ! , (       d  f       W$ = f)a  Load object from binary mapping with specified deserializer.

.. note::

    This function must be used to make a binary data of mapping
    which include QPY serialized values.
    It's easier to use JSON serializer followed by encoding for standard data formats.
    This only supports flat dictionary and key must be string.

Args:
    binary_data (bytes): Binary data to deserialize.
    deserializer (Callable): Deserializer callback that can handle mapping item.
        This must take type key and binary data of the mapping value and return object.
    kwargs: Options set to the deserializer.

Returns:
    dict: Deserialized object.
N)rP   rQ   r7   )rU   r   r    rT   r3   s        r   mapping_from_binaryrc   '  s=    & 
K	 IyA&A 
! N 
!	  Nra   zAQPY versions 10 through 12 with symengine parameter serializationpayloadc                    SSK nSSKJn  UR                  R	                  S5      nU S   nU S   n[        US   5      U:w  a  US:w  a  [        R                  " S5      eUS	:X  a  [        R                  " S
5      eUS;  a  [        R                  " SU SU S35      e[        US   5      nUS;  a$  [        R                  " SUR                   S35      e[        U 5      n X`S'   [        U 5      n U" U 5      $ )z@Load a symengine expression from it's serialized cereal payload.r   N)
load_basic.         zRQiskit doesn't support loading a symengine payload generated with symengine >= 1.0	   a  Qiskit doesn't support loading a historical QPY file with `use_symengine=True` generated in an environment using symengine 0.9.0. If you need to load this file you can do so with Qiskit 0.45.x or 0.46.x and re-export the QPY file using `use_symengine=False`.)   r   zIncompatible symengine version z! used to generate the QPY payloadz)Incompatible installed symengine version z to load this QPY payload)
	symenginesymengine.lib.symengine_wrapperrf   __version__splitintr   QpyError	bytearrayr)   )rd   rm   rf   symengine_versionmajorminorminor_versions          r   load_symengine_payloadrx   @  s*     "--33C8AJEAJE
Q E)A:%%d  A:%%)   %%1%% A   -a01(%%;I<Q<Q;R S# #  G$"
.gr   )__doc__rP   r   r1   qiskit.utils.optionalsr   
qiskit.qpyr   r   QPY_VERSIONQPY_COMPATIBILITY_VERSIONr0   r   r%   r7   r<   rC   rF   rL   rN   rV   rX   r[   r^   r`   rc   require_in_callr)   rx    r   r   <module>r      s    
   0 * 	/ 0$N

 B$$@A$&4"$2 bc)E ) d)r   