§
    ¹³ÙgÛ9  ã                  óp  — d Z ddlmZ ddlmZmZ ddlmZmZm	Z	m
Z
mZ ddlmZ ddlZddlZddlZddlmZ ddlmZ dd	lmZ dd
lmZmZmZmZ ddlmZmZ ddlm Z  ddl!m"Z" eeef         Z#dZ$ ej%        e$ej&        ej'        z  ¦  «        Z( e"ddd„ ¬¦  «        ddej)        fd%d„¦   «         Z*	 	 d&d'd$„Z+dS )(z!User interface of qpy serializer.é    )Úannotations)ÚJSONEncoderÚJSONDecoder)ÚUnionÚListÚBinaryIOÚTypeÚOptional)ÚIterableN)ÚQuantumCircuit)ÚScheduleBlock)ÚQiskitError)ÚformatsÚcommonÚ	binary_ioÚ	type_keys)Ú"QPYLoadingDeprecatedFeatureWarningÚQpyError)Ú__version__)Údeprecate_pulse_arga™  ^
    v?
    (?:
        (?:(?P<epoch>[0-9]+)!)?                           # epoch
        (?P<release>[0-9]+(?:\.[0-9]+)*)                  # release segment
        (?P<pre>                                          # pre-release
            [-_\.]?
            (?P<pre_l>(a|b|c|rc|alpha|beta|pre|preview))
            [-_\.]?
            (?P<pre_n>[0-9]+)?
        )?
        (?P<post>                                         # post release
            (?:-(?P<post_n1>[0-9]+))
            |
            (?:
                [-_\.]?
                (?P<post_l>post|rev|r)
                [-_\.]?
                (?P<post_n2>[0-9]+)?
            )
        )?
        (?P<dev>                                          # dev release
            [-_\.]?
            (?P<dev_l>dev)
            [-_\.]?
            (?P<dev_n>[0-9]+)?
        )?
    )
    (?:\+(?P<local>[a-z0-9]+(?:[-_\.][a-z0-9]+)*))?       # local version
$Úprogramsz%Passing `ScheduleBlock` to `programs`c                ó,   — t          | t          ¦  «        S )N)Ú
isinstancer   )Úps    úJ/granary/edev/quantum/lib/python3.11/site-packages/qiskit/qpy/interface.pyú<lambda>r   P   s   € �
 1¥mÑ4Ô4€ ó    )Údeprecation_descriptionÚ	predicateTú5Union[List[QPY_SUPPORTED_TYPES], QPY_SUPPORTED_TYPES]Úfile_objr   Úmetadata_serializerúOptional[Type[JSONEncoder]]Úuse_symengineÚboolÚversionÚintc                ó  — t          | t          ¦  «        s| g} t          ¦   «         }| D ]$}|                     t	          |¦  «        ¦  «         Œ%t          |¦  «        dk    rt          d¦  «        ‚t          t          |¦  «        ¦  «        }t          |t          ¦  «        rt          j        j        }t          j        }	n:|t           u rt          j        j        }t          j        }	nt'          d|› d�¦  «        ‚|€t(          j        }nLt(          j        |k    s|t(          j        k    r,t/          d|› dt(          j        › dt(          j        › �¦  «        ‚t0                               t4          ¦  «        }
d	„ |
                     d
¦  «                             d¦  «        D ¦   «         }t          j                             |¦  «        }t?          j         tB          j"        d||d         |d         |d         t          | ¦  «        |¦  «        }| #                    |¦  «         t)          j$        ||¦  «         d}| D ]0}|t          j        j        k    r	|j%        rd} |	|||||¬¦  «         Œ1|rtM          j'        tP          d¬¦  «         dS dS )a  Write QPY binary data to a file

    This function is used to save a circuit to a file for later use or transfer
    between machines. The QPY format is backwards compatible and can be
    loaded with future versions of Qiskit.

    For example:

    .. code-block:: python

        from qiskit.circuit import QuantumCircuit
        from qiskit import qpy

        qc = QuantumCircuit(2, name='Bell', metadata={'test': True})
        qc.h(0)
        qc.cx(0, 1)
        qc.measure_all()

    from this you can write the qpy data to a file:

    .. code-block:: python

        with open('bell.qpy', 'wb') as fd:
            qpy.dump(qc, fd)

    or a gzip compressed file:

    .. code-block:: python

        import gzip

        with gzip.open('bell.qpy.gz', 'wb') as fd:
            qpy.dump(qc, fd)

    Which will save the qpy serialized circuit to the provided file.

    Args:
        programs: QPY supported object(s) to store in the specified file like object.
            QPY supports :class:`.QuantumCircuit` and :class:`.ScheduleBlock`.
            Different data types must be separately serialized.
            Support for :class:`.ScheduleBlock` is deprecated since Qiskit 1.3.0.
        file_obj: The file like object to write the QPY data too
        metadata_serializer: An optional JSONEncoder class that
            will be passed the ``.metadata`` attribute for each program in ``programs`` and will be
            used as the ``cls`` kwarg on the `json.dump()`` call to JSON serialize that dictionary.
        use_symengine: If True, all objects containing symbolic expressions will be serialized
            using symengine's native mechanism. This is a faster serialization alternative,
            but not supported in all platforms. This flag only has an effect if the emitted QPY format
            version is 10, 11, or 12. For QPY format version >= 13 (which is the default starting in
            Qiskit 1.3.0) this flag is no longer used.
        version: The QPY format version to emit. By default this defaults to
            the latest supported format of :attr:`~.qpy.QPY_VERSION`, however for
            compatibility reasons if you need to load the generated QPY payload with an older
            version of Qiskit you can also select an older QPY format version down to the minimum
            supported export version, which only can change during a Qiskit major version release,
            to generate an older QPY format version.  You can access the current QPY version and
            minimum compatible version with :attr:`.qpy.QPY_VERSION` and
            :attr:`.qpy.QPY_COMPATIBILITY_VERSION` respectively.

            .. note::

                If specified with an older version of QPY the limitations and potential bugs stemming
                from the QPY format at that version will persist. This should only be used if
                compatibility with loading the payload with an older version of Qiskit is necessary.

            .. note::

                If serializing a :class:`.QuantumCircuit` or :class:`.ScheduleBlock` that contain
                :class:`.ParameterExpression` objects with ``version`` set low with the intent to
                load the payload using a historical release of Qiskit, it is safest to set the
                ``use_symengine`` flag to ``False``.  Versions of Qiskit prior to 1.2.4 cannot load
                QPY files containing ``symengine``-serialized :class:`.ParameterExpression` objects
                unless the version of ``symengine`` used between the loading and generating
                environments matches.


    Raises:
        QpyError: When multiple data format is mixed in the output.
        TypeError: When invalid data type is input.
        ValueError: When an unsupported version number is passed in for the ``version`` argument
    é   z^Input programs contain multiple data types. Different data type must be serialized separately.ú'z' is not supported data type.NzThe specified QPY version z^ is not support for dumping with this version, of Qiskit. The only supported versions between z and c                ó,   — g | ]}t          |¦  «        ‘ŒS © ©r'   ©Ú.0Úxs     r   ú
<listcomp>zdump.<locals>.<listcomp>Ë   s   € ÐOÐOÐO •S˜‘V”VÐOÐOÐOr   Úreleaseú.s   QISKITr   é   FT)r"   r$   r&   z[Pulse gates serialization is deprecated as of Qiskit 1.3. It will be removed in Qiskit 2.0.©ÚcategoryÚmessage))r   r   ÚsetÚaddÚtypeÚlenr   ÚnextÚiterÚ
issubclassr   r   ÚProgramÚCIRCUITr   Úwrite_circuitr   ÚSCHEDULE_BLOCKÚwrite_schedule_blockÚ	TypeErrorr   ÚQPY_VERSIONÚQPY_COMPATIBILITY_VERSIONÚ
ValueErrorÚVERSION_PATTERN_REGEXÚsearchr   ÚgroupÚsplitÚSymExprEncodingÚassignÚstructÚpackr   ÚFILE_HEADER_V10_PACKÚwriteÚwrite_type_keyÚ_calibrations_propÚwarningsÚwarnÚDeprecationWarning)r   r!   r"   r$   r&   Úprogram_typesÚprogramÚprogram_typeÚtype_keyÚwriterÚversion_matchÚversion_partsÚencodingÚheaderÚpulse_gatess                  r   Údumpra   M   sÄ  € õz �h¥Ñ)Ô)ð Ø�:ˆå‘E”E€MØð )ð )ˆØ×Ò�$˜w™-œ-Ñ(Ô(Ð(Ð(å
ˆ=ÑÔ˜AÒÐÝðAñ
ô 
ð 	
õ �˜]Ñ+Ô+Ñ,Ô,€Lå�,¥Ñ/Ô/ð IÝÔ$Ô,ˆÝÔ(ˆˆØ	�Ð	&Ð	&ÝÔ$Ô3ˆÝÔ/ˆˆåÐG˜LÐGÐGÐGÑHÔHÐHà€ÝÔ$ˆˆÝ	Ô	)¨GÒ	3Ð	3°wÅÔASÒ7SÐ7SÝð$¨ð $ð $Ý>DÔ>^ð$ð $åÔ!ð$ð $ñ
ô 
ð 	
õ *×0Ò0µÑ=Ô=€MØOÐO ]×%8Ò%8¸Ñ%CÔ%C×%IÒ%IÈ#Ñ%NÔ%NÐOÑOÔO€MÝÔ(×/Ò/°Ñ>Ô>€HÝŒ[ÝÔ$ØØØ�aÔØ�aÔØ�aÔÝˆH‰ŒØñ	ô 	€Fð ‡N‚N�6ÑÔÐÝ
Ô˜( HÑ-Ô-Ð-à€KØð 	
ð 	
ˆØ•yÔ(Ô0Ò0Ð0°WÔ5OÐ0ØˆKØˆØØØ 3Ø'Øð	
ñ 	
ô 	
ð 	
ð 	
ð ð 
ÝŒÝ'ð0ð	
ñ 	
ô 	
ð 	
ð 	
ð 	
ð
ð 
r   FÚmetadata_deserializerúOptional[Type[JSONDecoder]]Útrust_payloadÚreturnúList[QPY_SUPPORTED_TYPES]c           
     óæ  — t          j        d|                      d¦  «        ¦  «        d         }|                      d¦  «         |t          j        k    rt          d|› d�¦  «        ‚|dk     rZt          j         	                    t          j        t          j
        |                      t          j        ¦  «        ¦  «        ¦  «        }nYt          j         	                    t          j        t          j        |                      t          j        ¦  «        ¦  «        ¦  «        }|j                             t          j        ¦  «        dk    rt          d	¦  «        ‚t$                               t(          ¦  «        }d
„ |                     d¦  «                             d¦  «        D ¦   «         }|j        |j        |j        f}|d         |d         k     sZ|d         |d         k    r|d         |d         k    s6|d         |d         k    ra|d         |d         k    rO|d         |d         k    r=t5          j        dd                     d„ |D ¦   «         ¦  «        › dt(          › d�¦  «         |j        dk     rt<          j        j         }nt	          j!        | ¦  «        }|j        dk     rd}	n|j"        t<          j#        j$        k    }	|t<          j        j         k    rtJ          j&        }
nP|t<          j        j'        k    r(tJ          j(        }
t5          j        tR          d¬¦  «         ntU          d|› d�¦  «        ‚g }tW          |j,        ¦  «        D ]*}| -                     |
| |j        ||	|¬¦  «        ¦  «         Œ+|S )aì  Load a QPY binary file

    This function is used to load a serialized QPY Qiskit program file and create
    :class:`~qiskit.circuit.QuantumCircuit` objects or
    :class:`~qiskit.pulse.schedule.ScheduleBlock` objects from its contents.
    For example:

    .. code-block:: python

        from qiskit import qpy

        with open('bell.qpy', 'rb') as fd:
            circuits = qpy.load(fd)

    or with a gzip compressed file:

    .. code-block:: python

        import gzip
        from qiskit import qpy

        with gzip.open('bell.qpy.gz', 'rb') as fd:
            circuits = qpy.load(fd)

    which will read the contents of the qpy and return a list of
    :class:`~qiskit.circuit.QuantumCircuit` objects or
    :class:`~qiskit.pulse.schedule.ScheduleBlock` objects from the file.

    Args:
        file_obj: A file like object that contains the QPY binary
            data for a circuit or pulse schedule.
        metadata_deserializer: An optional JSONDecoder class
            that will be used for the ``cls`` kwarg on the internal
            ``json.load`` call used to deserialize the JSON payload used for
            the ``.metadata`` attribute for any programs in the QPY file.
            If this is not specified the circuit metadata will
            be parsed as JSON with the stdlib ``json.load()`` function using
            the default ``JSONDecoder`` class.
        trust_payload: if set to ``False`` (the default),
            :class:`.ScheduleBlock` objects in the payload that were
            serialized using ``sympy`` are not allowed and will error. This
            is because the ``sympy`` parsing uses :func:`eval`, which
            can allow for arbitrary code execution.
            The flag should only be set
            to ``True`` if you trust the QPY payload you are loading.

    .. warning::

        If ``trust_payload`` is set to ``True`` this can enable arbitrary
        code execution because internally ``sympy`` relies on :func:`eval`.

    Returns:
        The list of Qiskit programs contained in the QPY data.
        A list is always returned, even if there is only 1 program in the QPY data.

    Raises:
        QiskitError: if ``file_obj`` is not a valid QPY file
        TypeError: When invalid data type is loaded.
    z!6sBé   r)   r   z#The QPY format version being read, zh, isn't supported by this Qiskit version. Please upgrade your version of Qiskit to load this QPY payloadé
   ÚQISKITz"Input file is not a valid QPY filec                ó,   — g | ]}t          |¦  «        ‘ŒS r,   r-   r.   s     r   r1   zload.<locals>.<listcomp>K  s   € ÐTÐTÐT Q�#˜a™&œ&ÐTÐTÐTr   r2   r3   r4   z;The qiskit version used to generate the provided QPY file, c                ó,   — g | ]}t          |¦  «        ‘ŒS r,   )Ústrr.   s     r   r1   zload.<locals>.<listcomp>\  s   € Ð>Ð>Ð>¨!�s 1™vœvÐ>Ð>Ð>r   z+, is newer than the current qiskit version zj. This may result in an error if the QPY file uses instructions not present in this current qiskit versioné   Fa  Pulse gates deserialization is deprecated as of Qiskit 1.3 and will be removed in Qiskit 2.0. This is part of the deprecation plan for the entire Qiskit Pulse package. Once Pulse is removed, `ScheduleBlock` sections will be ignored when loading QPY files with pulse data.r5   z"Invalid payload format data kind 'z'.)rb   r$   Útrust_input).rN   ÚunpackÚreadÚseekr   rE   r   r   ÚFILE_HEADERÚ_makeÚFILE_HEADER_PACKÚFILE_HEADER_SIZEÚFILE_HEADER_V10rP   ÚFILE_HEADER_V10_SIZEÚprefaceÚdecodeÚENCODErH   rI   r   rJ   rK   Úmajor_versionÚminor_versionÚpatch_versionrT   rU   ÚjoinÚqpy_versionr   r?   r@   Úread_type_keyÚsymbolic_encodingrL   Ú	SYMENGINEr   Úread_circuitrB   Úread_schedule_blockr   rD   ÚrangeÚnum_programsÚappend)r!   rb   rd   r&   Údatar\   Úenv_qiskit_versionÚqiskit_versionrZ   r$   Úloaderr   Ú_s                r   ÚloadrŽ   î   s“  € õD Œm˜F H§M¢M°!Ñ$4Ô$4Ñ5Ô5°aÔ8€GØ‡M‚M�!ÑÔÐà•Ô#Ò#Ð#Ýðb°'ð bð bð bñ
ô 
ð 	
ð
 �‚|€|ÝÔ"×(Ò(ÝŒMÝÔ(Ø—’�gÔ6Ñ7Ô7ñô ñ
ô 
ˆˆõ Ô&×,Ò,ÝŒMÝÔ,Ø—’�gÔ:Ñ;Ô;ñô ñ
ô 
ˆð „|×Ò�6œ=Ñ)Ô)¨XÒ5Ð5ÝÐ>Ñ?Ô?Ð?Ý)×0Ò0µÑ=Ô=€MØTÐT¨-×*=Ò*=¸iÑ*HÔ*H×*NÒ*NÈsÑ*SÔ*SÐTÑTÔTÐàÔ(¨$Ô*<¸dÔ>PÐQ€Nð 	˜1Ô ¨qÔ 1Ò1Ð1à˜qÔ! ^°AÔ%6Ò6Ð6¸>È!Ô;LÐOaÐbcÔOdÒ;dÐ;dð ˜qÔ! ^°AÔ%6Ò6Ð6Ø˜qÔ!Ð%7¸Ô%:Ò:Ð:Ø˜qÔ!Ð$6°qÔ$9Ò9Ð9õ 	ŒðØ—X’XÐ>Ð>¨~Ð>Ñ>Ô>Ñ?Ô?ðð å8Cðð ð ñ	
ô 	
ð 	
ð Ô˜!ÒÐÝÔ$Ô,ˆˆåÔ'¨Ñ1Ô1ˆàÔ˜"ÒÐØˆˆàÔ.µ)Ô2KÔ2UÒUˆà•9Ô$Ô,Ò,Ð,ÝÔ'ˆˆØ	•YÔ&Ô5Ò	5Ð	5ÝÔ.ˆÝŒÝ7ðOð	
ñ 	
ô 	
ð 	
ð 	
õ ÐI¸XÐIÐIÐIÑJÔJÐJà€HÝ�4Ô$Ñ%Ô%ð 	
ð 	
ˆØ�ŠØˆFØØÔ Ø&;Ø+Ø)ðñ ô ñ	
ô 	
ð 	
ð 	
ð €Or   )
r   r    r!   r   r"   r#   r$   r%   r&   r'   )NF)r!   r   rb   rc   rd   r%   re   rf   ),Ú__doc__Ú
__future__r   Újsonr   r   Útypingr   r   r   r	   r
   Úcollections.abcr   rN   rT   ÚreÚqiskit.circuitr   Úqiskit.pulser   Úqiskit.exceptionsr   Ú
qiskit.qpyr   r   r   r   Úqiskit.qpy.exceptionsr   r   Úqiskit.versionr   Úqiskit.utils.deprecate_pulser   ÚQPY_SUPPORTED_TYPESÚVERSION_PATTERNÚcompileÚVERBOSEÚ
IGNORECASErH   rE   ra   rŽ   r,   r   r   ú<module>r¡      sì  ðð (Ð 'à "Ð "Ð "Ð "Ð "Ð "à )Ð )Ð )Ð )Ð )Ð )Ð )Ð )Ø 8Ð 8Ð 8Ð 8Ð 8Ð 8Ð 8Ð 8Ð 8Ð 8Ð 8Ð 8Ð 8Ð 8Ø $Ð $Ð $Ð $Ð $Ð $Ø €€€Ø €€€Ø 	€	€	€	à )Ð )Ð )Ð )Ð )Ð )Ø &Ð &Ð &Ð &Ð &Ð &Ø )Ð )Ð )Ð )Ð )Ð )Ø <Ð <Ð <Ð <Ð <Ð <Ð <Ð <Ð <Ð <Ð <Ð <Ø NÐ NÐ NÐ NÐ NÐ NÐ NÐ NØ &Ð &Ð &Ð &Ð &Ð &Ø <Ð <Ð <Ð <Ð <Ð <ð ˜N¨MÐ9Ô:Ð ð
ð ðD #˜œ
 ?°B´JÀÄÑ4NÑOÔOÐ ð ÐØØCØ4Ð4ðñ ô ð 8<ØØÔ%ðY
ð Y
ð Y
ð Y
ñô ð
Y
ð| :>ØðYð Yð Yð Yð Yð Yð Yr   