Ë
    7^(h€  ã                   óð   — d Z ddlZddl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 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  G d„ de«      Z G d„ de«      Zd„ Zd„ Z d„ Z!d„ Z"y)z�Shor's algorithm and helper functions.

Todo:

* Get the CMod gate working again using the new Gate API.
* Fix everything.
* Update docstrings and reformat.
é    N)ÚMul)ÚS)Úlog)Úsqrt)Úigcd)Úcontinued_fraction_periodic)Ú
variations)ÚGate)ÚQubitÚmeasure_partial_oneshot)Úqapply)ÚQFT)ÚQuantumErrorc                   ó   — e Zd Zy)ÚOrderFindingExceptionN)Ú__name__Ú
__module__Ú__qualname__© ó    úX/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/sympy/physics/quantum/shor.pyr   r      s   „ Ør   r   c                   óV   — e Zd ZdZed„ «       Zed„ «       Zed„ «       Zed„ «       Z	d„ Z
y)ÚCModzÍA controlled mod gate.

    This is black box controlled Mod function for use by shor's algorithm.
    TODO: implement a decompose property that returns how to do this in terms
    of elementary gates
    c                 ó   — t        d«      ‚)Nz%The CMod gate has not been completed.)ÚNotImplementedError)ÚclsÚargss     r   Ú
_eval_argszCMod._eval_args(   s   € ô
 "Ð"IÓJÐJr   c                 ó    — | j                   d   S )z4Size of 1/2 input register.  First 1/2 holds output.r   ©Úlabel©Úselfs    r   ÚtzCMod.t/   ó   € ð �z‰z˜!‰}Ðr   c                 ó    — | j                   d   S )z$Base of the controlled mod function.é   r    r"   s    r   ÚazCMod.a4   r%   r   c                 ó    — | j                   d   S )z1N is the type of modular arithmetic we are doing.é   r    r"   s    r   ÚNzCMod.N9   r%   r   c                 ó�  — d}d}t        | j                  «      D ]  }|||| j                  |z      z  z  }|dz  }Œ! t        | j                  |z  | j                  z  «      }t        |j                  d   d| j                   «      }t        t        | j                  «      «      D ]  }|j                  ||z	  dz  «       Œ t        |Ž S )zÔ
            This directly calculates the controlled mod of the second half of
            the register and puts it in the second
            This will look pretty when we get Tensor Symbolically working
        r'   r   r*   N)
Úranger$   Úintr(   r+   Úlistr   ÚreversedÚappendr   )r#   ÚqubitsÚoptionsÚnÚkÚiÚoutÚoutarrays           r   Ú_apply_operator_QubitzCMod._apply_operator_Qubit>   sÅ   € ð ˆØˆä�t—v‘v“ò 	ˆAØ��6˜$Ÿ&™& 1™*Ñ%Ñ%Ñ%ˆAØ�‰F‰Að	ô
 �$—&‘&˜!‘)˜dŸf™fÑ$Ó%ˆô ˜Ÿ™ A™ w¨¯©Ð/Ó0ˆô œ% §¡›-Ó(ò 	,ˆAØ�O‰O˜S A™X¨™NÕ+ð	,ô �hÐÐr   N)r   r   r   Ú__doc__Úclassmethodr   Úpropertyr$   r(   r+   r9   r   r   r   r   r       s^   „ ñð ñKó ðKð ñó ðð ñó ðð ñó ðó r   r   c                 ó  — t        j                  | dz
  «      dz   }t        | |«      dk7  rt        | |«      S t        || «      }|dz  dk(  rt	        | «       t        ||dz  z  dz
  | «      t        ||dz  z  dz   | «      f}|S )aã  This function implements Shor's factoring algorithm on the Integer N

    The algorithm starts by picking a random number (a) and seeing if it is
    coprime with N. If it is not, then the gcd of the two numbers is a factor
    and we are done. Otherwise, it begins the period_finding subroutine which
    finds the period of a in modulo N arithmetic. This period, if even, can
    be used to calculate factors by taking a**(r/2)-1 and a**(r/2)+1.
    These values are returned.
    r*   r'   )ÚrandomÚ	randranger   Úperiod_findÚshor)r+   r(   ÚrÚanswers       r   rA   rA   X   s‰   € ô 	×Ñ˜˜Q™Ó !Ñ#€AÜˆAˆqƒz�Q‚Ü�A�q‹zÐÜ�A�qÓ€AØˆ1�u�‚zÜˆQŒÜ�1�q˜‘s‘8˜a‘< Ó#¤T¨!¨a°©c©(°Q©,¸Ó%:Ð;€FØ€Mr   c                 ó6   — t        | |«      }t        ||«      }|S )N)Úcontinued_fractionÚratioize)ÚxÚyr+   ÚfractionÚtotals        r   ÚgetrrK   l   s   € Ü! ! QÓ'€Hä�X˜qÓ!€EØ€Lr   c                 ó‚   — | d   |kD  rt         j                  S t        | «      dk(  r| d   S | d   t        | dd  |«      z   S )Nr   r'   )r   ÚZeroÚlenrF   )r/   r+   s     r   rF   rF   s   sF   € ØˆA�w�‚{Ü�v‰vˆÜ
ˆ4ƒy�A‚~Ø�A‰wˆØ�‰7”X˜d 1 2˜h¨Ó*Ñ*Ð*r   c           	      óœ  — d}t        dt        j                  t        |d«      «      z  «      }t	        |«      D �cg c]  }d‘Œ }}dt        d|z  «      z  }d}t        t	        d«      |d¬«      D ]  }t        |«      |z   }	|t        |	Ž z   }Œ ||z  j                  «       }
t        || |«      |
z  }
t        |
«      }
t	        |«      D ]  }t        |
|«      }
Œ t        t        ||dz  «      j                  «       |
z  d¬«      }
t	        |«      D ]  }t        |
||z   «      }
Œ t        |
t        «      r|
}n<t        |
t         «      r|
j"                  d   }n|
j"                  d   j"                  d   }d}d}t	        t%        |«      dz  «      D ]  }|||||z      z  z  }|dz  }Œ |dk(  rt'        d	|z  «      ‚t)        |d|z  |«      }|S c c}w )
a0  Finds the period of a in modulo N arithmetic

    This is quantum part of Shor's algorithm. It takes two registers,
    puts first in superposition of states with Hadamards so: ``|k>|0>``
    with k being all possible choices. It then does a controlled mod and
    a QFT to determine the order of a.
    g      à?r*   r   r'   T)Ú
repetition)ÚfloatingPointéÿÿÿÿz/Order finder returned 0. Happens with chance %f)r.   ÚmathÚceilr   r-   r   r	   r/   r   Úexpandr   r   r   r   Ú	decomposeÚ
isinstancer   r   rN   r   rK   )r(   r+   Úepsilonr$   rG   ÚstartÚfactorr2   ÚarrÚ	qbitArrayÚcircuitr6   Úregisterr4   rC   Úgs                   r   r@   r@   {   sí  € ð €GäˆAŒd�i‰iœ˜A˜q›	Ó"Ñ"Ó#€Aä˜a›Ö!�1ŠQÐ!€EÐ!àŒt�A�q‘D‹z‰\€FØ€FÜœ% ›( A°$Ô7ò ,ˆÜ˜“I Ñ%ˆ	Øœ% Ð+Ñ+‰ð,ð �f‰}×$Ñ$Ó&€Gô �1�a˜‹m˜GÑ#€Gô �W‹o€GÜ�1‹Xò 6ˆÜ)¨'°1Ó5‰ð6ô ”S˜˜A˜a™C“[×*Ñ*Ó,¨WÑ4ÀDÔI€GÜ�1‹Xò :ˆÜ)¨'°1°q±5Ó9‰ð:ä�'œ5Ô!Ø‰Ü	�GœSÔ	!Ø—<‘< Ñ#‰à—<‘< Ñ#×(Ñ(¨Ñ,ˆà	€AØ€FÜ”3�x“= ‘?Ó#ò ˆØ�!�H˜Q ™U‘OÑ#Ñ#ˆØ�‰F‰ðð �‚{Ü#Ø=ÀÑGóIð 	Iô 	ˆV�Q˜‘T˜1Ó€AØ€HùòM "s   »	G	)#r:   rS   r>   Úsympy.core.mulr   Úsympy.core.singletonr   Ú&sympy.functions.elementary.exponentialr   Ú(sympy.functions.elementary.miscellaneousr   Úsympy.core.intfuncr   Úsympy.ntheoryr   rE   Úsympy.utilities.iterablesr	   Úsympy.physics.quantum.gater
   Úsympy.physics.quantum.qubitr   r   Úsympy.physics.quantum.qapplyr   Úsympy.physics.quantum.qftr   Úsympy.physics.quantum.qexprr   r   r   rA   rK   rF   r@   r   r   r   ú<module>rl      sc   ðñó Û å Ý "Ý 6Ý 9Ý #Ý KÝ 0å +ß FÝ /Ý )Ý 4ô	˜Lô 	ô5 ˆ4ô 5 òpò(ò+ó2r   