+
    
þÎj€  ã                   óò   € R t ^ RIt^ RIt^ RIHt ^ RIHt ^ RIHt ^ RI	H
t
 ^ RIHt ^ RIHt ^ RIHt ^ R	IHt ^ R
IHtHt ^ RIHt ^ RIHt ^ RIHt  ! R R]4      t ! R R]4      tR tR t R t!R t"R# )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                   ó   € ] tR t^tRtR# )ÚOrderFindingException© N)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__static_attributes__r   ó    ÚP/opt/data/home/.local/lib/python3.14/site-packages/sympy/physics/quantum/shor.pyr   r      s   † Ûr   r   c                   ój   a € ] tR t^ t o Rt]R 4       t]R 4       t]R 4       t	]R 4       t
R tRtV tR# )	Ú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                ó   € \        R 4      h)z%The CMod gate has not been completed.)ÚNotImplementedError)ÚclsÚargss   &&r   Ú
_eval_argsÚCMod._eval_args(   s   € ô
 "Ð"IÓJÐJr   c                ó(   € V P                   ^ ,          # )z4Size of 1/2 input register.  First 1/2 holds output.©Úlabel©Úselfs   &r   ÚtÚCMod.t/   ó   € ð �z‰z˜!�}Ðr   c                ó(   € V P                   ^,          # )z$Base of the controlled mod function.r"   r$   s   &r   ÚaÚCMod.a4   r(   r   c                ó(   € V P                   ^,          # )z1N is the type of modular arithmetic we are doing.r"   r$   s   &r   ÚNÚCMod.N9   r(   r   c                óä  € ^p^ p\        V P                  4       F2  pWCWP                  V,           ,          ,          ,          pV^,          pK4  	  \        V P                  V,          V P                  ,          4      p\        VP                  ^ ,          RV P                   4      p\        \        V P                  4      4       F!  pVP                  We,	          ^,          4       K#  	  \        V!  # )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
N)
Úranger&   Úintr*   r-   Úlistr   ÚreversedÚappendr
   )r%   ÚqubitsÚoptionsÚnÚkÚiÚoutÚoutarrays   &&,     r   Ú_apply_operator_QubitÚCMod._apply_operator_Qubit>   sµ   € ð ˆØˆä�t—v‘v–ˆAØ�6Ÿ&™& 1�*Õ%Õ%Õ%ˆAØ��FŠAñ ô
 �$—&‘&˜!•)˜dŸf™fÕ$Ó%ˆô ˜Ÿ™ A� w¨¯©Ð/Ó0ˆô œ% §¡›-Ö(ˆAØ�O‰O˜S�X¨�NÖ+ñ )ô �hÑÐr   r   N)r   r   r   r   Ú__doc__Úclassmethodr   Úpropertyr&   r*   r-   r<   r   Ú__classdictcell__)Ú__classdict__s   @r   r   r       sf   ø‡ € ñð ñKó ðKð ñó ðð ñó ðð ñó ð÷ ð  r   r   c                óL  € \         P                  ! V ^,
          4      ^,           p\        W4      ^8w  d   \        W4      # \        W4      pV^,          ^8X  d   \	        V 4       \        W^,          ,          ^,
          V 4      \        W^,          ,          ^,           V 4      3pV# )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.
)ÚrandomÚ	randranger   Úperiod_findÚshor)r-   r*   ÚrÚanswers   &   r   rG   rG   X   s   € ô 	×Ò˜˜Q�Ó !Õ#€AÜˆAƒz�Q„Ü�A‹zÐÜ�AÓ€AØˆ1…u�„zÜˆQŒÜ�1˜•s•8˜a•< Ó#¤T¨!°­c­(°Q­,¸Ó%:Ð;€FØ€Mr   c                 ó2   € \        W4      p\        W24      pV# )N)Úcontinued_fractionÚratioize)ÚxÚyr-   ÚfractionÚtotals   &&&  r   ÚgetrrQ   l   s   € Ü! !Ó'€Hä�XÓ!€EØ€Lr   c                 ó²   € V ^ ,          V8”  d   \         P                  # \        V 4      ^8X  d
   V ^ ,          # V ^ ,          \        V R,          V4      ,           # )é    :é   NN)r   ÚZeroÚlenrL   )r2   r-   s   &&r   rL   rL   s   sD   € ØˆA…w�„{Ü�v‰vˆÜ
ˆ4ƒy�A„~Ø�A�wˆØ��7”X˜d 2�h¨Ó*Õ*Ð*r   c           	     ój  € Rp\        ^\        P                  ! \        V^4      4      ,          4      p\	        V4       Uu. uF  p^ NK  	  pp^\        ^V,          4      ,          p^ p\        \	        ^4      VRR7       F%  p\        V4      V,           p	V\        V	!  ,           pK'  	  Wg,          P                  4       p
\        W0V4      V
,          p
\        V
4      p
\	        V4       F  p\        W«4      p
K  	  \        \        W3^,          4      P                  4       V
,          RR7      p
\	        V4       F  p\        W«V,           4      p
K  	  \        V
\        4      '       d   T
pMN\        V
\         4      '       d   V
P"                  R,          pM$V
P"                  R,          P"                  R,          p^p^ p\	        \%        V4      ^,          4       F(  pWíWËV,           ,          ,          ,          pV^,          pK*  	  V^ 8X  d   \'        RV,          4      h\)        V^V,          V4      pV# u upi )a  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      à?T)Ú
repetition)ÚfloatingPointz/Order finder returned 0. Happens with chance %féÿÿÿÿ)r1   ÚmathÚceilr   r0   r   r   r2   r
   Úexpandr   r   r   r   Ú	decomposeÚ
isinstancer   r   rV   r   rQ   )r*   r-   Úepsilonr&   rM   ÚstartÚfactorr5   ÚarrÚ	qbitArrayÚcircuitr9   Úregisterr7   rI   Úgs   &&              r   rF   rF   {   sÒ  € ð €GäˆAŒd�iŠiœ˜A˜q›	Ó"Õ"Ó#€Aä˜aœÓ!™�1‹Q™€EÐ!àŒt�A�q•D‹z�\€FØ€FÜœ% ›( A°$×7ˆÜ˜“I Õ%ˆ	Øœ% Ñ+Õ+Šñ 8ð �}×$Ñ$Ó&€Gô �1˜‹m˜GÕ#€Gô �W‹o€GÜ�1ŽXˆÜ)¨'Ó5Šñ ô ”S˜˜a�C“[×*Ñ*Ó,¨WÕ4ÀDÔI€GÜ�1ŽXˆÜ)¨'°qµ5Ó9Šñ ä�'œ5×!Ò!Ø‰Ü	�GœS×	!Ò	!Ø—<‘< Õ#‰à—<‘< Õ#×(Ñ(¨Õ,ˆà	€AØ€FÜ”3�x“= •?Ö#ˆØ�H �U•OÕ#Õ#ˆØ��FŠñ $ð �„{Ü#Ø=ÀÕGóIð 	Iô 	ˆV�Q˜•T˜1Ó€AØ€HùòM "s   Á H0)#r>   r[   rD   Úsympy.core.mulr   Úsympy.core.singletonr   Ú&sympy.functions.elementary.exponentialr   Ú(sympy.functions.elementary.miscellaneousr   Úsympy.core.intfuncr   Úsympy.ntheoryr   rK   Ú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   rG   rQ   rL   rF   r   r   r   Ú<module>rt      sc   ðñó Û å Ý "Ý 6Ý 9Ý #Ý KÝ 0å +ß FÝ /Ý )Ý 4ô	˜Lô 	ô5 ˆ4ô 5 òpò(ò+ô2r   