§
    ¹³Ùgâ/  ã                   ó¸   — d Z ddlZddlZddlZddlmZ ddlmZmZ dZ	dZ
dZd„ Zd	„ Zd
„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zdedej        fd„ZdS )zL
Common functions across several serialization and deserialization modules.
é    N)Ú
load_basic)ÚformatsÚ
exceptionsé   é
   Úutf8c                 óö   — t           j                             t          j        t           j        |                      t           j        ¦  «        ¦  «        ¦  «        }|j        |                      |j	        ¦  «        fS )zñRead 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     úG/granary/edev/quantum/lib/python3.11/site-packages/qiskit/qpy/common.pyÚread_generic_typed_datar   !   sZ   € õ Ô$×*Ò*ÝŒ•gÔ4°h·m²mÅGÔDbÑ6cÔ6cÑdÔdñô €Dð Œ9�h—m’m D¤IÑ.Ô.Ð.Ð.ó    c                 óH  — g }t           j                             t          j        t           j        |                      t           j        ¦  «        ¦  «        ¦  «        }t          |j	        ¦  «        D ]1}t          | ¦  «        \  }}|                      |||fi |¤Ž¦  «         Œ2|S )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%   1   s    € ð €HåÔ×!Ò!ÝŒ•gÔ+¨X¯]ª]½7Ô;PÑ-QÔ-QÑRÔRñô €Dõ �4Ô$Ñ%Ô%ð Gð GˆÝ 7¸Ñ AÔ AÑˆ�+Ø�Š˜˜ X¨{ÐEÐE¸fÐEÐEÑFÔFÐFÐFà€Or   c           	      óX  — i }t           j                             t          j        t           j        |                      t           j        ¦  «        ¦  «        ¦  «        }t          |j	        ¦  «        D ]¹}t           j
                             t          j        t           j        |                      t           j        ¦  «        ¦  «        ¦  «        }|                      |j        ¦  «                             t          ¦  «        } ||j        |                      |j        ¦  «        fi |¤Ž}|||<   Œº|S )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.
    )r   r   r   r   r   r   r   r   r   r   ÚMAP_ITEMÚMAP_ITEM_PACKÚMAP_ITEM_SIZEÚkey_sizeÚdecodeÚENCODEr   r   )	r   r   r    Úmappingr   r"   Ú
map_headerÚkeyÚdatums	            r   Úread_mappingr1   I   sõ   € ð& €GåÔ×!Ò!ÝŒ•gÔ+¨X¯]ª]½7Ô;PÑ-QÔ-QÑRÔRñô €Dõ �4Ô$Ñ%Ô%ð ð ˆÝÔ%×+Ò+ÝŒM�'Ô/°·²½wÔ?TÑ1UÔ1UÑVÔVñ
ô 
ˆ
ð �mŠm˜JÔ/Ñ0Ô0×7Ò7½Ñ?Ô?ˆØ�˜Zœ_¨h¯mªm¸J¼OÑ.LÔ.LÐWÐWÐPVÐWÐWˆØˆ�‰ˆà€Nr   c                 ó†   — t          j        d¦  «        }t          j        d|                      |¦  «        ¦  «        d         S )z°Read 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.
    ú!1cr   )r   Úcalcsizer   r   )r   r*   s     r   Úread_type_keyr5   l   s6   € õ Œ˜uÑ%Ô%€HÝŒ=˜ §¢¨hÑ 7Ô 7Ñ8Ô8¸Ô;Ð;r   c                 ó´   — t          j        t          j        |t	          |¦  «        ¦  «        }|                      |¦  «         |                      |¦  «         dS )zëWrite 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_datar<   y   sL   € õ ”+�gÔ<¸hÍÈKÑHXÔHXÑYÔY€KØ‡N‚N�;ÑÔÐØ‡N‚N�;ÑÔÐÐÐr   c                 óÌ   — t          |¦  «        }|                      t          j        t          j        |¦  «        ¦  «         |D ]} ||fi |¤Ž\  }}t          | ||¦  «         Œ dS )az  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)r8   r9   r   r7   r   r   r<   )r   r!   Ú
serializerr    r   r0   r#   r$   s           r   Úwrite_sequencer?   †   s~   € õ �x‘=”=€Là‡N‚N•6”;�wÔ4°lÑCÔCÑDÔDÐDØð Bð BˆØ * 
¨5Ð ;Ð ;°FÐ ;Ð ;Ñˆ�+Ý  ¨8°[ÑAÔAÐAÐAðBð Br   c           	      óü  — t          |¦  «        }|                      t          j        t          j        |¦  «        ¦  «         |                     ¦   «         D ]¥\  }}|                     t          ¦  «        } ||fi |¤Ž\  }}	t          j        t          j	        t          |¦  «        |t          |	¦  «        ¦  «        }
|                      |
¦  «         |                      |¦  «         |                      |	¦  «         Œ¦dS )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.
    N)
r8   r9   r   r7   r   r   ÚitemsÚencoder,   r(   )r   r-   r>   r    r   r/   r0   Ú	key_bytesr#   r$   Úitem_headers              r   Úwrite_mappingrE   ˜   sã   € õ" �w‘<”<€Là‡N‚N•6”;�wÔ4°lÑCÔCÑDÔDÐDØ—m’m‘o”oð $ð $‰
ˆˆUØ—J’J�vÑ&Ô&ˆ	Ø * 
¨5Ð ;Ð ;°FÐ ;Ð ;Ñˆ�+Ý”k¥'Ô"7½¸Y¹¼ÈÕSVÐWbÑScÔScÑdÔdˆØ�Š�{Ñ#Ô#Ð#Ø�Š�yÑ!Ô!Ð!Ø�Š�{Ñ#Ô#Ð#Ð#ð$ð $r   c                 óV   — |                       t          j        d|¦  «        ¦  «         dS )zµWrite 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.
    r3   N)r9   r   r7   )r   r#   s     r   Úwrite_type_keyrG   µ   s(   € ð ‡N‚N•6”;˜u hÑ/Ô/Ñ0Ô0Ð0Ð0Ð0r   c                 ó˜   — t          j        ¦   «         5 } ||| fi |¤Ž |                     ¦   «         }ddd¦  «         n# 1 swxY w Y   |S )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)Úobjr>   r    Ú	containerÚbinary_datas        r   Údata_to_binaryrO   ¿   s•   € õ 
Œ‰Œð +˜Øˆ
�9˜cÐ,Ð, VÐ,Ð,Ð,Ø×(Ò(Ñ*Ô*ˆð+ð +ð +ñ +ô +ð +ð +ð +ð +ð +ð +øøøð +ð +ð +ð +ð Ðs   ”?¿AÁAc                 ó¢   — t          j        ¦   «         5 }t          || |fi |¤Ž |                     ¦   «         }ddd¦  «         n# 1 swxY w Y   |S )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)rI   rJ   r?   rK   )r!   r>   r    rM   rN   s        r   Úsequence_to_binaryrQ   Ñ   s•   € õ 
Œ‰Œð +˜Ý�y (¨JÐAÐA¸&ÐAÐAÐAØ×(Ò(Ñ*Ô*ˆð+ð +ð +ñ +ô +ð +ð +ð +ð +ð +ð +øøøð +ð +ð +ð +ð Ðó   ”$AÁAÁAc                 ó¢   — t          j        ¦   «         5 }t          || |fi |¤Ž |                     ¦   «         }ddd¦  «         n# 1 swxY w Y   |S )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)rI   rJ   rE   rK   )r-   r>   r    rM   rN   s        r   Úmapping_to_binaryrT   ä   s•   € õ& 
Œ‰Œð +˜Ý�i ¨*Ð?Ð?¸Ð?Ð?Ð?Ø×(Ò(Ñ*Ô*ˆð+ð +ð +ñ +ô +ð +ð +ð +ð +ð +ð +øøøð +ð +ð +ð +ð ÐrR   c                 óš   — t          j        | ¦  «        5 }|                     d¦  «          ||fi |¤Ž}ddd¦  «         n# 1 swxY w Y   |S )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)rI   rJ   Úseek)rN   r   r    rM   rL   s        r   Údata_from_binaryrW   þ   s–   € õ 
Œ�KÑ	 Ô	 ð 0 IØ�Š�qÑÔÐØˆl˜9Ð/Ð/¨Ð/Ð/ˆð0ð 0ð 0ñ 0ô 0ð 0ð 0ð 0ð 0ð 0ð 0øøøð 0ð 0ð 0ð 0ð €Js   •A Á AÁAc                 óz   — t          j        | ¦  «        5 }t          ||fi |¤Ž}ddd¦  «         n# 1 swxY w Y   |S )a™  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)rI   rJ   r%   )rN   r   r    rM   r!   s        r   Úsequence_from_binaryrY     s”   € õ 
Œ�KÑ	 Ô	 ð D IÝ  ¨LÐCÐC¸FÐCÐCˆðDð Dð Dñ Dô Dð Dð Dð Dð Dð Dð Døøøð Dð Dð Dð Dð €Oó   •0°4·4c                 óz   — t          j        | ¦  «        5 }t          ||fi |¤Ž}ddd¦  «         n# 1 swxY w Y   |S )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)rI   rJ   r1   )rN   r   r    rM   r-   s        r   Úmapping_from_binaryr\   !  s”   € õ& 
Œ�KÑ	 Ô	 ð B IÝ˜y¨,ÐAÐA¸&ÐAÐAˆðBð Bð Bñ Bô Bð Bð Bð Bð Bð Bð Bøøøð Bð Bð Bð Bð €NrZ   ÚpayloadÚreturnc                 ó  — t           j                             d¦  «        }| d         }| d         }t          |d         ¦  «        |k    r±|dk    rt	          j        d¦  «        ‚|dk    rt	          j        d¦  «        ‚|d	vrt	          j        d
|› d|› d�¦  «        ‚t          |d         ¦  «        }|d	vr"t	          j        dt           j        › d�¦  «        ‚t          | ¦  «        } || d<   t          | ¦  «        } t          | ¦  «        S )z@Load a symengine expression from it's serialized cereal payload.ú.é   é   é   r   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Ú__version__ÚsplitÚintr   ÚQpyErrorÚ	bytearrayÚbytesr   )r]   Úsymengine_versionÚmajorÚminorÚminor_versions        r   Úload_symengine_payloadrq   :  sO  € õ "Ô-×3Ò3°CÑ8Ô8ÐØ�AŒJ€EØ�AŒJ€EÝ
Ð˜QÔÑ Ô  EÒ)Ð)Ø�AŠ:ˆ:ÝÔ%Ødñô ð ð �AŠ:ˆ:ÝÔ%ð)ñô ð ð ˜Ð Ð ÝÔ%ð°%ð ð ¸%ð ð ð ñô ð õ Ð-¨aÔ0Ñ1Ô1ˆØ Ð(Ð(ÝÔ%ð#½IÔ<Qð #ð #ð #ñô ð õ ˜GÑ$Ô$ˆØ"ˆ�‰
Ý˜‘.”.ˆÝ�gÑÔÐr   )Ú__doc__rI   r   rf   Úsymengine.lib.symengine_wrapperr   Ú
qiskit.qpyr   r   ÚQPY_VERSIONÚQPY_COMPATIBILITY_VERSIONr,   r   r%   r1   r5   r<   r?   rE   rG   rO   rQ   rT   rW   rY   r\   rl   ÚExprrq   © r   r   ú<module>ry      s{  ððð ð 
€	€	€	Ø €€€à Ð Ð Ð ðð ð ð ð ð ð +Ð *Ð *Ð *Ð *Ð *Ð *Ð *à€ØÐ Ø	€ð/ð /ð /ð ð ð ð0 ð  ð  ðF
<ð 
<ð 
<ð
 ð 
 ð 
 ðBð Bð Bð$$ð $ð $ð:1ð 1ð 1ðð ð ð$ð ð ð&ð ð ð4ð ð ð"ð ð ð$ð ð ð2$ Eð $¨i¬nð $ð $ð $ð $ð $ð $r   