
    \hL                        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	Qr " S
 S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      rg)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                       \ rS rSrSrg)r   "    N)__name__
__module____qualname____firstlineno____static_attributes__r       U/var/www/auris/envauris/lib/python3.13/site-packages/sympy/physics/quantum/hilbert.pyr   r   "   s    r   r   c                   h    \ rS rSrSrS r\S 5       rS rS r	S r
S rSS
 jrS rS rS rS rSrg	)r   *   aV  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                 2    [         R                  " U 5      nU$ Nr   __new__clsobjs     r   r"   HilbertSpace.__new__>       mmC 
r   c                     [        S5      e)z*Return the Hilbert dimension of the space.z$This Hilbert space has no dimension.)NotImplementedErrorselfs    r   	dimensionHilbertSpace.dimensionB   s     ""HIIr   c                     [        X5      $ r    r   r+   others     r   __add__HilbertSpace.__add__G   s    $T11r   c                     [        X5      $ r    r/   r0   s     r   __radd__HilbertSpace.__radd__J   s    $U11r   c                     [        X5      $ r    r   r0   s     r   __mul__HilbertSpace.__mul__M   s    (55r   c                     [        X5      $ r    r8   r0   s     r   __rmul__HilbertSpace.__rmul__P   s    (55r   Nc                 4    Ub  [        S5      e[        X5      $ )NzNThe third argument to __pow__ is not supported             for Hilbert spaces.)
ValueErrorr   )r+   r1   mods      r   __pow__HilbertSpace.__pow__S   s$    ? ! " "&t33r   c                 N    UR                   R                  U R                  :X  a  gg)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__r0   s     r   __contains__HilbertSpace.__contains__Y   s"     ((DNN:r   c                     gNHr   r+   printerargss      r   	_sympystrHilbertSpace._sympystre       r   c                     Sn[        U5      $ rI   r   r+   rL   rM   ustrs       r   _prettyHilbertSpace._prettyh       +$r   c                     g)Nz\mathcal{H}r   rK   s      r   _latexHilbertSpace._latexl       r   r   r    )r   r   r   r   __doc__r"   propertyr,   r2   r5   r9   r<   rA   rF   rN   rT   rX   r   r   r   r   r   r   *   sO    & J J22664
 r   r   c                   V    \ rS rSrSrS r\S 5       r\S 5       r	S r
S rS rS	 rS
rg)r   p   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                     [        U5      nU R                  U5      n[        U[        5      (       a  U$ [        R                  " X5      nU$ r    )r   eval
isinstancer   r"   )r$   r,   rr%   s       r   r"   ComplexSpace.__new__   s?    I&	HHYaHmmC+
r   c                    [        UR                  5       5      S:X  aL  UR                  (       a  US:  d4  U[        R                  L d   UR
                  (       d  [        SU-  5      eg g g UR                  5        HJ  nUR                  (       a  M  U[        R                  L a  M+  UR
                  (       a  M>  [        SU-  5      e   g )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,   dims      r   r`   ComplexSpace.eval   s    y !Q&((Y]i1::>U"" !M"+!, - - # ?V] !(#*;s}}}# %M&)%* + + )r   c                      U R                   S   $ Nr   rM   r*   s    r   r,   ComplexSpace.dimension       yy|r   c                 t    U R                   R                  < SUR                  " U R                  /UQ76 < S3$ )N())rE   r   _printr,   rK   s      r   
_sympyreprComplexSpace._sympyrepr   s0    >>22">>$..@4@B 	Br   c                 B    SUR                   " U R                  /UQ76 -  $ )NzC(%s)rv   r,   rK   s      r   rN   ComplexSpace._sympystr   s    >>>>r   c                 ^    SnUR                   " U R                  /UQ76 n[        U5      nXT-  $ )NC)rv   r,   r	   )r+   rL   rM   rS   	pform_exp
pform_bases         r   rT   ComplexSpace._pretty   s1    +NN4>>9D9	%
$$r   c                 B    SUR                   " U R                  /UQ76 -  $ )Nz\mathcal{C}^{%s}rz   rK   s      r   rX   ComplexSpace._latex   s    "W^^DNN%JT%JJJr   r   N)r   r   r   r   r[   r"   classmethodr`   r\   r,   rw   rN   rT   rX   r   r   r   r   r   r   p   sJ    > + +  B?%Kr   r   c                   V    \ rS rSrSrS r\S 5       r\S 5       rS r	S r
S rS	 rS
rg)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                 x    [        U[        5      (       d  [        SU-  5      e[        R                  " X5      nU$ )Nz,L2 interval must be an Interval instance: %r)ra   r   rk   r   r"   )r$   intervalr%   s      r   r"   
L2.__new__   s:    (H--J  mmC*
r   c                 "    [         R                  $ r    r   ri   r*   s    r   r,   L2.dimension       zzr   c                      U R                   S   $ ro   rp   r*   s    r   r   L2.interval   rr   r   c                 B    SUR                   " U R                  /UQ76 -  $ NzL2(%s)rv   r   rK   s      r   rw   L2._sympyrepr       '..>>>>r   c                 B    SUR                   " U R                  /UQ76 -  $ r   r   rK   s      r   rN   L2._sympystr   r   r   c                 6    [        S5      n[        S5      nXC-  $ )N2Lr   r+   rL   rM   r~   r   s        r   rT   
L2._pretty   s    sO	_
$$r   c                 F    UR                   " U R                  /UQ76 nSU-  $ )Nz {\mathcal{L}^2}\left( %s \right)r   )r+   rL   rM   r   s       r   rX   	L2._latex   s$    >>$--7$72X==r   r   N)r   r   r   r   r[   r"   r\   r,   r   rw   rN   rT   rX   r   r   r   r   r   r      sH    (    ??%
>r   r   c                   F    \ rS rSrSrS r\S 5       rS rS r	S r
S rS	rg
)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                 2    [         R                  " U 5      nU$ r    r!   r#   s     r   r"   FockSpace.__new__  r'   r   c                 "    [         R                  $ r    r   r*   s    r   r,   FockSpace.dimension  r   r   c                     g)NzFockSpace()r   rK   s      r   rw   FockSpace._sympyrepr  s    r   c                     gNFr   rK   s      r   rN   FockSpace._sympystr  rP   r   c                     Sn[        U5      $ r   r   rR   s       r   rT   FockSpace._pretty  rV   r   c                     g)Nz\mathcal{F}r   rK   s      r   rX   FockSpace._latex  rZ   r   r   N)r   r   r   r   r[   r"   r\   r,   rw   rN   rT   rX   r   r   r   r   r   r      s4    ,   r   r   c                   l    \ rS rSrSrS r\S 5       r\S 5       r	\S 5       r
S rS rS	 rS
 rS rSrg)r   i  af  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                     U R                  U5      n[        U[        5      (       a  U$ [        R                  " U /UQ76 nU$ r    r`   ra   r   r"   r$   rM   rb   r%   s       r   r"   !TensorProductHilbertSpace.__new__H  9    HHTNaHmmC'$'
r   c                    / nSnU Hq  n[        U[        5      (       a  UR                  UR                  5        SnM7  [        U[        [
        45      (       a  UR                  U5        Me  [        SU-  5      e   / nSnU GH  nUb  [        U[
        5      (       aW  [        U[
        5      (       aB  UR                  UR                  :X  a(  UR                  UR                  UR                  -   -  nMs  [        U[
        5      (       a#  UR                  U:X  a  XgR                  S-   -  nM  [        U[
        5      (       a"  XvR                  :X  a  XvR                  S-   -  nM  Xv:X  a  US-  nM  UR                  U5        UnGM  Ub  GM
  UnGM     UR                  U5        U(       a  [        U6 $ [        U5      S:X  a&  [        US   R                  US   R                  5      $ g)Evaluates the direct product.FTzQHilbert spaces can only be multiplied by                 other Hilbert spaces: %rNre      r   )ra   r   extendrM   r   r   appendrk   baseexprf   )r$   rM   new_argsrecallarg	comb_argsprev_argnew_args           r   r`   TensorProductHilbertSpace.evalO  s    C#899)C,0G!HII$ !*,/!0 1 1  	G#g'>??x)@AA5&||gkkHLL.HIH)@AA0'++/:H*ABB==0&)9:H(&zH$$X.&H!"%  & 	",i88^q *9Q<+<+<il>N>NOOr   c                     U R                    Vs/ s H  oR                  PM     nn[        R                  U;   a  [        R                  $ [	        S U5      $ s  snf )Nc                 
    X-  $ r    r   xys     r   <lambda>5TensorProductHilbertSpace.dimension.<locals>.<lambda>  s    qsr   rM   r,   r   ri   r   r+   r   arg_lists      r   r,   #TensorProductHilbertSpace.dimension|  sE    -1YY7YcMMY7::!::*H55	 8   Ac                     U R                   $ )z5A tuple of the Hilbert spaces in this tensor product.rp   r*   s    r   spaces TensorProductHilbertSpace.spaces       yyr   c                     / nU R                    HA  nUR                  " U/UQ76 n[        U[        5      (       a  SU-  nUR	                  U5        MC     U$ )Nz(%s))rM   rv   ra   r   r   )r+   rL   rM   spaces_strsr   ss         r   _spaces_printer)TensorProductHilbertSpace._spaces_printer  sU    99Cs*T*A#455QJq!	 
 r   c                 P    U R                   " U/UQ76 nSSR                  U5      -  $ )NzTensorProductHilbertSpace(%s),r   join)r+   rL   rM   spaces_reprss       r   rw   $TensorProductHilbertSpace._sympyrepr  s+    ++G;d;.,1GGGr   c                 J    U R                   " U/UQ76 nSR                  U5      $ )N*r   )r+   rL   rM   r   s       r   rN   #TensorProductHilbertSpace._sympystr  s&    **7:T:xx$$r   c                    [        U R                  5      nUR                  " S/UQ76 n[        U5       H  nUR                  " U R                  U   /UQ76 n[	        U R                  U   [
        [        45      (       a  [        UR                  SSS96 n[        UR                  U5      6 nXSS-
  :w  d  M  UR                  (       a  [        UR                  S5      6 nM  [        UR                  S5      6 nM     U$ )N rt   ru   leftrightre   u    ⨂ z x rf   rM   rv   rangera   r   r   r	   parensr   _use_unicoder+   rL   rM   lengthpformi
next_pforms          r   rT   !TensorProductHilbertSpace._pretty  s    TYYr)D)vA 		!<t<J$))A,)>3)5 6 6'&&Cs&;
 J 78EQJ''&4b(cdE&E(:;E  r   c                    [        U R                  5      nSn[        U5       Hc  nUR                  " U R                  U   /UQ76 n[	        U R                  U   [
        [        45      (       a  SU-  nXF-   nXSS-
  :w  d  M^  US-   nMe     U$ )Nr   \left(%s\right)re   z\otimes rf   rM   r   rv   ra   r   r   r+   rL   rM   r   r   r   arg_ss          r   rX    TensorProductHilbertSpace._latex  s    TYYvANN499Q<7$7E$))A,)>*), - -*U2	AQJO  r   r   N)r   r   r   r   r[   r"   r   r`   r\   r,   r   r   rw   rN   rT   rX   r   r   r   r   r   r     sd    (T * *X 6 6  H%$r   r   c                   f    \ rS rSrSrS r\S 5       r\S 5       r	\S 5       r
S rS rS	 rS
 rSrg)r   i  ax  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                     U R                  U5      n[        U[        5      (       a  U$ [        R                  " U /UQ76 nU$ r    r   r   s       r   r"   DirectSumHilbertSpace.__new__  r   r   c                    / nSnU Hk  n[        U[        5      (       a  UR                  UR                  5        SnM7  [        U[        5      (       a  UR                  U5        M_  [        SU-  5      e   U(       a  [        U6 $ g)r   FTzOHilbert spaces can only be summed with other                 Hilbert spaces: %rN)ra   r   r   rM   r   r   rk   )r$   rM   r   r   r   s        r   r`   DirectSumHilbertSpace.eval  s     C#455)C..$ !$&)!* + +  ((33r   c                     U R                    Vs/ s H  oR                  PM     nn[        R                  U;   a  [        R                  $ [	        S U5      $ s  snf )Nc                 
    X-   $ r    r   r   s     r   r   1DirectSumHilbertSpace.dimension.<locals>.<lambda>  s    qur   r   r   s      r   r,   DirectSumHilbertSpace.dimension  sE    -1YY7YcMMY7::!::,h77	 8r   c                     U R                   $ )z1A tuple of the Hilbert spaces in this direct sum.rp   r*   s    r   r   DirectSumHilbertSpace.spaces  r   r   c                     U R                    Vs/ s H  o1R                  " U/UQ76 PM     nnSSR                  U5      -  $ s  snf )NzDirectSumHilbertSpace(%s)r   rM   rv   r   )r+   rL   rM   r   r   s        r   rw    DirectSumHilbertSpace._sympyrepr   s@    >BiiHiss2T2iH*SXXl-CCC Is   Ac                     U R                    Vs/ s H  o1R                  " U/UQ76 PM     nnSR                  U5      $ s  snf )N+r   )r+   rL   rM   r   r   s        r   rN   DirectSumHilbertSpace._sympystr  s;    =AYYGYc~~c1D1YGxx$$ Hs   >c                    [        U R                  5      nUR                  " S/UQ76 n[        U5       H  nUR                  " U R                  U   /UQ76 n[	        U R                  U   [
        [        45      (       a  [        UR                  SSS96 n[        UR                  U5      6 nXSS-
  :w  d  M  UR                  (       a  [        UR                  S5      6 nM  [        UR                  S5      6 nM     U$ )Nr   rt   ru   r   re   u    ⊕ z + r   r   s          r   rT   DirectSumHilbertSpace._pretty  s    TYYr)D)vA 		!<t<J$))A,)>3)5 6 6'&&Cs&;
 J 78EQJ''&4H(IJE&E(:;E  r   c                    [        U R                  5      nSn[        U5       Hc  nUR                  " U R                  U   /UQ76 n[	        U R                  U   [
        [        45      (       a  SU-  nXF-   nXSS-
  :w  d  M^  US-   nMe     U$ )Nr   r   re   z\oplus r   r   s          r   rX   DirectSumHilbertSpace._latex  s    TYYvANN499Q<7$7E$))A,)>*), - -*U2	AQJ
N  r   r   N)r   r   r   r   r[   r"   r   r`   r\   r,   r   rw   rN   rT   rX   r   r   r   r   r   r     s]    :  & 8 8  D%$r   r   c                   v    \ 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S rSrg)r   i(  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                     U R                  U5      n[        U[        5      (       a  U$ [        R                  " U /UQ76 $ r    r   )r$   rM   rb   s      r   r"   TensorPowerHilbertSpace.__new__N  s5    HHTNaH}}S%1%%r   c                    US   [        US   5      4nUS   nU[        R                  L a  US   $ U[        R                  L a  [        R                  $ [	        UR                  5       5      S:X  a8  UR                  (       a  US:  d  UR                  (       d  [        SU-  5      eU$ UR                  5        H5  nUR                  (       a  M  UR                  (       a  M)  [        SU-  5      e   U$ )Nr   re   zUHilbert spaces can only be raised to                 positive integers or Symbols: %rzJTensor powers can only contain integers                     or Symbols: %r)	r   r   OneZerorf   rg   rh   rj   r?   )r$   rM   r   r   powers        r   r`   TensorPowerHilbertSpace.evalT  s    7GDG,,qk!%%<7N!&&=55Lsyy{q NNsax3==  "247"8 9 9 	 (((EOOO$ &$&+&, - - % r   c                      U R                   S   $ ro   rp   r*   s    r   r   TensorPowerHilbertSpace.basej  rr   r   c                      U R                   S   $ )Nre   rp   r*   s    r   r   TensorPowerHilbertSpace.expn  rr   r   c                     U R                   R                  [        R                  L a  [        R                  $ U R                   R                  U R                  -  $ r    )r   r,   r   ri   r   r*   s    r   r,   !TensorPowerHilbertSpace.dimensionr  s;    99!**,::99&&00r   c                     SUR                   " U R                  /UQ76 < SUR                   " U R                  /UQ76 < S3$ )NzTensorPowerHilbertSpace(r   ru   rv   r   r   rK   s      r   rw   "TensorPowerHilbertSpace._sympyrepry  s;    3:>>$)) 4	4txx/$/1 	1r   c                     UR                   " U R                  /UQ76 < SUR                   " U R                  /UQ76 < 3$ )Nz**r  rK   s      r   rN   !TensorPowerHilbertSpace._sympystr}  s6    ">>$));d;txx'$') 	)r   c                 "   UR                   " U R                  /UQ76 nUR                  (       a!  [        UR	                  [        S5      5      6 nO [        UR	                  [        S5      5      6 nUR                   " U R
                  /UQ76 nXC-  $ )Nu   ⨂r   )rv   r   r   r	   r   r   r   s        r   rT   TensorPowerHilbertSpace._pretty  sr    NN4883d3	"INN:>`3a$bcI"INN:c?$CDI^^DII55
$$r   c                     UR                   " U R                  /UQ76 nUR                   " U R                  /UQ76 nSU< SU< S3$ )N{z}^{\otimes }r  )r+   rL   rM   r   r   s        r   rX   TensorPowerHilbertSpace._latex  s;    ~~dii/$/nnTXX--'+S11r   r   N)r   r   r   r   r[   r"   r   r`   r\   r   r   r,   rw   rN   rT   rX   r   r   r   r   r   r   (  sq    #J&  *     1 11)%2r   r   N)r[   	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   <module>r*     s     " " & $ 7 4	 	 	C5 CLIK< IKX1> 1>h* *ZZ ZzkL k\e2l e2r   