o
    §ƒðj  ã                   @   s  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„ deƒZ$e$Z%G dd„ deƒZ&G dd„ de&ƒZ'G d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                   @   sZ   e Zd ZdZdZdZdd„ Zedd„ ƒZe	dd	„ ƒZ
e	d
d„ ƒZe	dd„ ƒZddd„ZdS )r   z This is the R_k gate of the QTF.r   ÚRc                 G   s„   t |ƒdkrtd| ƒ‚|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Úinst© r,   ú›/tmp/claude-1000/-var-www-html-academy/3f42efd1-c2b7-4d08-9b05-30d86bb35479/scratchpad/piper/venv/lib/python3.10/site-packages/sympy/physics/quantum/qft.pyr$   1   s    ÿ
zRkGate.__new__c                 C   s
   t  |¡S ©N)r   r#   )r'   r(   r,   r,   r-   r#   F   s   
zRkGate._eval_argsc                 C   ó
   | j d S ©Nr    ©Úlabel©Úselfr,   r,   r-   r*   L   ó   
zRkGate.kc                 C   s   | j d d… S r0   r1   r3   r,   r,   r-   ÚtargetsP   s   zRkGate.targetsc                 C   s   d| j t| jƒf S )Nz$%s_%s$)Úgate_name_latexÚstrr*   r3   r,   r,   r-   Úgate_name_plotT   s   zRkGate.gate_name_plotÚsympyc                 C   sT   |dkr$t ddgdtt| jƒtdƒ t t tdƒt| jƒ  ƒggƒS td| ƒ‚)Nr:   r    r   r   z#Invalid format for the R_k gate: %r)	r	   r   r   r*   r   r   r   ÚabsÚNotImplementedError)r4   Úformatr,   r,   r-   Úget_target_matrixX   s
   @ÿzRkGate.get_target_matrixN)r:   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú	gate_namer7   r$   Úclassmethodr#   Úpropertyr*   r6   r9   r>   r,   r,   r,   r-   r   ,   s    



r   c                   @   s\   e Zd ZdZedd„ ƒZdd„ Zdd„ Zedd	„ ƒZ	ed
d„ ƒZ
edd„ ƒZedd„ ƒZdS )ÚFourierz@Superclass of Quantum Fourier and Inverse Quantum Fourier Gates.c                 C   s:   t |ƒdkrtd| ƒ‚|d |d krtdƒ‚t |¡S )Nr   z*QFT/IQFT only takes two arguments, got: %rr   r    z!Start must be smaller than finish)r"   r   r   r#   )r4   r(   r,   r,   r-   r#   e   s   ÿ
zFourier._eval_argsc                 K   s   | j di |¤ŽS )Nr.   )Ú_represent_ZGate)r4   Úoptionsr,   r,   r-   Ú_represent_default_basiso   ó   z Fourier._represent_default_basisc                    sª   |  dd¡}|dkrtdƒ‚|| jk rtd| ƒ‚| j‰| j‰ ‡ ‡fdd„tˆƒD ƒ}t|ƒ}| jd dkrBtt	d| jd  ƒ|ƒ}| j|k rSt|t	d|| j  ƒƒ}|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                    s&   g | ]‰ ‡ ‡‡fd d„t ˆƒD ƒ‘qS )c                    s$   g | ]}ˆ|ˆ  ˆ  t ˆƒ ‘qS r,   r
   )Ú.0Úi)ÚjÚomegaÚsizer,   r-   Ú
<listcomp>‚   s    ÿ
ÿÿz7Fourier._represent_ZGate.<locals>.<listcomp>.<listcomp>)Úrange)rL   ©rO   rP   )rN   r-   rQ   ‚   s
    ÿ
ÿz,Fourier._represent_ZGate.<locals>.<listcomp>r   )
Úgetr   Ú
min_qubitsrP   rO   rR   r	   r2   r   r   )r4   ÚbasisrH   rK   ÚarrayFTÚmatrixFTr,   rS   r-   rG   r   s,   ÿ
ÿÿ
ÿzFourier._represent_ZGatec                 C   s   t | jd | jd ƒS )Nr   r    )rR   r2   r3   r,   r,   r-   r6   �   ó   zFourier.targetsc                 C   r/   r0   r1   r3   r,   r,   r-   rU   “   r5   zFourier.min_qubitsc                 C   s   d| j d | j d   S )z"Size is the size of the QFT matrixr   r    r   r1   r3   r,   r,   r-   rP   —   s   zFourier.sizec                 C   s   t dƒS )NrO   r   r3   r,   r,   r-   rO   œ   s   zFourier.omegaN)r?   r@   rA   rB   rD   r#   rI   rG   rE   r6   rU   rP   rO   r,   r,   r,   r-   rF   b   s    
	


rF   c                   @   s<   e Zd ZdZd Zd Zdd„ Zdd„ Zdd„ Ze	dd	„ ƒZ
d
S )r   z&The forward quantum Fourier transform.c                 C   sž   | j d }| j d }d}tt||ƒƒD ]!}t|ƒ| }t|| ƒD ]}t|| d t||d ƒƒ| }q!qt|| d ƒD ]}t|| || d ƒ| }q=|S )z%Decomposes QFT into elementary gates.r   r    r   )r2   ÚreversedrR   r   r   r   r   )r4   ÚstartÚfinishÚcircuitÚlevelrM   r,   r,   r-   Ú	decompose§   s   

"ÿzQFT.decomposec                 K   s   t |  ¡ | ƒS r.   )r   r_   )r4   ÚqubitsrH   r,   r,   r-   Ú_apply_operator_Qubit´   rJ   zQFT._apply_operator_Qubitc                 C   ó
   t | jŽ S r.   )r   r(   r3   r,   r,   r-   Ú_eval_inverse·   ó   
zQFT._eval_inversec                 C   ó   t dt t | j ƒS )Nr   ©r   r   r   rP   r3   r,   r,   r-   rO   º   rY   z	QFT.omegaN)r?   r@   rA   rB   rC   r7   r_   ra   rc   rE   rO   r,   r,   r,   r-   r   ¡   s    r   c                   @   s4   e Zd ZdZd ZdZdd„ Zdd„ Zedd„ ƒZ	d	S )
r   z&The inverse quantum Fourier transform.z
{QFT^{-1}}c                 C   s    | j d }| j d }d}t|| d ƒD ]}t|| || d ƒ| }qt||ƒD ]$}tt|| ƒƒD ]}t|| d t|| d ƒƒ| }q3t|ƒ| }q)|S )z&Decomposes IQFT into elementary gates.r   r    r   )r(   rR   r   rZ   r   r   r   )r4   r[   r\   r]   rM   r^   r,   r,   r-   r_   Å   s   

$zIQFT.decomposec                 C   rb   r.   )r   r(   r3   r,   r,   r-   rc   Ò   rd   zIQFT._eval_inversec                 C   re   )Néþÿÿÿrf   r3   r,   r,   r-   rO   Õ   rY   z
IQFT.omegaN)
r?   r@   rA   rB   rC   r7   r_   rc   rE   rO   r,   r,   r,   r-   r   ¿   s    r   N))rB   Ú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   rF   r   r   r,   r,   r,   r-   Ú<module>   s&    (3?