
    \h(                        S 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  SSKJr  SSKJr  SSKJr  SSKJr  / SQrS r " S S\5      r " S S\5      r " S S\5      rS rSS jr g)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)GateIntQubit)
OracleGateWGatesuperposition_basisgrover_iterationapply_groverc                 l   ^ ^ S[        ST -  5      -  m[        UU 4S j[        ST -  5       5       5      $ )a  Creates 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
      c              3   >   >#    U  H  nT[        UTS 9-  v   M     g7f)nqubitsNr   ).0nampr   s     T/var/www/auris/envauris/lib/python3.13/site-packages/sympy/physics/quantum/grover.py	<genexpr>&superposition_basis.<locals>.<genexpr><   s     K9JAs8Aw//9Js   )r   sumrange)r   r   s   `@r    r   r   "   s2    2 DG
CKq'z9JKKK    c                   *    \ rS rSrSrS rS rS rSrg)OracleGateFunction>   z2Wrapper for python functions used in `OracleGate`sc                 z    [        U5      (       d  [        SU-  5      e[        R                  " U 5      nXl        U$ )NzCallable expected, got: %r)callable	TypeErrorr   __new__function)clsr-   objs      r    r,   OracleGateFunction.__new__A   s6    !!88CDDll3
r%   c                 0    [        U 5      U R                  4$ N)typer-   selfs    r    _hashable_content$OracleGateFunction._hashable_contentH   s    Dz4==((r%   c                      U R                   " U6 $ r2   )r-   )r5   argss     r    __call__OracleGateFunction.__call__K   s    }}d##r%    N)	__name__
__module____qualname____firstlineno____doc__r,   r6   r:   __static_attributes__r<   r%   r    r'   r'   >   s    <)$r%   r'   c                   l    \ rS rSrSrSrSr\S 5       r\S 5       r	\
S 5       r\
S 5       rS rS	 rS
rg)r   O   a  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>
Vc                    [        U5      S:w  a  [        S5      eUS   4n[        R                  " U5      nUS   R                  (       d  [        SUS   -  5      eUS   n[        U[        5      (       d  [        U5      nUS   U4$ )Nr   zhInsufficient/excessive arguments to Oracle.  Please supply the number of qubits and an unknown function.r   Integer expected, got: %rr   )lenr   r   
_eval_args
is_Integerr+   
isinstancer'   )r.   r9   sub_argsr-   s       r    rI   OracleGate._eval_argsv   s    t9>G  G:"--h7{%%7(1+EFF7($677)(3HX&&r%   c                 $    [        S5      US   -  $ )z1This returns the smallest possible Hilbert space.r   r   r   r.   r9   s     r    _eval_hilbert_spaceOracleGate._eval_hilbert_space   s     AQ''r%   c                      U R                   S   $ )z=The unknown function that helps find the sought after qubits.r   )labelr4   s    r    search_functionOracleGate.search_function   s     zz!}r%   c                 V    [        [        [        U R                  S   5      5      5      $ )zA tuple of target qubits.r   )r   tupler$   r9   r4   s    r    targetsOracleGate.targets   s!     uU499Q<0122r%   c                     UR                   U R                   :w  a'  [        SU R                   < SUR                   < 35      eU R                  U5      (       a  U* $ U$ )zApply this operator to a Qubit subclass.

Parameters
==========

qubits : Qubit
    The qubit subclass to apply this operator to.

Returns
=======

state : Expr
    The resulting quantum state.
zOracleGate operates on  qubits, got: )r   r   rT   )r5   qubitsoptionss      r    _apply_operator_Qubit OracleGate._apply_operator_Qubit   sR     >>T\\)<<1  ''7NMr%   c                     SU R                   -  n[        U5      n[        U5       H:  nU R                  [	        XPR                   S95      (       d  M,  [        5       XEU4'   M<     U$ )z6
Represent the OracleGate in the computational basis.
r   r   )r   r   r$   rT   r   r	   )r5   basisr]   nbasismatrixOracleis         r    _represent_ZGateOracleGate._represent_ZGate   sX     DLL6{vA##HQ$EFF%0]T"  r%   r<   N)r=   r>   r?   r@   rA   	gate_namegate_name_latexclassmethodrI   rP   propertyrT   rX   r^   re   rB   r<   r%   r    r   r   O   sm    > IO ' '" ( (   3 3>
r%   r   c                   F    \ rS rSrSrSrSr\S 5       r\	S 5       r
S rSrg)	r      a  General n qubit W Gate in Grover's algorithm.

The gate performs the operation ``2|phi><phi| - 1`` on some qubits.
``|phi> = (tensor product of n Hadamards)*(|0> with n qubits)``

Parameters
==========

nqubits : int
    The number of qubits to operate on

Wc                     [        U5      S:w  a  [        S5      e[        R                  " U5      nUS   R                  (       d  [        SUS   -  5      eU$ )Nr   z^Insufficient/excessive arguments to W gate.  Please supply the number of qubits to operate on.r   rG   )rH   r   r   rI   rJ   r+   rO   s     r    rI   WGate._eval_args   sX    t9>=  ))$/Aw!!7$q'ABBr%   c           
      h    [        [        [        [        U R                  S   5      5      5      5      $ )Nr   )r   rW   reversedr$   r9   r4   s    r    rX   WGate.targets   s$    uXeDIIaL&9:;<<r%   c                     UR                   U R                   :w  a'  [        SU R                   < SUR                   < 35      e[        U R                   5      nS[        SU R                   -  5      -  U-  nXA-
  $ )zV
qubits: a set of qubits (Qubit)
Returns: quantum object (quantum expression - QExpr)
zWGate operates on r[   r   )r   r   r   r   )r5   r\   r]   basis_stateschange_to_basiss        r    r^   WGate._apply_operator_Qubit   sh    
 >>T\\)<<1  +4<<8T!T\\/22L@''r%   r<   N)r=   r>   r?   r@   rA   rg   rh   ri   rI   rj   rX   r^   rB   r<   r%   r    r   r      s>     IO	 	 = =(r%   r   c                 :    [        UR                  5      nX!-  U -  $ )a  Applies 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>

)r   r   )qstateoraclewgates      r    r   r     s     D &..!E<r%   Nc                     US::  a  [        SU-  5      eUc!  [        [        SU-  5      [        S-  -  5      n[	        X5      n[        U5      n[        U5       H  n[        XC5      n[        U5      nM     U$ )az  Applies 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>

r   z8Grover's algorithm needs nqubits > 0, received %r qubitsr      )	r   r   r   r   r   r   r$   r   r
   )ry   r   
iterationsviteratediters         r    r   r   .  s    : !|G
 	
 47
+RT23
7#A"7+Hj!#H0(# " Or%   r2   )!rA   sympy.core.numbersr   sympy.core.sympifyr   sympy.core.basicr   #sympy.functions.elementary.integersr   (sympy.functions.elementary.miscellaneousr   sympy.matrices.denser   r	   sympy.physics.quantum.qapplyr
   sympy.physics.quantum.qexprr   sympy.physics.quantum.hilbertr   sympy.physics.quantum.operatorr   sympy.physics.quantum.gater   sympy.physics.quantum.qubitr   __all__r   r'   r   r   r   r   r<   r%   r    <module>r      ss    " & ! 5 9 $ * / 4 6 : + 0L8$ $"y yx:(D :(z#L+r%   