o
    §ƒðj%  ã                   @   sê   d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZ ddlmZmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZmZ ddlmZmZmZmZ ddl m!Z! dgZ"dd„ Z#dd„ Z$dd„ Z%dd„ Z&dS )z}Logic for applying operators to states.

Todo:
* Sometimes the final result needs to be expanded, we should do this by hand.
é    )ÚSum)ÚAdd)Ú
NumberKind)ÚMul)ÚPow)ÚS)ÚsympifyÚ_sympify)ÚAntiCommutator)Ú
Commutator)ÚDagger)ÚInnerProduct)ÚOuterProductÚOperator)ÚStateÚKetBaseÚBraBaseÚWavefunction)ÚTensorProductÚqapplyc                 C   ó   |   tdd„ ¡S )zETransform the inner products in an expression by calling ``.doit()``.c                  W   ó   t | Ž  ¡ S ©N)r   Údoit©Úargs© r   úž/tmp/claude-1000/-var-www-html-academy/3f42efd1-c2b7-4d08-9b05-30d86bb35479/scratchpad/piper/venv/lib/python3.10/site-packages/sympy/physics/quantum/qapply.pyÚ<lambda>#   ó    zip_doit_func.<locals>.<lambda>)Úreplacer   ©Úer   r   r   Úip_doit_func!   ó   r#   c                 C   r   )z;Transform the sums in an expression by calling ``.doit()``.c                  W   r   r   )r   r   r   r   r   r   r   (   r   zsum_doit_func.<locals>.<lambda>)r    r   r!   r   r   r   Úsum_doit_func&   r$   r%   c                    s  ddl m} ˆ  dd¡}ˆ  dd¡}ˆ  dd¡}t| ƒ} | jtkr)|r't| ƒS | S | jddd�} t| t	ƒr7| S t| t
ƒrRd}| jD ]}|t|fi ˆ ¤Ž7 }qA| ¡ S t| |ƒre‡ fd	d
„| jD ƒ}||Ž S t| tƒrvt‡ fdd
„| jD ƒŽ S t| tƒr—tt| jfi ˆ ¤Žg| j¢R Ž }|r“t|ƒ}|S |}|S t| tƒr¨t| jfi ˆ ¤Ž| j S t| tƒ�r|  ¡ \}	}
t|	Ž }t|
Ž }|
sÁ|}nt|tƒrÑ|t|fi ˆ ¤Ž }n
|t|fi ˆ ¤Ž }|| krí|ríttt| ƒfi ˆ ¤Žƒ}|rót|ƒn|}|rýt|ƒ}|S |}|S | S )aá  Apply operators to states in a quantum expression.

    Parameters
    ==========

    e : Expr
        The expression containing operators and states. This expression tree
        will be walked to find operators acting on states symbolically.
    options : dict
        A dict of key/value pairs that determine how the operator actions
        are carried out.

        The following options are valid:

        * ``dagger``: try to apply Dagger operators to the left
          (default: False).
        * ``ip_doit``: call ``.doit()`` in inner products when they are
          encountered (default: True).
        * ``sum_doit``: call ``.doit()`` on sums when they are encountered
          (default: False). This is helpful for collapsing sums over Kronecker
          delta's that are created when calling ``qapply``.

    Returns
    =======

    e : Expr
        The original expression, but with the operators applied to states.

    Examples
    ========

        >>> from sympy.physics.quantum import qapply, Ket, Bra
        >>> b = Bra('b')
        >>> k = Ket('k')
        >>> A = k * b
        >>> A
        |k><b|
        >>> qapply(A * b.dual / (b * b.dual))
        |k>
        >>> qapply(k.dual * A / (k.dual * k))
        <b|
    r   )ÚDensityÚdaggerFÚsum_doitÚip_doitT)Ú
commutatorÚtensorproductc                    s$   g | ]\}}t |fi ˆ ¤Ž|f‘qS r   ©r   )Ú.0ÚstateÚprob©Úoptionsr   r   Ú
<listcomp>y   s   
 ÿzqapply.<locals>.<listcomp>c                    s   g | ]
}t |fi ˆ ¤Ž‘qS r   r,   )r-   Útr0   r   r   r2      s    )Úsympy.physics.quantum.densityr&   Úgetr	   Úkindr   r#   ÚexpandÚ
isinstancer   r   r   r   r   r   ÚfunctionÚlimitsr%   r   ÚbaseÚexpr   Úargs_cncÚ
qapply_Mulr   )r"   r1   r&   r'   r(   r)   ÚresultÚargÚnew_argsÚc_partÚnc_partÚc_mulÚnc_mulr   r0   r   r   +   s^   +





ÿ

 ÿ

ÿc           	         sT  t | jƒ}tj}d }t|ƒdkst| tƒs| S | ¡ ‰| ¡ ‰ tˆtƒs)t	ˆƒj
s3tˆ tƒs5t	ˆ ƒj
r5| S tˆ tƒrLˆ jjrL| ˆ jˆ jd  ¡ ˆ j‰ tˆ tƒrZ| ˆ j¡ ˆ j‰ tˆtƒreˆj}ˆj‰tˆ ttfƒr¦ˆ  ¡ }t|tƒr•t| j||jd ˆg Ž | j||jd ˆg Ž  fi ˆ¤Ž| S t| j|Ž | ˆ fi ˆ¤Ž| S tˆ tƒrótdd„ ˆ jD ƒƒrótˆtƒrótdd„ ˆjD ƒƒrótˆ jƒtˆjƒkrót‡ ‡‡fdd„ttˆ jƒƒD ƒŽ jdd	�}t| j|Ž fi ˆ¤Ž| | S tˆtƒ�rStˆ tƒ�r4tˆ j ƒ !tˆj ƒ¡�rt"d
ƒ‚ˆ j#ˆj# }ttˆ j$ˆj$ fi ˆ¤Žg|¢R Ž }t| j|Ž | fi ˆ¤ŽS ttˆ ˆj$ fi ˆ¤Žgˆj#¢R Ž }t| j|Ž | fi ˆ¤ŽS tˆ tƒ�rxttˆ j$ˆ fi ˆ¤Žgˆ j#¢R Ž }t| j|Ž | fi ˆ¤ŽS t%ˆ dd ƒ}|d u�ršz
|ˆfi ˆ¤Ž}W n t&�y™   d }Y nw d }|d u �rÃt%ˆdd ƒ}|d u�rÃz
|ˆ fi ˆ¤Ž}W n t&�yÂ   d }Y nw |d u �rÙtˆ t'ƒ�rÙtˆt(ƒ�rÙt)ˆ ˆƒ}t|t*t+t,fƒ�ræt-|ƒS |d u �rt|ƒdk�rô| S t| j|ˆ g Ž fi ˆ¤Žˆ | S t|t)ƒ�r|t| j|Ž fi ˆ¤Ž | S t| j|Ž | fi ˆ¤Ž| S )Né   r   c                 s   ó*   � | ]}t |ttttfƒp|d kV  qdS ©rF   N©r8   r   r   r   r   ©r-   r@   r   r   r   Ú	<genexpr>Ï   ó   €( zqapply_Mul.<locals>.<genexpr>c                 s   rG   rH   rI   rJ   r   r   r   rK   Ð   rL   c                    s,   g | ]}t ˆ j| ˆj|  fi ˆ¤Ž‘qS r   )r   r   )r-   Ún©Úlhsr1   Úrhsr   r   r2   Ò   s   , zqapply_Mul.<locals>.<listcomp>T)r+   z4Duplicated dummy indices in separate sums in qapply.Ú_apply_operatorÚ_apply_from_right_to).Úlistr   r   ÚOneÚlenr8   r   Úpopr   r   Úis_commutativer   r<   Ú
is_IntegerÚappendr;   r   ÚketÚbrar   r
   r   r   r   Úfuncr   ÚallÚranger7   r>   r   ÚsetÚ	variablesÚintersectionÚ
ValueErrorr:   r9   ÚgetattrÚNotImplementedErrorr   r   r   ÚintÚcomplexÚfloatr	   )	r"   r1   r   Úextrar?   Úcommr:   Ú_applyÚ_apply_rightr   rN   r   r>   ¢   s¦   
ÿÿ


ÿÿýü"ÿÿ,$$$
ÿ

ÿ


$r>   N)'Ú__doc__Úsympy.concreter   Úsympy.core.addr   Úsympy.core.kindr   Úsympy.core.mulr   Úsympy.core.powerr   Úsympy.core.singletonr   Úsympy.core.sympifyr   r	   Ú$sympy.physics.quantum.anticommutatorr
   Ú sympy.physics.quantum.commutatorr   Úsympy.physics.quantum.daggerr   Ú"sympy.physics.quantum.innerproductr   Úsympy.physics.quantum.operatorr   r   Úsympy.physics.quantum.stater   r   r   r   Ú#sympy.physics.quantum.tensorproductr   Ú__all__r#   r%   r   r>   r   r   r   r   Ú<module>   s*    ÿ
w