K idZddlZddlZddlmZddlmZddlmZddl m Z ddl m Z ddl mZdd lmZdd lmZdd lmZmZdd lmZdd lmZddlmZGddeZGddeZdZdZ dZ!dZ"y)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)Qubitmeasure_partial_oneshot)qapply)QFT) QuantumErrorc eZdZy)OrderFindingExceptionN)__name__ __module__ __qualname__`/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/physics/quantum/shor.pyrrsrrcVeZdZdZedZedZedZedZ dZ y)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 ctd)Nz%The CMod gate has not been completed.)NotImplementedError)clsargss r _eval_argszCMod._eval_args(s ""IJJrc |jdS)z4Size of 1/2 input register. First 1/2 holds output.rlabelselfs rtzCMod.t/zz!}rc |jdS)z$Base of the controlled mod function.r r"s razCMod.a4r%rc |jdS)z1N is the type of modular arithmetic we are doing.r r"s rNzCMod.N9r%rc d}d}t|jD]}||||j|zzz }|dz}!t|j|z|jz}t |j dd|j}tt|jD]}|j||z dzt|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'rr*N) ranger$intr(r+listrreversedappendr )r#qubitsoptionsnkioutoutarrays r_apply_operator_QubitzCMod._apply_operator_Qubit>s  tvv A 6$&&1*%% %A FA  $&&!)dff$% Aw/0%-( ,A OOSAXN + ,hrN) rrr__doc__ classmethodrpropertyr$r(r+r9rrrrr s^KK  rrctj|dz dz}t||dk7r t||St||}|dzdk(r t |t||dz zdz |t||dz zdz|f}|S)aThis 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_findshor)r+r(ranswers rrArAXs Q!#A AqzQAqzAqA1uz Q1qs8a<#T!ac(Q,%: ;F Mrc6t||}t||}|S)N)continued_fractionratioize)xyr+fractiontotals rgetrrKls!!Q'H Xq !E Lrc|d|kDrtjSt|dk(r|dS|dt|dd|zS)Nrr')rZerolenrF)r/r+s rrFrFssF Aw{vv  4yA~Aw 7Xd12h* **rc d}tdtjt|dz}t |Dcgc]}d}}dt d|zz }d}t t d|dD]}t||z} |t| z}||zj} t|||| z} t| } t |D]} t| | } tt||dzj| zd} t |D]} t| | |z} t| tr| } n|0>`` with k being all possible choices. It then does a controlled mod and a QFT to determine the order of a. g?r*rr'T) repetition) floatingPointz/Order finder returned 0. Happens with chance %f)r.mathceilrr-rr r/r expandrr r r decompose isinstancerrrNrrK)r(r+epsilonr$rGstartfactorr2arr qbitArraycircuitr6registerr4rCgs rr@r@{sG AdiiAq " "#Aa !1Q !E ! tAqDz\F F%(A$7,I% %++,f}$$&G1amG#GWoG 1X6)'156SAaC[**,W4DIG 1X:)'1q59:'5! GS !<<#<<#((, A F 3x=? #!HQUO## F{# = GI I VQT1A HM "s G )#r:rSr>sympy.core.mulrsympy.core.singletonr&sympy.functions.elementary.exponentialr(sympy.functions.elementary.miscellaneousrsympy.core.intfuncr sympy.ntheoryrrEsympy.utilities.iterablesr sympy.physics.quantum.gater sympy.physics.quantum.qubitr r sympy.physics.quantum.qapplyr sympy.physics.quantum.qftrsympy.physics.quantum.qexprrrrrArKrFr@rrrrlsc "69#K0+F/)4 L 5 45 p(+2 r