K i$dZddlmZddlmZmZmZddlmZddl m Z ddl m Z ddl mZddlmZdd lmZmZdd lmZdd lmZdd lmZmZmZmZmZmZmZm Z dd l!m"Z"gdZ#GddeZ$e$Z%GddeZ&Gdde&Z'Gdde&Z(y)atAn implementation of qubits and gates acting on them. Todo: * Update docstrings. * Update tests. * Implement apply using decompose. * Implement represent using decompose or something smarter. For this to work we first have to implement represent for SWAP. * Decide if we want upper index to be inclusive in the constructor. * Fix the printing of Rk gates in plotting. )Expr)IIntegerpiSymbol)exp)Matrix)sqrt)qapply) QuantumErrorQExpr)eye)matrix_tensor_product)Gate HadamardGateSwapGate OneQubitGateCGate PhaseGateTGateZGate)sign)QFTIQFTRkGateRkcfeZdZdZdZdZdZedZe dZ e dZ e dZ d d Z y ) rz This is the R_k gate of the QTF.rRc,t|dk7rtd|z|d}|d}|dk(r t|S|dk(r t|S|dk(r t |S|j |}t j|g|}|j||_ |S)Nz)Rk gates only take two arguments, got: %rr) lenr rrr _eval_argsr__new___eval_hilbert_space hilbert_space)clsargstargetkinsts _/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/physics/quantum/qft.pyr&zRkGate.__new__1s t9>;dB  a G 6= !VV$ $ !V= ~~d#||C'$' 44T: c,tj|SN)rr%)r)r*s r.r%zRkGate._eval_argsFs%%r/c |jdSNr"labelselfs r.r,zRkGate.kLzz!}r/c |jddSr3r4r6s r.targetszRkGate.targetsPszz"1~r/cNd|jdt|jdS)N$_)gate_name_latexstrr,r6s r.gate_name_plotzRkGate.gate_name_plotTs!113tvv;??r/c |dk(ritddgdtt|jt dzt zt zt dt|jzz ggStd|z)Nsympyr"rr!z#Invalid format for the R_k gate: %r) r r rr,rrrabsNotImplementedError)r7formats r.get_target_matrixzRkGate.get_target_matrixXst W Aq6As4< +B2+Ea+GQRUXY]Y_Y_U`I`+a'b#cde e! 1F :< r& classmethodr%propertyr,r:r@rFr/r.rr,sk*IO*&& @@E$K9'( aC$JT #9OO'? ::a=A ,SDJJqM1A-BHMH ??W $,#a'DOO";<=?H9Os&D8D DDcNt|jd|jdS)Nrr")r\r5r6s r.r:zFourier.targetssTZZ]DJJqM22r/c |jdSr3r4r6s r.rYzFourier.min_qubitsr8r/cFd|jd|jdz zS)z"Size is the size of the QFT matrixr!r"rr4r6s r.rZz Fourier.sizes$4::a=4::a=011r/ctdS)Nr[rr6s r.r[z Fourier.omegas gr/N) rGrHrIrJrLr%rUrSrMr:rYrZr[rNr/r.rPrPbsqJ%%6:3322r/rPc:eZdZdZdZdZdZdZdZe dZ y)rz&The forward quantum Fourier transform.c \|jd}|jd}d}tt||D]E}t||z}t||z D]$}t ||z dz t ||dz|z}&Gt||z dzD]}t ||z||z dz |z}|S)z%Decomposes QFT into elementary gates.rr"r!)r5reversedr\rrrr)r7startfinishcircuitlevelr_s r. decomposez QFT.decomposes 1 AeE623 ME"5)'1G55=) M A veQU/CDWL M M*+ BAq5y&1*q.9'AG Br/c :t|j|zSr1)r rm)r7qubitsrTs r._apply_operator_QubitzQFT._apply_operator_Qubitsdnn&v-..r/c&t|jSr1)rr*r6s r. _eval_inversezQFT._eval_inversesTYYr/cNtdtztz|jz S)Nr!r rrrZr6s r.r[z QFT.omegas1R46$))#$$r/N) rGrHrIrJrKr>rmrprrrMr[rNr/r.rrs20IO / %%r/rc4eZdZdZdZdZdZdZedZ y)rz&The inverse quantum Fourier transform.z {QFT^{-1}}c ^|jd}|jd}d}t||z dzD]}t||z||z dz |z}t||D]O}tt||z D]%}t ||z dz t || dz |z}'t ||z}Q|S)z&Decomposes IQFT into elementary gates.rr"r!)r*r\rrhrrr)r7rirjrkr_rls r.rmzIQFT.decomposes ! 1*+ BAq5y&1*q.9'AG B5&) 2EeEEM23 N A veaR!V/DEgM N"5)'1G 2r/c&t|jSr1)rr*r6s r.rrzIQFT._eval_inversesDIIr/cNtdtztz|jz S)Nrtr6s r.r[z IQFT.omegas2b57499$%%r/N) rGrHrIrJrKr>rmrrrMr[rNr/r.rrs-0I"O &&r/rN))rJsympy.core.exprrsympy.core.numbersrrrsympy.core.symbolr&sympy.functions.elementary.exponentialr sympy.matrices.denser sympy.functionsr sympy.physics.quantum.qapplyr sympy.physics.quantum.qexprr rsympy.matricesr#sympy.physics.quantum.tensorproductrsympy.physics.quantum.gaterrrrrrrr$sympy.functions.elementary.complexesr__all__rrrPrrrNr/r.rs !//$6' /;E6 0<\0