§
    ¹³ÙgÀ3  ã                  ó:  — d Z ddlmZ ddgZddlZddlZddlZddlZddl	Z	ddl
Z
ddlmZmZmZmZmZmZmZmZ ddlmZ dd	lmZ  eg d
¢¦  «        Z eg d¢¦  «        Zd!d„Zd"d„Zd#d„Zd$d„Zd„ Z d„ Z! ed¦  «         ed¦  «         ed¦  «        gZ"d„ Z#d „ Z$dS )%zExport tools for OpenQASM 2.é    )ÚannotationsÚdumpÚdumpsN)ÚQuantumCircuitÚInstructionÚQuantumRegisterÚClassicalRegisterÚQubitÚClbitÚ	ParameterÚlibrary©Úpi_checké   )ÚQASM2ExportError)-ÚbarrierÚmeasureÚresetÚu3Úu2Úu1ÚcxÚidÚu0ÚuÚpÚxÚyÚzÚhÚsÚsdgÚtÚtdgÚrxÚryÚrzÚsxÚsxdgÚczÚcyÚswapÚchÚccxÚcswapÚcrxÚcryÚcrzÚcu1ÚcpÚcu3ÚcsxÚcuÚrxxÚrzzÚrccxÚrc3xÚc3xÚc3sxÚc4x)ÚOPENQASMÚqregÚcregÚincludeÚgateÚopaqueÚUÚCXr   r   Úifr   Úcircuitr   Úfilename_or_streamúos.PathLike | io.TextIOBasec               ó  — t          |t          j        ¦  «        r t          t	          | ¦  «        |¬¦  «         dS t          |d¦  «        5 }t          t	          | ¦  «        |¬¦  «         ddd¦  «         dS # 1 swxY w Y   dS )a€  Dump a circuit as an OpenQASM 2 program to a file or stream.

    Args:
        circuit: the :class:`.QuantumCircuit` to be exported.
        filename_or_stream: either a path-like object (likely a :class:`str` or
            :class:`pathlib.Path`), or an already opened text-mode stream.

    Raises:
        QASM2ExportError: if the circuit cannot be represented by OpenQASM 2.
    )ÚfileNÚw)Ú
isinstanceÚioÚ
TextIOBaseÚprintr   Úopen)rH   rI   Ústreams      úI/granary/edev/quantum/lib/python3.11/site-packages/qiskit/qasm2/export.pyr   r   k   sÃ   € õ Ð$¥b¤mÑ4Ô4ð Ý�e�G‰nŒnÐ#5Ð6Ñ6Ô6Ð6ØˆÝ	Ð  #Ñ	&Ô	&ð +¨&Ý�e�G‰nŒn 6Ð*Ñ*Ô*Ð*ð+ð +ð +ñ +ô +ð +ð +ð +ð +ð +ð +ð +øøøð +ð +ð +ð +ð +ð +s   ÁA7Á7A;Á>A;ÚreturnÚstrc          
     ó  ‡ ‡— ‰ j         dk    rt          d¦  «        ‚t          j        ¦   «         }ˆ fd„‰ j        D ¦   «         }ˆ fd„‰ j        D ¦   «         }g }|r$|                     t          d|¬¦  «        ¦  «         |r$|                     t          d|¬¦  «        ¦  «         i }‰ j	        ‰ j
        |fD ]-}|D ](}||t          t          |j        d¦  «        |¦  «        <   Œ)Œ.d	„ |                     ¦   «         D ¦   «         Šd
                     d„ |                     ¦   «         D ¦   «         ¦  «        }g }	‰ j        D ]Ñ}
|
j        }|j        dk    r0|
j        d         }|
j        d         }d‰|         › d‰|         › d�}nx|j        dk    rd‰|
j        d                  › d�}nU|j        dk    r9|
j        sŒzd                     ˆfd„|
j        D ¦   «         ¦  «        }|sdnd|› d�}nt%          |
|‰¦  «        }|	                     |¦  «         ŒÒd
                     d„ |	D ¦   «         ¦  «        }d
                     d„ |                     ¦   «         D ¦   «         ¦  «        }d
                     d„ dd|||fD ¦   «         ¦  «        S )a$  Export a circuit to an OpenQASM 2 program in a string.

    Args:
        circuit: the :class:`.QuantumCircuit` to be exported.

    Returns:
        An OpenQASM 2 string representing the circuit.

    Raises:
        QASM2ExportError: if the circuit cannot be represented by OpenQASM 2.
    r   z@Cannot represent circuits with unbound parameters in OpenQASM 2.c                óH   •— g | ]}‰                      |¦  «        j        °|‘ŒS © ©Úfind_bitÚ	registers©Ú.0ÚbitrH   s     €rT   ú
<listcomp>zdumps.<locals>.<listcomp>’   ó/   ø€ Ð[Ð[Ð[˜c¸7×;KÒ;KÈCÑ;PÔ;PÔ;ZÐ[�cÐ[Ð[Ð[ó    c                óH   •— g | ]}‰                      |¦  «        j        °|‘ŒS rY   rZ   r]   s     €rT   r`   zdumps.<locals>.<listcomp>“   ra   rb   Úqregless)ÚnameÚbitsÚcreglessÚreg_c                óP   — i | ]#\  }}t          |¦  «        D ]\  }}||› d |› d�“ŒŒ$S )ú[ú])Ú	enumerate)r^   re   ÚregisterÚidxr_   s        rT   ú
<dictcomp>zdumps.<locals>.<dictcomp>Ÿ   sb   € ð ,ð ,ð ,áˆD�(Ý# HÑ-Ô-ð,ð ,ñ ˆS�#ð 	�ÐÐ�sÐÐÐð,ð ,ð ,ð ,rb   ú
c              3  ój   K  — | ].\  }}t          |t          ¦  «        rd nd› d|› d|j        › d�V — Œ/dS )r@   rA   ú rj   z];N)rN   r   Úsize)r^   re   Úregs      rT   ú	<genexpr>zdumps.<locals>.<genexpr>¤   sh   è è € ð *ð *áˆD�#õ   ¥_Ñ5Ô5ÐAˆ6ˆ6¸6ÐWÐWÀDÐWÐWÈ3Ì8ÐWÐWÐWð*ð *ð *ð *ð *ð *rb   r   zmeasure z -> ú;r   zreset r   ú,c              3  ó(   •K  — | ]}‰|         V — Œd S ©NrY   )r^   ÚqÚ
bit_labelss     €rT   ru   zdumps.<locals>.<genexpr>¶   s'   øè è € ÐGÐG¨q˜Z¨œ]ÐGÐGÐGÐGÐGÐGrb   zbarrier;zbarrier c              3  ó   K  — | ]}|› V — Œd S ry   rY   )r^   Úcalls     rT   ru   zdumps.<locals>.<genexpr>»   s$   è è € Ð!JÐ!J° T )Ð!JÐ!JÐ!JÐ!JÐ!JÐ!Jrb   c              3  ó"   K  — | ]
\  }}|› V — Œd S ry   rY   )r^   Ú_Úqasms      rT   ru   zdumps.<locals>.<genexpr>¼   s(   è è € Ð%XÐ%X±G°A°t¨ iÐ%XÐ%XÐ%XÐ%XÐ%XÐ%Xrb   c              3  ó   K  — | ]}|¯|V — Œ	d S ry   rY   )r^   Úparts     rT   ru   zdumps.<locals>.<genexpr>¾   s>   è è € ð 
ð 
àð ð
Øð
ð 
ð 
ð 
ð 
ð 
rb   zOPENQASM 2.0;zinclude "qelib1.inc";)Únum_parametersr   ÚcollectionsÚOrderedDictÚqubitsÚclbitsÚappendr   r	   ÚqregsÚcregsÚ_make_uniqueÚ_escape_namere   ÚitemsÚjoinÚ_dataÚ	operationÚ_custom_operation_statementÚvalues)rH   Úgates_to_defineÚregless_qubitsÚregless_clbitsÚdummy_registersÚregister_escaped_namesÚregsrt   Úregister_definitions_qasmÚinstruction_callsÚinstructionr�   ÚqubitÚclbitÚinstruction_qasmÚqargsÚinstructions_qasmÚgate_definitions_qasmr{   s   `                 @rT   r   r   }   sR  øø€ ð Ô Ò!Ð!ÝÐaÑbÔbÐbõ
 	ÔÑ!Ô!ð ð \Ð[Ð[Ð[ W¤^Ð[Ñ[Ô[€NØ[Ð[Ð[Ð[ W¤^Ð[Ñ[Ô[€NØAC€OØð VØ×Ò�°JÀ^ÐTÑTÔTÑUÔUÐUØð XØ×ÒÕ0°jÀ~ÐVÑVÔVÑWÔWÐWØMOÐØ” ¤¨Ð?ð ð ˆØð 	ð 	ˆCð ð #Ý�\¨#¬(°FÑ;Ô;Ð=SÑTÔTñð ð	ð,ð ,à4×:Ò:Ñ<Ô<ð,ñ ,ô ,€Jð
 !%§	¢	ð *ð *à/×5Ò5Ñ7Ô7ð*ñ *ô *ñ !ô !Ðð ÐØ”}ð 3ð 3ˆØÔ)ˆ	ØŒ>˜YÒ&Ð&ØÔ& qÔ)ˆEØÔ& qÔ)ˆEØU¨*°UÔ*;ÐUÐUÀÈEÔARÐUÐUÐUÐÐØŒ^˜wÒ&Ð&ØL¨
°;Ô3EÀaÔ3HÔ(IÐLÐLÐLÐÐØŒ^˜yÒ(Ð(ØÔ%ð ð Ø—H’HÐGÐGÐGÐG°KÔ4FÐGÑGÔGÑGÔGˆEØ16ÐO˜z˜zÐ<OÀuÐ<OÐ<OÐ<OÐÐå:¸;ÈÐYcÑdÔdÐØ× Ò Ð!1Ñ2Ô2Ð2Ð2ØŸ	š	Ð!JÐ!JÐ8IÐ!JÑ!JÔ!JÑJÔJÐØ ŸIšIÐ%XÐ%X¸×?UÒ?UÑ?WÔ?WÐ%XÑ%XÔ%XÑXÔXÐà�9Š9ð 
ð 
ð Ø#Ø!Ø%Øð
ð
ñ 
ô 
ñ 
ô 
ð 
rb   re   Úprefixc                ó’   — t          j        dd| t           j        ¬¦  «        }|r|d         t          j        vs	|t
          v r||z   }|S )z‚Returns a valid OpenQASM 2.0 identifier, using `prefix` as a prefix if necessary.  `prefix`
    must itself be a valid identifier.z\Wr   )Úflagsr   )ÚreÚsubÚASCIIÚstringÚascii_lowercaseÚ	_RESERVED)re   r¢   Úescaped_names      rT   rŒ   rŒ   Ë   sS   € õ ”6˜%  dµ"´(Ð;Ñ;Ô;€Làð-à˜Œ?¥&Ô"8Ð8Ð8Ø�9Ð$Ð$à Ñ,ˆØÐrb   Úalready_definedúcollections.abc.Set[str]c                ó@  ‡ ‡— ‰ |vr‰ S ˆ fd„|D ¦   «         }‰ rt           j        t           j        z   nt           j        Št          j                             ˆfd„t          j        d¦  «        D ¦   «         ¦  «        D ]"}d                     |¦  «        }||vr‰ |z   c S Œ#‰ S )zRGenerate a name by suffixing the given stem that is unique within the defined set.c                óh   •— h | ].}|                      ‰¦  «        ¯|t          ‰¦  «        d …         ’Œ/S ry   )Ú
startswithÚlen)r^   Úin_usere   s     €rT   ú	<setcomp>z_make_unique.<locals>.<setcomp>Ý   s<   ø€ ÐYÐYÐY FÀ×ARÒARÐSWÑAXÔAXÐYˆF•3�t‘9”9�;�;ÔÐYÐYÐYrb   c              3  óD   •K  — | ]}t          j        ‰|¬ ¦  «        V — ŒdS ))ÚrepeatN)Ú	itertoolsÚproduct)r^   ÚnÚ
characterss     €rT   ru   z_make_unique.<locals>.<genexpr>ß   sD   øè è € ð /ð /Ø45�	Ô˜*¨QÐ/Ñ/Ô/ð/ð /ð /ð /ð /ð /rb   r   Ú )r¨   ÚdigitsÚascii_lettersr¶   ÚchainÚfrom_iterableÚcountrŽ   )re   r¬   ÚusedÚpartsÚsuffixr¹   s   `    @rT   r‹   r‹   Ù   sÒ   øø€ à�?Ð"Ð"ØˆØYÐYÐYÐY¨oÐYÑYÔY€DØ;?ÐY•&”-¥&Ô"6Ñ6Ð6ÅVÔEY€JÝ”×.Ò.ð /ð /ð /ð /Ý9B¼ÈÑ9KÔ9Kð/ñ /ô /ñ ô ð !ð !ˆð —’˜‘”ˆØ˜ÐÐØ˜&‘=Ð Ð Ð ð ð €Krb   c                ód   — | j         › dt          | ¦  «        › �}|                      |¬¦  «        }|S )z]Returns the operation with a new name following this pattern: {operation name}_{operation id}r   ©re   )re   r   Úcopy)r�   Únew_nameÚupdated_operations      rT   Ú_rename_operationrÈ   é   s7   € à”.Ð2Ð2¥2 i¡=¤=Ð2Ð2€HØ!Ÿš¨H˜Ñ5Ô5ÐØÐrb   c                óR  — | j         dk    rd}n| j         }| j        r,d                     d„ | j        D ¦   «         ¦  «        }|› d|› d�}| j        �Wt	          | j        d         t
          ¦  «        st          d	¦  «        ‚d
| j        d         j         › d| j        d         d›d�|z   }|S )z0Return an OpenQASM 2 string for the instruction.r=   Úc3sqrtxrw   c                ó2   — g | ]}t          |d d¬¦  «        ‘ŒS )r€   gê-�™—q=)ÚoutputÚepsr   ©r^   Úis     rT   r`   z*_instruction_call_site.<locals>.<listcomp>÷   s'   € Ð[Ð[Ð[ÀQ�8 A¨f¸%Ð@Ñ@Ô@Ð[Ð[Ð[rb   ú(ú)Nr   zNOpenQASM 2 can only condition on registers, but got '{operation.condition[0]}'zif(z==r   Údz) )re   ÚparamsrŽ   Ú
_conditionrN   r	   r   )r�   Ú
qasm2_callrÓ   s      rT   Ú_instruction_call_siterÖ   ð   sÜ   € à„~˜ÒÐØˆ
ˆ
à”^ˆ
ØÔð /Ø—’Ð[Ð[È)ÔJZÐ[Ñ[Ô[Ñ\Ô\ˆØ"Ð.Ð. VÐ.Ð.Ð.ˆ
ØÔÐ'Ý˜)Ô.¨qÔ1Õ3DÑEÔEð 	Ý"Ø`ñô ð ð P�)Ô& qÔ)Ô.ÐOÐO°)Ô2FÀqÔ2IÐOÐOÐOÐOÐR\Ñ\ð 	ð Ðrb   Úparam0Úparam1Úparam2c                óö   ‡— t          | j        |¦  «        }| j        r t          j        | j        | j        ¦  «        }n| j        }d                     ˆfd„|D ¦   «         ¦  «        }t          |¦  «        › d|› d�S )Nrw   c              3  ó(   •K  — | ]}‰|         V — Œd S ry   rY   )r^   Újr{   s     €rT   ru   z._custom_operation_statement.<locals>.<genexpr>  s'   øè è € Ð5Ð5¨1˜ AœÐ5Ð5Ð5Ð5Ð5Ð5rb   rr   rv   )Ú_define_custom_operationr�   r‡   r¶   r½   r†   rŽ   rÖ   )r›   r“   r{   r�   rf   Ú	bits_qasms     `   rT   r‘   r‘   
  s†   ø€ Ý(¨Ô)>ÀÑPÔP€IàÔð "ÝŒ˜{Ô1°;Ô3EÑFÔFˆˆàÔ!ˆØ—’Ð5Ð5Ð5Ð5°Ð5Ñ5Ô5Ñ5Ô5€IÝ$ YÑ/Ô/Ð>Ð>°)Ð>Ð>Ð>Ð>rb   c                ó\  ‡‡— | j         t          v r| S t          | j         d¦  «        }|| j         k    r|                      |¬¦  «        } t          j        t          j        t          j        t          j        t          j	        t          j
        t          j        t          j        t          j        t          j        t          j        t          j        t          j        t          j        t          j        h}| j        |v r2 t+          | ¦  «        t,          dt/          | j        ¦  «        …         Ž }nRt3          | d¦  «        r@|                      ¦   «         }|                     t          |j         d¦  «        ¬¦  «        x}} n| }‰                     | j         d¦  «        \  }}||k    r| S | j         ‰v rt9          | ¦  «        } | j         }|j        rEdd                     d„ t=          t/          |j        ¦  «        ¦  «        D ¦   «         ¦  «        z   d	z   }	nd
}	| j        dk    rtA          d| j         › d�¦  «        ‚| j!        dk    r tA          d| j         › d| j!        › d�¦  «        ‚d                     d„ t=          |j        ¦  «        D ¦   «         ¦  «        }
tE          |dd¦  «        }|€|d|› |	› d|
› d�f‰|<   nyd„ tG          |j$        ¦  «        D ¦   «         Šd                     ˆˆfd„|j%        D ¦   «         ¦  «        }| j         ‰v rt9          | ¦  «        } | j         }d|› |	› d|
› d|› d�}||f‰|<   | S )a:  Extract a custom definition from the given operation, and append any necessary additional
    subcomponents' definitions to the ``gates_to_define`` ordered dictionary.

    Returns a potentially new :class:`.Instruction`, which should be used for the
    :meth:`~.Instruction.qasm` call (it may have been renamed).Úgate_rÄ   NÚ_qasm_decomposition)NNrÐ   rw   c              3  ó    K  — | ]	}d |› �V — Œ
dS )ÚparamNrY   rÎ   s     rT   ru   z+_define_custom_operation.<locals>.<genexpr>P  s(   è è € Ð[Ð[¨1˜; 1˜;˜;Ð[Ð[Ð[Ð[Ð[Ð[rb   rÑ   rº   r   zOpenQASM 2 cannot represent 'z, which acts on zero qubits.z', which acts on z classical bits.c              3  ó    K  — | ]	}d |› �V — Œ
dS )rz   NrY   rÎ   s     rT   ru   z+_define_custom_operation.<locals>.<genexpr>_  s(   è è € ÐVÐV q˜7˜q˜7˜7ÐVÐVÐVÐVÐVÐVrb   Ú
definitionzopaque rr   rv   c                ó    — i | ]\  }}|d |› �“ŒS )rz   rY   )r^   rÏ   r_   s      rT   ro   z,_define_custom_operation.<locals>.<dictcomp>g  s$   € Ð^Ð^Ð^©¨¨C˜˜W ˜W˜WÐ^Ð^Ð^rb   c              3  ó:   •K  — | ]}t          |‰‰¦  «        V — Œd S ry   )r‘   )r^   r›   r“   Úqubit_labelss     €€rT   ru   z+_define_custom_operation.<locals>.<genexpr>h  sC   øè è € ð 
ð 
àõ (¨°_ÀlÑSÔSð
ð 
ð 
ð 
ð 
ð 
rb   zgate z { z })&re   Ú_EXISTING_GATE_NAMESrŒ   rÅ   ÚlibÚ	PhaseGateÚRGateÚRXGateÚRXXGateÚRYGateÚRYYGateÚRZGateÚRZXGateÚRZZGateÚXXMinusYYGateÚXXPlusYYGateÚUGateÚU1GateÚU2GateÚU3GateÚ
base_classÚtypeÚ_FIXED_PARAMETERSr±   rÓ   Úhasattrrá   ÚgetrÈ   rŽ   ÚrangeÚ
num_qubitsr   Ú
num_clbitsÚgetattrrl   r†   Údata)r�   r“   ÚescapedÚknown_good_parameterizedÚparameterized_operationÚnew_opÚprevious_definition_sourcer   rÆ   Úparameters_qasmÚqubits_qasmÚparameterized_definitionÚ	body_qasmÚdefinition_qasmrè   s    `            @rT   rÝ   rÝ     s‹  øø€ ð „~Õ-Ð-Ð-ØÐõ ˜9œ>¨7Ñ3Ô3€GØ�)”.Ò Ð Ø—N’N¨�NÑ0Ô0ˆ	õ 	ŒÝŒ	ÝŒ
ÝŒÝŒ
ÝŒÝŒ
ÝŒÝŒÝÔÝÔÝŒ	ÝŒ
ÝŒ
ÝŒ
ð Ðð( ÔÐ7Ð7Ð7Ø"1¥$ y¡/¤/Õ3DÐE\ÅsÈ9ÔK[ÑG\ÔG\ÐE\Ô3]Ð"^ÐÐÝ	�Ð1Ñ	2Ô	2ð ,Ø×.Ò.Ñ0Ô0ˆØ.4¯kªk½|ÈFÌKÐY`Ñ?aÔ?a¨kÑ.bÔ.bÐbÐ ) )à"+Ðð %4×$7Ò$7¸	¼ÈÑ$UÔ$UÑ!Ð ØÐ"<Ò<Ð<ØÐð „~˜Ð(Ð(Ý% iÑ0Ô0ˆ	àŒ~€HàÔ%ð à�#—(’(Ð[Ð[µµcÐ:QÔ:XÑ6YÔ6YÑ0ZÔ0ZÐ[Ñ[Ô[Ñ[Ô[Ñ[Ð^aÑað 	ˆð ˆàÔ˜qÒ Ð ÝØX¨I¬NÐXÐXÐXñ
ô 
ð 	
ð Ô˜qÒ Ð Ýð¨I¬Nð ð ÈYÔMað ð ð ñ
ô 
ð 	
ð
 —(’(ÐVÐV­EÐ2IÔ2TÑ,UÔ,UÐVÑVÔVÑVÔV€KÝ&Ð'>ÀÈdÑSÔSÐØÐ'à#Ø@�hÐ@ Ð@Ð@°+Ð@Ð@Ð@ð%
ˆ˜Ñ!Ð!ð
 _Ð^µ9Ð=UÔ=\Ñ3]Ô3]Ð^Ñ^Ô^ˆØ—H’Hð 
ð 
ð 
ð 
ð 
à7Ô<ð
ñ 
ô 
ñ 
ô 
ˆ	ð Œ>˜_Ð,Ð,Ý)¨)Ñ4Ô4ˆIØ ”~ˆHà] (Ð]¨OÐ]Ð]¸kÐ]Ð]ÈyÐ]Ð]Ð]ˆØ%<¸oÐ$Nˆ˜Ñ!ØÐrb   )rH   r   rI   rJ   )rH   r   rU   rV   )re   rV   r¢   rV   rU   rV   )re   rV   r¬   r­   rU   rV   )%Ú__doc__Ú
__future__r   Ú__all__Úcollections.abcr„   rO   r¶   Úosr¥   r¨   Úqiskit.circuitr   r   r   r	   r
   r   r   r   rê   Úqiskit.circuit.toolsr   Ú
exceptionsr   Ú	frozensetré   rª   r   r   rŒ   r‹   rÈ   rÖ   rü   r‘   rÝ   rY   rb   rT   ú<module>r     s   ðð #Ð "à "Ð "Ð "Ð "Ð "Ð "à�7Ð
€à Ð Ð Ð Ø 	€	€	€	Ø Ð Ð Ð Ø 	€	€	€	Ø 	€	€	€	Ø €€€ð	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð *Ð )Ð )Ð )Ð )Ð )Ø (Ð (Ð (Ð (Ð (Ð (à �yð.ð .ð .ñ0ô 0Ð ðd ˆIðð ð ñô €	ð$+ð +ð +ð +ð$Kð Kð Kð Kð\ð ð ð ðð ð ð ð ð ð ðð ð ð. �Y˜xÑ(Ô(¨)¨)°HÑ*=Ô*=¸y¸yÈÑ?RÔ?RÐSÐ ð?ð ?ð ?ð_ð _ð _ð _ð _rb   