
    z	i}2                    "   S r SSKJr  SS/rSSKrSSKrSSKrSSKrSSK	r	SSK
r
SSKJrJrJrJrJrJrJrJr  SSKJr  SS	KJr  \" / S
Q5      r\" / SQ5      rSS jrSS jrSS jrSS jrS r S r!\" S5      \" S5      \" S5      /r"S r#S r$g)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   c                   [        U[        R                  5      (       a  [        [	        U 5      US9  g[        US5       n[        [	        U 5      US9  SSS5        g! , (       d  f       g= f)ad  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)circuitfilename_or_streamstreams      M/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/qasm2/export.pyr   r   k   sO     $bmm44eGn#56	 #	&&eGn6* 
'	&	&s    A
A+c                 ^ U R                   S:  a  [        S5      e[        R                  " 5       nU R                   Vs/ s H&  o R                  U5      R                  (       a  M$  UPM(     nnU R                   Vs/ s H&  o R                  U5      R                  (       a  M$  UPM(     nn/ nU(       a  UR                  [        SUS95        U(       a  UR                  [        SUS95        0 nU R                  U R                  U4 H/  nU H&  nUU[        [        UR                  S5      U5      '   M(     M1     UR!                  5        V	V
VVs0 s H"  u  p[#        U
5        H  u  pX) SU S3_M     M$     snnn
n	mS	R%                  S
 UR!                  5        5       5      n/ nU R&                   H  nUR(                  nUR                  S:X  a.  UR                  S   nUR                  S   nSTU    STU    S3nOUR                  S:X  a  STUR                  S       S3nOdUR                  S:X  aH  UR                  (       d  M  SR%                  U4S jUR                   5       5      nU(       d  SOSU S3nO[+        XT5      nUR                  U5        M     S	R%                  S U 5       5      nS	R%                  S UR-                  5        5       5      nS	R%                  S SSUUU4 5       5      $ s  snf s  snf s  snnn
n	f )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.qregless)namebitscreglessreg_[]
c              3  ~   #    U  H3  u  p[        U[        5      (       a  S OS SU SUR                   S3v   M5     g7f)r?   r@    rY   z];N)rJ   r   size).0rU   regs      rR   	<genexpr>dumps.<locals>.<genexpr>   s@      *7ID  _5566
B!D6388*TVW7s   ;=r   zmeasure z -> ;r   zreset r   ,c              3  .   >#    U  H
  nTU   v   M     g 7fN )r_   q
bit_labelss     rR   ra   rb      s     G4FqZ]4F   zbarrier;zbarrier c              3  &   #    U  H  o v   M	     g 7frf   rg   )r_   calls     rR   ra   rb      s     !J8IF)8Is   c              3  ,   #    U  H
  u  pU v   M     g 7frf   rg   )r_   _qasms      rR   ra   rb      s     %X?WGAi?W   c              3  8   #    U  H  nU(       d  M  Uv   M     g 7frf   rg   )r_   parts     rR   ra   rb      s"      

D  	
s   	zOPENQASM 2.0;zinclude "qelib1.inc";)num_parametersr   collectionsOrderedDictqubitsfind_bit	registersclbitsappendr   r	   qregscregs_make_unique_escape_namerU   items	enumeratejoin_data	operation_custom_operation_statementvalues)rO   gates_to_definebitregless_qubitsregless_clbitsdummy_registersregister_escaped_namesregsr`   rU   registeridxregister_definitions_qasminstruction_callsinstructionr   qubitclbitinstruction_qasmqargsinstructions_qasmgate_definitions_qasmri   s                         @rR   r   r   }   s    !abb
 	!  &-^^[^c;K;KC;P;Z;Zc^N[%,^^[^c;K;KC;P;Z;Zc^N[ACOJ^TU0j~VWMO?C  #\#((F;=ST  @ 5::<,<ND#H-JS 	vQse1- 	<,J
 !%		 */557* ! }}))	>>Y&&&q)E&&q)E!)*U*;)<DEAR@SSTU^^w&!'
;3E3Ea3H(I'J!L^^y(%% HHGK4F4FGGE16zhugQ<O:;Ycd  !12# %$ 		!J8I!JJ II%X?U?U?W%XX99 
 #!%

 
 
Y \[,s    #K<'K<=#L$L)L
c                    [         R                  " SSU [         R                  S9nU(       a!  US   [        R                  ;  d
  U[
        ;   a  X-   nU$ )z~Returns a valid OpenQASM 2.0 identifier, using `prefix` as a prefix if necessary.  `prefix`
must itself be a valid identifier.z\Wrn   )flagsr   )resubASCIIstringascii_lowercase	_RESERVED)rU   prefixescaped_names      rR   r~   r~      sG     66%d"((;L?&"8"889$,    c                  ^ X;  a  U $ U Vs1 s H(  o"R                  U 5      (       d  M  U[        U 5      S iM*     nnU (       a!  [        R                  [        R                  -   O[        R                  m[
        R                  R                  U4S j[
        R                  " S5       5       5       H  nSR                  U5      nXS;  d  M  X-   s  $    U $ s  snf )zRGenerate a name by suffixing the given stem that is unique within the defined set.Nc              3  N   >#    U  H  n[         R                  " TUS 9v   M     g7f))repeatN)	itertoolsproduct)r_   n
characterss     rR   ra   _make_unique.<locals>.<genexpr>   s"      /9KA	*Q/9Ks   "%r    )

startswithlenr   digitsascii_lettersr   chainfrom_iterablecountr   )rU   already_definedin_useusedpartssuffixr   s         @rR   r}   r}      s    ".=YoFARARSWAXF3t9;oDY;?&--&"6"66VEYEYJ.. /9B9K/  =  K Zs
   CCc                X    U R                    S[        U 5       3nU R                  US9nU$ )z]Returns the operation with a new name following this pattern: {operation name}_{operation id}rn   rU   )rU   r   copy)r   new_nameupdated_operations      rR   _rename_operationr      s2    ..!2i=/2H!H5r   c                    U R                   S:X  a  SnOU R                   nU R                  (       a=  SR                  U R                   Vs/ s H  n[        USSS9PM     sn5      nU SU S3nU$ s  snf )	z0Return an OpenQASM 2 string for the instruction.r<   c3sqrtxrd   ro   g-q=)outputeps())rU   paramsr   r   )r   
qasm2_callir   s       rR   _instruction_call_siter      sq    ~~
^^
)JZJZ[JZQ8Af%@JZ[\"|1VHA.
 \s   
A0param0param1param2c                  ^ [        U R                  U5      nU R                  (       a,  [        R                  " U R
                  U R                  5      nOU R
                  nSR                  U4S jU 5       5      n[        U5       SU S3$ )Nrd   c              3  .   >#    U  H
  nTU   v   M     g 7frf   rg   )r_   jri   s     rR   ra   ._custom_operation_statement.<locals>.<genexpr>	  s     51Arj   r]   rc   )_define_custom_operationr   ry   r   r   rv   r   r   )r   r   ri   r   rV   	bits_qasms     `   rR   r   r     sr    ()>)>PI{11;3E3EF!!555I$Y/0)A>>r   c                  ^^ U R                   [        ;   a  U $ [        U R                   S5      nX R                   :w  a  U R                  US9n [        R
                  [        R                  [        R                  [        R                  [        R                  [        R                  [        R                  [        R                  [        R                  [        R                  [        R                  [        R                   [        R"                  [        R$                  [        R&                  1nU R(                  U;   a)  [+        U 5      " [,        S[/        U R0                  5       6 nOH[3        U S5      (       a5  U R5                  5       nUR                  [        UR                   S5      S9=p@OU nTR7                  U R                   S5      u  pgXF:X  a  U $ U R                   T;   a  [9        U 5      n U R                   nUR0                  (       a;  SSR;                  S [=        [/        UR0                  5      5       5       5      -   S	-   n	OS
n	U R>                  S:X  a  [A        SU R                    S35      eU RB                  S:w  a&  [A        SU R                    SU RB                   S35      eSR;                  S [=        UR>                  5       5       5      n
[E        USS5      nUc  USU U	 SU
 S34TU'   U $ [G        URH                  5       VVs0 s H  u  pUSU 3_M     snnmSR;                  UU4S jURJ                   5       5      nU R                   T;   a  [9        U 5      n U R                   nSU U	 SU
 SU S3nXO4TU'   U $ s  snnf )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   rd   c              3  ,   #    U  H
  nS U 3v   M     g7f)paramNrg   r_   r   s     rR   ra   +_define_custom_operation.<locals>.<genexpr>H  s     [0Z1U1#;0Zrp   r   r   r   zOpenQASM 2 cannot represent 'z, which acts on zero qubits.z', which acts on z classical bits.c              3  ,   #    U  H
  nS U 3v   M     g7f)rh   Nrg   r   s     rR   ra   r   W  s     V,UqQqc7,Urp   
definitionzopaque r]   rc   rh   c              3  >   >#    U  H  n[        UTT5      v   M     g 7frf   )r   )r_   r   r   qubit_labelss     rR   ra   r   `  s$      
< (_lSS<s   zgate z { z })&rU   _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getattrr   rv   data)r   r   escapedknown_good_parameterizedparameterized_operationnew_opprevious_definition_sourcern   r   parameters_qasmqubits_qasmparameterized_definitionr   r   	body_qasmdefinition_qasmr   s    `              @rR   r   r     s<    ~~-- 9>>73G.. NNN0	 			





		





 ( 77"&y/3DE\s9K[K[G\3]"^	1	2	2..0.4kk|FKKY`?ak.bb)"+ %4$7$7	$U!< ~~(%i0	~~H%%#(([c:Q:X:X6Y0Z[[[^aa 	 q +INN+;;WX
 	
 q +INN+;;LYMaMaLb
 	

 ((VE2I2T2T,UVVK&'>dS'#hZ0+a@%
!$  4==U=\=\3]^3]qW3]^HH 
7<<
 
	 >>_,))4I ~~H!(O+<Ak]$ykY\]%<$N! _s   N)rO   r   rP   zos.PathLike | io.TextIOBase)rO   r   returnstr)rU   r  r   r  r  r  )rU   r  r   zcollections.abc.Set[str]r  r  )%__doc__
__future__r   __all__collections.abcrt   rK   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   rg   r   rR   <module>r     s    # "7
  	  	 	 	 	 	 * ( .0 d 	$+$K\ 	 x()H*=y?RS ?_r   