
    g                     >   d Z ddlmZ ddl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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mZmZmZmZmZm Z  ddl!m"Z" g dZ# G d de          Z$e$Z% G d de          Z& G d de&          Z' G d de&          Z(dS )at  An implementation of qubits and gates acting on them.

Todo:

* Update docstrings.
* Update tests.
* Implement apply using decompose.
* Implement represent using decompose or something smarter. For this to
  work we first have to implement represent for SWAP.
* Decide if we want upper index to be inclusive in the constructor.
* Fix the printing of Rk gates in plotting.
    )Expr)IIntegerpiSymbol)exp)Matrixsqrt)qapply)QuantumErrorQExpr)eye)matrix_tensor_product)GateHadamardGateSwapGateOneQubitGateCGate	PhaseGateTGateZGate)sign)QFTIQFTRkGateRkc                       e Zd ZdZdZdZd Zed             Ze	d             Z
e	d             Ze	d             Zdd
ZdS )r   z This is the R_k gate of the QTF.r   Rc                 p   t          |          dk    rt          d|z            |d         }|d         }|dk    rt          |          S |dk    rt          |          S |dk    rt	          |          S |                     |          }t          j        | g|R  }|                     |          |_	        |S )N   z)Rk gates only take two arguments, got: %rr         )
lenr   r   r   r   
_eval_argsr   __new___eval_hilbert_spacehilbert_space)clsargstargetkinsts        U/var/www/html/ai-engine/env/lib/python3.11/site-packages/sympy/physics/quantum/qft.pyr'   zRkGate.__new__1   s    t99>>;dB   aG66== !VVV$$$!VV== ~~d##|C'$''' 44T::    c                 *    t          j        |          S N)r   r&   )r*   r+   s     r/   r&   zRkGate._eval_argsF   s     %%%r0   c                     | j         d         S Nr#   labelselfs    r/   r-   zRkGate.kL       z!}r0   c                      | j         d d         S r4   r5   r7   s    r/   targetszRkGate.targetsP   s    z"1"~r0   c                 B    d| j         dt          | j                  dS )N$_)gate_name_latexstrr-   r7   s    r/   gate_name_plotzRkGate.gate_name_plotT   s$     !1113tv;;;;??r0   sympyc                 $   |dk    ryt          ddgdt          t          | j                  t	          d          z  t
          z  t          z  t	          d          t          | j                  z  z            gg          S t          d|z            )NrB   r#   r   r"   z#Invalid format for the R_k gate: %r)	r
   r	   r   r-   r   r   r   absNotImplementedError)r8   formats     r/   get_target_matrixzRkGate.get_target_matrixX   s    WAq6As4<<

+B2+Ea+GQRUXY]Y_U`U`I`+a'b'b#cdeee!1F:< < 	<r0   N)rB   )__name__
__module____qualname____doc__	gate_namer?   r'   classmethodr&   propertyr-   r;   rA   rG    r0   r/   r   r   ,   s        **IO  * & & [&
   X   X @ @ X@< < < < < <r0   r   c                       e Zd ZdZed             Zd Zd Zed             Z	ed             Z
ed             Zed             Zd	S )
Fourierz@Superclass of Quantum Fourier and Inverse Quantum Fourier Gates.c                     t          |          dk    rt          d|z            |d         |d         k    rt          d          t          j        |          S )Nr"   z*QFT/IQFT only takes two arguments, got: %rr   r#   z!Start must be smaller than finish)r%   r   r   r&   )r8   r+   s     r/   r&   zFourier._eval_argse   s_    t99>><tC   7d1gBCCCt$$$r0   c                      | j         di |S )Nr2   )_represent_ZGate)r8   optionss     r/   _represent_default_basisz Fourier._represent_default_basiso   s    $t$55W555r0   c                    |                     dd          }|dk    rt          d          || j        k     rt          d|z            | j        | j        fdt                    D             }t          |          }| j        d         dk    r+t          t          d| j        d         z            |          }| j        |k     r(t          |t          d|| j        z
  z                      }|S )z:
            Represents the (I)QFT In the Z Basis
        nqubitsr   z.The number of qubits must be given as nqubits.z2The number of qubits %r is too small for the gate.c                 L    g | ]fd t                    D              S )c                 F    g | ]}|z  z  z  t                    z  S rO   r   ).0ijomegasizes     r/   
<listcomp>z7Fourier._represent_ZGate.<locals>.<listcomp>.<listcomp>   sG     9 9 9'( aC$JT

# 9 9 9r0   )range)r[   r]   r^   r_   s    @r/   r`   z,Fourier._represent_ZGate.<locals>.<listcomp>   sa     O O O=>9 9 9 9 9 9,1$KK9 9 9 O O Or0   r"   )
getr   
min_qubitsr_   r^   ra   r
   r6   r   r   )r8   basisrU   rX   arrayFTmatrixFTr^   r_   s         @@r/   rT   zFourier._represent_ZGater   s(    ++i++a<<@B B BT_$$DwN   y
O O O O OBG++O O O'?? :a=A,SDJqM1A-B-BHMMH?W$$,#a'DO";<==? ?H r0   c                 N    t          | j        d         | j        d                   S )Nr   r#   )ra   r6   r7   s    r/   r;   zFourier.targets   s    TZ]DJqM222r0   c                     | j         d         S r4   r5   r7   s    r/   rc   zFourier.min_qubits   r9   r0   c                 >    d| j         d         | j         d         z
  z  S )z"Size is the size of the QFT matrixr"   r#   r   r5   r7   s    r/   r_   zFourier.size   s      4:a=4:a=011r0   c                      t          d          S )Nr^   r   r7   s    r/   r^   zFourier.omega   s    gr0   N)rH   rI   rJ   rK   rM   r&   rV   rT   rN   r;   rc   r_   r^   rO   r0   r/   rQ   rQ   b   s        JJ% % [%6 6 6  : 3 3 X3   X 2 2 X2   X  r0   rQ   c                   B    e Zd ZdZd Zd Zd Zd Zd Ze	d             Z
dS )r   z&The forward quantum Fourier transform.c           
         | j         d         }| j         d         }d}t          t          ||                    D ]S}t          |          |z  }t          ||z
            D ],}t	          ||z
  dz
  t          ||dz                       |z  }-Tt          ||z
  dz            D ]}t          ||z   ||z
  dz
            |z  }|S )z%Decomposes QFT into elementary gates.r   r#   r"   )r6   reversedra   r   r   r   r   )r8   startfinishcircuitlevelr\   s         r/   	decomposezQFT.decompose   s    
1AeE62233 	M 	ME"5))'1G55=)) M M	AveQU/C/CDDWLM*++ 	B 	BAq5y&1*q.99'AGGr0   c                 J    t          |                                 |z            S r2   )r   rr   )r8   qubitsrU   s      r/   _apply_operator_QubitzQFT._apply_operator_Qubit   s    dnn&&v-...r0   c                     t          | j         S r2   )r   r+   r7   s    r/   _eval_inversezQFT._eval_inverse   s    TYr0   c                 P    t          dt          z  t          z  | j        z            S )Nr"   r	   r   r   r_   r7   s    r/   r^   z	QFT.omega   s    1R46$)#$$$r0   N)rH   rI   rJ   rK   rL   r?   rr   ru   rw   rN   r^   rO   r0   r/   r   r      sl        00IO  / / /      % % X% % %r0   r   c                   <    e Zd ZdZd ZdZd Zd Zed             Z	dS )r   z&The inverse quantum Fourier transform.z
{QFT^{-1}}c           
         | j         d         }| j         d         }d}t          ||z
  dz            D ]}t          ||z   ||z
  dz
            |z  }t          ||          D ]a}t          t          ||z
                      D ]-}t	          ||z
  dz
  t          || dz
                      |z  }.t          |          |z  }b|S )z&Decomposes IQFT into elementary gates.r   r#   r"   )r+   ra   r   rm   r   r   r   )r8   rn   ro   rp   r\   rq   s         r/   rr   zIQFT.decompose   s    	!1*++ 	B 	BAq5y&1*q.99'AGG5&)) 	2 	2EeEEM2233 N N	AveaR!V/D/DEEgM"5))'1GGr0   c                     t          | j         S r2   )r   r+   r7   s    r/   rw   zIQFT._eval_inverse   s    DIr0   c                 P    t          dt          z  t          z  | j        z            S )Nry   r7   s    r/   r^   z
IQFT.omega   s    2b5749$%%%r0   N)
rH   rI   rJ   rK   rL   r?   rr   rw   rN   r^   rO   r0   r/   r   r      s]        00I"O     & & X& & &r0   r   N))rK   sympy.core.exprr   sympy.core.numbersr   r   r   sympy.core.symbolr   &sympy.functions.elementary.exponentialr	   sympy.matrices.denser
   sympy.functionsr   sympy.physics.quantum.qapplyr   sympy.physics.quantum.qexprr   r   sympy.matricesr   #sympy.physics.quantum.tensorproductr   sympy.physics.quantum.gater   r   r   r   r   r   r   r   $sympy.functions.elementary.complexesr   __all__r   r   rQ   r   r   rO   r0   r/   <module>r      s    !           / / / / / / / / / / $ $ $ $ $ $ 6 6 6 6 6 6 ' ' ' ' ' '             / / / / / / ; ; ; ; ; ; ; ;       E E E E E E                    6 5 5 5 5 5  0< 0< 0< 0< 0<\ 0< 0< 0<f < < < < <d < < <~% % % % %' % % %<& & & & &7 & & & & &r0   