o
    3Ih%                     @   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   \/home/air/sanwanet/gpt-api/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rtt| jfi  g| jR  }|rt|}|S |}|S t| trt| jfi  | j S t| tr|  \}	}
t|	 }t|
 }|
s|}nt|tr|t|fi   }n
|t|fi   }|| kr|rttt| fi  }|rt|n|}|rt|}|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rt| j||jd g  | j||jd g   fi | S t| j| |  fi | S t trtdd  jD rttrtdd jD rt jtjkrt 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rz
|fi }W n t&y   d }Y nw d }|d u rt%dd }|d urz
| fi }W n t&y   d }Y nw |d u rt t'rtt(rt) }t|t*t+t,frt-|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