K i(dZddlmZddlmZddlmZddlmZddl m Z ddl m Z ddlm Z dd lmZdd lmZdd lmZdd lmZdd lmZddlmZgdZdZGddeZGddeZGddeZdZddZ y)a Grover's algorithm and helper functions. Todo: * W gate construction (or perhaps -W gate based on Mermin's book) * Generalize the algorithm for an unknown function that returns 1 on multiple qubit states, not just one. * Implement _represent_ZGate in OracleGate )pi)sympify)Atom)floor)sqrt)eye) NegativeOne)qapply) QuantumError ComplexSpace)UnitaryOperator)GateIntQubit) OracleGateWGatesuperposition_basisgrover_iteration apply_grovercjdtdzz tfdtdzDS)aCreates an equal superposition of the computational basis. Parameters ========== nqubits : int The number of qubits. Returns ======= state : Qubit An equal superposition of the computational basis with nqubits. Examples ======== Create an equal superposition of 2 qubits:: >>> from sympy.physics.quantum.grover import superposition_basis >>> superposition_basis(2) |0>/2 + |1>/2 + |2>/2 + |3>/2 c3>K|]}t|zyw)nqubitsNr).0namprs b/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/physics/quantum/grover.py z&superposition_basis..<sKAs8Aw//Ks)rsumrange)rrs`@r rr"s22 DG  C Kq'z9JK KKc"eZdZdZdZdZdZy)OracleGateFunctionz2Wrapper for python functions used in `OracleGate`scpt|std|ztj|}||_|S)NzCallable expected, got: %r)callable TypeErrorr__new__function)clsr+objs r r*zOracleGateFunction.__new__As5!88CD Dll3  r$c0t||jfSN)typer+selfs r _hashable_contentz$OracleGateFunction._hashable_contentHsDz4==((r$c |j|Sr/)r+)r2argss r __call__zOracleGateFunction.__call__Kst}}d##r$N)__name__ __module__ __qualname____doc__r*r3r6r$r r&r&>s<)$r$r&cdeZdZdZdZdZedZedZe dZ e dZ dZ dZ y ) ra_A black box gate. The gate marks the desired qubits of an unknown function by flipping the sign of the qubits. The unknown function returns true when it finds its desired qubits and false otherwise. Parameters ========== qubits : int Number of qubits. oracle : callable A callable function that returns a boolean on a computational basis. Examples ======== Apply an Oracle gate that flips the sign of ``|2>`` on different qubits:: >>> from sympy.physics.quantum.qubit import IntQubit >>> from sympy.physics.quantum.qapply import qapply >>> from sympy.physics.quantum.grover import OracleGate >>> f = lambda qubits: qubits == IntQubit(2) >>> v = OracleGate(2, f) >>> qapply(v*IntQubit(2)) -|2> >>> qapply(v*IntQubit(3)) |3> Vct|dk7r td|df}tj|}|djst d|dz|d}t |ts t|}|d|fS)NrzhInsufficient/excessive arguments to Oracle. Please supply the number of qubits and an unknown function.rInteger expected, got: %rr)lenr r _eval_args is_Integerr) isinstancer&)r,r5sub_argsr+s r rAzOracleGate._eval_argsvs t9>G G:"--h7{%%7(1+EF F7($67)(3H X&&r$c$td|dzS)z1This returns the smallest possible Hilbert space.rrr r,r5s r _eval_hilbert_spacezOracleGate._eval_hilbert_spacesAQ''r$c |jdS)z=The unknown function that helps find the sought after qubits.r)labelr1s r search_functionzOracleGate.search_functionszz!}r$cVttt|jdS)zA tuple of target qubits.r)rtupler#r5r1s r targetszOracleGate.targetss!uU499Q<0122r$c |j|jk7r%td|jd|j|j|r| S|S)a Apply this operator to a Qubit subclass. Parameters ========== qubits : Qubit The qubit subclass to apply this operator to. Returns ======= state : Expr The resulting quantum state. zOracleGate operates on  qubits, got: )rr rJ)r2qubitsoptionss r _apply_operator_Qubitz OracleGate._apply_operator_QubitsO >>T\\ )<<1     '7NMr$c d|jz}t|}t|D]8}|jt ||js*t |||f<:|S)zF Represent the OracleGate in the computational basis. rr)rrr#rJrr )r2basisrQnbasis matrixOracleis r _represent_ZGatezOracleGate._represent_ZGates^DLL6{ v 3A##HQ $EF%0] QT" 3r$N)r7r8r9r: gate_namegate_name_latex classmethodrArGpropertyrJrMrRrXr;r$r rrOsm>IO ''"((33> r$rc>eZdZdZdZdZedZedZ dZ y)raGeneral n qubit W Gate in Grover's algorithm. The gate performs the operation ``2|phi> = (tensor product of n Hadamards)*(|0> with n qubits)`` Parameters ========== nqubits : int The number of qubits to operate on Wct|dk7r tdtj|}|djst d|dz|S)Nrz^Insufficient/excessive arguments to W gate. Please supply the number of qubits to operate on.rr?)r@r rrArBr)rFs r rAzWGate._eval_argssX t9>= ))$/Aw!!7$q'AB B r$c htttt|jdS)Nr)rrLreversedr#r5r1s r rMz WGate.targetss$uXeDIIaL&9:;<>T\\ )<<1 +4<<8 T!T\\/22L@''r$N) r7r8r9r:rYrZr[rAr\rMrRr;r$r rrs> IO  ==(r$rc<t|j}||z|zS)aApplies one application of the Oracle and W Gate, WV. Parameters ========== qstate : Qubit A superposition of qubits. oracle : OracleGate The black box operator that flips the sign of the desired basis qubits. Returns ======= Qubit : The qubits after applying the Oracle and W gate. Examples ======== Perform one iteration of grover's algorithm to see a phase change:: >>> from sympy.physics.quantum.qapply import qapply >>> from sympy.physics.quantum.qubit import IntQubit >>> from sympy.physics.quantum.grover import OracleGate >>> from sympy.physics.quantum.grover import superposition_basis >>> from sympy.physics.quantum.grover import grover_iteration >>> numqubits = 2 >>> basis_states = superposition_basis(numqubits) >>> f = lambda qubits: qubits == IntQubit(2) >>> v = OracleGate(numqubits, f) >>> qapply(grover_iteration(basis_states, v)) |2> )rr)qstateoraclewgates r rrs"D &.. !E < r$Nc|dkrtd|z|!ttd|ztdz z}t ||}t |}t |D]}t||}t|}|S)aApplies grover's algorithm. Parameters ========== oracle : callable The unknown callable function that returns true when applied to the desired qubits and false otherwise. Returns ======= state : Expr The resulting state after Grover's algorithm has been iterated. Examples ======== Apply grover's algorithm to an even superposition of 2 qubits:: >>> from sympy.physics.quantum.qapply import qapply >>> from sympy.physics.quantum.qubit import IntQubit >>> from sympy.physics.quantum.grover import apply_grover >>> f = lambda qubits: qubits == IntQubit(2) >>> qapply(apply_grover(f, 2)) |2> rz8Grover's algorithm needs nqubits > 0, received %r qubitsr) r rrrrrr#rr )rgr iterationsviteratediters r rr.s:!| G   47 +RT23 7F#A"7+Hj!$#Ha0(#$ Or$r/)!r:sympy.core.numbersrsympy.core.sympifyrsympy.core.basicr#sympy.functions.elementary.integersr(sympy.functions.elementary.miscellaneousrsympy.matrices.denserr sympy.physics.quantum.qapplyr sympy.physics.quantum.qexprr sympy.physics.quantum.hilbertr sympy.physics.quantum.operatorrsympy.physics.quantum.gatersympy.physics.quantum.qubitr__all__rr&rrrrr;r$r r|ss"&!59$*/46:+0 L8$$"yyx:(D:(z#L+r$