§
    ¹³Ùg·ò  ã                  óº  — d Z ddlm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	Z	ddl
Z
ddlmZmZmZmZ ddlmZ ddlmZmZmZmZmZ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#m$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l0m1Z1 ddl2m3Z3 ddl4m5Z5 ddl6m7Z7 ddl8m9Z9  e:h d£¦  «        Z; e
j<        de
j=        ¬¦  «        Z> e
j<        de
j=        ¬¦  «        Z? e
j<        de
j=        ¬¦  «        Z@ G d„ d¦  «        ZA ed¦  «         ed¦  «         ed¦  «         ed¦  «        fZBd„  ejC        ¦   «         D ¦   «         ZDd„  ejC        ¦   «         D ¦   «         ZEdi deDejF                 “d eDejG                 “d!eDejH                 “d"eDejI                 “d#eDejJ                 “d$eDejK                 “d%eDejL                 “d&eDejM                 “d'eDejN                 “d(eDejO                 “d)eDejP                 “d*eDejQ                 “d+eDejR                 “d,eEejS                 d         “d-eEejT                 d         “d.eEejU                 d         “d/eEejV                 d         “eEejW                 d         eEejX                 d         eEejY                 d         eEejZ                 d         eDej[                 eEej\                 d0         eEej]                 d         eEej^                 d         eEejS                 d         eDejF                 eEejV                 d         eDej_                 eDej`                 eDeja                 eDejb                 d1œ¥iZcd2eDejd                 iZeejf         G d3„ d4¦  «        ¦   «         Zg G d5„ d6¦  «        ZhdNd:„Ziejf         G d;„ d<¦  «        ¦   «         Zj G d=„ d>¦  «        ZkdOdF„ZldG„ ZmdPdK„Zn G dL„ dMe#jo        e3jp                 ¦  «        ZqdS )QzQASM3 Exporteré    )ÚannotationsN)ÚIterableÚListÚSequenceÚUnion)ÚStandardGate)ÚlibraryÚBarrierÚCircuitInstructionÚClbitÚGateÚMeasureÚ	ParameterÚParameterExpressionÚQuantumCircuitÚQubitÚResetÚDelayÚStore)ÚBit)ÚexprÚtypes)ÚIfElseOpÚ	ForLoopOpÚWhileLoopOpÚSwitchCaseOpÚControlFlowOpÚBreakLoopOpÚContinueLoopOpÚCASE_DEFAULT)ÚRegister)Úpi_checké   )Úast)ÚExperimentalFeatures)ÚQASM3ExporterError)ÚBasicPrinter>/   ÚifÚinÚbitÚboxÚcalÚdefÚendÚforÚintÚinvÚletÚpowÚboolÚcregÚctrlÚelseÚgateÚqregÚuintÚangleÚarrayÚbreakÚconstÚdelayÚfloatÚinputÚqubitÚresetÚwhileÚdefcalÚexternÚgphaseÚoutputÚreturnÚsizeofÚbarrierÚcomplexÚincludeÚmeasureÚmutableÚnegctrlÚstretchÚOPENQASMÚcontinueÚdurationÚ
durationofÚdefcalgrammarz([\w][\w\d]*))Úflagsz\$[\d]+z[^\w\d]c            	      óH   — e Zd ZdZdddddd ed¦  «        fdd„Zd„ Zd„ ZdS )ÚExporterzQASM3 exporter main class.)ústdgates.inc)ÚUFNz  r   ÚincludesúSequence[str]Úbasis_gatesÚdisable_constantsr4   Úalias_classical_registersÚallow_aliasingÚindentÚstrÚexperimentalr%   c                ó€   — || _         || _        |�|n|pd| _        t          |¦  «        | _        || _        || _        dS )a‹  
        Args:
            includes: the filenames that should be emitted as includes.

                .. note::

                    At present, only the standard-library file ``stdgates.inc`` is properly
                    understood by the exporter, in the sense that it knows the gates it defines.
                    You can specify other includes, but you will need to pass the names of the gates
                    they define in the ``basis_gates`` argument to avoid the exporter outputting a
                    separate ``gate`` definition.

            basis_gates: the basic defined gate set of the backend.
            disable_constants: if ``True``, always emit floating-point constants for numeric
                parameter values.  If ``False`` (the default), then values close to multiples of
                OpenQASM 3 constants (``pi``, ``euler``, and ``tau``) will be emitted in terms of those
                constants instead, potentially improving accuracy in the output.
            alias_classical_registers: If ``True``, then bits may be contained in more than one
                register.  If so, the registers will be emitted using "alias" definitions, which
                might not be well supported by consumers of OpenQASM 3.

                .. seealso::
                    Parameter ``allow_aliasing``
                        A value for ``allow_aliasing`` overrides any value given here, and
                        supersedes this parameter.
            allow_aliasing: If ``True``, then bits may be contained in more than one register.  If
                so, the registers will be emitted using "alias" definitions, which might not be
                well supported by consumers of OpenQASM 3.  Defaults to ``False`` or the value of
                ``alias_classical_registers``.

                .. versionadded:: 0.25.0
            indent: the indentation string to use for each level within an indented block.  Can be
                set to the empty string to disable indentation.
            experimental: any experimental features to enable during the export.  See
                :class:`ExperimentalFeatures` for more details.
        NF)r^   r_   ra   Úlistr\   rb   rd   )Úselfr\   r^   r_   r`   ra   rb   rd   s           úK/granary/edev/quantum/lib/python3.11/site-packages/qiskit/qasm3/exporter.pyÚ__init__zExporter.__init__‚   sT   € ð\ 'ˆÔØ!2ˆÔà,Ð8ˆNˆNÐ?XÐ?aÐ\að 	Ôõ ˜X™œˆŒØˆŒØ(ˆÔÐÐó    c                ó°   — t          j        ¦   «         5 }|                      ||¦  «         |                     ¦   «         cddd¦  «         S # 1 swxY w Y   dS )zDConvert the circuit to OpenQASM 3, returning the result as a string.N)ÚioÚStringIOÚdumpÚgetvalue)rg   ÚcircuitÚstreams      rh   ÚdumpszExporter.dumps¹   s�   € åŒ[‰]Œ]ð 	%˜fØ�IŠI�g˜vÑ&Ô&Ð&Ø—?’?Ñ$Ô$ð	%ð 	%ð 	%ð 	%ñ 	%ô 	%ð 	%ð 	%ð 	%ð 	%ð 	%ð 	%øøøð 	%ð 	%ð 	%ð 	%ð 	%ð 	%s   ”*AÁAÁAc                óä   — t          || j        | j        | j        | j        | j        ¬¦  «        }t          || j        | j        ¬¦  «                             | 	                    ¦   «         ¦  «         dS )zOConvert the circuit to OpenQASM 3, dumping the result to a file or text stream.)Úincludeslistr^   r_   ra   rd   )rb   rd   N)
ÚQASM3Builderr\   r^   r_   ra   rd   r'   rb   ÚvisitÚbuild_program)rg   rp   rq   Úbuilders       rh   rn   zExporter.dump¿   s}   € åØØœØÔ(Ø"Ô4ØÔ.ØÔ*ð
ñ 
ô 
ˆõ 	�V D¤K¸dÔ>OÐPÑPÔP×VÒVØ×!Ò!Ñ#Ô#ñ	
ô 	
ð 	
ð 	
ð 	
rj   )r\   r]   r^   r]   r_   r4   r`   r4   ra   r4   rb   rc   rd   r%   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r%   ri   rr   rn   © rj   rh   rY   rY      st   € € € € € Ø$Ð$ð #4Ø%+Ø"'Ø*.Ø#ØØ-AÐ-AÀ!Ñ-DÔ-Dð5)ð 5)ð 5)ð 5)ð 5)ðn%ð %ð %ð
ð 
ð 
ð 
ð 
rj   rY   Úp0Úp1Úp2Úp3c                óV   — i | ]&}|j         °	| |j        t          d |j        …         Ž “Œ'S ©N)Úis_controlled_gateÚ
gate_classÚ_FIXED_PARAMETERSÚ
num_params©Ú.0Ústandards     rh   ú
<dictcomp>r‹   Ó   sM   € ð ð ð àØÔ&ðØÐ!ˆhÔ!Õ#4Ð5J°xÔ7JÐ5JÔ#KÐLðð ð rj   c                óf   ‡— i | ]-Š‰j         ¯	‰ˆfd „t          d‰j        z  ¦  «        D ¦   «         “Œ.S )c                óN   •— g | ]!} ‰j         t          d ‰j        …         d|iŽ‘Œ"S )NÚ
ctrl_state)r…   r†   r‡   )r‰   rŽ   rŠ   s     €rh   ú
<listcomp>z<dictcomp>.<listcomp>Ù   sK   ø€ ð ð ð àð 	ˆÔÕ.Ð/D°Ô1DÐ/DÔEÐ]ÐR\Ð]Ð]ðð ð rj   r#   )r„   ÚrangeÚnum_ctrl_qubitsrˆ   s    @rh   r‹   r‹   Ø   sj   ø€ ð (ð (ð (ð
 	ØÔ"ð(Øð ð ð ð å  XÔ%=Ñ =Ñ>Ô>ðñ ô ð(ð (ð (rj   rZ   ÚpÚxÚyÚzÚhÚsÚsdgÚtÚtdgÚsxÚrxÚryÚrzÚcxÚcyÚczÚcpé   )ÚcrxÚcryÚcrzÚchÚswapÚccxÚcswapÚcuÚCXÚphaseÚcphaseÚidÚu1Úu2Úu3r[   c                  ó*   — e Zd ZU dZded<   	 ded<   dS )ÚGateInfoz#Symbol-table information on a gate.úGate | NoneÚ	canonicalz ast.QuantumGateDefinition | NoneÚnodeN©ry   rz   r{   r|   Ú__annotations__r}   rj   rh   r´   r´     s=   € € € € € € à-Ð-àÐÐÑðð +Ð*Ð*Ñ*ðFð Frj   r´   c                  ó‚   — e Zd ZdZd„ Zd„ Zd„ Zd)d„Zd*d
„Zd*d„Z	ddœd+d„Z
dddœd,d„Zd-d„Zd.d„Zd/d„Zd0d%„Zd1d'„Zd(S )2ÚSymbolTablezCTrack Qiskit objects and the OQ3 identifiers used to refer to them.c                óŠ   — i | _         	 i | _        	 i | _        	 i g| _        	 i g| _        	 t          j        ¦   «         | _        d S rƒ   )ÚgatesÚstandard_gate_identsÚuser_gate_identsÚ	variablesÚobjectsÚ	itertoolsÚcountÚ_counter©rg   s    rh   ri   zSymbolTable.__init__   s^   € ØDFˆŒ
ð	Uð IKˆÔ!ØaØ;=ˆÔØMà35°$ˆŒð	eð =?¸4ˆŒð	eõ "œÑ)Ô)ˆŒˆˆrj   c                ón   — | j                              i ¦  «         | j                             i ¦  «         dS )zEnter a new variable scope.N)rÀ   ÚappendrÁ   rÅ   s    rh   Ú
push_scopezSymbolTable.push_scope;  s4   € àŒ×Ò˜bÑ!Ô!Ð!ØŒ×Ò˜BÑÔÐÐÐrj   c                ój   — | j                              ¦   «          | j                             ¦   «          dS )z6Exit the current scope, returning to a previous scope.N)rÁ   ÚpoprÀ   rÅ   s    rh   Ú	pop_scopezSymbolTable.pop_scope@  s0   € àŒ×ÒÑÔÐØŒ×ÒÑÔÐÐÐrj   rI   c                ój   — t          ¦   «         }| j        |_        | j        |_        | j        |_        |S )z¸Create a new context, such as for a gate definition.

        Contexts share the same set of globally defined gates, but have no access to other variables
        defined in any scope.)r»   r½   r¾   r¿   )rg   Úouts     rh   Únew_contextzSymbolTable.new_contextE  s1   € õ
 ‰mŒmˆØ”JˆŒ	Ø#'Ô#<ˆÔ Ø#Ô4ˆÔØˆ
rj   Únamerc   r4   c                óŒ   — |t           v p;|| j        v p2|t          j                             t          | j        ¦  «        ¦  «        v S )z6Whether this identifier has a defined meaning already.)Ú_RESERVED_KEYWORDSr½   rÂ   ÚchainÚfrom_iterableÚreversedrÀ   ©rg   rÏ   s     rh   Úsymbol_definedzSymbolTable.symbol_definedP  sJ   € ð Õ&Ð&ð OØ�t”zÐ!ðOà•y”×4Ò4µX¸d¼nÑ5MÔ5MÑNÔNÐNð	
rj   c                óD   — || j         d         vo|| j        vo|t          vS )zbWhether a new definition of this symbol can be made within the OpenQASM 3 shadowing
        rules.éÿÿÿÿ)rÀ   r½   rÑ   rÕ   s     rh   Úcan_shadow_symbolzSymbolTable.can_shadow_symbolX  s5   € ð ˜œ rÔ*Ð*ð /Ø˜DœJÐ&ð/àÕ.Ð.ð	
rj   F)ÚuniqueÚallow_renamerÚ   c               ó¬  ‡ — |rˆ fd„n‰ j         }t          }|rf|                     |¦  «        sdt                               d|¦  «        z   }|} ||¦  «        s$|› dt          ‰ j        ¦  «        › �} ||¦  «        ¯$|S |                     |¦  «        st          d|› d�¦  «        ‚|t          v rt          d|› d�¦  «        ‚ ||¦  «        s†‰ j	         
                    |¦  «        �t          d|› d	�¦  «        ‚t          ‰ j        ¦  «        D ]}| 
                    |¦  «        x}� nŒt          d
|› d�¦  «        ‚t          d|› d|› d�¦  «        ‚|S )zÜGet an identifier based on ``name`` that can be safely shadowed within this scope.

        If ``unique`` is ``True``, then the name is required to be unique across all live scopes,
        not just able to be redefined.c                ó0   •— ‰                      | ¦  «         S rƒ   )rÖ   )rÏ   rg   s    €rh   ú<lambda>z5SymbolTable.escaped_declarable_name.<locals>.<lambda>g  s   ø€ ˜d×1Ò1°$Ñ7Ô7Ð7€ rj   Ú_zcannot use 'z)' as a name; it is not a valid identifierzcannot use the keyword 'z' as a variable nameNzcannot shadow variable 'z%', as it is already defined as a gatez/internal error: could not locate unshadowable 'ú'z ', as it is already defined as ')rÙ   Ú_VALID_DECLARABLE_IDENTIFIERÚ	fullmatchÚ_BAD_IDENTIFIER_CHARACTERSÚsubÚnextrÄ   r&   rÑ   r½   ÚgetrÔ   rÀ   ÚRuntimeError)	rg   rÏ   rÛ   rÚ   Úname_allowedÚvalid_identifierÚbaseÚscopeÚothers	   `        rh   Úescaped_declarable_namez#SymbolTable.escaped_declarable_namea  sÚ  ø€ ð =CÐ^Ð7Ð7Ð7Ð7Ð7ÈÔH^ð 	õ 8ÐØð 	Ø#×-Ò-¨dÑ3Ô3ð GØÕ7×;Ò;¸CÀÑFÔFÑF�ØˆDØ"�l 4Ñ(Ô(ð 7ØÐ6Ð6¥ d¤mÑ!4Ô!4Ð6Ð6�ð #�l 4Ñ(Ô(ð 7àˆKØ×)Ò)¨$Ñ/Ô/ð 	eÝ$Ð%c°DÐ%cÐ%cÐ%cÑdÔdÐdØÕ%Ð%Ð%Ý$Ð%ZÀÐ%ZÐ%ZÐ%ZÑ[Ô[Ð[Øˆ|˜DÑ!Ô!ð 	ØŒz�~Š~˜dÑ#Ô#Ð/Ý(ØZ¨tÐZÐZÐZñô ð õ " $¤.Ñ1Ô1ð ^ð ^�Ø"ŸYšY t™_œ_Ð,�EÐ9Ø�Eð :õ #Ð#\ÐUYÐ#\Ð#\Ð#\Ñ]Ô]Ð]Ý$ØY¨4ÐYÐYÐQVÐYÐYÐYñô ð ð ˆrj   )Úforce_globalÚallow_hardware_qubitÚvariableÚobjectrî   rï   úast.Identifierc               ó6  — |rdnd}|rBt                                |¦  «        r(|                      |¦  «        rt          d|› �¦  «        ‚n|                      |||¬¦  «        }t          j        |¦  «        }|| j        |         |<   |�|| j        |         |<   |S )a%  Register a variable in the symbol table for the given scope, returning the name that
        should be used to refer to the variable.  The same name will be returned by subsequent calls
        to :meth:`get_variable` within the same scope.

        Args:
            name: the name to base the identifier on.
            variable: the Qiskit object this refers to.  This can be ``None`` in the case of
                reserving a dummy variable name that does not actually have a Qiskit object backing
                it.
            allow_rename: whether to allow the name to be mutated to escape it and/or make it safe
                to define (avoiding keywords, subject to shadowing rules, etc).
            force_global: force this declaration to be in the global scope.
            allow_hardware_qubit: whether to allow hardware qubits to pass through as identifiers.
                Hardware qubits are a dollar sign followed by a non-negative integer, and cannot be
                declared, so are not suitable identifiers for most objects.
        r   rØ   z0internal error: cannot redeclare hardware qubit ©rÛ   rÚ   )	Ú_VALID_HARDWARE_QUBITrâ   rÖ   r&   rí   r$   Ú
IdentifierrÀ   rÁ   )rg   rÏ   rð   rÛ   rî   rï   Úscope_indexÚ
identifiers           rh   Úregister_variablezSymbolTable.register_variable„  sÉ   € ð2 (Ð/�a�a¨Rˆð
  ð 	Õ$9×$CÒ$CÀDÑ$IÔ$Ið 	Ø×"Ò" 4Ñ(Ô(ð dÝ(Ð)bÐ\`Ð)bÐ)bÑcÔcÐcðdð ×/Ò/Ø <¸ð 0ñ ô ˆDõ ”^ DÑ)Ô)ˆ
Ø,4ˆŒ�{Ô# DÑ)ØÐØ2<ˆDŒL˜Ô% hÑ/ØÐrj   Úidentc                ó&   — || j         d         |<   dS )aL  Set the identifier used to refer to a given object for this scope.

        This overwrites any previously set identifier, such as during the original registration.

        This is generally only useful for tracking "sub" objects, like bits out of a register, which
        will have an `SubscriptedIdentifier` as their identifier.rØ   N)rÁ   )rg   rú   rð   s      rh   Úset_object_identzSymbolTable.set_object_ident¯  s   € ð &+ˆŒ�RÔ˜Ñ"Ð"Ð"rj   c                óŒ   — t          | j        ¦  «        D ]}|                     |¦  «        x}�|c S Œt          d|› d�¦  «        ‚)z/Lookup a non-gate variable in the symbol table.Nrà   z'' is not defined in the current context)rÔ   rÁ   ræ   ÚKeyError)rg   rð   rë   rÍ   s       rh   Úget_variablezSymbolTable.get_variable¸  sZ   € å˜dœlÑ+Ô+ð 	ð 	ˆEØ—y’y Ñ*Ô*Ð*�Ð7Ø�
�
�
ð 8åÐL˜8ÐLÐLÐLÑMÔMÐMrj   r8   rµ   c                ó>  — |                       |dd¬¦  «        }t          j        |¦  «        }|€t          dd¦  «        | j        |<   nUt          |¦  «        }t          |d¦  «        | j        |<   |j        �|| j        |j        <   n|| j        t          |¦  «        <   |S )a§  Register a gate that does not require an OQ3 definition.

        If the ``gate`` is given, it will be used to validate that a call to it is compatible (such
        as a known gate from an included file).  If it is not given, it is treated as a user-defined
        "basis gate" that assumes that all calling signatures are valid and that all gates of this
        name are exactly compatible, which is somewhat dangerous.Frô   N)
rí   r$   rö   r´   r½   Ú_gate_canonical_formÚ_standard_gater¾   r¿   r¯   )rg   rÏ   r8   rú   r¶   s        rh   Ú register_gate_without_definitionz,SymbolTable.register_gate_without_definition¿  s¢   € ð ×+Ò+¨D¸uÈUÐ+ÑSÔSˆÝ”˜tÑ$Ô$ˆØˆ<Ý'¨¨dÑ3Ô3ˆDŒJ�tÑÐå,¨TÑ2Ô2ˆIÝ'¨	°4Ñ8Ô8ˆDŒJ�tÑØÔ'Ð3ØFK�Ô)¨)Ô*BÑCÐCà7<�Ô%¥b¨¡m¤mÑ4Øˆrj   Úsourcer   ÚparamsúIterable[ast.Identifier]ÚqubitsÚbodyúast.QuantumBlockc           
     óH  — |                       |dd¬¦  «        }t          j        |¦  «        }t          |t          j        |t          |¦  «        t          |¦  «        |¦  «        ¦  «        | j        |<   |j        �|| j        |j        <   n|| j	        t          |¦  «        <   |S )ztRegister the given gate in the symbol table, using the given components to build up the
        full AST definition.TFrô   )rí   r$   rö   r´   ÚQuantumGateDefinitionÚtupler½   r  r¾   r¿   r¯   )rg   rÏ   r  r  r  r  rú   s          rh   Úregister_gatezSymbolTable.register_gateÔ  sœ   € ð ×+Ò+¨D¸tÈEÐ+ÑRÔRˆÝ”˜tÑ$Ô$ˆÝ#Ø•CÔ-¨eµU¸6±]´]ÅEÈ&ÁMÄMÐSWÑXÔXñ
ô 
ˆŒ
�4Ñð Ô Ð,Ø?DˆDÔ% fÔ&;Ñ<Ð<à05ˆDÔ!¥" V¡*¤*Ñ-Øˆrj   úast.Identifier | Nonec                óª  — t          |¦  «        }| j                             |j        ¦  «        x}�4|j        |u s|j        �|j        |k    rt          j        |j        ¦  «        S |j        �B| j                             |j        ¦  «        x}€dS | j        |j	                 j        |k    r|ndS | j
                             t          |¦  «        ¦  «        S )z7Lookup the identifier for a given `Gate`, if it exists.N)r  r½   ræ   rÏ   r¶   r$   rö   r  r¾   Ústringr¿   r¯   )rg   r8   r¶   Úour_defnÚ	our_idents        rh   Úget_gatezSymbolTable.get_gateì  sÏ   € å(¨Ñ.Ô.ˆ	Øœ
Ÿš t¤yÑ1Ô1Ð1ˆHÐ>ð Ô )Ð+Ð+àÔ!Ð)àÔ! YÒ.Ð.å”> $¤)Ñ,Ô,Ð,ØÔ#Ð/Ø!Ô6×:Ò:¸9Ô;SÑTÔTÐT�	Ð]Ø�tØ $¤
¨9Ô+;Ô <Ô FÈ)Ò SÐ S�9�9ÐY]Ð]àÔ$×(Ò(­¨I©¬Ñ7Ô7Ð7rj   N)rI   r»   )rÏ   rc   rI   r4   )rÏ   rc   rÛ   r4   rÚ   r4   )rÏ   rc   rð   rñ   rÛ   r4   rî   r4   rï   r4   rI   rò   )rú   rò   rð   rñ   )rð   rñ   rI   rò   )rÏ   rc   r8   rµ   rI   rò   )rÏ   rc   r  r   r  r  r  r  r  r	  rI   rò   )r8   r   rI   r  )ry   rz   r{   r|   ri   rÈ   rË   rÎ   rÖ   rÙ   rí   rù   rü   rÿ   r  r  r  r}   rj   rh   r»   r»     s8  € € € € € ØMÐMð*ð *ð *ð6 ð  ð  ð
ð ð ð
	ð 	ð 	ð 	ð
ð 
ð 
ð 
ð
ð 
ð 
ð 
ð X]ð !ð !ð !ð !ð !ð !ðR #Ø%*ð)ð )ð )ð )ð )ð )ðV+ð +ð +ð +ðNð Nð Nð Nðð ð ð ð*ð ð ð ð08ð 8ð 8ð 8ð 8ð 8rj   r»   r8   r   rI   c                óp   — | j         }|€| S |j        rt          |         | j                 nt          |         S )aƒ  Get the canonical form of a gate.

    This is the gate object that should be used to provide the OpenQASM 3 definition of a gate (but
    not the call site; that's the input object).  This lets us return a re-parametrised gate in
    terms of general parameters, in cases where we can be sure that that is valid.  This is
    currently only Qiskit standard gates.  This lets multiple call-site gates match the same symbol,
    in the case of parametric gates.

    The definition source provides the number of qubits, the parameter signature and the body of the
    `gate` statement.  It does not provide the name of the symbol being defined.)r  r„   Ú$_CANONICAL_CONTROLLED_STANDARD_GATESrŽ   Ú_CANONICAL_STANDARD_GATES)r8   rŠ   s     rh   r  r    sC   € ð Ô"€HØÐØˆð Ô&ð	1Õ,¨XÔ6°t´ÔGÐGå& xÔ0ðrj   c                  ó*   — e Zd ZU dZded<   	 ded<   dS )Ú
BuildScopez’The structure used in the builder to store the contexts and re-mappings of bits from the
    top-level scope where the bits were actually defined.r   rp   zdict[Bit, Bit]Úbit_mapNr¸   r}   rj   rh   r  r    s=   € € € € € € ð=ð =ð ÐÐÑØFØÐÐÑð8ð 8rj   r  c                  ó*  — e Zd ZdZeeeeee	e
fZdZdZdZdZ ed¦  «        fd„Zej        d:d„¦   «         Zej        d;d„¦   «         Zd<d„Zd„ Zd„ Zd=d„Zd„ Zd„ Zd„ Zd„ Zd„ Zd>d!„Zd?d#„Z d@d'„Z!dAd)„Z"dBd+„Z#dCd-„Z$d.„ Z%dDd2„Z&dEd4„Z'dFd6„Z(d7„ Z)dGd8„Z*d9S )Hru   z6QASM3 builder constructs an AST from a QuantumCircuit.Ú_bitÚ_qubitÚ_gate_pÚ_gate_qr   c                ó  — t          |d„ t          j        |j        |j        ¦  «        D ¦   «         ¦  «        | _        t          ¦   «         | _        g | _        g | _	        || _
        || _        || _        || _        || _        d S )Nc                ó   — i | ]}||“ŒS r}   r}   )r‰   r“   s     rh   r‹   z)QASM3Builder.__init__.<locals>.<dictcomp>9  s   € ÐYÐYÐY�aˆQ�ÐYÐYÐYrj   )r  rÂ   rÒ   r  Úclbitsrë   r»   ÚsymbolsÚ_global_io_declarationsÚ&_global_classical_forward_declarationsr_   ra   r\   r^   rd   )rg   Úquantumcircuitrt   r^   r_   ra   rd   s          rh   ri   zQASM3Builder.__init__.  s‡   € õ  ØØYÐY�9œ?¨>Ô+@À.ÔBWÑXÔXÐYÑYÔYñ
ô 
ˆŒ
õ #‘}”}ˆŒð (*ˆÔ$Ø68ˆÔ3Ø!2ˆÔØ,ˆÔØ$ˆŒØ&ˆÔØ(ˆÔÐÐrj   rp   r   r  úIterable[Qubit]r!  úIterable[Clbit]c           	   #  óÞ  ‡K  — | j         j        Št          ˆfd„|D ¦   «         ¦  «        }t          ˆfd„|D ¦   «         ¦  «        }|j        t	          |¦  «        k    r(t          d|j        › dt	          |¦  «        › d�¦  «        ‚|j        t	          |¦  «        k    r(t          d|j        › dt	          |¦  «        › d�¦  «        ‚t          t          j	        t          |j        |¦  «        t          |j        |¦  «        ¦  «        ¦  «        }| j                             ¦   «          | j         t          ||¦  «        c}| _         | j         V — || _         | j                             ¦   «          dS )	zwContext manager that pushes a new scope (like a ``for`` or ``while`` loop body) onto the
        current context stack.c              3  ó(   •K  — | ]}‰|         V — Œd S rƒ   r}   )r‰   rB   Úcurrent_maps     €rh   ú	<genexpr>z)QASM3Builder.new_scope.<locals>.<genexpr>O  ó(   øè è € Ð>Ð>¨e�{ 5Ô)Ð>Ð>Ð>Ð>Ð>Ð>rj   c              3  ó(   •K  — | ]}‰|         V — Œd S rƒ   r}   )r‰   Úclbitr*  s     €rh   r+  z)QASM3Builder.new_scope.<locals>.<genexpr>P  r,  rj   z*Tried to push a scope whose circuit needs z qubits, but only provided z qubits to create the mapping.z clbits, but only provided z clbits to create the mapping.N)rë   r  r  Ú
num_qubitsÚlenr&   Ú
num_clbitsÚdictrÂ   rÒ   Úzipr  r!  r"  rÈ   r  rË   )rg   rp   r  r!  ÚmappingÚ	old_scoper*  s         @rh   Ú	new_scopezQASM3Builder.new_scopeJ  sš  øè è € ð ”jÔ(ˆÝÐ>Ð>Ð>Ð>°vÐ>Ñ>Ô>Ñ>Ô>ˆÝÐ>Ð>Ð>Ð>°vÐ>Ñ>Ô>Ñ>Ô>ˆØÔ¥ V¡¤Ò,Ð,Ý$ðI¸WÔ=Oð Ið IÝ  ™[œ[ðIð Ið Iñô ð ð Ô¥ V¡¤Ò,Ð,Ý$ðI¸WÔ=Oð Ið IÝ  ™[œ[ðIð Ið Iñô ð õ •y”¥s¨7¬>¸6Ñ'BÔ'BÅCÈÌÐX^ÑD_ÔD_Ñ`Ô`ÑaÔaˆØŒ×ÒÑ!Ô!Ð!Ø $¤
­J°wÀÑ,HÔ,HÐˆ	�4”:ØŒjÐÐÐØˆŒ
ØŒ×ÒÑ Ô Ð Ð Ð rj   r  c              #  ó  K  — d„ t          j        |j        |j        ¦  «        D ¦   «         }| j        | j                             ¦   «         c}| _        | j        t          ||¦  «        c}| _        | j        V — || _        || _        dS )zHPush a new context (like for a ``gate`` or ``def`` body) onto the stack.c                ó   — i | ]}||“ŒS r}   r}   )r‰   r*   s     rh   r‹   z,QASM3Builder.new_context.<locals>.<dictcomp>e  s   € ÐQÐQÐQ �3˜ÐQÐQÐQrj   N)rÂ   rÒ   r  r!  r"  rÎ   rë   r  )rg   r  r4  Úold_symbolsr5  s        rh   rÎ   zQASM3Builder.new_contextb  s„   è è € ð RÐQ¥y¤°t´{ÀDÄKÑ'PÔ'PÐQÑQÔQˆà$(¤L°$´,×2JÒ2JÑ2LÔ2LÐ!ˆ�T”\Ø $¤
­J°t¸WÑ,EÔ,EÐˆ	�4”:ØŒjÐÐÐØˆŒ
Ø"ˆŒˆˆrj   rI   rò   c                óV   — | j                              | j        j        |         ¦  «        S )z‰Lookup a Qiskit bit within the current context, and return the name that should be
        used to represent it in OpenQASM 3 programmes.)r"  rÿ   rë   r  )rg   r*   s     rh   Ú_lookup_bitzQASM3Builder._lookup_bitm  s$   € ð Œ|×(Ò(¨¬Ô);¸CÔ)@ÑAÔAÐArj   c                óf  — | j         j        }|j        rt          d¦  «        ‚t                               ¦   «         D ] \  }}| j                             ||¦  «         Œ!| j        D ]N}|t          v rŒ	 | j                             |d¦  «         Œ)# t          $ r}t          d|› d�¦  «        |‚d}~ww xY wt          j
        t          j        d¦  «        t          |                      ¦   «         ¦  «        ¦  «        }|                      ¦   «          |                      ¦   «          |                      ¦   «          |                      ¦   «         }|                      ¦   «         }d„ | j        d„ | j        j                             ¦   «         D ¦   «         | j        ||fD ¦   «         }t          j        ||¦  «        S )zBuilds a ProgramzKcannot export an inner scope with captured variables as a top-level programNzCannot use 'z‚' as a basis gate for the reason in the prior exception. Consider renaming the gate if needed, or omitting this basis gate if not.z3.0c                ó   — g | ]	}|D ]}|‘ŒŒ
S r}   r}   )r‰   r  Ú	statements      rh   r�   z.QASM3Builder.build_program.<locals>.<listcomp>®  sA   € ð 
ð 
ð 
àð $ð
ð 
ð ð ð
ð 
ð 
ð 
rj   c              3  ó2   K  — | ]}|j         ®	|j         V — Œd S rƒ   )r·   )r‰   r8   s     rh   r+  z-QASM3Builder.build_program.<locals>.<genexpr>´  s+   è è € Ð\Ð\˜tÀdÄiÐF[�”ÐF[ÐF[ÐF[ÐF[Ð\Ð\rj   )rë   rp   Únum_captured_varsr&   Ú_BUILTIN_GATESÚitemsr"  r  r^   r$   ÚHeaderÚVersionrf   Úbuild_includesÚ#hoist_global_parameter_declarationsÚ%hoist_classical_register_declarationsÚ#hoist_classical_io_var_declarationsÚbuild_quantum_declarationsÚbuild_current_scoper#  r½   Úvaluesr$  ÚProgram)	rg   rp   Úbuiltinr8   ÚexcÚheaderÚquantum_declarationsÚmain_statementsÚ
statementss	            rh   rw   zQASM3Builder.build_programr  sø  € à”*Ô$ˆØÔ$ð 	Ý$Ø]ñô ð õ& ,×1Ò1Ñ3Ô3ð 	Ið 	I‰MˆG�TØŒL×9Ò9¸'À4ÑHÔHÐHÐHØÔ'ð 
	ð 
	ˆGØ�.Ð(Ð(àðØ”×=Ò=¸gÀtÑLÔLÐLÐLøÝ%ð ð ð Ý(ða 7ð að að añô ð ðøøøøðøøøõ ”�CœK¨Ñ.Ô.µ°T×5HÒ5HÑ5JÔ5JÑ0KÔ0KÑLÔLˆð 	×0Ò0Ñ2Ô2Ð2ð 	×2Ò2Ñ4Ô4Ð4ð 	×0Ò0Ñ2Ô2Ð2ð  $×>Ò>Ñ@Ô@Ðð ×2Ò2Ñ4Ô4ˆð
ð 
ð
 Ô,Ø\Ð\ t¤|Ô'9×'@Ò'@Ñ'BÔ'BÐ\Ñ\Ô\ØÔ;Ø$Øðð
ñ 
ô 
ˆ
õ Œ{˜6 :Ñ.Ô.Ð.s   Á1BÂ
B0ÂB+Â+B0c              #  óæ   K  — | j         D ]f}t                               |i ¦  «                             ¦   «         D ] \  }}| j                             ||¦  «         Œ!t          j        |¦  «        V — ŒgdS )z Builds a list of included files.N)r\   Ú_KNOWN_INCLUDESræ   rB  r"  r  r$   ÚInclude)rg   ÚfilenamerÏ   r8   s       rh   rE  zQASM3Builder.build_includes½  sˆ   è è € àœð 	(ð 	(ˆHõ .×1Ò1°(¸BÑ?Ô?×EÒEÑGÔGð Jð J‘
��dØ”×=Ò=¸dÀDÑIÔIÐIÐIÝ”+˜hÑ'Ô'Ð'Ð'Ð'Ð'ð	(ð 	(rj   r8   r   c                óN  ‡ — t          |j        t          j        ¦  «        �r|j        dk    �rt          j        d¦  «        t          j        d¦  «        }}t          j        t          j        ‰ j	         
                    t          j        t          j        dt          j        ¦  «        ¦  «        ||gt
          j        j        t          j        d¦  «        t
          j        j        gt          j        t
          j        j        ¦  «        g¬¦  «        g¦  «        }‰ j	                             |j        |d||f|¦  «        S |j        €t/          d|j        › d	�¦  «        ‚t1          |¦  «        }‰                      |j        ¦  «        5  ‰ j        j        }|j        dk    r]|j        t;          |j        ¦  «        k    r,t/          d
t;          |j        ¦  «        › d|j        › �¦  «        ‚ˆ fd„|j        D ¦   «         }n-ˆ fd„tA          t;          |j        ¦  «        ¦  «        D ¦   «         }ˆ fd„tC          |j"        ¦  «        D ¦   «         }t          j        ‰  #                    ¦   «         ¦  «        }ddd¦  «         n# 1 swxY w Y   ‰ j	                             |j        ||||¦  «        S )zžDefine a gate in the symbol table, including building the gate-definition statement for
        it.

        This recurses through gate-definition statements.r#   Úcr™   r   )Ú
parametersÚ	modifiersr}   Nzfailed to export gate 'z' that has no definitionz7parameter mismatch in definition of '{gate}': call has z, definition has c                óT   •— g | ]$}‰j                              |j        |d ¬¦  «        ‘Œ%S ©T©rÛ   )r"  rù   rÏ   ©r‰   Úparamrg   s     €rh   r�   z,QASM3Builder.define_gate.<locals>.<listcomp>ð  sC   ø€ ð ð ð àð ”L×2Ò2°5´:¸uÐSWÐ2ÑXÔXðð ð rj   c                ó^   •— g | ])}‰j                              ‰j        › d |› �dd¬¦  «        ‘Œ*S )rß   NTr]  )r"  rù   Úgate_parameter_prefix)r‰   Úirg   s     €rh   r�   z,QASM3Builder.define_gate.<locals>.<listcomp>÷  s[   ø€ ð ð ð ð ð ”L×2Ò2ØÔ5Ð;Ð;¸Ð;Ð;¸TÐPTð 3ñ ô ðð ð rj   c                ód   •— g | ],\  }}‰j                              ‰j        › d |› �|d¬¦  «        ‘Œ-S )rß   Tr]  )r"  rù   Úgate_qubit_prefix©r‰   rb  rB   rg   s      €rh   r�   z,QASM3Builder.define_gate.<locals>.<listcomp>ý  s^   ø€ ð ð ð ñ �A�uð ”×.Ò.ØÔ-Ð3Ð3°Ð3Ð3°UÈð /ñ ô ðð ð rj   )$Ú
issubclassÚ
base_classr	   ÚCXGaterŽ   r$   rö   ÚQuantumBlockÚQuantumGateCallr"  r  ÚUGateÚmathÚpiÚConstantÚPIÚIntegerLiteralÚQuantumGateModifierÚQuantumGateModifierNameÚCTRLr  rÏ   Ú
definitionr&   r  rÎ   rë   rp   Únum_parametersr0  r  rY  r�   Ú	enumerater  rJ  )	rg   r8   ÚcontrolÚtargetr  r¶   Údefnr  r  s	   `        rh   Údefine_gatezQASM3Builder.define_gateÆ  sþ  ø€ õ
 �d”o¥w¤~Ñ6Ô6ñ 	\¸4¼?ÈaÒ;OÑ;Oõ
 "œn¨SÑ1Ô1µ3´>À#Ñ3FÔ3F�VˆGÝÔ#åÔ'Øœ×-Ò-­g¬m½D¼GÀQÍÌÑ.PÔ.PÑQÔQØ  &Ð)Ý$'¤L¤OµSÔ5GÈÑ5JÔ5JÍCÌLÌOÐ#\Ý#&Ô#:½3Ô;VÔ;[Ñ#\Ô#\Ð"]ð	ñ ô ðñ	ô 	ˆDð ”<×-Ò-¨d¬i¸¸rÀGÈVÐCTÐVZÑ[Ô[Ð[ØŒ?Ð"Ý$Ð%b¸t¼yÐ%bÐ%bÐ%bÑcÔcÐcÝ(¨Ñ.Ô.ˆ	Ø×Ò˜iÔ2Ñ3Ô3ð $	@ð $	@Ø”:Ô%ˆDð Ô" QÒ&Ð&ØÔ&­#¨d¬kÑ*:Ô*:Ò:Ð:Ý,ð^Ý%(¨¬Ñ%5Ô%5ð^ð ^ØHLÔH[ð^ð ^ñô ð ðð ð ð à!%¤ðñ ô ��ðð ð ð õ #¥3 t¤{Ñ#3Ô#3Ñ4Ô4ð	ñ ô �ðð ð ð õ !*¨$¬+Ñ 6Ô 6ð	ñ ô ˆFõ Ô# D×$<Ò$<Ñ$>Ô$>Ñ?Ô?ˆDðI$	@ð $	@ð $	@ñ $	@ô $	@ð $	@ð $	@ð $	@ð $	@ð $	@ð $	@øøøð $	@ð $	@ð $	@ð $	@ðP Œ|×)Ò)¨)¬.¸)ÀVÈVÐUYÑZÔZÐZs   ÆC(I9É9I=Ê I=c                ó^   — t          | j        j        ¦  «        dk    rt          d¦  «        ‚dS )zIRaise an error if we are not in the global scope, as a defensive measure.r#   z!not currently in the global scopeN)r0  r"  rÀ   rç   rÅ   s    rh   Úassert_global_scopez QASM3Builder.assert_global_scope	  s1   € åˆtŒ|Ô%Ñ&Ô&¨Ò*Ð*ÝÐBÑCÔCÐCð +Ð*rj   c                ód  — |                       ¦   «          | j        j        }|j        D ]‡}| j                             |j        |d¬¦  «        }t          |||¦  «        }|€Œ8t          |t          j
        ¦  «        r| j                             |¦  «         Œm| j                             |¦  «         ŒˆdS )zÙExtend ``self._global_io_declarations`` and ``self._global_classical_declarations`` with
        any implicit declarations used to support the early IBM efforts to use :class:`.Parameter`
        as an input variable.Tr]  N)r|  rë   rp   rY  r"  rù   rÏ   Ú_infer_variable_declarationÚ
isinstancer$   ÚIODeclarationr#  rÇ   r$  )rg   rp   Ú	parameterÚparameter_nameÚdeclarations        rh   rF  z0QASM3Builder.hoist_global_parameter_declarations  sË   € ð 	× Ò Ñ"Ô"Ð"Ø”*Ô$ˆØ Ô+ð 
	Pð 
	PˆIØ!œ\×;Ò;Ø” 	¸ð <ñ ô ˆNõ 6°g¸yÈ.ÑYÔYˆKØÐ"ØÝ˜+¥sÔ'8Ñ9Ô9ð PØÔ,×3Ò3°KÑ@Ô@Ð@Ð@àÔ;×BÒBÀ;ÑOÔOÐOÐOð
	Pð 
	Prj   c           
     ó¨  ‡ ‡— ‰                       ¦   «          ‰ j        j        Št          ˆfd„‰j        D ¦   «         ¦  «        r„‰ j        st          d¦  «        ‚ˆ fd„t          ‰j        ¦  «        D ¦   «         }‰ j         	                    |¦  «         ‰ j         	                    ‰  
                    ‰j        ¦  «        ¦  «         dS ‰ j         	                    ˆˆ fd„t          ‰j        ¦  «        D ¦   «         ¦  «         ‰j        D ]Ê}‰ j                             |j        |d¬¦  «        }t          |¦  «        D ]J\  }}‰ j                             t!          j        |j        t!          j        |¦  «        ¦  «        |¦  «         ŒK‰ j                             t!          j        t!          j        t/          |¦  «        ¦  «        |¦  «        ¦  «         ŒËdS )a8  Extend the global classical declarations with AST nodes declaring all the global-scope
        circuit :class:`.Clbit` and :class:`.ClassicalRegister` instances.  Qiskit's data model
        doesn't involve the declaration of *new* bits or registers in inner scopes; only the
        :class:`.expr.Var` mechanism allows that.

        The behavior of this function depends on the setting ``allow_aliasing``. If this
        is ``True``, then the output will be in the same form as the output of
        :meth:`.build_classical_declarations`, with the registers being aliases.  If ``False``, it
        will instead return a :obj:`.ast.ClassicalDeclaration` for each classical register, and one
        for the loose :obj:`.Clbit` instances, and will raise :obj:`QASM3ExporterError` if any
        registers overlap.
        c              3  ón   •K  — | ]/}t          ‰                     |¦  «        j        ¦  «        d k    V — Œ0dS ©r#   N©r0  Úfind_bitÚ	registers©r‰   Úqrp   s     €rh   r+  zEQASM3Builder.hoist_classical_register_declarations.<locals>.<genexpr>/  óA   øè è € ÐNÐN¸!�s�7×#Ò# AÑ&Ô&Ô0Ñ1Ô1°AÒ5ÐNÐNÐNÐNÐNÐNrj   zlSome classical registers in this circuit overlap and need aliases to express, but 'allow_aliasing' is false.c           	   3  ó²   •K  — | ]Q\  }}t          j        t          j        ¦   «         ‰j                             ‰j        › |› �|d ¬¦  «        ¦  «        V — ŒRdS ©Tr]  N)r$   ÚClassicalDeclarationÚBitTyper"  rù   Úloose_bit_prefix)r‰   rb  r.  rg   s      €rh   r+  zEQASM3Builder.hoist_classical_register_declarations.<locals>.<genexpr>6  s‚   øè è € ð ð ñ �A�uõ Ô(Ý”K‘M”MØ”L×2Ò2ØÔ0Ð5°!Ð5Ð5°uÈ4ð 3ñ ô ñô ðð ð ð ð ð rj   Nc           	   3  óæ   •K  — | ]k\  }}‰                      |¦  «        j        °t          j        t          j        ¦   «         ‰j                             ‰j        › |› �|d ¬¦  «        ¦  «        V — ŒldS rŽ  )rˆ  r‰  r$   r�  r�  r"  rù   r‘  )r‰   rb  r.  rp   rg   s      €€rh   r+  zEQASM3Builder.hoist_classical_register_declarations.<locals>.<genexpr>D  s™   øè è € ð 	;
ð 	;
ñ ��5Ø×#Ò# EÑ*Ô*Ô4ð	;
ÝÔ$Ý”‘”Ø”×.Ò.ØÔ,Ð1¨aÐ1Ð1°5Àtð /ñ ô ñô ð	;
ð 	;
ð 	;
ð 	;
ð 	;
ð 	;
rj   Tr]  )r|  rë   rp   Úanyr!  ra   r&   rv  r$  ÚextendÚbuild_aliasesÚcregsr"  rù   rÏ   rü   r$   ÚSubscriptedIdentifierr  rp  rÇ   r�  ÚBitArrayTyper0  )rg   r!  ÚregisterrÏ   rb  r*   rp   s   `     @rh   rG  z2QASM3Builder.hoist_classical_register_declarations   s  øø€ ð 	× Ò Ñ"Ô"Ð"Ø”*Ô$ˆÝÐNÐNÐNÐN¸w¼~ÐNÑNÔNÑNÔNð 	àÔ&ð Ý(ð6ñô ð ðð ð ð õ !*¨'¬.Ñ 9Ô 9ðñ ô ˆFð Ô7×>Ò>¸vÑFÔFÐFØÔ7×>Ò>¸t×?QÒ?QÐRYÔR_Ñ?`Ô?`ÑaÔaÐaØˆFð 	Ô3×:Ò:ð 	;
ð 	;
ð 	;
ð 	;
ð 	;
õ & g¤nÑ5Ô5ð	;
ñ 	;
ô 	;
ñ 		
ô 		
ð 		
ð  œð 	ð 	ˆHØ”<×1Ò1°(´-ÀÐX\Ð1Ñ]Ô]ˆDÝ# HÑ-Ô-ð ð ‘��3Ø”×-Ò-ÝÔ-¨d¬k½3Ô;MÈaÑ;PÔ;PÑQÔQÐSVñô ð ð ð Ô7×>Ò>ÝÔ(­Ô)9½#¸h¹-¼-Ñ)HÔ)HÈ$ÑOÔOñô ð ð ð	ð 	rj   c                óR  — |                       ¦   «          | j        j        }|                     ¦   «         D ]q}| j                             t          j        t          j        j	        t          |j        ¦  «        | j                             |j        |d¬¦  «        ¦  «        ¦  «         ŒrdS )aE  Hoist the declarations of classical IO :class:`.expr.Var` nodes into the global state.

        Local :class:`.expr.Var` declarations are handled by the regular local-block scope builder,
        and the :class:`.QuantumCircuit` data model ensures that the only time an IO variable can
        occur is in an outermost block.Tr]  N)r|  rë   rp   Úiter_input_varsr#  rÇ   r$   r€  Ú
IOModifierÚINPUTÚ_build_ast_typeÚtyper"  rù   rÏ   )rg   rp   Úvars      rh   rH  z0QASM3Builder.hoist_classical_io_var_declarationsX  s¦   € ð 	× Ò Ñ"Ô"Ð"Ø”*Ô$ˆØ×*Ò*Ñ,Ô,ð 	ð 	ˆCØÔ(×/Ò/ÝÔ!Ý”NÔ(Ý# C¤HÑ-Ô-Ø”L×2Ò2°3´8¸SÈtÐ2ÑTÔTñô ñô ð ð ð	ð 	rj   c                óÂ  ‡ ‡— ‰                       ¦   «          ‰ j        j        Š‰j        �=t	          ‰j        ¦  «        D ]&\  }}‰ j                             d|› �|dd¬¦  «         Œ'g S t          ˆfd„‰j        D ¦   «         ¦  «        rS‰ j	        st          d¦  «        ‚ˆ fd„t	          ‰j        ¦  «        D ¦   «         }|‰                      ‰j        ¦  «        z   S ˆˆ fd	„t	          ‰j        ¦  «        D ¦   «         }g }‰j        D ]×}‰ j                             |j        |d¬
¦  «        }t	          |¦  «        D ]J\  }}‰ j                             t          j        |j        t          j        |¦  «        ¦  «        |¦  «         ŒK|                     t          j        |t          j        t          j        t-          |¦  «        ¦  «        ¦  «        ¦  «        ¦  «         ŒØ||z   S )z‚Return a list of AST nodes declaring all the qubits in the current scope, and all the
        alias declarations for these qubits.Nú$FT)rÛ   rï   c              3  ón   •K  — | ]/}t          ‰                     |¦  «        j        ¦  «        d k    V — Œ0dS r†  r‡  rŠ  s     €rh   r+  z:QASM3Builder.build_quantum_declarations.<locals>.<genexpr>v  rŒ  rj   zjSome quantum registers in this circuit overlap and need aliases to express, but 'allow_aliasing' is false.c           	     ó†   •— g | ]=\  }}t          j        ‰j                             ‰j        › |› �|d ¬¦  «        ¦  «        ‘Œ>S r\  )r$   ÚQuantumDeclarationr"  rù   Úloose_qubit_prefixre  s      €rh   r�   z;QASM3Builder.build_quantum_declarations.<locals>.<listcomp>}  sk   ø€ ð ð ð ñ �A�uõ Ô&Ø”L×2Ò2ØÔ2Ð7°AÐ7Ð7¸ÈTð 3ñ ô ñô ðð ð rj   c           	     óº   •— g | ]W\  }}‰                      |¦  «        j        °t          j        ‰j                             ‰j        › |› �|d ¬¦  «        ¦  «        ‘ŒXS r\  )rˆ  r‰  r$   r¥  r"  rù   r¦  )r‰   rb  rB   rp   rg   s      €€rh   r�   z;QASM3Builder.build_quantum_declarations.<locals>.<listcomp>ˆ  s‚   ø€ ð 
ð 
ð 
ñ ��5Ø×#Ò# EÑ*Ô*Ô4ð
ÝÔ"Ø”×.Ò.ØÔ.Ð3°Ð3Ð3°UÈð /ñ ô ñô ð
ð 
ð 
rj   r]  )r|  rë   rp   Úlayoutrv  r  r"  rù   r“  ra   r&   r•  ÚqregsrÏ   rü   r$   r—  r  rp  rÇ   r¥  Ú
Designatorr0  )	rg   rb  r*   r  Úloose_qubitsr‰  r™  rÏ   rp   s	   `       @rh   rI  z'QASM3Builder.build_quantum_declarationsi  s?  øø€ ð 	× Ò Ñ"Ô"Ð"Ø”*Ô$ˆØŒ>Ð%õ $ G¤NÑ3Ô3ð ð ‘��3Ø”×.Ò.Ø˜�G�G˜S¨uÈ4ð /ñ ô ð ð ð ˆIÝÐNÐNÐNÐN¸w¼~ÐNÑNÔNÑNÔNð 	>àÔ&ð Ý(ð6ñô ð ðð ð ð õ !*¨'¬.Ñ 9Ô 9ðñ ô ˆFð ˜D×.Ò.¨w¬}Ñ=Ô=Ñ=Ð=ð
ð 
ð 
ð 
ð 
õ & g¤nÑ5Ô5ð
ñ 
ô 
ˆð ˆ	Øœð 	ð 	ˆHØ”<×1Ò1°(´-ÀÐX\Ð1Ñ]Ô]ˆDÝ# HÑ-Ô-ð ð ‘��3Ø”×-Ò-ÝÔ-¨d¬k½3Ô;MÈaÑ;PÔ;PÑQÔQÐSVñô ð ð ð ×ÒÝÔ& t­S¬^½CÔ<NÍsÐS[É}Ì}Ñ<]Ô<]Ñ-^Ô-^Ñ_Ô_ñô ð ð ð ˜iÑ'Ð'rj   r‰  úIterable[Register]úList[ast.AliasStatement]c           
     óž  ‡ — g }|D ]Æ}‰ j                              |j        |d¬¦  «        }ˆ fd„|D ¦   «         }t          |¦  «        D ]J\  }}‰ j                              t          j        |j        t          j        |¦  «        ¦  «        |¦  «         ŒK| 	                    t          j
        |t          j        |¦  «        ¦  «        ¦  «         ŒÇ|S )zwReturn a list of alias declarations for the given registers.  The registers can be either
        classical or quantum.Tr]  c                ó:   •— g | ]}‰                      |¦  «        ‘ŒS r}   ©r;  )r‰   r*   rg   s     €rh   r�   z.QASM3Builder.build_aliases.<locals>.<listcomp>£  s'   ø€ ÐBÐBÐB°#˜×(Ò(¨Ñ-Ô-ÐBÐBÐBrj   )r"  rù   rÏ   rv  rü   r$   r—  r  rp  rÇ   ÚAliasStatementÚIndexSet)rg   r‰  rÍ   r™  rÏ   Úelementsrb  r*   s   `       rh   r•  zQASM3Builder.build_aliases�  sà   ø€ ð ˆØ!ð 	Ið 	IˆHØ”<×1Ò1°(´-ÀÐX\Ð1Ñ]Ô]ˆDØBÐBÐBÐB¸ÐBÑBÔBˆHÝ# HÑ-Ô-ð ð ‘��3à”×-Ò-ÝÔ-¨d¬k½3Ô;MÈaÑ;PÔ;PÑQÔQÐSVñô ð ð ð �JŠJ•sÔ)¨$µ´¸XÑ0FÔ0FÑGÔGÑHÔHÐHÐHØˆ
rj   úList[ast.Statement]c           
     óv  ‡ — ˆ fd„‰ j         j                             ¦   «         D ¦   «         }‰ j         j        j        D �]ú}t	          |j        t          ¦  «        �r%t	          |j        t          ¦  «        r)|                     ‰  	                    |¦  «        ¦  «         nàt	          |j        t          ¦  «        r)|                     ‰                      |¦  «        ¦  «         n�t	          |j        t          ¦  «        r)|                     ‰                      |¦  «        ¦  «         nZt	          |j        t          ¦  «        r)|                     ‰                      |¦  «        ¦  «         nt#          d|j        › �¦  «        ‚�ŒCt	          |j        t$          ¦  «        r‰                      |¦  «        g}�nüt	          |j        t(          ¦  «        r*ˆ fd„|j        D ¦   «         }t-          j        |¦  «        g}�n¸t	          |j        t0          ¦  «        r]t-          j        ˆ fd„|j        D ¦   «         ¦  «        }‰                      |j        d         ¦  «        }t-          j        ||¦  «        g}�nAt	          |j        t:          ¦  «        rˆ fd„|j        D ¦   «         }�nt	          |j        t<          ¦  «        r‰                      |¦  «        g}nát	          |j        t@          ¦  «        rQt-          j!        ‰  "                    |j        j#        ¦  «        ‰  "                    |j        j$        ¦  «        ¦  «        g}nvt	          |j        tJ          ¦  «        rt-          j&        ¦   «         g}nGt	          |j        tN          ¦  «        rt-          j(        ¦   «         g}ntS          d|j        › d�¦  «        ‚|j        j*        €|                     |¦  «         �Œ”t-          j+        |¦  «        }|                     t-          j,        ‰  "                    t[          |j        j*        ¦  «        ¦  «        |¦  «        ¦  «         �Œü|S )	zÞBuild the instructions that occur in the current scope.

        In addition to everything literally in the circuit's ``data`` field, this also includes
        declarations for any local :class:`.expr.Var` nodes.
        c           
     óž   •— g | ]I}t          j        t          |j        ¦  «        ‰j                             |j        |d ¬¦  «        ¦  «        ‘ŒJS r\  )r$   r�  rž  rŸ  r"  rù   rÏ   )r‰   r   rg   s     €rh   r�   z4QASM3Builder.build_current_scope.<locals>.<listcomp>º  sa   ø€ ð 
ð 
ð 
ð
 õ	 Ô$Ý ¤Ñ)Ô)Ø”×.Ò.¨s¬x¸È4Ð.ÑPÔPñô ð
ð 
ð 
rj   z"unhandled control-flow construct: c                ó:   •— g | ]}‰                      |¦  «        ‘ŒS r}   r°  ©r‰   Úoperandrg   s     €rh   r�   z4QASM3Builder.build_current_scope.<locals>.<listcomp>Ò  s'   ø€ ÐXÐXÐX¸'˜D×,Ò,¨WÑ5Ô5ÐXÐXÐXrj   c                ó:   •— g | ]}‰                      |¦  «        ‘ŒS r}   r°  r¸  s     €rh   r�   z4QASM3Builder.build_current_scope.<locals>.<listcomp>Ö  s'   ø€ ÐQÐQÐQ°7�T×%Ò% gÑ.Ô.ÐQÐQÐQrj   r   c                ó^   •— g | ])}t          j        ‰                     |¦  «        ¦  «        ‘Œ*S r}   )r$   ÚQuantumResetr;  r¸  s     €rh   r�   z4QASM3Builder.build_current_scope.<locals>.<listcomp>Û  s?   ø€ ð ð ð ØDK•CÔ$ T×%5Ò%5°gÑ%>Ô%>Ñ?Ô?ðð ð rj   zBnon-unitary subroutine calls are not yet supported, but received 'rà   ).rë   rp   Úiter_declared_varsÚdatar  Ú	operationr   r   rÇ   Úbuild_for_loopr   Úbuild_while_loopr   Úbuild_if_statementr   r”  Úbuild_switch_statementrç   r   Úbuild_gate_callr
   r  r$   ÚQuantumBarrierr   ÚQuantumMeasurementr;  r!  ÚQuantumMeasurementAssignmentr   r   Úbuild_delayr   ÚAssignmentStatementÚbuild_expressionÚlvalueÚrvaluer   ÚBreakStatementr   ÚContinueStatementr&   Ú
_conditionÚProgramBlockÚBranchingStatementÚ_lift_condition)rg   rR  ÚinstructionÚnodesÚoperandsÚmeasurementrB   r  s   `       rh   rJ  z QASM3Builder.build_current_scope¬  s4  ø€ ð
ð 
ð 
ð 
ð
 ”zÔ)×<Ò<Ñ>Ô>ð
ñ 
ô 
ˆ
ð  œ:Ô-Ô2ð 9	ñ 9	ˆKÝ˜+Ô/µÑ?Ô?ñ Ý˜kÔ3µYÑ?Ô?ð 	eØ×%Ò% d×&9Ò&9¸+Ñ&FÔ&FÑGÔGÐGÐGÝ Ô 5µ{ÑCÔCð eØ×%Ò% d×&;Ò&;¸KÑ&HÔ&HÑIÔIÐIÐIÝ Ô 5µxÑ@Ô@ð eØ×%Ò% d×&=Ò&=¸kÑ&JÔ&JÑKÔKÐKÐKÝ Ô 5µ|ÑDÔDð eØ×%Ò% d×&AÒ&AÀ+Ñ&NÔ&NÑOÔOÐOÐOå&Ð'cÈKÔLaÐ'cÐ'cÑdÔdÐdÙå˜+Ô/µÑ6Ô6ð  Ø×-Ò-¨kÑ:Ô:Ð;�‘Ý˜KÔ1µ7Ñ;Ô;ð ØXÐXÐXÐXÀ[ÔEWÐXÑXÔX�ÝÔ+¨HÑ5Ô5Ð6�‘Ý˜KÔ1µ7Ñ;Ô;ð Ý!Ô4ØQÐQÐQÐQ¸kÔ>PÐQÑQÔQñô �ð ×(Ò(¨Ô);¸AÔ)>Ñ?Ô?�ÝÔ9¸%ÀÑMÔMÐN�‘Ý˜KÔ1µ5Ñ9Ô9ð ðð ð ð ØOZÔOaðñ ô �‘õ ˜KÔ1µ5Ñ9Ô9ð Ø×)Ò)¨+Ñ6Ô6Ð7��Ý˜KÔ1µ5Ñ9Ô9ð åÔ+Ø×-Ò-¨kÔ.CÔ.JÑKÔKØ×-Ò-¨kÔ.CÔ.JÑKÔKñô ð��õ ˜KÔ1µ;Ñ?Ô?ð ÝÔ+Ñ-Ô-Ð.��Ý˜KÔ1µ>ÑBÔBð ÝÔ.Ñ0Ô0Ð1��å(ð?Ø&1Ô&;ð?ð ?ð ?ñô ð ð
 Ô$Ô/Ð7Ø×!Ò! %Ñ(Ô(Ð(Ñ(åÔ'¨Ñ.Ô.�Ø×!Ò!ÝÔ*Ø×-Ò-­o¸kÔ>SÔ>^Ñ._Ô._Ñ`Ô`Øñô ñô ð ñ ð Ðrj   rÓ  r   úast.BranchingStatementc                ó°  — |                       t          |j        j        ¦  «        ¦  «        }|j        j        d         }|                      ||j        |j        ¦  «        5  t          j	        |  
                    ¦   «         ¦  «        }ddd¦  «         n# 1 swxY w Y   t          |j        j        ¦  «        dk    rt          j        ||d¦  «        S |j        j        d         }|                      ||j        |j        ¦  «        5  t          j	        |  
                    ¦   «         ¦  «        }ddd¦  «         n# 1 swxY w Y   t          j        |||¦  «        S )z@Build an :obj:`.IfElseOp` into a :obj:`.ast.BranchingStatement`.r   Nr#   )rÊ  rÒ  r¿  Ú	conditionÚblocksr6  r  r!  r$   rÐ  rJ  r0  rÑ  )rg   rÓ  rÙ  Útrue_circuitÚ	true_bodyÚfalse_circuitÚ
false_bodys          rh   rÂ  zQASM3Builder.build_if_statementý  sÂ  € à×)Ò)­/¸+Ô:OÔ:YÑ*ZÔ*ZÑ[Ô[ˆ	à"Ô,Ô3°AÔ6ˆØ�^Š^˜L¨+Ô*<¸kÔ>PÑQÔQð 	Eð 	EÝÔ(¨×)AÒ)AÑ)CÔ)CÑDÔDˆIð	Eð 	Eð 	Eñ 	Eô 	Eð 	Eð 	Eð 	Eð 	Eð 	Eð 	Eøøøð 	Eð 	Eð 	Eð 	Eåˆ{Ô$Ô+Ñ,Ô,°Ò1Ð1ÝÔ)¨)°YÀÑEÔEÐEà#Ô-Ô4°QÔ7ˆØ�^Š^˜M¨;Ô+=¸{Ô?QÑRÔRð 	Fð 	FÝÔ)¨$×*BÒ*BÑ*DÔ*DÑEÔEˆJð	Fð 	Fð 	Fñ 	Fô 	Fð 	Fð 	Fð 	Fð 	Fð 	Fð 	Føøøð 	Fð 	Fð 	Fð 	FåÔ% i°¸JÑGÔGÐGs$   Á 'BÂBÂBÄ'D7Ä7D;Ä>D;úIterable[ast.Statement]c                óª  ‡ ‡‡	— ‰                       t          j        ‰j        j        ¦  «        ¦  «        }‰ j                             dddd¬¦  «        }‰ j                             t          j
        t          j        ¦   «         |d¦  «        ¦  «         t          j        ‰ j        v rSˆˆ fd„Š	t          j        ||¦  «        t          j        |ˆ	fd„‰j                             ¦   «         D ¦   «         ¦  «        gS g }d}‰j                             ¦   «         D ]“\  }}‰                      |‰j        ‰j        ¦  «        5  t          j        ‰                      ¦   «         ¦  «        }ddd¦  «         n# 1 swxY w Y   t.          |v r|}Œp|                     ˆ fd„|D ¦   «         |f¦  «         Œ”t          j        ||¦  «        t          j        |||¬¦  «        gS )	zBBuild a :obj:`.SwitchCaseOp` into a :class:`.ast.SwitchStatement`.Úswitch_dummyNT)rÛ   rî   c                óæ   •— ˆfd„| D ¦   «         } ‰                      |‰j        ‰j        ¦  «        5  t          j        ‰                     ¦   «         ¦  «        }d d d ¦  «         n# 1 swxY w Y   | |fS )Nc                ór   •— g | ]3}|t           u rt          j        ¦   «         n‰                     |¦  «        ‘Œ4S r}   )r    r$   ÚDefaultCaseÚbuild_integer)r‰   Úvrg   s     €rh   r�   zEQASM3Builder.build_switch_statement.<locals>.case.<locals>.<listcomp>  sO   ø€ ð ð ð àð *+­lÐ):Ð):•C”OÑ%Ô%Ð%À×@RÒ@RÐSTÑ@UÔ@Uðð ð rj   )r6  r  r!  r$   rÐ  rJ  )rK  Ú
case_blockÚ	case_bodyrÓ  rg   s      €€rh   Úcasez1QASM3Builder.build_switch_statement.<locals>.case  sÖ   ø€ ðð ð ð à#ðñ ô �ð —^’^ J°Ô0BÀKÔDVÑWÔWð Mð MÝ #Ô 0°×1IÒ1IÑ1KÔ1KÑ LÔ L�IðMð Mð Mñ Mô Mð Mð Mð Mð Mð Mð Møøøð Mð Mð Mð Mà˜yÐ(Ð(s   ±'A$Á$A(Á+A(c              3  ó6   •K  — | ]\  }} ‰||¦  «        V — Œd S rƒ   r}   )r‰   rK  Úblockré  s      €rh   r+  z6QASM3Builder.build_switch_statement.<locals>.<genexpr>&  sG   øè è € ð ð á)˜F Eð ˜˜V UÑ+Ô+ðð ð ð ð ð rj   c                ó:   •— g | ]}‰                      |¦  «        ‘ŒS r}   ©rå  ©r‰   Úvaluerg   s     €rh   r�   z7QASM3Builder.build_switch_statement.<locals>.<listcomp>9  s'   ø€ ÐIÐIÐI¸˜4×-Ò-¨eÑ4Ô4ÐIÐIÐIrj   )Údefault)rÊ  r   Úliftr¿  rx  r"  rù   r$  rÇ   r$   r�  ÚIntTyper%   ÚSWITCH_CASE_V1rd   rÉ  ÚSwitchStatementPreviewÚcases_specifierr6  r  r!  rÐ  rJ  r    ÚSwitchStatement)
rg   rÓ  Úreal_targetrx  Úcasesrð  rK  rë  rè  ré  s
   ``       @rh   rÃ  z#QASM3Builder.build_switch_statement  sZ  øøø€ à×+Ò+­D¬I°kÔ6KÔ6RÑ,SÔ,SÑTÔTˆØ”×/Ò/Ø˜D¨tÀ$ð 0ñ 
ô 
ˆð 	Ô3×:Ò:ÝÔ$¥S¤[¡]¤]°F¸DÑAÔAñ	
ô 	
ð 	
õ  Ô.°$Ô2CÐCÐCð)ð )ð )ð )ð )ð )õ Ô'¨°Ñ<Ô<ÝÔ*Øðð ð ð à-8Ô-B×-RÒ-RÑ-TÔ-Tðñ ô ñô ð	ð 	ð ˆØˆØ(Ô2×BÒBÑDÔDð 		Wð 		W‰MˆF�EØ—’  {Ô'9¸;Ô;MÑNÔNð Ið IÝÔ,¨T×-EÒ-EÑ-GÔ-GÑHÔH�	ðIð Ið Iñ Iô Ið Ið Ið Ið Ið Ið Iøøøð Ið Ið Ið Iå˜vÐ%Ð%ð $�ØØ�LŠLÐIÐIÐIÐIÀ&ÐIÑIÔIÈ9ÐUÑVÔVÐVÐVõ Ô# F¨KÑ8Ô8ÝÔ ¨°wÐ?Ñ?Ô?ð
ð 	
s   Ä;'E.Å.E2	Å5E2	úast.WhileLoopStatementc                óf  — |                       t          |j        j        ¦  «        ¦  «        }|j        j        d         }|                      ||j        |j        ¦  «        5  t          j	        |  
                    ¦   «         ¦  «        }ddd¦  «         n# 1 swxY w Y   t          j        ||¦  «        S )zBBuild a :obj:`.WhileLoopOp` into a :obj:`.ast.WhileLoopStatement`.r   N)rÊ  rÒ  r¿  rÙ  rÚ  r6  r  r!  r$   rÐ  rJ  ÚWhileLoopStatement)rg   rÓ  rÙ  Úloop_circuitÚ	loop_bodys        rh   rÁ  zQASM3Builder.build_while_loop@  sä   € à×)Ò)­/¸+Ô:OÔ:YÑ*ZÔ*ZÑ[Ô[ˆ	Ø"Ô,Ô3°AÔ6ˆØ�^Š^˜L¨+Ô*<¸kÔ>PÑQÔQð 	Eð 	EÝÔ(¨×)AÒ)AÑ)CÔ)CÑDÔDˆIð	Eð 	Eð 	Eñ 	Eô 	Eð 	Eð 	Eð 	Eð 	Eð 	Eð 	Eøøøð 	Eð 	Eð 	Eð 	EåÔ% i°Ñ;Ô;Ð;s   Á 'BÂBÂBúast.ForLoopStatementc                óð  ‡ — |j         j        \  }}}‰                      ||j        |j        ¦  «        5  |€dn|j        }‰ j                             ||d¬¦  «        }t          |t          ¦  «        rpt          j        ‰                      |j        ¦  «        ‰                      |j        dz
  ¦  «        |j        dk    r‰                      |j        ¦  «        nd¬¦  «        }nD	 t          j        ˆ fd„|D ¦   «         ¦  «        }n"# t"          $ r t#          d|› d	�¦  «        d‚w xY wt          j        ‰                      ¦   «         ¦  «        }ddd¦  «         n# 1 swxY w Y   t          j        |||¦  «        S )
z>Build a :obj:`.ForLoopOp` into a :obj:`.ast.ForLoopStatement`.Nrß   Tr]  r#   )Ústartr.   Ústepc                ó:   •— g | ]}‰                      |¦  «        ‘ŒS r}   rí  rî  s     €rh   r�   z/QASM3Builder.build_for_loop.<locals>.<listcomp>Y  s'   ø€ Ð0aÐ0aÐ0aÈu°×1CÒ1CÀEÑ1JÔ1JÐ0aÐ0aÐ0arj   zIThe values in OpenQASM 3 'for' loops must all be integers, but received 'z'.)r¿  r  r6  r  r!  rÏ   r"  rù   r  r�   r$   ÚRangerå  r   Ústopr  r²  r&   rÐ  rJ  ÚForLoopStatement)	rg   rÓ  ÚindexsetÚloop_parameterrü  rÏ   Úloop_parameter_astÚindexset_astÚbody_asts	   `        rh   rÀ  zQASM3Builder.build_for_loopH  sô  ø€ à1<Ô1FÔ1MÑ.ˆ�. ,Ø�^Š^˜L¨+Ô*<¸kÔ>PÑQÔQð 	Dð 	DØ(Ð0�3�3°nÔ6IˆDØ!%¤×!?Ò!?Ø�n°4ð "@ñ "ô "Ðõ ˜(¥EÑ*Ô*ð  å"œyØ×,Ò,¨X¬^Ñ<Ô<Ø×*Ò*¨8¬=¸1Ñ+<Ñ=Ô=Ø>F¼mÈqÒ>PÐ>P˜×+Ò+¨H¬MÑ:Ô:Ð:ÐVZð ñ  ô  ��ð Ý#&¤<Ð0aÐ0aÐ0aÐ0aÐX`Ð0aÑ0aÔ0aÑ#bÔ#b�L�LøÝ)ð  ð  ð  Ý,ð*Ø%ð*ð *ð *ñô ð  ð ð øøøõ
 Ô'¨×(@Ò(@Ñ(BÔ(BÑCÔCˆHð)	Dð 	Dð 	Dñ 	Dô 	Dð 	Dð 	Dð 	Dð 	Dð 	Dð 	Døøøð 	Dð 	Dð 	Dð 	Dõ* Ô# LÐ2DÀhÑOÔOÐOs*   ³B.EÃ" DÄEÄD"Ä")EÅEÅEc                óŠ   — t          |t          ¦  «        r|                      |¦  «        S | j                             |¦  «        S rƒ   )r  r   r;  r"  rÿ   )rg   r   s     rh   Ú_lookup_variable_for_expressionz,QASM3Builder._lookup_variable_for_expressionb  s>   € Ý�c�3ÑÔð 	)Ø×#Ò# CÑ(Ô(Ð(ØŒ|×(Ò(¨Ñ-Ô-Ð-rj   r·   ú	expr.Exprúast.Expressionc                óP   — |                      t          | j        ¦  «        ¦  «        S )zBuild an expression.)ÚacceptÚ_ExprBuilderr  ©rg   r·   s     rh   rÊ  zQASM3Builder.build_expressiong  s   € à�{Š{�<¨Ô(LÑMÔMÑNÔNÐNrj   úast.QuantumDelayc                óê  ‡ — |j         rt          d|› �¦  «        ‚|j        j        |j        j        }}|dk    r(t          j        d|z  t
          j        j        ¦  «        }nnt
          j        j        t
          j        j	        t
          j        j
        t
          j        j        t
          j        j        dœ}t          j        |||         ¦  «        }t          j        |ˆ fd„|j        D ¦   «         ¦  «        S )z!Build a built-in delay statement.z4Found a delay instruction acting on classical bits: Úpsiè  )ÚnsÚusÚmsr—   Údtc                ó:   •— g | ]}‰                      |¦  «        ‘ŒS r}   r°  ©r‰   rB   rg   s     €rh   r�   z,QASM3Builder.build_delay.<locals>.<listcomp>}  s'   ø€ Ð*cÐ*cÐ*cÀu¨4×+;Ò+;¸EÑ+BÔ+BÐ*cÐ*cÐ*crj   )r!  r&   r¿  rT   Úunitr$   ÚDurationLiteralÚDurationUnitÚ
NANOSECONDÚMICROSECONDÚMILLISECONDÚSECONDÚSAMPLEÚQuantumDelayr  )rg   rÓ  Úduration_valuer  rT   Úunit_maps   `     rh   rÈ  zQASM3Builder.build_delayk  sé   ø€ àÔð 	Ý$ØTÀ{ÐTÐTñô ð ð  +Ô4Ô=¸{Ô?TÔ?Y˜ˆØ�4Š<ˆ<ÝÔ*¨4°.Ñ+@Å#ÔBRÔB]Ñ^Ô^ˆHˆHõ Ô&Ô1ÝÔ&Ô2ÝÔ&Ô2ÝÔ%Ô,ÝÔ&Ô-ðð ˆHõ Ô*¨>¸8ÀD¼>ÑJÔJˆHÝÔ Ð*cÐ*cÐ*cÐ*cÐP[ÔPbÐ*cÑ*cÔ*cÑdÔdÐdrj   úast.IntegerLiteralc                óž   — t          |t          j        ¦  «        st          d|› d�¦  «        ‚t	          j        t          |¦  «        ¦  «        S )zwBuild an integer literal, raising a :obj:`.QASM3ExporterError` if the input is not
        actually an
        integer.rà   z' is not an integer)r  ÚnumbersÚIntegralr&   r$   rp  r0   )rg   rï  s     rh   rå  zQASM3Builder.build_integer  sM   € õ ˜%¥Ô!1Ñ2Ô2ð 	Eõ %Ð%C¨Ð%CÐ%CÐ%CÑDÔDÐDÝÔ!¥# e¡*¤*Ñ-Ô-Ð-rj   c                óæ   ‡ — t          |t          ¦  «        s|S t          |t          ¦  «        r‰ j                             |¦  «        j        S |                     ˆ fd„|j        D ¦   «         ¦  «        S )zÅIf the input is a :class:`.ParameterExpression`, rebind any internal
        :class:`.Parameter`\ s so that their names match their names in the scope.  Other inputs
        are returned unchanged.c                óx   •— i | ]6}|t          ‰j                             |¦  «        j        |j        ¬ ¦  «        “Œ7S ))Úuuid)r   r"  rÿ   r  r-  r^  s     €rh   r‹   z:QASM3Builder._rebind_scoped_parameters.<locals>.<dictcomp>”  sM   ø€ ð ð ð àð •y ¤×!:Ò!:¸5Ñ!AÔ!AÔ!HÈuÌzÐZÑZÔZðð ð rj   )r  r   r   r"  rÿ   r  ÚsubsrY  )rg   Ú
expressions   ` rh   Ú_rebind_scoped_parametersz&QASM3Builder._rebind_scoped_parameters‰  s‰   ø€ õ ˜*Õ&9Ñ:Ô:ð 	ØÐÝ�j¥)Ñ,Ô,ð 	@Ø”<×,Ò,¨ZÑ8Ô8Ô?Ð?Ø�Šðð ð ð à'Ô2ðñ ô ñ
ô 
ð 	
rj   c                ó„  ‡ — |j         }t          |d¦  «        r|                     ¦   «         }‰ j                             |¦  «        }|€‰                      |¦  «        }ˆ fd„|j        D ¦   «         }ˆ fd„|j        D ¦   «         }‰ j        s |D ]}t          |j
        d¬¦  «        |_
        Œt          j        |||¬¦  «        S )zŸBuilds a gate-call AST node.

        This will also push the gate into the symbol table (if required), including recursively
        defining the gate blocks.Ú_qasm_decompositionNc                ó:   •— g | ]}‰                      |¦  «        ‘ŒS r}   r°  r  s     €rh   r�   z0QASM3Builder.build_gate_call.<locals>.<listcomp>¥  s'   ø€ ÐJÐJÐJ¨e�$×"Ò" 5Ñ)Ô)ÐJÐJÐJrj   c                ó^   •— g | ])}t          j        ‰                     |¦  «        ¦  «        ‘Œ*S r}   )r$   ÚStringifyAndPrayr0  r^  s     €rh   r�   z0QASM3Builder.build_gate_call.<locals>.<listcomp>¦  sB   ø€ ð 
ð 
ð 
àõ Ô  ×!?Ò!?ÀÑ!FÔ!FÑGÔGð
ð 
ð 
rj   Úqasm)rH   )rY  )r¿  Úhasattrr2  r"  r  rz  r  r  r_   r"   Úobjr$   rj  )rg   rÓ  r¿  rú   r  rY  r�  s   `      rh   rÄ  zQASM3Builder.build_gate_callš  sô   ø€ ð
  Ô)ˆ	Ý�9Ð3Ñ4Ô4ð 	8Ø!×5Ò5Ñ7Ô7ˆIØ”×%Ò% iÑ0Ô0ˆØˆ=Ø×$Ò$ YÑ/Ô/ˆEØJÐJÐJÐJ°{Ô7IÐJÑJÔJˆð
ð 
ð 
ð 
à"Ô)ð
ñ 
ô 
ˆ
ð Ô%ð 	GØ'ð Gð G�	Ý (¨¬¸vÐ FÑ FÔ F�	”�ÝÔ" 5¨&¸ZÐHÑHÔHÐHrj   N)rp   r   r  r&  r!  r'  )r  r   )rI   rò   )r8   r   rI   rò   )r‰  r¬  rI   r­  )rI   r´  )rÓ  r   rI   r×  )rÓ  r   rI   rß  )rÓ  r   rI   rù  )rÓ  r   rI   rþ  )r·   r  rI   r  )rÓ  r   rI   r  )rI   r'  )rÓ  r   )+ry   rz   r{   r|   r
   r   r   r   r   r   r   Úbuiltinsr‘  r¦  ra  rd  r%   ri   Ú
contextlibÚcontextmanagerr6  rÎ   r;  rw   rE  rz  r|  rF  rG  rH  rI  r•  rJ  rÂ  rÃ  rÁ  rÀ  r  rÊ  rÈ  rå  r0  rÄ  r}   rj   rh   ru   ru   %  s^  € € € € € Ø@Ð@à˜ %¨°¸^ÈUÐS€HØÐØ!ÐØ%ÐØ!Ðð *Ð)¨!Ñ,Ô,ð)ð )ð )ð )ð8 Ôð!ð !ð !ñ Ôð!ð. Ôð#ð #ð #ñ Ôð#ðBð Bð Bð Bð
I/ð I/ð I/ðV(ð (ð (ðA[ð A[ð A[ð A[ðFDð Dð Dð
Pð Pð Pð$6ð 6ð 6ðpð ð ð"2(ð 2(ð 2(ðhð ð ð ðOð Oð Oð OðbHð Hð Hð Hð2
ð 2
ð 2
ð 2
ðh<ð <ð <ð <ðPð Pð Pð Pð4.ð .ð .ð
Oð Oð Oð Oðeð eð eð eð(.ð .ð .ð .ð
ð 
ð 
ð"Ið Ið Ið Ið Ið Irj   ru   rp   r   r�  r   r‚  rò   ú%Union[ast.ClassicalDeclaration, None]c                ó’   ‡— ˆfd„Š ‰| |¦  «        rdS t          j        t           j        j        t           j        j        |¦  «        S )am  Attempt to infer what type a parameter should be declared as to work with a circuit.

    This is very simplistic; it assumes all parameters are real numbers that need to be input to the
    program, unless one is used as a loop variable, in which case it shouldn't be declared at all,
    because the ``for`` loop declares it implicitly (per the Qiskit/qe-compiler reading of the
    OpenQASM spec at openqasm/openqasm@8ee55ec).

    .. note::

        This is a hack around not having a proper type system implemented in Terra, and really this
        whole function should be removed in favour of proper symbol-table building and lookups.
        This function is purely to try and hack the parameters for ``for`` loops into the exporter
        for now.

    Args:
        circuit: The global-scope circuit, which is the base of the exported program.
        parameter: The parameter to infer the type of.
        parameter_name: The name of the parameter to use in the declaration.

    Returns:
        A suitable :obj:`.ast.ClassicalDeclaration` node, or, if the parameter should *not* be
        declared, then ``None``.
    c                ó  •— | j                              |¦  «        D ]g\  }}|€Œ| j        |         j        }t	          |t
          ¦  «        r	|dk    r dS t	          |t          ¦  «        r ‰|j        |         |¦  «        r dS ŒhdS )z‹Recurse into the instructions a parameter is used in, checking at every level if it is
        used as the loop variable of a ``for`` loop.Nr#   TF)Ú_dataÚ_raw_parameter_table_entryr¾  r¿  r  r   r   r  )rp   r�  Úinstr_indexÚindexrÓ  Úis_loop_variables        €rh   rC  z5_infer_variable_declaration.<locals>.is_loop_variableË  sŸ   ø€ ð #*¤-×"JÒ"JÈ9Ñ"UÔ"Uð 
	 ð 
	 ÑˆK˜ØÐ"ØØ!œ, {Ô3Ô=ˆKÝ˜+¥yÑ1Ô1ð  à˜A’:�:Ø˜4˜4Ý˜+¥}Ñ5Ô5ð  Ø#Ð# KÔ$6°uÔ$=¸yÑIÔIð  Ø˜4˜4øØˆurj   N)r$   r€  rœ  r�  Ú	FloatTypeÚDOUBLE)rp   r�  r‚  rC  s      @rh   r~  r~  °  sY   ø€ ð6ð ð ð ð ð( Ð˜ Ñ+Ô+ð Øˆtõ Ô�Sœ^Ô1µ3´=Ô3GÈÑXÔXÐXrj   c                ób   — t          | t          j        ¦  «        r| S t          j        | ¦  «        S rƒ   )r  r   ÚExprÚlift_legacy_condition)rÙ  s    rh   rÒ  rÒ  æ  s-   € Ý�)�TœYÑ'Ô'ð ØÐÝÔ% iÑ0Ô0Ð0rj   Útype_ú
types.Typeúast.ClassicalTypec                óÌ   — | j         t          j        u rt          j        ¦   «         S | j         t          j        u rt          j        | j        ¦  «        S t          d| › d�¦  «        ‚)Nzunhandled expr type 'rà   )	Úkindr   ÚBoolr$   ÚBoolTypeÚUintÚUintTypeÚwidthrç   )rI  s    rh   rž  rž  ì  sX   € Ø„z•U”ZÐÐÝŒ|‰~Œ~ÐØ„z•U”ZÐÐÝŒ|˜EœKÑ(Ô(Ð(Ý
Ð7¨uÐ7Ð7Ð7Ñ
8Ô
8Ð8rj   c                  ó<   — e Zd ZdZd„ Zd„ Zd„ Zd„ Zd„ Zd„ Z	d„ Z
d	S )
r  ©Úlookupc                ó   — || _         d S rƒ   rT  )rg   rU  s     rh   ri   z_ExprBuilder.__init__û  s   € ØˆŒˆˆrj   c               ón   — |j         r|                      |¦  «        n|                      |j        ¦  «        S rƒ   )Ú
standalonerU  r   r  s     rh   Ú	visit_varz_ExprBuilder.visit_varþ  s.   € Ø$(¤OÐNˆt�{Š{˜4Ñ Ô Ð ¸¿ºÀTÄXÑ9NÔ9NÐNrj   c               óì   — |j         j        t          j        u rt	          j        |j        ¦  «        S |j         j        t          j        u rt	          j        |j        ¦  «        S t          d|› d�¦  «        ‚)Nzunhandled Value type 'rà   )
rŸ  rM  r   rN  r$   ÚBooleanLiteralrï  rP  rp  rç   r  s     rh   Úvisit_valuez_ExprBuilder.visit_value  sd   € ØŒ9Œ>�UœZÐ'Ð'ÝÔ% d¤jÑ1Ô1Ð1ØŒ9Œ>�UœZÐ'Ð'ÝÔ% d¤jÑ1Ô1Ð1ÝÐ;°DÐ;Ð;Ð;Ñ<Ô<Ð<rj   c               óÂ   — |j         r|j                             | ¦  «        S t          j        t          |j        ¦  «        |j                             | ¦  «        ¦  «        S rƒ   )Úimplicitr¹  r  r$   ÚCastrž  rŸ  r  s     rh   Ú
visit_castz_ExprBuilder.visit_cast  sN   € ØŒ=ð 	-Ø”<×&Ò& tÑ,Ô,Ð,ÝŒx�¨¬	Ñ2Ô2°D´L×4GÒ4GÈÑ4MÔ4MÑNÔNÐNrj   c               óš   — t          j        t           j        j        |j        j                 |j                             | ¦  «        ¦  «        S rƒ   )r$   ÚUnaryÚOpÚoprÏ   r¹  r  r  s     rh   Úvisit_unaryz_ExprBuilder.visit_unary  s1   € ÝŒy�œœ d¤g¤lÔ3°T´\×5HÒ5HÈÑ5NÔ5NÑOÔOÐOrj   c               óÌ   — t          j        t           j        j        |j        j                 |j                             | ¦  «        |j                             | ¦  «        ¦  «        S rƒ   )r$   ÚBinaryrc  rd  rÏ   Úleftr  Úrightr  s     rh   Úvisit_binaryz_ExprBuilder.visit_binary  sK   € ÝŒzÝŒJŒM˜$œ'œ,Ô'¨¬×)9Ò)9¸$Ñ)?Ô)?ÀÄ×ARÒARÐSWÑAXÔAXñ
ô 
ð 	
rj   c               óŒ   — t          j        |j                             | ¦  «        |j                             | ¦  «        ¦  «        S rƒ   )r$   ÚIndexrx  r  rB  r  s     rh   Úvisit_indexz_ExprBuilder.visit_index  s4   € ÝŒy˜œ×+Ò+¨DÑ1Ô1°4´:×3DÒ3DÀTÑ3JÔ3JÑKÔKÐKrj   N)ry   rz   r{   Ú	__slots__ri   rY  r\  r`  re  rj  rm  r}   rj   rh   r  r  ô  s�   € € € € € Ø€Iðð ð ðOð Oð Oð=ð =ð =ðOð Oð Oð
Pð Pð Pð
ð 
ð 
ð
Lð Lð Lð Lð Lrj   r  )r8   r   rI   r   )rp   r   r�  r   r‚  rò   rI   r<  )rI  rJ  rI   rK  )rr|   Ú
__future__r   Úcollectionsr:  Údataclassesrl   rÂ   rl  r)  ÚreÚtypingr   r   r   r   Úqiskit._accelerate.circuitr   Úqiskit.circuitr	   r
   r   r   r   r   r   r   r   r   r   r   r   Úqiskit.circuit.bitr   Úqiskit.circuit.classicalr   r   Úqiskit.circuit.controlflowr   r   r   r   r   r   r   r    Úqiskit.circuit.registerr!   Úqiskit.circuit.toolsr"   Ú r$   rd   r%   Ú
exceptionsr&   Úprinterr'   Ú	frozensetrÑ   Úcompiler[   rá   rõ   rã   rY   r†   Ú	all_gatesr  r  Ú	PhaseGateÚXGateÚYGateÚZGateÚHGateÚSGateÚSdgGateÚTGateÚTdgGateÚSXGateÚRXGateÚRYGateÚRZGaterh  ÚCYGateÚCZGateÚ
CPhaseGateÚCRXGateÚCRYGateÚCRZGateÚCHGateÚSwapGateÚCCXGateÚ	CSwapGateÚCUGateÚIGateÚU1GateÚU2GateÚU3GaterT  rk  rA  Ú	dataclassr´   r»   r  r  ru   r~  rÒ  rž  ÚExprVisitorÚ
Expressionr  r}   rj   rh   ú<module>r      se  ðð Ð à "Ð "Ð "Ð "Ð "Ð "à Ð Ð Ð Ø Ð Ð Ð Ø Ð Ð Ð Ø 	€	€	€	Ø Ð Ð Ð Ø €€€Ø €€€Ø 	€	€	€	Ø 2Ð 2Ð 2Ð 2Ð 2Ð 2Ð 2Ð 2Ð 2Ð 2Ð 2Ð 2à 3Ð 3Ð 3Ð 3Ð 3Ð 3ðð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð #Ð "Ð "Ð "Ð "Ð "Ø 0Ð 0Ð 0Ð 0Ð 0Ð 0Ð 0Ð 0ð	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð -Ð ,Ð ,Ð ,Ð ,Ð ,Ø )Ð )Ð )Ð )Ð )Ð )à Ð Ð Ð Ð Ð Ø .Ð .Ð .Ð .Ð .Ð .Ø *Ð *Ð *Ð *Ð *Ð *Ø !Ð !Ð !Ð !Ð !Ð !ð �Yð0ð 0ð 0ñ2ô 2Ð ðn  *˜rœzÐ*:À"Ä$ÐGÑGÔGÐ Ø"˜œ
 :°R´TÐ:Ñ:Ô:Ð Ø'˜RœZ¨
¸"¼$Ð?Ñ?Ô?Ð ðL
ð L
ð L
ð L
ð L
ñ L
ô L
ð L
ðd �Y˜t‘_”_ i i°¡o¤o°y°yÀ±´È	È	ÐRVÉÌÐXÐ ðð à*�LÔ*Ñ,Ô,ðñ ô Ð ð
(ð (ð
 +�LÔ*Ñ,Ô,ð(ñ (ô (Ð $ð ð !ØÐ& |Ô'=Ô>ð!àÐ& |Ô'9Ô:ð!ð 	Ð& |Ô'9Ô:ð!ð 	Ð& |Ô'9Ô:ð	!ð
 	Ð& |Ô'9Ô:ð!ð 	Ð& |Ô'9Ô:ð!ð 	Ð(¨Ô)=Ô>ð!ð 	Ð& |Ô'9Ô:ð!ð 	Ð(¨Ô)=Ô>ð!ð 	Ð'¨Ô(;Ô<ð!ð 	Ð'¨Ô(;Ô<ð!ð 	Ð'¨Ô(;Ô<ð!ð 	Ð'¨Ô(;Ô<ð!ð 	Ð2°<Ô3FÔGÈÔJð!ð 	Ð2°<Ô3FÔGÈÔJð!ð  	Ð2°<Ô3FÔGÈÔJð!!ð" 	Ð2°<Ô3JÔKÈAÔNð#!ð$ 4°LÔ4HÔIÈ!ÔLØ3°LÔ4HÔIÈ!ÔLØ3°LÔ4HÔIÈ!ÔLØ2°<Ô3FÔGÈÔJØ)¨,Ô*?Ô@Ø3°LÔ4HÔIÈ!ÔLØ5°lÔ6LÔMÈaÔPØ2°<Ô3FÔGÈÔJØ2°<Ô3FÔGÈÔJØ*¨<Ô+AÔBØ6°|Ô7NÔOÐPQÔRØ'¨Ô(:Ô;Ø'¨Ô(;Ô<Ø'¨Ô(;Ô<Ø'¨Ô(;Ô<ðA!ð !ð !ð#€ðL Ð	" <Ô#5Ô	6ð€ð
 ÔðFð Fð Fð Fð Fñ Fô Fñ ÔðFð a8ð a8ð a8ð a8ð a8ñ a8ô a8ð a8ðHð ð ð ð0 Ôð8ð 8ð 8ð 8ð 8ñ 8ô 8ñ Ôð8ðH
Ið H
Ið H
Ið H
Ið H
Iñ H
Iô H
Ið H
IðV3Yð 3Yð 3Yð 3Yðl1ð 1ð 1ð9ð 9ð 9ð 9ð"Lð "Lð "Lð "Lð "L�4Ô# C¤NÔ3ñ "Lô "Lð "Lð "Lð "Lrj   