
    g0                        d dl mZ d dlmZmZmZmZ d dlmZm	Z	 d dl
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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* d d
l+m,Z, d dl-m.Z. d dl/m0Z0m1Z1m2Z2m3Z3 d dl4m5Z5 d dl6m7Z7 d dl8m9Z9 d Z:d Z;d Z<d Z=d Z>d Z?d Z@d ZAd ZBd ZCd ZDd ZEd ZFd ZGd ZHd ZId  ZJd! ZKd" ZLd# ZMd$ ZNd% ZOd& ZPd' ZQd( ZRd) ZSd*S )+    )Mul)IIntegerRationalpi)Wildsymbols)exp)sqrt)MatrixImmutableMatrix)XGateYGateZGaterandom_circuitCNOTIdentityGateHXYSTZSwapGate	gate_simp	gate_sortCNotGateTGateHadamardGate	PhaseGateUGateCGate)
Commutator)AntiCommutator)	represent)qapply)QubitIntQubitqubit_to_matrixmatrix_to_qubit)matrix_to_zero)	sqrt2_inv)Daggerc                     t          d          } | j        dk    sJ | j        dk    sJ t          d          }t          d          }t          |          }t          |          }t          |          }||k    sJ ||k    sJ ||k    sJ t	          ||          }t	          ||          }t	          ||          }||k    sJ ||k    sJ ||k    sJ dS )zTest a basic gate.      i0i1N)r   
min_qubitsnqubitsr   r   )	hr1   r2   h0_w1h0_w2h1_w1
cnot_10_w1
cnot_10_w2
cnot_01_w1s	            a/var/www/html/ai-engine/env/lib/python3.11/site-packages/sympy/physics/quantum/tests/test_gate.py	test_gater=      s    QA<19>>>>	dB	dBEEEE>>>>E>>>>E>>>>b"Jb"Jb"J##############    c                     t          d          \  } }}}t          | |g||gg          }t          d|          }t          |d          |k    sJ t	          |t          d          z            | t          d          z  |t          d          z  z   k    sJ t	          |t          d          z            |t          d          z  |t          d          z  z   k    sJ t          d|          }t          |d          }t          d	          D ]S}|t          t          |d                    z  t          t	          |t          |d          z                      k    sJ Td S )
Na,b,c,dr   r/   r4   01r/   r0      )	r	   r   r!   r%   r&   r'   ranger)   r(   )	abcduMatu1u2u2Repis	            r<   
test_UGaterQ   /   sk   ##JAq!QAq6Aq6"##D 
tT		BR###t++++"U3ZZ-  AeCjjL1U3ZZ<$?????"U3ZZ-  AeCjjL1U3ZZ<$????? 
tT		Bb!$$$E1XX 7 7_Xa^^444F2hq!nn#455667 7 7 7 77 7r>   c                  H   t          g dg dg dg dg          } t          t          dt          d                    d          | k    sJ t          d	t          d                    }t          |d
          t          g dg dg dg dg dg dg dg dg          k    sJ t          dt          d                    }t	          |t          d          z            t          t          |t          d          z  d                    k    sJ t	          |t          d          z            t          t          |t          d          z  d                    k    sJ t          dt          d                    }t          dddddt           fddt          dff          }t          |	                                d          |k    sJ t          dt          d                    }t          d          }t	          |t          d          z            t          d           k    sJ t          t          |t          d          z  d                    t          d           k    sJ t          |	                                d          |k    sJ t          dt          d                    }t	          |t          d          z            t          t          d          z  k    sJ t          t          |t          d          z  d                    t          t          d          z  k    sJ t          |          |k    sJ t          |d          t          |          k    sJ t          |          |                                k    sJ t          |          |k    sJ t          |          |                                k    sJ t          |          t          |d          k    sJ t          |d          |                                k    sJ dS )zTest the general CGate.r/   r   r   r   r   r/   r   r   r   r   r   r/   r   r   r/   r   r/   r   r0   rB   )r/   r0      )r/   r   r   r   r   r   r   r   )r   r/   r   r   r   r   r   r   )r   r   r/   r   r   r   r   r   )r   r   r   r/   r   r   r   r   )r   r   r   r   r/   r   r   r   )r   r   r   r   r   r/   r   r   )r   r   r   r   r   r   r   r/   )r   r   r   r   r   r   r/   r   )rW   r   1001rF   0000)rS   rT   rV   )r   r   r   11rZ   N)r   r%   r"   r   r&   r'   r*   r   r   	decomposer   r    r-   powinverse)
CNOTMatrixToffoliGateCYGateCYGate_matrixCZGateCZGate_matrix
CPhaseGates          r<   
test_cgaterf   A   s_    	|||\\\<<<@B BJU1eAhh''333zAAAA a))K[!,,,%%%'?'?'?AYAYAY666 9 9 9***	 	    a))K+eFmm+,,	+eFmm";QGGGHHI I I I+eFmm+,,	+eFmm";QGGGHHI I I I 1eAhhF	|aAr]Q1aLAC CM V%%''333}DDDD1eAhhFAC CM&t$%%%++55559VE$KK%7CCCDD	t    V%%''333}DDDDq)A,,''J*U4[[())	%++   9Zd%;QGGGHH	%++    &>>V####vq>>VF^^++++&>>V^^------*++++*!3!3!5!55555*Z!4!44444z2*"4"4"6"6666666r>   c                     t          d          \  } }}}t          | |g||gg          }t          g dg ddd| |gdd||gg          }t          d|          }t          d|          }t	          |d          |k    sJ t          |t          d	          z            | t          d	          z  |t          d
          z  z   k    sJ t          |t          d
          z            |t          d	          z  |t          d
          z  z   k    sJ t          |t          d          z            t          d          k    sJ t          |t          d          z            t          d          k    sJ t          d|          }t	          |d          }	t          d          D ]S}
|	t          t          |
d                    z  t          t          |t          |
d          z                      k    sJ Td S )Nr@   rS   rT   r   rA   r/   r0   rB   10r[   0100rE   rF   )
r	   r   r!   r"   r%   r&   r'   rG   r)   r(   )rH   rI   rJ   rK   rL   cMatrM   cu1rN   rO   rP   s              r<   test_UGate_CGate_comborm   v   s   ##JAq!QAq6Aq6"##D<<<1a|aAq\JKKD 
tT		B
2,,CS!$$$,,,,#eDkk/""admadm&CCCCC#eDkk/""admadm&CCCCC#eDkk/""eDkk1111#eDkk/""eDkk1111 
tT		Bb!$$$E1XX 7 7_Xa^^444F2hq!nn#455667 7 7 7 77 7r>   c            	         t          d          \  } }}}t          | |g||gg          }t          | |dz   ddg|dz   |ddgdd| |dz   gdd|dz   |gg          }t          d          t	          d|          z   }t          |d          |k    sJ t          dt          d          z  dt          d          z  gt          t          d          z  t           t          d          z  gg          }t          t          dd          t          dz  z   t          dd          t          dz  z
  gt          dd          t          dz  z
  t          dd          t          dz  z   gg          }t          ddgdt          gg          }	t	          d|          }
t          t          d          |
z  d          |k    sJ t          |
t          d          z  d          |	k    sJ d S )Nzv w f gr/   r   r0   rB   )
r	   r   r   r   r!   r%   r   r   r   r   )vwfguMat1cMat1rM   uMat2cMat2_1cMat2_2rN   s              r<   test_UGate_OneQubitGate_comborx      s   ##JAq!QaVaV,--EQAq!$q1uaA&6Aq!a%8H1aQRUVQVXYJZ[\\E	
1a	BR###u,,,,aQi4773aQi!DGG5LMNNEx1~~!+Xa^^ac-AB1~~!+Xa^^ac-ABD E EGq!fq!f%&&G	q%BQqTT"Wa(((G3333R!Wa(((G333333r>   c                      t          d          t          d          z  } t          | d          }|t          t          t          ddg          k    sJ dS )z-Test the representation of the hadamard gate.r   rj   r0   rB   N)r   r'   r%   r   r,   circuitanswers     r<   test_represent_hadamardr}      sR    1ooeDkk)Gw***FVY	1a899999999r>   c                      t          d          t          d          z  } t          | d          }t          g d          |k    sJ dS )z&Test the representation of the X gate.r   rj   r0   rB   rT   N)r   r'   r%   r   rz   s     r<   test_represent_xgater      M    AhhuT{{"Gw***F,,,6))))))r>   c                      t          d          t          d          z  } t          | d          }|d         dk    r)|d         t          k    r|d         dk    r|d         dk    sJ dS )z&Test the representation of the Y gate.r   rj   r0   rB   r/   rW   N)r   r'   r%   r   rz   s     r<   test_represent_ygater      sd    AhhuT{{"Gw***F!9>>fQi1nnq	Q6!9>>>>>>r>   c                      t          d          t          d          z  } t          | d          }t          g d          |k    sJ dS )z&Test the representation of the Z gate.r   rj   r0   rB   rS   N)r   r'   r%   r   rz   s     r<   test_represent_zgater      r   r>   c                      t          d          t          d          z  } t          | d          }t          dt          ddg          |k    sJ dS )z&Test the representation of the S gate.r   ri   r0   rB   N)r    r'   r%   r   r   rz   s     r<   test_represent_phasegater      sQ    ll5;;&Gw***F1aA,6))))))r>   c                      t          d          t          d          z  } t          dt          t          t
          z  dz            ddg          t          | d          k    sJ dS )z&Test the representation of the T gate.r   ri   rF   r0   rB   N)r   r'   r   r
   r   r   r%   r{   s    r<   test_represent_tgater      s[    AhhuT{{"G1c!B$q&kk1a())Yw-J-J-JJJJJJJr>   c                  L   t          d          t          d          z  t          d          z  t          d          z  t	          d          z  } t          | d          }t          t          t          d          z  t          t          d          z  ddg          |k    sJ dS )z$Test a compound gate representation.r   rj   r0   rB   N)	r   r   r   r   r'   r%   r   r   r   rz   s     r<   test_compound_gatesr      s    AhhuQxxa(a8tDGw***F1T!WW9aQiA.//6999999r>   c                     t          dd          } t          | d          t          g dg dg dg dg          k    sJ | t          d	          z  } t	          t          | d
                    t          |           k    sJ t          dd          } t          |           | k    sJ t          t          |                     | k    sJ | | z  dk    sJ dS )zTest the CNOT gate.r/   r   r0   rB   rS   rT   rU   rV   111rW   N)r   r%   r   r'   r*   r&   r-   r   s    r<   test_cnot_gater      s    q!nnGWa(((lllLLL,,,GHHI I I IeEll"G9Wa88899w    q!nnG'??g%%%%&//""g----7?ar>   c                  	   t           t          t          t          t          t
          fD ]Y} t           | d           | d          z   | d          z             | d           | d          z   | d          z  k    sJ Zt          t          d          t          d          dz  z  t          dd          z  t          d          z  t          d          z            }|t          d          dz  t          dd          z  t          d          z  t          d          dz  z  k    sJ t          t          d          t          d          z            t          d           t          d          z  k    sJ t          t          d          t          d          dz  z            t          d          dz  t          d          z  k    sJ t          t          d          t          d          z            t          d           t          d          z  k    sJ t          t          d          t          d          z            t          d           t          d          z  k    sJ t          t          d          t          d          z            t          d           t          d          z  k    sJ t          t          d          t	          d          z            t	          d          t          d          z  k    sJ t          t          d          t	          d          z            t	          d          t          d          z  k    sJ t          t          d          t          d          z            t          d          t          d          z  k    sJ t          t          d          t          dd          z            t          dd          t          d          z  k    sJ t          t	          d          t          dd          z            t          dd          t	          d          z  k    sJ t          t          d          t          dd          z            t          dd          t          d          z  k    sJ t          t          d          t          dd          z            t          dd          t          d          z  k    sJ dS )zTest gate_sort.r0   r/   r   N)r   r   r   r   r   r   r   r   )rr   es     r<   test_gate_sortr      s   Aq!Q ; ;1aadd11Q44((AAaDD1IaaddN:::::!A$$qttQw,tAqzz)!A$$.qtt344A!aQ

"1Q44'!a/////QqTT!A$$YAaDD51:----QqTT!A$$'\""addAgaddl2222QqTT!A$$YAaDD51:----QqTT!A$$YAaDD51:----QqTT!A$$YAaDD51:----QqTT!A$$Y1Q44!9,,,,QqTT!A$$Y1Q44!9,,,,QqTT!A$$Y1Q44!9,,,,QqTT$q!**_%%aAaDD8888QqTT$q!**_%%aAaDD8888QqTT$q!**_%%aAaDD8888QqTT$q!**_%%aAaDD888888r>   c                  X   t          d          t          d          z  t          d          dz  z  t          dd          z  t          d          dz  z  t          d          z  t          d          dz  z  t	          d          dz  z  } t          |           t          d          t          dd          z  t	          d          z  t          d          z  t          d          z  k    sJ t          t          d          t          d          z            dk    sJ t          t          d          t          d          z            dk    sJ t          t          d          t          d          z            dk    sJ t          t          d          t          d          z            dk    sJ t          t          d          t          d          z            t	          d          k    sJ t          t	          d          t	          d          z            t          d          k    sJ t          t          d                    t          d          k    sJ t          t          d          dz  t          d          dz  z             t          d          k    sJ dS )zTest gate_simp.r   r/   r0   rW   rF   N)	r   r   r   r   r   r   r   r   r   )r   s    r<   test_gate_simpr      s   	!QqTT	!A$$'$q!**$QqTT1W,QqTT1!A$$'9!A$$'AAQ<<1Q44Q

?1Q44/!4QqTT99999QqTT!A$$Y1$$$$QqTT!A$$Y1$$$$QqTT!A$$Y1$$$$QqTT!A$$Y1$$$$QqTT!A$$Y1Q44''''QqTT!A$$Y1Q44''''WQZZ  GAJJ....QqTT1WqttQw&''71::555555r>   c            	         t          d          } t          t          dd                                          d          | k    sJ t	          t          dd          t          d          z            t          d          k    sJ d	}t          |          D ]h}t          |          D ]V}t          t          ||          |          t          t          ||                                          |          k    sJ Wid
S )zTest the SWAP gate.)rS   rV   rT   rU   r/   r   r0   rB   rW   00101000rF   N)r   r%   r   r\   r&   r'   rG   )swap_gate_matrixr4   rP   js       r<   test_swap_gater     s   @B BXa^^--//;;;?OOOOO(1a..v.//5==@@@@G7^^ G Gq 	G 	GAXa^^W===(1a..2244gFFFG G G G G	GG Gr>   c            	      .   t           t          t          t          t          t
          t          fD ]} t           t          t          t          t          t
          t          fD ]}t           | d           |d                    }t          t          |dd                    }t          t          |
                                dd                    }||k    sJ t           | d           |d                    }|
                                dk    sJ dS )z-Test single qubit gate commutation relations.r   r/   sympyr4   formatN)r   r   r   r   r   r   r   r#   r+   r%   doitg1g2r   rH   rI   s        r<   test_one_qubit_commutatorsr     s    Q1aA. ! !Aq!Q2 	! 	!B22a55""Q%%((AyAgFFFGGAy1WMMMNNA666622a55""Q%%((A6688q=====	!! !r>   c            	      ~   t           t          t          t          t          fD ]} t           t          t          t          t          fD ]}t           | d           |d                    }t          t          |dd                    }t          t          |                                dd                    }||k    sJ t           | d           |d                    }t          t          |dd                    }t          t          |                                dd                    }||k    sJ dS )z1Test single qubit gate anticommutation relations.r   r/   r   r   r0   N)	r   r   r   r   r   r$   r+   r%   r   r   s        r<   test_one_qubit_anticommutatorsr     s   Q1a( 	 	Aq!, 	 	Brr!uubbee,,AyAgFFFGGAy1WMMMNNA6666rr!uubbee,,AyAgFFFGGAy1WMMMNNA66666		 	r>   c                  >   t          t          dd          t          d                                                    dk    sJ t          t          dd          t	          d                                                    dk    sJ t          t          dd          t          d                                                    dk    sJ t          t          dd          t          d                                                    dk    sJ t          t          dd          t          dd                                                    dk    sJ t          t          dd          t          dd                                                    dk    sJ t          t          dd          t          dd                                                    dk    sJ t          t          dd          t          dd                                                    dk    sJ dS )z)Test commutators of involving CNOT gates.r   r/   r0   N)r#   r   r   r   r   r   r    r>   r<   test_cnot_commutatorsr   -  s   d1ajj!A$$'',,..!3333d1ajj!A$$'',,..!3333d1ajj!A$$'',,..!3333d1ajj!A$$'',,..!3333d1ajj$q!**--22449999d1ajj$q!**--22449999d1ajj$q!**--22449999d1ajj$q!**--2244999999r>   c                      t          dd          } t          | t                    sJ t          | d          }|j        dk    sJ t          |t
                    sJ d S )N
   rW   rB   )   r   )r   
isinstancer   r%   shaper   )rJ   ms     r<   test_random_circuitr   9  sh    r1Aa!QA7fa       r>   c                  T    t          dd          } t          |           }| |k    sJ d S Nr/   r0   r   r-   xx_daggers     r<   test_hermitian_XGater   A  +    aAayyHMMMMMMr>   c                  T    t          dd          } t          |           }| |k    sJ d S r   r   r-   yy_daggers     r<   test_hermitian_YGater   H  r   r>   c                  T    t          dd          } t          |           }| |k    sJ d S r   r   r-   zz_daggers     r<   test_hermitian_ZGater   O  r   r>   c                  Z    t          dd          } t          |           }| |z  dk    sJ d S r   r   r   s     r<   test_unitary_XGater   V  /    aAayyHhJ!OOOOOOr>   c                  Z    t          dd          } t          |           }| |z  dk    sJ d S r   r   r   s     r<   test_unitary_YGater   ]  r   r>   c                  Z    t          dd          } t          |           }| |z  dk    sJ d S r   r   r   s     r<   test_unitary_ZGater   d  r   r>   N)Tsympy.core.mulr   sympy.core.numbersr   r   r   r   sympy.core.symbolr   r	   &sympy.functions.elementary.exponentialr
   (sympy.functions.elementary.miscellaneousr   sympy.matricesr   r   sympy.physics.quantum.gater   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"    sympy.physics.quantum.commutatorr#   $sympy.physics.quantum.anticommutatorr$   sympy.physics.quantum.representr%   sympy.physics.quantum.qapplyr&   sympy.physics.quantum.qubitr'   r(   r)   r*   !sympy.physics.quantum.matrixutilsr+   !sympy.physics.quantum.matrixcacher,   sympy.physics.quantumr-   r=   rQ   rf   rm   rx   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r>   r<   <module>r      s         9 9 9 9 9 9 9 9 9 9 9 9 - - - - - - - - 6 6 6 6 6 6 9 9 9 9 9 9 2 2 2 2 2 2 2 2@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ 8 7 7 7 7 7 ? ? ? ? ? ? 5 5 5 5 5 5 / / / / / /            < < < < < < 7 7 7 7 7 7 ( ( ( ( ( ($ $ $27 7 7$27 27 27j7 7 7*4 4 4: : :* * ** * ** * ** * *K K K: : :     9 9 986 6 6
G 
G 
G
! 
! 
!  	: 	: 	:! ! !              r>   