o
    jgL                     @   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 g d	ZG d
d deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZdS )zMHilbert spaces for quantum mechanics.

Authors:
* Brian Granger
* Matt Curry
    )reduce)Basic)S)sympify)Interval
prettyForm)QuantumError)HilbertSpaceErrorHilbertSpaceTensorProductHilbertSpaceTensorPowerHilbertSpaceDirectSumHilbertSpaceComplexSpaceL2	FockSpacec                   @   s   e Zd ZdS )r
   N)__name__
__module____qualname__ r   r   U/var/www/html/zoom/venv/lib/python3.10/site-packages/sympy/physics/quantum/hilbert.pyr
   "   s    r
   c                   @   sn   e Zd ZdZdd Zedd Zdd Zdd	 Zd
d Z	dd Z
dddZdd Zdd Zdd Zdd ZdS )r   a  An abstract Hilbert space for quantum mechanics.

    In short, a Hilbert space is an abstract vector space that is complete
    with inner products defined [1]_.

    Examples
    ========

    >>> from sympy.physics.quantum.hilbert import HilbertSpace
    >>> hs = HilbertSpace()
    >>> hs
    H

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Hilbert_space
    c                 C      t | }|S Nr   __new__clsobjr   r   r   r   >      
zHilbertSpace.__new__c                 C   s   t d)z*Return the Hilbert dimension of the space.z$This Hilbert space has no dimension.)NotImplementedErrorselfr   r   r   	dimensionB   s   zHilbertSpace.dimensionc                 C   
   t | |S r   r   r!   otherr   r   r   __add__G      
zHilbertSpace.__add__c                 C   
   t || S r   r$   r%   r   r   r   __radd__J   r(   zHilbertSpace.__radd__c                 C   r#   r   r   r%   r   r   r   __mul__M   r(   zHilbertSpace.__mul__c                 C   r)   r   r+   r%   r   r   r   __rmul__P   r(   zHilbertSpace.__rmul__Nc                 C   s   |d urt dt| |S )NzNThe third argument to __pow__ is not supported             for Hilbert spaces.)
ValueErrorr   )r!   r&   modr   r   r   __pow__S   s   
zHilbertSpace.__pow__c                 C   s   |j j| jkr	dS dS )zIs the operator or state in this Hilbert space.

        This is checked by comparing the classes of the Hilbert spaces, not
        the instances. This is to allow Hilbert Spaces with symbolic
        dimensions.
        TF)hilbert_space	__class__r%   r   r   r   __contains__Y   s   zHilbertSpace.__contains__c                 G      dS NHr   r!   printerargsr   r   r   	_sympystre      zHilbertSpace._sympystrc                 G      d}t |S r5   r   r!   r8   r9   ustrr   r   r   _prettyh      zHilbertSpace._prettyc                 G   r4   )Nz\mathcal{H}r   r7   r   r   r   _latexl   r;   zHilbertSpace._latexr   )r   r   r   __doc__r   propertyr"   r'   r*   r,   r-   r0   r3   r:   r?   rA   r   r   r   r   r   *   s    

r   c                   @   sP   e Zd ZdZdd Zedd Zedd Zdd	 Z	d
d Z
dd Zdd ZdS )r   a%  Finite dimensional Hilbert space of complex vectors.

    The elements of this Hilbert space are n-dimensional complex valued
    vectors with the usual inner product that takes the complex conjugate
    of the vector on the right.

    A classic example of this type of Hilbert space is spin-1/2, which is
    ``ComplexSpace(2)``. Generalizing to spin-s, the space is
    ``ComplexSpace(2*s+1)``.  Quantum computing with N qubits is done with the
    direct product space ``ComplexSpace(2)**N``.

    Examples
    ========

    >>> from sympy import symbols
    >>> from sympy.physics.quantum.hilbert import ComplexSpace
    >>> c1 = ComplexSpace(2)
    >>> c1
    C(2)
    >>> c1.dimension
    2

    >>> n = symbols('n')
    >>> c2 = ComplexSpace(n)
    >>> c2
    C(n)
    >>> c2.dimension
    n

    c                 C   s0   t |}| |}t|tr|S t| |}|S r   )r   eval
isinstancer   r   )r   r"   rr   r   r   r   r      s   

zComplexSpace.__new__c                 C   sz   t | dkr#|jr|dks|tju s|js!td| d S d S d S | D ]}|js:|tju s:|js:td| q'd S )N   r   zRThe dimension of a ComplexSpace can onlybe a positive integer, oo, or a Symbol: %rzNThe dimension of a ComplexSpace can only contain integers, oo, or a Symbol: %r)lenatoms
is_Integerr   Infinity	is_Symbol	TypeError)r   r"   dimr   r   r   rD      s   zComplexSpace.evalc                 C   
   | j d S Nr   r9   r    r   r   r   r"         
zComplexSpace.dimensionc                 G   s    d| j j|j| jg|R  f S )Nz%s(%s))r2   r   _printr"   r7   r   r   r   
_sympyrepr   s   zComplexSpace._sympyreprc                 G      d|j | jg|R   S )NzC(%s)rS   r"   r7   r   r   r   r:         zComplexSpace._sympystrc                 G   s(   d}|j | jg|R  }t|}|| S )NC)rS   r"   r   )r!   r8   r9   r>   	pform_exp
pform_baser   r   r   r?      s   zComplexSpace._prettyc                 G   rU   )Nz\mathcal{C}^{%s}rV   r7   r   r   r   rA      rW   zComplexSpace._latexN)r   r   r   rB   r   classmethodrD   rC   r"   rT   r:   r?   rA   r   r   r   r   r   p   s    

r   c                   @   sP   e Zd ZdZdd Zedd Zedd Zdd	 Zd
d Z	dd Z
dd ZdS )r   a  The Hilbert space of square integrable functions on an interval.

    An L2 object takes in a single SymPy Interval argument which represents
    the interval its functions (vectors) are defined on.

    Examples
    ========

    >>> from sympy import Interval, oo
    >>> from sympy.physics.quantum.hilbert import L2
    >>> hs = L2(Interval(0,oo))
    >>> hs
    L2(Interval(0, oo))
    >>> hs.dimension
    oo
    >>> hs.interval
    Interval(0, oo)

    c                 C   s&   t |tstd| t| |}|S )Nz,L2 interval must be an Interval instance: %r)rE   r   rM   r   r   )r   intervalr   r   r   r   r      s   
z
L2.__new__c                 C      t jS r   r   rK   r    r   r   r   r"         zL2.dimensionc                 C   rO   rP   rQ   r    r   r   r   r\      rR   zL2.intervalc                 G   rU   NzL2(%s)rS   r\   r7   r   r   r   rT      rW   zL2._sympyreprc                 G   rU   r`   ra   r7   r   r   r   r:      rW   zL2._sympystrc                 G   s   t d}t d}|| S )N2Lr   r!   r8   r9   rY   rZ   r   r   r   r?      s   z
L2._prettyc                 G   s   |j | jg|R  }d| S )Nz {\mathcal{L}^2}\left( %s \right)ra   )r!   r8   r9   r\   r   r   r   rA      s   z	L2._latexN)r   r   r   rB   r   rC   r"   r\   rT   r:   r?   rA   r   r   r   r   r      s    

r   c                   @   sD   e Zd ZdZdd Zedd Zdd Zdd	 Zd
d Z	dd Z
dS )r   a  The Hilbert space for second quantization.

    Technically, this Hilbert space is a infinite direct sum of direct
    products of single particle Hilbert spaces [1]_. This is a mess, so we have
    a class to represent it directly.

    Examples
    ========

    >>> from sympy.physics.quantum.hilbert import FockSpace
    >>> hs = FockSpace()
    >>> hs
    F
    >>> hs.dimension
    oo

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Fock_space
    c                 C   r   r   r   r   r   r   r   r     r   zFockSpace.__new__c                 C   r]   r   r^   r    r   r   r   r"     r_   zFockSpace.dimensionc                 G   r4   )NzFockSpace()r   r7   r   r   r   rT     r;   zFockSpace._sympyreprc                 G   r4   NFr   r7   r   r   r   r:     r;   zFockSpace._sympystrc                 G   r<   re   r   r=   r   r   r   r?     r@   zFockSpace._prettyc                 G   r4   )Nz\mathcal{F}r   r7   r   r   r   rA     r;   zFockSpace._latexN)r   r   r   rB   r   rC   r"   rT   r:   r?   rA   r   r   r   r   r      s    
r   c                   @   sd   e Zd ZdZdd Zedd Zedd Zedd	 Z	d
d Z
dd Zdd Zdd Zdd ZdS )r   a  A tensor product of Hilbert spaces [1]_.

    The tensor product between Hilbert spaces is represented by the
    operator ``*`` Products of the same Hilbert space will be combined into
    tensor powers.

    A ``TensorProductHilbertSpace`` object takes in an arbitrary number of
    ``HilbertSpace`` objects as its arguments. In addition, multiplication of
    ``HilbertSpace`` objects will automatically return this tensor product
    object.

    Examples
    ========

    >>> from sympy.physics.quantum.hilbert import ComplexSpace, FockSpace
    >>> from sympy import symbols

    >>> c = ComplexSpace(2)
    >>> f = FockSpace()
    >>> hs = c*f
    >>> hs
    C(2)*F
    >>> hs.dimension
    oo
    >>> hs.spaces
    (C(2), F)

    >>> c1 = ComplexSpace(2)
    >>> n = symbols('n')
    >>> c2 = ComplexSpace(n)
    >>> hs = c1*c2
    >>> hs
    C(2)*C(n)
    >>> hs.dimension
    2*n

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Hilbert_space#Tensor_products
    c                 G   .   |  |}t|tr|S tj| g|R  }|S r   rD   rE   r   r   r   r9   rF   r   r   r   r   r   H  
   

z!TensorProductHilbertSpace.__new__c                 C   sR  g }d}|D ]"}t |tr||j d}qt |ttfr#|| qtd| g }d}|D ][}|durt |trOt |trO|j|jkrO|j|j	|j	  }q/t |tra|j|kra||j	d  }q/t |trs||jkrs||j	d  }q/||kr||d }q/|| |}q/|du r|}q/|| |rt| S t
|dkrt|d j|d j	S dS )Evaluates the direct product.FTzQHilbert spaces can only be multiplied by                 other Hilbert spaces: %rNrG      r   )rE   r   extendr9   r   r   appendrM   baseexprH   )r   r9   new_argsrecallarg	comb_argsprev_argnew_argr   r   r   rD   O  sN   








zTensorProductHilbertSpace.evalc                 C   .   dd | j D }tj|v rtjS tdd |S )Nc                 S      g | ]}|j qS r   r"   .0rs   r   r   r   
<listcomp>~      z7TensorProductHilbertSpace.dimension.<locals>.<listcomp>c                 S   s   | | S r   r   xyr   r   r   <lambda>      z5TensorProductHilbertSpace.dimension.<locals>.<lambda>r9   r   rK   r   r!   arg_listr   r   r   r"   |     
z#TensorProductHilbertSpace.dimensionc                 C      | j S )z5A tuple of the Hilbert spaces in this tensor product.rQ   r    r   r   r   spaces     z TensorProductHilbertSpace.spacesc                 G   sB   g }| j D ]}|j|g|R  }t|trd| }|| q|S )Nz(%s))r9   rS   rE   r   rn   )r!   r8   r9   spaces_strsrs   sr   r   r   _spaces_printer  s   

z)TensorProductHilbertSpace._spaces_printerc                 G   s    | j |g|R  }dd| S )NzTensorProductHilbertSpace(%s),r   joinr!   r8   r9   spaces_reprsr   r   r   rT     s   z$TensorProductHilbertSpace._sympyreprc                 G   s   | j |g|R  }d|S )N*r   r!   r8   r9   r   r   r   r   r:     s   
z#TensorProductHilbertSpace._sympystrc                 G      t | j}|jdg|R  }t|D ]@}|j| j| g|R  }t| j| ttfr3t|jddd }t|	| }||d krR|j
rKt|	d }qt|	d }q|S )N ()leftrightrG   u    ⨂ z x rH   r9   rS   rangerE   r   r   r   parensr   _use_unicoder!   r8   r9   lengthpformi
next_pformr   r   r   r?     "   
z!TensorProductHilbertSpace._prettyc                 G   p   t | j}d}t|D ]*}|j| j| g|R  }t| j| ttfr'd| }|| }||d kr5|d }q|S )Nr   \left(%s\right)rG   z\otimes rH   r9   r   rS   rE   r   r   r!   r8   r9   r   r   r   arg_sr   r   r   rA        
z TensorProductHilbertSpace._latexN)r   r   r   rB   r   r[   rD   rC   r"   r   r   rT   r:   r?   rA   r   r   r   r   r     s    *
,

	r   c                   @   s\   e Zd ZdZdd Zedd Zedd Zedd	 Z	d
d Z
dd Zdd Zdd ZdS )r   a  A direct sum of Hilbert spaces [1]_.

    This class uses the ``+`` operator to represent direct sums between
    different Hilbert spaces.

    A ``DirectSumHilbertSpace`` object takes in an arbitrary number of
    ``HilbertSpace`` objects as its arguments. Also, addition of
    ``HilbertSpace`` objects will automatically return a direct sum object.

    Examples
    ========

    >>> from sympy.physics.quantum.hilbert import ComplexSpace, FockSpace

    >>> c = ComplexSpace(2)
    >>> f = FockSpace()
    >>> hs = c+f
    >>> hs
    C(2)+F
    >>> hs.dimension
    oo
    >>> list(hs.spaces)
    [C(2), F]

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Hilbert_space#Direct_sums
    c                 G   rg   r   rh   ri   r   r   r   r     rj   zDirectSumHilbertSpace.__new__c                 C   s^   g }d}|D ] }t |tr||j d}qt |tr!|| qtd| |r-t| S dS )rk   FTzOHilbert spaces can only be summed with other                 Hilbert spaces: %rN)rE   r   rm   r9   r   rn   rM   )r   r9   rq   rr   rs   r   r   r   rD     s   

zDirectSumHilbertSpace.evalc                 C   rw   )Nc                 S   rx   r   ry   rz   r   r   r   r|     r}   z3DirectSumHilbertSpace.dimension.<locals>.<listcomp>c                 S   s   | | S r   r   r~   r   r   r   r     r   z1DirectSumHilbertSpace.dimension.<locals>.<lambda>r   r   r   r   r   r"     r   zDirectSumHilbertSpace.dimensionc                 C   r   )z1A tuple of the Hilbert spaces in this direct sum.rQ   r    r   r   r   r     r   zDirectSumHilbertSpace.spacesc                    s$    fdd| j D }dd| S )Nc                       g | ]}j |g R  qS r   rS   rz   r9   r8   r   r   r|         z4DirectSumHilbertSpace._sympyrepr.<locals>.<listcomp>zDirectSumHilbertSpace(%s)r   r9   r   r   r   r   r   rT      s   z DirectSumHilbertSpace._sympyreprc                    s     fdd| j D }d|S )Nc                    r   r   r   rz   r   r   r   r|     r   z3DirectSumHilbertSpace._sympystr.<locals>.<listcomp>+r   r   r   r   r   r:     s   
zDirectSumHilbertSpace._sympystrc                 G   r   )Nr   r   r   r   rG   u    ⊕ z + r   r   r   r   r   r?     r   zDirectSumHilbertSpace._prettyc                 G   r   )Nr   r   rG   z\oplus r   r   r   r   r   rA     r   zDirectSumHilbertSpace._latexN)r   r   r   rB   r   r[   rD   rC   r"   r   rT   r:   r?   rA   r   r   r   r   r     s    


r   c                   @   sh   e Zd ZdZdd Zedd Zedd Zedd	 Z	ed
d Z
dd Zdd Zdd Zdd ZdS )r   a  An exponentiated Hilbert space [1]_.

    Tensor powers (repeated tensor products) are represented by the
    operator ``**`` Identical Hilbert spaces that are multiplied together
    will be automatically combined into a single tensor power object.

    Any Hilbert space, product, or sum may be raised to a tensor power. The
    ``TensorPowerHilbertSpace`` takes two arguments: the Hilbert space; and the
    tensor power (number).

    Examples
    ========

    >>> from sympy.physics.quantum.hilbert import ComplexSpace, FockSpace
    >>> from sympy import symbols

    >>> n = symbols('n')
    >>> c = ComplexSpace(2)
    >>> hs = c**n
    >>> hs
    C(2)**n
    >>> hs.dimension
    2**n

    >>> c = ComplexSpace(2)
    >>> c*c
    C(2)**2
    >>> f = FockSpace()
    >>> c*f*f
    C(2)*F**2

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Hilbert_space#Tensor_products
    c                 G   s*   |  |}t|tr|S tj| g|R  S r   rh   )r   r9   rF   r   r   r   r   N  s   

zTensorPowerHilbertSpace.__new__c                 C   s   |d t |d f}|d }|tju r|d S |tju rtjS t| dkr9|jr.|dks7|js7td| |S | D ]}|jsK|jsKtd| q=|S )Nr   rG   zUHilbert spaces can only be raised to                 positive integers or Symbols: %rzJTensor powers can only contain integers                     or Symbols: %r)	r   r   OneZerorH   rI   rJ   rL   r.   )r   r9   rq   rp   powerr   r   r   rD   T  s&   

zTensorPowerHilbertSpace.evalc                 C   rO   rP   rQ   r    r   r   r   ro   j  rR   zTensorPowerHilbertSpace.basec                 C   rO   )NrG   rQ   r    r   r   r   rp   n  rR   zTensorPowerHilbertSpace.expc                 C   s"   | j jtju r
tjS | j j| j S r   )ro   r"   r   rK   rp   r    r   r   r   r"   r  s   z!TensorPowerHilbertSpace.dimensionc                 G   ,   d|j | jg|R  |j | jg|R  f S )NzTensorPowerHilbertSpace(%s,%s)rS   ro   rp   r7   r   r   r   rT   y  s
   z"TensorPowerHilbertSpace._sympyreprc                 G   r   )Nz%s**%sr   r7   r   r   r   r:   }  s   z!TensorPowerHilbertSpace._sympystrc                 G   s\   |j | jg|R  }|jrt|td }n	t|td }|j | jg|R  }|| S )Nu   ⨂r   )rS   rp   r   r   r   ro   rd   r   r   r   r?     s   zTensorPowerHilbertSpace._prettyc                 G   s4   |j | jg|R  }|j | jg|R  }d||f S )Nz{%s}^{\otimes %s}r   )r!   r8   r9   ro   rp   r   r   r   rA     s   zTensorPowerHilbertSpace._latexN)r   r   r   rB   r   r[   rD   rC   ro   rp   r"   rT   r:   r?   rA   r   r   r   r   r   (  s    %



	r   N)rB   	functoolsr   sympy.core.basicr   sympy.core.singletonr   sympy.core.sympifyr   sympy.sets.setsr    sympy.printing.pretty.stringpictr   sympy.physics.quantum.qexprr	   __all__r
   r   r   r   r   r   r   r   r   r   r   r   <module>   s$    FL4- n