
    sg0                        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) ZSy*)+    )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                  h   t        d      } | j                  dk(  sJ | j                  dk(  sJ t        d      }t        d      }t        |      }t        |      }t        |      }||k(  sJ ||k7  sJ ||k7  sJ t	        ||      }t	        ||      }t	        ||      }||k(  sJ ||k7  sJ ||k7  sJ y)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	            X/var/www/html/venv/lib/python3.12/site-packages/sympy/physics/quantum/tests/test_gate.py	test_gater=      s    QA<<199>>	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 ]>  }|t        t        |d            z  t        t	        |t        |d      z              k(  r>J  y )
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   /   s   #JAq!QAq6Aq6"#D 
tT	BR#t+++"U3Z- AeCjL1U3Z<$????"U3Z- AeCjL1U3Z<$???? 
tT	Bb!$E1X 7_Xa^44F2hq!n#4567 	7 77r>   c                  L   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        |j                         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        |j                         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        |      |j                         k(  sJ t        |      |k7  sJ t        |      |j                         k(  sJ t        |      t        |d      k(  sJ t        |d      |j                         k(  sJ y)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JU1eAh'3zAAA a)K[!,%'?AY6 9*	   a)K+eFm+,	+eFm";QGHI I I+eFm+,	+eFm";QGHI I I 1eAhF	|aAr]Q1aLACM V%%'3}DDD1eAhFACM&t$%%+5559VE$K%7CD	t   V%%'3}DDDq)A,'J*U4[()	%+  9Zd%;QGH	%+   &>V###vq>VF^+++&>V^^----*+++*!3!3!5555*Z!4444z2*"4"4"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 ]>  }
|	t        t        |
d            z  t        t        |t        |
d      z              k(  r>J  y )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KD 
tT	B
2,CS!$,,,#eDk/"admadm&CCCC#eDk/"admadm&CCCC#eDk/"eDk111#eDk/"eDk111 
tT	Bb!$E1X 7_Xa^44F2hq!n#456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 y )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73aQi!DG5LMNEx1~!+Xa^ac-AB1~!+Xa^ac-ABD EGq!fq!f%&G	q%BQqT"Wa(G333R!Wa(G333r>   c                      t        d      t        d      z  } t        | d      }|t        t        t        ddg      k(  sJ y)z-Test the representation of the hadamard gate.r   rj   r0   rB   N)r   r'   r%   r   r,   circuitanswers     r<   test_represent_hadamardr}      s<    1oeDk)Gw*FVY	1a89999r>   c                  p    t        d      t        d      z  } t        | d      }t        g d      |k(  sJ y)z&Test the representation of the X gate.r   rj   r0   rB   rT   N)r   r'   r%   r   rz   s     r<   test_represent_xgater      3    AhuT{"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 y)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      sW    AhuT{"Gw*F!9>fQi1nq	Q6!9>* *)r>   c                  p    t        d      t        d      z  } t        | d      }t        g d      |k(  sJ y)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 y)z&Test the representation of the S gate.r   ri   r0   rB   N)r    r'   r%   r   r   rz   s     r<   test_represent_phasegater      s;    l5;&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 y)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      sC    AhuT{"G1c!B$q&k1a()Yw-JJJJr>   c                      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 y)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      sg    AhuQxa(a8tDGw*F1T!W9aQiA./6999r>   c                  L   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 y)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!nGWa(lL,GHI I IeEl"G9Wa89w   q!nG'?g%%%&/"g---7?ar>   c                  h   t         t        t        t        t        t
        fD ]B  } t         | d       | d      z   | d      z         | d       | d      z   | d      z  k(  rBJ  t        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 y)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ad1Q4(AaD1IadN:::;!A$qtQw,tAqz)!A$.qt34A!aQ
"1Q4'!a////QqT!A$YAaD51:---QqT!A$'\"adAgadl222QqT!A$YAaD51:---QqT!A$YAaD51:---QqT!A$YAaD51:---QqT!A$Y1Q4!9,,,QqT!A$Y1Q4!9,,,QqT!A$Y1Q4!9,,,QqT$q!*_%aAaD888QqT$q!*_%aAaD888QqT$q!*_%aAaD888QqT$q!*_%aAaD888r>   c                     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 y)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   	!QqT	!A$'$q!*$QqT1W,QqT1!A$'9!A$'AAQ<1Q4Q
?1Q4/!4QqT9999QqT!A$Y1$$$QqT!A$Y1$$$QqT!A$Y1$$$QqT!A$Y1$$$QqT!A$Y1Q4'''QqT!A$Y1Q4'''WQZ GAJ...QqT1WqtQw&'71:555r>   c            	         t        d      } t        t        dd      j                         d      | k(  sJ t	        t        dd      t        d      z        t        d      k(  sJ d	}t        |      D ]Q  }t        |      D ]A  }t        t        ||      |      t        t        ||      j                         |      k(  rAJ  S y
)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Xa^--/;?OOOO(1a.v./5=@@@G7^ Gq 	GAXa^W=(1a.224gF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        |j                         dd            }||k(  sJ t         | d       |d            }|j                         dk(  rJ   y)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2a5"Q%(AyAgFGAy1WMNA6M62a5"Q%(A668q= =	!!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        |j                         dd            }||k(  sJ t         | d       |d            }t        t        |dd            }t        t        |j                         dd            }||k(  rJ   y)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r!ube,AyAgFGAy1WMNA6M6r!ube,AyAgFGAy1WMNA6M6		r>   c                  ,   t        t        dd      t        d            j                         dk(  sJ t        t        dd      t	        d            j                         dk(  sJ t        t        dd      t        d            j                         dk(  sJ t        t        dd      t        d            j                         dk(  sJ t        t        dd      t        dd            j                         dk(  sJ t        t        dd      t        dd            j                         dk(  sJ t        t        dd      t        dd            j                         dk(  sJ t        t        dd      t        dd            j                         dk(  sJ y)z)Test commutators of involving CNOT gates.r   r/   r0   N)r#   r   r   r   r   r   r    r>   r<   test_cnot_commutatorsr   -  sJ   d1aj!A$',,.!333d1aj!A$',,.!333d1aj!A$',,.!333d1aj!A$',,.!333d1aj$q!*-224999d1aj$q!*-224999d1aj$q!*-224999d1aj$q!*-224999r>   c                      t        dd      } t        | t              sJ t        | d      }|j                  dk(  sJ t        |t
              sJ y )N
   rW   rB   )   r   )r   
isinstancer   r%   shaper   )rJ   ms     r<   test_random_circuitr   9  sL    r1Aa!QA77fa   r>   c                  @    t        dd      } t        |       }| |k(  sJ y Nr/   r0   r   r-   xx_daggers     r<   test_hermitian_XGater   A  "    aAayHMMr>   c                  @    t        dd      } t        |       }| |k(  sJ y r   r   r-   yy_daggers     r<   test_hermitian_YGater   H  r   r>   c                  @    t        dd      } t        |       }| |k(  sJ y r   r   r-   zz_daggers     r<   test_hermitian_ZGater   O  r   r>   c                  F    t        dd      } t        |       }| |z  dk(  sJ y r   r   r   s     r<   test_unitary_XGater   V  &    aAayHhJ!OOr>   c                  F    t        dd      } t        |       }| |z  dk(  sJ y r   r   r   s     r<   test_unitary_YGater   ]  r   r>   c                  F    t        dd      } t        |       }| |z  dk(  sJ y 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 - 6 9 2@ @ @ @ @ @ 8 ? 5 /  < 7 ($27$27j7*4:****K: 986
G
!	:!r>   