o
    g0                     @   s  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d Z:dd Z;dd Z<dd Z=dd Z>dd Z?dd Z@dd ZAd d! ZBd"d# ZCd$d% ZDd&d' ZEd(d) ZFd*d+ ZGd,d- ZHd.d/ ZId0d1 ZJd2d3 ZKd4d5 ZLd6d7 ZMd8d9 ZNd:d; ZOd<d= ZPd>d? ZQd@dA ZRdBdC ZSdDS )E    )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            	      C   s   t d} | jdksJ | jdksJ td}td}t |}t |}t |}||ks,J ||ks2J ||ks8J t||}t||}t||}||ksMJ ||ksSJ ||ksYJ dS )zTest a basic gate.      i0i1N)r   
min_qubitsnqubitsr   r   )	hr/   r0   h0_w1h0_w2h1_w1
cnot_10_w1
cnot_10_w2
cnot_01_w1 r:   \/var/www/visachat/venv/lib/python3.10/site-packages/sympy/physics/quantum/tests/test_gate.py	test_gate   s"   


r<   c            	      C   s   t d\} }}}t| |g||gg}td|}t|dd|ks!J t|td | td |td  ks7J t|td |td |td  ksMJ td|}t|dd}td	D ]}|tt|d tt|t|d kstJ q\d S )
Na,b,c,dr   r-   r2   01r-   r.      )	r   r   r    r$   r%   r&   ranger(   r'   )	abcduMatu1u2u2Repir:   r:   r;   
test_UGate/   s   
,,

rN   c                  C   s  t g dg dg dg dg} ttdtddd| ksJ td	td}t|d
dt g dg dg dg dg dg dg dg dgksJJ tdtd}t|td tt|td ddksgJ t|td tt|td ddks}J tdtd}t dddddt fddtdff}t|	 dd|ksJ tdt
d}t d}t|td td ksJ tt|td ddtd ksJ t|	 dd|ksJ tdtd}t|td ttd ksJ tt|td ddttd 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sFJ t|d| ksRJ 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   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   r   r   r-   r   )rS   r   1001rC   0000)rO   rP   rR   )r   r   r   11rV   N)r   r$   r!   r   r%   r&   r)   r   r   	decomposer   r   r,   powinverse)
CNOTMatrixToffoliGateCYGateCYGate_matrixCZGateCZGate_matrix
CPhaseGater:   r:   r;   
test_cgateA   sb   



rb   c                  C   sX  t d\} }}}t| |g||gg}tg dg ddd| |gdd||gg}td|}td|}t|dd|ks:J t|td	 | td	 |td
  ksPJ t|td
 |td	 |td
  ksfJ t|td tdkstJ t|td tdksJ td|}t|dd}	tdD ]}
|	tt	|
d tt|t	|
d ksJ qd S )Nr=   rO   rP   r   r>   r-   r.   r?   10rW   0100rB   rC   )
r   r   r    r!   r$   r%   r&   rD   r(   r'   )rE   rF   rG   rH   rI   cMatrJ   cu1rK   rL   rM   r:   r:   r;   test_UGate_CGate_combov   s"   (

,,

rh   c                  C   sf  t d\} }}}t| |g||gg}t| |d ddg|d |ddgdd| |d gdd|d |gg}tdtd| }t|dd|ksEJ tdtd dtd gttd t td gg}ttddtd  tddtd  gtddtd  tddtd  gg}tddgdtgg}	td|}
tt	d|
 dd|ksJ t|
t	d dd|	ksJ d S )Nzv w f gr-   r   r.   r?   )
r   r   r   r   r    r$   r
   r   r   r   )vwfguMat1cMat1rJ   uMat2cMat2_1cMat2_2rK   r:   r:   r;   test_UGate_OneQubitGate_combo   s   @6$"
 rr   c                  C   s8   t dtd } t| dd}|tttddgksJ dS )z-Test the representation of the hadamard gate.r   re   r.   r?   N)r   r&   r$   r   r+   circuitanswerr:   r:   r;   test_represent_hadamard   s   rv   c                  C   4   t dtd } t| dd}tg d|ksJ dS )z&Test the representation of the X gate.r   re   r.   r?   rP   N)r   r&   r$   r   rs   r:   r:   r;   test_represent_xgate      rx   c                  C   sT   t dtd } t| d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   re   r.   r?   r-   rS   N)r   r&   r$   r   rs   r:   r:   r;   test_represent_ygate   s    rz   c                  C   rw   )z&Test the representation of the Z gate.r   re   r.   r?   rO   N)r   r&   r$   r   rs   r:   r:   r;   test_represent_zgate   ry   r{   c                  C   s8   t dtd } t| dd}tdtddg|ksJ dS )z&Test the representation of the S gate.r   rd   r.   r?   N)r   r&   r$   r   r   rs   r:   r:   r;   test_represent_phasegate   s   r|   c                  C   s@   t dtd } tdttt d ddgt| ddksJ dS )z&Test the representation of the T gate.r   rd   rC   r.   r?   N)r   r&   r   r	   r   r   r$   rt   r:   r:   r;   test_represent_tgate   s   0r~   c                  C   s`   t dtd td td td } t| dd}tttd ttd ddg|ks.J dS )z$Test a compound gate representation.r   re   r.   r?   N)	r   r   r   r   r&   r$   r   r   r
   rs   r:   r:   r;   test_compound_gates   s   (,r   c                  C   s   t dd} t| ddtg dg dg dg dgksJ | td	 } tt| d
dt| ks1J t dd} t| | ks>J tt| | ksHJ | |  dksPJ dS )zTest the CNOT gate.r-   r   r.   r?   rO   rP   rQ   rR   111rS   N)r   r$   r   r&   r)   r%   r,   r}   r:   r:   r;   test_cnot_gate   s   


r   c                  C   s  t tttttfD ]} t| d| d | d | d| d | d ks&J qtt dtdd  tdd t d t d }|tdd tdd t d t dd  ksYJ ttdt d t d td ksnJ ttdt dd  t dd td ksJ ttdtd td td ksJ ttdt d t d td ksJ ttdtd td td ksJ ttdtd tdtd ksJ ttdtd tdtd ksJ ttdtd tdtd ksJ ttdtdd tddtd ksJ ttdtdd tddtd ks0J ttdtdd tddtd ksGJ tt dtdd tddt d ks^J dS )zTest gate_sort.r.   r-   r   N)r   r   r   r   r   r   r   r   )rl   er:   r:   r;   test_gate_sort   s    :22*0***((*...2r   c                  C   s|  t dtd t dd  tdd tdd  td tdd  tdd  } t| t dtdd td td td ksDJ ttdtd dksRJ ttdtd dks`J ttdtd dksnJ tt dt d dks|J ttdtd tdksJ ttdtd tdksJ ttdtdksJ ttdd tdd  tdksJ dS )zTest gate_simp.r   r-   r.   rS   rC   N)	r   r   r   r   r   r   r   r   r   )r   r:   r:   r;   test_gate_simp   s   R6  ,r   c                  C   s   t d} ttdd dd| ksJ ttddtd tdks$J d	}t|D ]}t|D ]}tt|||dtt|| |dksHJ q0q*d
S )zTest the SWAP gate.)rO   rR   rP   rQ   r-   r   r.   r?   rS   00101000rC   N)r   r$   r   rX   r%   r&   rD   )swap_gate_matrixr2   rM   jr:   r:   r;   test_swap_gate  s   "
r   c                  C   s   t ttttttfD ]B} t ttttttfD ]6}t| d|d}tt	|ddd}tt	|
 ddd}||ks9J t| d|d}|
 dksJJ qq	dS )z-Test single qubit gate commutation relations.r   r-   sympyr2   formatN)r   r   r   r   r   r   r   r"   r*   r$   doitg1g2r   rE   rF   r:   r:   r;   test_one_qubit_commutators  s   r   c                  C   s   t ttttfD ]R} t ttttfD ]H}t| d|d}tt|ddd}tt| ddd}||ks5J t| d|d}tt|ddd}tt| ddd}||ksXJ qqdS )z1Test single qubit gate anticommutation relations.r   r-   r   r   r.   N)	r   r   r   r   r   r#   r*   r$   r   r   r:   r:   r;   test_one_qubit_anticommutators  s   r   c                   C   s  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s0J t tddtd dks@J t tddtdd dksQJ t tddtdd dksbJ t tddtdd dkssJ t tddtdd dksJ dS )z)Test commutators of involving CNOT gates.r   r-   r.   N)r"   r   r   r   r   r   r   r:   r:   r:   r;   test_cnot_commutators-  s       """&r   c                  C   sD   t dd} t| tsJ t| dd}|jdksJ t|ts J d S )N
   rS   r?   )   r   )r   
isinstancer   r$   shaper   )rG   mr:   r:   r;   test_random_circuit9  s
   
r   c                  C   "   t dd} t| }| |ksJ d S Nr-   r.   r   r,   xx_daggerr:   r:   r;   test_hermitian_XGateA     
r   c                  C   r   r   r   r,   yy_daggerr:   r:   r;   test_hermitian_YGateH  r   r   c                  C   r   r   r   r,   zz_daggerr:   r:   r;   test_hermitian_ZGateO  r   r   c                  C   &   t dd} t| }| | dksJ d S r   r   r   r:   r:   r;   test_unitary_XGateV     
r   c                  C   r   r   r   r   r:   r:   r;   test_unitary_YGate]  r   r   c                  C   r   r   r   r   r:   r:   r;   test_unitary_ZGated  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<   rN   rb   rh   rr   rv   rx   rz   r{   r|   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r:   r:   r:   r;   <module>   sR    \5