+
    i                         R t R.t^ RIHt ^ RIHtHtHtHtH	t	H
t
HtHtHtHt ^ RIHt R tR tR tR tR	 tR
 tR tR tR tR t ! R R4      tR# )a  

qasm.py - Functions to parse a set of qasm commands into a SymPy Circuit.

Examples taken from Chuang's page: https://web.archive.org/web/20220120121541/https://www.media.mit.edu/quanta/qasm2circ/

The code returns a circuit and an associated list of labels.

>>> from sympy.physics.quantum.qasm import Qasm
>>> q = Qasm('qubit q0', 'qubit q1', 'h q0', 'cnot q0,q1')
>>> q.get_circuit()
CNOT(1,0)*H(1)

>>> q = Qasm('qubit q0', 'qubit q1', 'cnot q0,q1', 'cnot q1,q0', 'cnot q0,q1')
>>> q.get_circuit()
CNOT(1,0)*CNOT(0,1)*CNOT(1,0)
Qasm)prod)
HCNOTXZCGateCGateSSWAPSTCPHASE)Mzc                 0    \        V P                  4       !  # N)r   
splitlines)liness   &z/Users/tonyclaw/.openclaw/workspace/skills/math-calculator/venv/lib/python3.14/site-packages/sympy/physics/quantum/qasm.py	read_qasmr      s    !!#$$    c                 B    \        \        V 4      P                  4       !  # r   )r   open	readlines)filenames   &r   read_qasm_filer      s    h))+,,r   c                     W,
          ^,
          # )zReorder qubit indices from largest to smallest.

>>> from sympy.physics.quantum.qasm import flip_index
>>> flip_index(0, 2)
1
>>> flip_index(1, 2)
0
 )ins   &&r   
flip_indexr   "   s     3q5Lr   c                D    RV 9  d   V # V P                  R4      ^ ,          # )zRemove everything following comment # characters in line.

>>> from sympy.physics.quantum.qasm import trim
>>> trim('nothing happens here')
'nothing happens here'
>>> trim('something #happens here')
'something '
#)split)lines   &r   trimr$   -   s#     $::c?1r   c                N    \        V4      p\        VP                  V 4      V4      # )zGet qubit labels from the rest of the line,and return indices

>>> from sympy.physics.quantum.qasm import get_index
>>> get_index('q0', ['q0', 'q1'])
1
>>> get_index('q1', ['q0', 'q1'])
0
)lenr   index)targetlabelsnqs   && r   	get_indexr+   :   s#     
VBfll6*B//r   c                 B    V  Uu. uF  p\        W!4      NK  	  up# u upi r   )r+   )targetsr)   ts   && r   get_indicesr/   F   s    *12'QIa '222s   c              #   n   "   V  F*  p\        V4      pVP                  4       '       d   K&  Vx  K,  	  R # 5ir   )r$   isspace)argsr#   s   & r   nonblankr3   I   s0     Dz<<>>
	 
 s   35c                     V P                  4       pR P                  VR,          4      p\        V^ ,          4      VP                  R4       Uu. uF  q3P                  4       NK  	  up3# u upi ) :   NN,)r"   join
fixcommandstrip)r#   wordsrestss   &   r   	fullsplitr>   Q   sQ    JJLE88E"IDeAhTZZ_!E_'')_!EEE!Es   A)c                t    R.pV P                  4       p V F  pV P                  VR4      p K  	  V R8X  d   R# V # )zkFix Qasm command names.

Remove all of forbidden characters from command c, and
replace 'def' with 'qdef'.
- defqdef)lowerreplace)cforbidden_characterschars   &  r   r9   r9   V   s@      5		A$IIdB %EzHr   c                N    V P                  RR4      p V P                  RR4      p V # )zReplace explicit quotes in a string.

>>> from sympy.physics.quantum.qasm import stripquotes
>>> stripquotes("'S'") == 'S'
True
>>> stripquotes('"S"') == 'S'
True
>>> stripquotes('S') == 'S'
True
"rA   ')rE   )r=   s   &r   stripquotesrL   d   s)     	
		#rA			#rAHr   c                      a  ] tR t^st o RtR tR tR tR tR t	RR lt
R	 tR
 tR tR tR tR tR tR tR tR tR tR tR tR tR tR tR tRtV tR# )r   a6  Class to form objects from Qasm lines

>>> from sympy.physics.quantum.qasm import Qasm
>>> q = Qasm('qubit q0', 'qubit q1', 'h q0', 'cnot q0,q1')
>>> q.get_circuit()
CNOT(1,0)*H(1)
>>> q = Qasm('qubit q0', 'qubit q1', 'cnot q0,q1', 'cnot q1,q0', 'cnot q0,q1')
>>> q.get_circuit()
CNOT(1,0)*CNOT(0,1)*CNOT(1,0)
c                j    / V n         . V n        . V n        / V n        V P                  ! V!   W n        R # r   )defscircuitr)   initsaddkwargs)selfr2   rS   s   &*,r   __init__Qasm.__init__~   s.    	
$r   c                   \        V4       F  p\        V4      w  r4V P                  P                  V4      '       d   V P                  P                  V4      pV P	                  V4      p\        V4      ^8X  d+   V P                  P                  V! V^ ,          4      4       K  V P                  P                  V! VRR VR,          4      4       K  \        W4      '       d   \        W4      pV! V!   K  \        RV,          4       K  	  R# )r6   Nz!Function %s not defined. Skipping)r3   r>   rO   getindicesr&   rP   appendhasattrgetattrprint)rT   r   r#   commandr<   functionrZ   s   &*     r   rR   Qasm.add   s    UOD%dOMGyy}}W%%99==1,,t,w<1$LL''(<=LL''"wr{(KL''"41$9GCD $r   c                >    \        \        V P                  4      4      # r   )r   reversedrP   rT   s   &r   get_circuitQasm.get_circuit   s    HT\\*++r   c                >    \        \        V P                  4      4      # r   )listrc   r)   rd   s   &r   
get_labelsQasm.get_labels   s    HT[[)**r   c                    ^ RI Hp V P                  4       V P                  4       r2V! V\	        V4      W0P
                  R7       R# )    )CircuitPlot)r)   rQ   N)!sympy.physics.quantum.circuitplotrm   re   ri   r&   rQ   )rT   rm   rP   r)   s   &   r   plot	Qasm.plot   s0    A**,doo.?GS[zzJr   Nc                l    V P                   P                  V4       V'       d   W P                  V&   R # R # r   )r)   r[   rQ   )rT   arginits   &&&r   qubit
Qasm.qubit   s#    34C4r   c                ,    \        WP                  4      # r   )r/   r)   rT   r2   s   &&r   rZ   Qasm.indices   s    4--r   c                ,    \        WP                  4      # r   )r+   r)   rT   rr   s   &&r   r'   
Qasm.index   s    kk**r   c                    R # r   r   rw   s   &*r   nopQasm.nop   s    r   c                l    V P                   P                  \        V P                  V4      4      4       R # r   )rP   r[   r   r'   rz   s   &&r   xQasm.x   !    Adjjo./r   c                l    V P                   P                  \        V P                  V4      4      4       R # r   )rP   r[   r   r'   rz   s   &&r   zQasm.z   r   r   c                l    V P                   P                  \        V P                  V4      4      4       R # r   )rP   r[   r   r'   rz   s   &&r   hQasm.h   r   r   c                l    V P                   P                  \        V P                  V4      4      4       R # r   )rP   r[   r   r'   rz   s   &&r   r=   Qasm.s   r   r   c                l    V P                   P                  \        V P                  V4      4      4       R # r   )rP   r[   r   r'   rz   s   &&r   r.   Qasm.t   r   r   c                l    V P                   P                  \        V P                  V4      4      4       R # r   )rP   r[   r   r'   rz   s   &&r   measureQasm.measure   s!    Btzz#/0r   c                j    V P                   P                  \        V P                  W.4      !  4       R # r   )rP   r[   r   rZ   rT   a1a2s   &&&r   cnot	Qasm.cnot   $    D$,,x"89:r   c                j    V P                   P                  \        V P                  W.4      !  4       R # r   )rP   r[   r
   rZ   r   s   &&&r   swap	Qasm.swap   r   r   c                j    V P                   P                  \        V P                  W.4      !  4       R # r   )rP   r[   r   rZ   r   s   &&&r   cphaseQasm.cphase   s$    FDLL"$:;<r   c                    V P                  WV.4      w  rEpV P                  P                  \        WE3\	        V4      4      4       R # r   )rZ   rP   r[   r	   r   )rT   r   r   a3i1i2i3s   &&&&   r   toffoliQasm.toffoli   s7    \\22,/
FB8QrU34r   c                    V P                  W.4      w  r4V P                  P                  \        V\	        V4      4      4       R # r   )rZ   rP   r[   r   r   rT   r   r   fifjs   &&&  r   cxQasm.cx   1    rh'E"ae,-r   c                    V P                  W.4      w  r4V P                  P                  \        V\	        V4      4      4       R # r   )rZ   rP   r[   r   r   r   s   &&&  r   czQasm.cz   r   r   c                    \        R V4       R# )z$defbox not supported yet. Skipping: N)r^   rw   s   &*r   defboxQasm.defbox   s    4d;r   c                    ^ RI HpHp \        V4      p\	        V4      p\        V4      pV^ 8  d   V! V4      V P                  V&   R# V! V4      V P                  V&   R# )rl   )CreateOneQubitGateCreateCGateN)rn   r   r   intr9   rL   rO   )rT   name	ncontrolssymbolr   r   r_   s   &&&&   r   rC   	Qasm.qdef   sO    U	N	T"V$q=!,V!4DIIg!3F!;DIIgr   )rP   rO   rQ   rS   r)   r   )__name__
__module____qualname____firstlineno____doc__rU   rR   re   ri   ro   rt   rZ   r'   r}   r   r   r   r=   r.   r   r   r   r   r   r   r   r   rC   __static_attributes____classdictcell__)__classdict__s   @r   r   r   s   s     	E ,+K
(.+000001;;=5..<< <r   N)r   __all__mathr   sympy.physics.quantum.gater   r   r   r   r   r	   r
   r   r   r   rn   r   r   r   r   r$   r+   r/   r3   r>   r9   rL   r   r   r   r   <module>r      sf   &   V V V 0%-	
03F
m< m<r   