
    \h                         S r SSKrSSKrSSKJr  SSKJr  SSKJr  SSK	J
r
  SSKJr  SSKJr  SS	KJr  SS
KJr  SSKJrJr  SSKJr  SSKJr  SSKJr   " S S\5      r " S S\5      rS rS r S r!S r"g)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                       \ rS rSrSrg)OrderFindingException    N)__name__
__module____qualname____firstlineno____static_attributes__r       R/var/www/auris/envauris/lib/python3.13/site-packages/sympy/physics/quantum/shor.pyr   r      s    r   r   c                   ^    \ rS rSrSr\S 5       r\S 5       r\S 5       r	\S 5       r
S rSrg	)
CMod    zA 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                     [        S5      e)Nz%The CMod gate has not been completed.)NotImplementedError)clsargss     r   
_eval_argsCMod._eval_args(   s    
 ""IJJr   c                      U R                   S   $ )z4Size of 1/2 input register.  First 1/2 holds output.r   labelselfs    r   tCMod.t/        zz!}r   c                      U R                   S   $ )z$Base of the controlled mod function.   r%   r'   s    r   aCMod.a4   r+   r   c                      U R                   S   $ )z1N is the type of modular arithmetic we are doing.   r%   r'   s    r   NCMod.N9   r+   r   c                    SnSn[        U R                  5       H  nXCXR                  U-      -  -  nUS-  nM      [        U R                  U-  U R                  -  5      n[        UR                  S   SU R                   5      n[        [        U R                  5      5       H  nUR                  Xe-	  S-  5        M     [        U6 $ )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   r1   N)
ranger)   intr.   r2   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.   r2   rA   r   r   r   r   r   r       s^     K K       r   r   c                     [         R                  " U S-
  5      S-   n[        X5      S:w  a  [        X5      $ [        X5      nUS-  S:X  a  [	        U 5        [        XS-  -  S-
  U 5      [        XS-  -  S-   U 5      4nU$ )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.
r1   r-   )random	randranger   period_findshor)r2   r.   ranswers       r   rJ   rJ   X   s     	Q!#AAzQAzAA1uzQ1s8a<#T!c(Q,%:;FMr   c                 2    [        X5      n[        X25      nU$ )N)continued_fractionratioize)xyr2   fractiontotals        r   getrrT   l   s    !!'HX!ELr   c                     U S   U:  a  [         R                  $ [        U 5      S:X  a  U S   $ U S   [        U SS  U5      -   $ )Nr   r-   )r   ZerolenrO   )r7   r2   s     r   rO   rO   s   sF    Aw{vv
4yA~Aw7Xd12h***r   c           	         Sn[        S[        R                  " [        US5      5      -  5      n[	        U5       Vs/ s H  nSPM     nnS[        SU-  5      -  nSn[        [	        S5      USS9 H  n[        U5      U-   n	U[        U	6 -   nM     Xg-  R                  5       n
[        X0U5      U
-  n
[        U
5      n
[	        U5       H  n[        X5      n
M     [        [        X3S-  5      R                  5       U
-  SS9n
[	        U5       H  n[        XU-   5      n
M     [        U
[        5      (       a  U
nOA[        U
[         5      (       a  U
R"                  S   nOU
R"                  S   R"                  S   nSnSn[	        [%        U5      S-  5       H  nXXU-      -  -  nUS-  nM     US:X  a  ['        S	U-  5      e[)        USU-  U5      nU$ s  snf )
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      ?r1   r   r-   T)
repetition)floatingPointz/Order finder returned 0. Happens with chance %f)r6   mathceilr   r5   r   r	   r7   r   expandr   r   r   r   	decompose
isinstancer   r!   rW   r   rT   )r.   r2   epsilonr)   rP   startfactorr:   arr	qbitArraycircuitr>   registerr<   rL   gs                   r   rI   rI   {   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   G)#rC   r\   rG   sympy.core.mulr   sympy.core.singletonr   &sympy.functions.elementary.exponentialr   (sympy.functions.elementary.miscellaneousr   sympy.core.intfuncr   sympy.ntheoryr   rN   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   rJ   rT   rO   rI   r   r   r   <module>ru      sc       " 6 9 # K 0 + F / ) 4	L 	5 4 5 p(+2r   