
    z	i|H                     n
   S r SSKrSSKrSSKJrJr  SSKrSSKJ	r	J
r
JrJr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JrJrJrJrJrJrJ r J!r!  SSK"J#r#  \RH                  \RJ                  \RL                  \RN                  S	 \RP                  \RR                  \RT                  \RV                  \RX                  \RZ                  \R\                  \R^                  \R`                  \Rb                  \Rd                  \Rf                  \Rh                  \Rj                  \Rl                  \Rn                  \Rp                  \Rr                  4r:\Rv                  " S
S9 " S S5      5       r<S\=4S jr> " S S\5      r?/ \<" SSS\RH                  5      P\<" SSS\RJ                  5      P\<" SSS\RL                  5      P\<" SSS\RN                  5      P\<" SSS\R                  5      P\<" SSS\?S
S9P\<" SSS\R                  S
S9P\<" SSS\R                  S
S9P\<" SSS\RP                  5      P\<" SSS\RR                  5      P\<" SSS\RT                  5      P\<" S SS\RV                  5      P\<" S!SS\RX                  5      P\<" S"SS\RZ                  5      P\<" S#SS\R\                  5      P\<" S$SS\R^                  5      P\<" S%SS\R`                  5      P\<" S&SS\Rb                  5      P\<" S'SS\Rd                  5      P\<" S(SS\R                  S
S9P\<" S)SS\R                  S
S9P\<" S*SS\Rf                  5      P\<" S+SS\Rh                  5      P\<" S,SS\R                  S
S9P\<" S-SS\Rj                  5      P\<" S.SS\Rl                  5      P\<" S/SS\R                  S
S9P\<" S0SS\R                  S
S9P\<" S1SS\R                  S
S9P\<" S2SS\Rn                  5      P\<" S3SS\Rp                  5      P\<" S4SS\R                  S
S9P\<" S5SS\Rr                  5      P\<" S6SS\R                  S
S9P\<" S7S8S\R                  S
S9P\<" S9SS\R                  S
S9P\<" S:SS\R                  S
S9P\<" S;SS\R                  S
S9P\<" S<SS8\R                  S
S9P\<" S=SS8\R                  S
S9P\<" S>SS8\R                  S
S9P\<" S?SS@\R                  S
S9P\<" SASS\>5      P7rS\" SBS\R                  5      \" SCS\R                  5      \" SDS\R                  5      4rWSE\\<   4SF jrX " SG SH\5      rYSI rZSJ r[SK r\g)LzOPython-space bytecode interpreter for the output of the main Rust parser logic.    N)IterableCallable)BarrierCircuitInstructionClassicalRegisterDelayGateInstructionMeasureQuantumCircuitQuantumRegisterQubitResetlibrary)Operator)	OpCodeUnaryOpCodeBinaryOpCodeCustomClassicalExprConstantExprArgument	ExprUnary
ExprBinary
ExprCustom   )QASM2ParseErrorc                  2    [         R                  " SSS5      $ )Nr   )libUGate     L/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/qasm2/parse.py<lambda>r#   9   s    CIIaAr!   T)frozenc                   Z    \ rS rSr% Sr\\S'   \\S'   \\S'   \S\	4   \S'   Sr
\\S	'   S
rg)CustomInstructionO   a  Information about a custom instruction that should be defined during the parse.

The ``name``, ``num_params`` and ``num_qubits`` fields are self-explanatory.  The
``constructor`` field should be a callable object with signature ``*args -> Instruction``, where
each of the ``num_params`` ``args`` is a floating-point value.  Most of the built-in Qiskit gate
classes have this form.

There is a final ``builtin`` field.  This is optional, and if set true will cause the
instruction to be defined and available within the parsing, even if there is no definition in
any included OpenQASM 2 file.

Examples:

    Instruct the importer to use Qiskit's :class:`.ECRGate` and :class:`.RZXGate` objects to
    interpret ``gate`` statements that are known to have been created from those same objects
    during OpenQASM 2 export::

        from qiskit import qasm2
        from qiskit.circuit import QuantumCircuit, library

        qc = QuantumCircuit(2)
        qc.ecr(0, 1)
        qc.rzx(0.3, 0, 1)
        qc.rzx(0.7, 1, 0)
        qc.rzx(1.5, 0, 1)
        qc.ecr(1, 0)

        # This output string includes `gate ecr q0, q1 { ... }` and `gate rzx(p) q0, q1 { ... }`
        # statements, since `ecr` and `rzx` are neither built-in gates nor in ``qelib1.inc``.
        dumped = qasm2.dumps(qc)

        # Tell the importer how to interpret the `gate` statements, which we know are safe
        # because we controlled the input OpenQASM 2 source.
        custom = [
            qasm2.CustomInstruction("ecr", 0, 2, library.ECRGate),
            qasm2.CustomInstruction("rzx", 1, 2, library.RZXGate),
        ]

        loaded = qasm2.loads(dumped, custom_instructions=custom)
name
num_params
num_qubits.constructorFbuiltinr    N)__name__
__module____qualname____firstlineno____doc__str__annotations__intr   r
   r,   bool__static_attributes__r    r!   r"   r&   r&   O   s5    'R IOO #{*++GTr!   r&   timec                 \    [        U 5      U :w  a  [        S5      e[        [        U 5      SS9$ )NzCthe custom 'delay' instruction can only accept an integer parameterdt)unit)r4   r   r   )r7   s    r"   _generate_delayr;      s-     4yDcddT&&r!   c                   .   ^  \ rS rSrU 4S jrS rSrU =r$ )_U0Gate   c                 p   > [        U5      U:w  a  [        S5      e[        TU ]  SS[        U5      /5        g )Nz;the number of single-qubit delay lengths must be an integeru0r   )r4   r   super__init__)selfcount	__class__s     r"   rB   _U0Gate.__init__   s2    u:!"_``q3u:,/r!   c                     [        S5      U l        S /U R                  S   -   H  nU R                  R                  S5        M      g )Nr   r   )r   _definitionparamsid)rC   _s     r"   _define_U0Gate._define   s<    )!,$++a.(A" )r!   )rH   )r-   r.   r/   r0   rB   rL   r6   __classcell__rE   s   @r"   r=   r=      s    0
# #r!   r=   u3   u2   u1cxrJ   r@   )r,   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c3sqrtxc4x   delayasinacosatancustom_instructionsc                 
   [        5       n/ n/ n/ nSu  pgU HF  nUR                  UR                  5        UR                  S:X  a  SnM2  UR                  S:X  d  MD  SnMH     U(       d  UR                  [        R
                  5        U(       d  UR                  [        R                  5        [        U 5      n	U	 GH|  n
U
R                  nU[        R                  :X  aD  U
R                  u  pnUR                  [        X\   " U6 U Vs/ s H  oU   PM	     sn5      5        Mh  U[        R                  :X  ah  U
R                  u  pnnnUR                  UR                   U   U45         X\   " U6 nUR                  UU Vs/ s H  oU   PM	     sn5        SSS5        M  U[        R"                  :X  a>  U
R                  u  nnUR                  [        [#        5       UU   4UU   45      5        GM6  U[        R$                  :X  ab  U
R                  u  nnnnUR                  UR                   U   U45         [#        5       nUR                  UUU   4UU   45        SSS5        GM  U[        R&                  :X  a6  UR                  [        ['        5       X:R                  S      45      5        GM  U[        R(                  :X  a\  U
R                  u  nnnUR                  UR                   U   U45         ['        5       nUR                  UUU   45        SSS5        GMf  U[        R*                  :X  aR  U
R                  S   nUR                  [        [+        [-        U5      5      U Vs/ s H  oU   PM	     sn5      5        GM  U[        R.                  :X  a7  U
R                  u  nn[1        UU5      nUUSS -  nUR3                  U5        GM  U[        R4                  :X  a7  U
R                  u  nn[7        UU5      nUUSS -  nUR3                  U5        GMb  U[        R8                  :X  a2  U
R                  u  nU H  nUR                  [:        U   5        M     GM  U[        R<                  :X  at  U
R                  u  nn/ nU	 H4  nUR                  [        R>                  :X  a    OUR                  U5        M6     UR                  [A        UU[C        U5      U5      5        GM0  U[        RD                  :X  a-  U
R                  u  nnUR                  [G        UU5      5        GMq  [I        SU
 35      e   U$ s  snf s  snf ! , (       d  f       GM  = f! , (       d  f       GM  = f! , (       d  f       GM  = fs  snf )a  Loop through the Rust bytecode iterator `bytecode` producing a
:class:`~qiskit.circuit.QuantumCircuit` instance from it.  All the hard work is done in Rust
space where operations are faster; here, we're just about looping through the instructions as
fast as possible, doing as little calculation as we can in Python space.  The Python-space
components are the vast majority of the runtime.

The "bytecode", and so also this Python function, is very tightly coupled to the output of the
Rust parser.  The bytecode itself is largely defined by Rust; from Python space, the iterator is
over essentially a 2-tuple of `(opcode, operands)`.  The `operands` are fixed by Rust, and
assumed to be correct by this function.

The Rust code is responsible for all validation.  If this function causes any errors to be
raised by Qiskit (except perhaps for some symbolic manipulations of `Parameter` objects), we
should consider that a bug in the Rust code.)FFUTCXNr   zinvalid operation: )%r   appendr+   r(   r   r   CXGateiteropcoder   r	   operands_appendr   ConditionedGateif_testcregsr   ConditionedMeasurer   ConditionedResetr   lenDeclareQregr   add_registerDeclareCregr   SpecialIncludeQELIB1DeclareGateEndDeclareGate_gate_buildertupleDeclareOpaque_opaque_builder
ValueError)bytecoder   qcqubitsclbitsgateshas_uhas_cxcustombcopr   gate_id
parameters	op_qubitsqcregvaluegatequbitclbitmeasureresetr(   sizeregisterindicesindexr*   inner_bcinner_ops                                  r"   from_bytecoder      s   $ 
	BFFE ME%V''(;;#E[[D F & SYYSZZ  
hB  V[[ -/[[*GJJ"5>:#>T]@^T]qT]@^_ v---:<++7GD%RXXd^U34~z2		$I >IqI >? 54 v~~%;;LE5JJ)')fUm5EuGWXYv000(*%E5$RXXd^U34!)		'F5M#3fUm5EF 54 v||#JJ)%'F;;q>4J3LMNv...!#E4RXXd^U34		%&-!12 54 v~~%AIJJ)'#i.*AW`CaW`RS1IW`Cabcv)))JD$&tT2Hhqk!FOOH%v)))JD$(t4Hhqk!FOOH%v,,, JW VE]+ !v)))!{{D* H??f&;&;;)  LLtZuxPQv+++!{{D*LLz:;22$788S T I} A_ !? 54 54 54
 DbsB   T='U?UU5&U4!U-*V U
U	
U*	-
U=	c                   N   ^  \ rS rSrSrU 4S jrS rS	S jrS rU 4S jr	Sr
U =r$ )
_DefinedGatei?  zA gate object defined by a `gate` statement in an OpenQASM 2 program.  This object lazily
binds its parameters to its definition, so it is only synthesized when required.c                 P   > X@l         XPl        [        TU ]  X[	        U5      5        g N)_gates	_bytecoderA   rB   list)rC   r(   r*   rI   r   r   rE   s         r"   rB   _DefinedGate.__init__C  s     !4<8r!   c                   ^  S /T R                   -   Vs/ s H  n[        5       PM     nn[        U5      nT R                   H  nUR                  [
        R                  :X  aY  UR                  u  pVnUR                  [        T R                  U   " U 4S jU 5       6 U Vs/ s H  oU   PM	     sn5      5        Mz  UR                  [
        R                  :X  aQ  UR                  S   nUR                  [        [        [        U5      5      U Vs/ s H  oU   PM	     sn5      5        M  [        SU 35      e   UT l        g s  snf s  snf s  snf )Nc              3   P   >#    U  H  n[        UTR                  5      v   M     g 7fr   )_evaluate_argumentrI   ).0arC   s     r"   	<genexpr>'_DefinedGate._define.<locals>.<genexpr>S  s#     .`[_VW/A!T[[/Q/Q[_s   #&r   z)received invalid bytecode to build gate: )r*   r   r   r   r   r   r	   r   r   r   r   r   r   r   rH   )	rC   rK   r   r   r   r   argsr   r   s	   `        r"   rL   _DefinedGate._defineH  s     %)6DOO#;<#;a%'#;<F#..ByyFKK'+-;;(y

&G,.`[_.`a,56IqI6 fnn,KKN	

&ws9~'>T]@^T]qT]@^_ !#LRD!QRR !  % = 7 A_s   E EEc                 r    USL a  [        S5      e[        R                  " [        U R                  5      US9$ )NFz9unable to avoid copy while creating an array as requested)dtype)r   npasarrayr   
definition)rC   r   copys      r"   	__array___DefinedGate.__array__`  s/    5=XYYzz(4??35AAr!   c                 ^    U R                   U R                  U R                  U R                  4$ r   )r(   r*   rI   r   )rC   s    r"   __getstate___DefinedGate.__getstate__h  s!    		4??DKKIIr!   c                 X   > Uu  p#pE[         TU ]  X#U5        SU l        SU l        XPl        g )Nr    )rA   rB   r   r   rH   )rC   stater(   r*   rI   r   rE   s         r"   __setstate___DefinedGate.__setstate__k  s0    /4,&62%r!   )r   rH   r   )NN)r-   r.   r/   r0   r1   rB   rL   r   r   r   r6   rN   rO   s   @r"   r   r   ?  s)    X9
0BJ& &r!   r   c                     ^ ^^^ UUU U4S jnU$ )ae  Create a gate-builder function of the signature `*params -> Gate` for a gate with a given
`name`.  This produces a `_DefinedGate` class, whose `_define` method runs through the given
`bytecode` using the current list of `known_gates` to interpret the gate indices.

The indirection here is mostly needed to correctly close over `known_gates` and `bytecode`.c            	      4   > [        TTU T[        T5      5      $ r   )r   r   )rI   r   known_gatesr(   r*   s    r"   definer_gate_builder.<locals>.definerz  s    D*fk5?SSr!   r    )r(   r*   r   r   r   s   ```` r"   r   r   s  s    T T Nr!   c                    ^ ^ U U4S jnU$ )zCreate a gate-builder function of the signature `*params -> Gate` for an opaque gate with a
given `name`, which takes the given numbers of qubits.c                     > [        TTU 5      $ r   )r	   )rI   r(   r*   s    r"   r    _opaque_builder.<locals>.definer  s    D*f--r!   r    )r(   r*   r   s   `` r"   r   r     s    . Nr!   c                 r  ^ [        U [        5      (       a  U R                  $ [        U [        5      (       a  TU R                     $ [        U [
        5      (       GaC  [        U R                  T5      nU R                  nU[        R                  :X  a  U* $ U[        R                  :X  a  [        R                  " U5      $ U[        R                  :X  a  [        R                  " U5      $ U[        R                   :X  a  [        R"                  " U5      $ U[        R$                  :X  a  [        R&                  " U5      $ U[        R(                  :X  a  [        R*                  " U5      $ U[        R,                  :X  a  [        R.                  " U5      $ [1        SU 35      e[        U [2        5      (       a  [        U R4                  T5      n[        U R6                  T5      nU R                  nU[8        R:                  :X  a  XE-   $ U[8        R<                  :X  a  XE-
  $ U[8        R>                  :X  a  XE-  $ U[8        R@                  :X  a  XE-  $ U[8        RB                  :X  a  XE-  $ [1        SU 35      e[        U [D        5      (       a#  U RF                  " U4S jU RH                   5       6 $ [1        SU  35      e)zInner recursive function to calculate the value of a mathematical expression given the
concrete values in the `parameters` field.zunhandled unary opcode: zunhandled binary opcode: c              3   <   >#    U  H  n[        UT5      v   M     g 7fr   )r   )r   rX   r   s     r"   r   %_evaluate_argument.<locals>.<genexpr>  s     Y.Q1!Z@@.s   zunhandled expression type: )%
isinstancer   r   r   r   r   r   argumentr   r   NegateCosmathcosExpexpLnlogSinsinSqrtsqrtTantanr   r   leftrightr   AddSubtractMultiplyDividePowerr   callable	arguments)exprr   innerr   r   r   s    `    r"   r   r     s    $%%zz$%%$**%%$	"""4==*=['''6M[__$88E?"[__$88E?"[^^#88E?"[__$88E?"[%%%99U##[__$88E?"3F8<==$
##!$))Z8"4::z:\%%%<\***<\***<\(((<\''';4VH=>>$
##}}Y$..YZZ
24&9
::r!   )]r1   dataclassesr   typingr   r   numpyr   qiskit.circuitr   r   r   r   r	   r
   r   r   r   r   r   r   r   qiskit.quantum_infor   qiskit._accelerate.qasm2r   r   r   r   r   r   r   r   r   
exceptionsr   U3GateU2GateU1Gater   XGateYGateZGateHGateSGateSdgGateTGateTdgGateRXGateRYGateRZGateCZGateCYGateCHGateCCXGateCRZGateCU1GateCU3Gater   	dataclassr&   floatr;   r=   IGater   	PhaseGateSXGateSXdgGateSwapGate	CSwapGateCRXGateCRYGate
CPhaseGateCSXGateCUGateRXXGateRZZGateRCCXGateRC3XGateC3XGateC3SXGateC4XGateLEGACY_CUSTOM_INSTRUCTIONSr}   r~   r   LEGACY_CUSTOM_CLASSICALr   r   r   r   r   r    r!   r"   <module>r0     s   V   %     )
 
 
 (
 JJJJJJJJ IIIIIIIIIIKKIIKKJJJJJJJJJJJJKKKKKKKK3
: d#0 0 $0f'% '	#d 	#-dAq#**--dAq#**-- dAq#**-- dAq#**-	- dAq#)),- dAq'48- c1aD9- c1a=- c1a+- c1a+- c1a+- c1a+- c1a+- eQ3;;/-  c1a+!-" eQ3;;/#-$ dAq#**-%-& dAq#**-'-( dAq#**-)-* dAq#**d;+-, faCLL$?--. dAq#**-/-0 dAq#**-1-2 faCLL$?3-4 dAq#**-5-6 eQ3;;/7-8 gq!S]]DA9-: eQ3;;=;-< eQ3;;==-> eQ3;;/?-@ eQ3;;/A-B dAq#..$?C-D eQ3;;/E-F eQ3;;=G-H dAq#**d;I-J eQ3;;=K-L eQ3;;=M-N faCLL$?O-P faCLL$?Q-R eQ3;;=S-T iAs||TBU-V eQ3;;=W-X gq!_5Y- ` FAtyy)FAtyy)FAtyy) n:K1L nb1&4 1&h
*;r!   