+
    i                         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# )zShor'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       X/var/www/html/photoedit/myenv/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# )	CModzA 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/        zz!}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     tvvA6&&1*%%%AFA 
 $&&!)dff$% Aw/0 %-(AOOSX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AzQAzAA1uzQ1s8a<#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    Aw{vv
4yA~Aw7Xd2h***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\        W4      p
K  	  \        \        W3^,          4      P                  4       V
,          RR7      p
\	        V4       F  p\        WV,           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WWV,           ,          ,          ,          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AdiiAq	""#Aa!1QE!tAqDz\FF%(A$7I%	%++ 8 }$$&G 1mG#G WoG1X)'5  SaC[**,W4DIG1X)'q59 '5!!	GS	!	!<<#<<#((,	AF3x=?#HUO##F $ {#=GI 	I 	VQT1AHM "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   