o
    jg6                     @   s   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 d dlmZ d d	lmZmZmZmZmZ d
dgZG dd
 d
eZdd Zdd ZG dd deZdd Zdd Zdd ZdS )    )Expr)Symbol)sympify)Matrix)
prettyForm)Tuple)is_sequence)Dagger)numpy_ndarrayscipy_sparse_matrixto_sympyto_numpyto_scipy_sparseQuantumErrorQExprc                   @   s   e Zd ZdS )r   N)__name__
__module____qualname__ r   r   S/var/www/html/zoom/venv/lib/python3.10/site-packages/sympy/physics/quantum/qexpr.pyr      s    c                 C   s   t t| S )a  Convert elements of a sequence to standard form.

    This is like sympify, but it performs special logic for arguments passed
    to QExpr. The following conversions are done:

    * (list, tuple, Tuple) => _qsympify_sequence each element and convert
      sequence to a Tuple.
    * basestring => Symbol
    * Matrix => Matrix
    * other => sympify

    Strings are passed to Symbol, not sympify to make sure that variables like
    'pi' are kept as Symbols, not the SymPy built-in number subclasses.

    Examples
    ========

    >>> from sympy.physics.quantum.qexpr import _qsympify_sequence
    >>> _qsympify_sequence((1,2,[3,4,[1,]]))
    (1, 2, (3, 4, (1,)))

    )tuple__qsympify_sequence_helper)seqr   r   r   _qsympify_sequence   s   r   c                 C   sT   t | st| tr| S t| trt| S t| S t| tr| S dd | D }t| S )z^
       Helper function for _qsympify_sequence
       This function does the actual work.
    c                 S   s   g | ]}t |qS r   )r   ).0itemr   r   r   
<listcomp>L   s    z.__qsympify_sequence_helper.<locals>.<listcomp>)r   
isinstancer   strr   r   r   r   )r   resultr   r   r   r   8   s   


r   c                   @   s   e Zd ZdZdZdZdZedd Zdd Z	e
d	d
 Zedd Zedd Ze
d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dAd!d"Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Z d7d8 Z!d9d: Z"d;d<d=d>Z#d?d@ Z$d;S )Br   z>A base class for all quantum object like operators and states.)hilbert_spaceF c                 C   s   | hS Nr   selfr   r   r   free_symbolsd   s   zQExpr.free_symbolsc                 O   sZ   | j |fi |}t|dkr| j t|  fi |}tj| g|R  }| ||_|S )a:  Construct a new quantum object.

        Parameters
        ==========

        args : tuple
            The list of numbers or parameters that uniquely specify the
            quantum object. For a state, this will be its symbol or its
            set of quantum numbers.

        Examples
        ========

        >>> from sympy.physics.quantum.qexpr import QExpr
        >>> q = QExpr(0)
        >>> q
        0
        >>> q.label
        (0,)
        >>> q.hilbert_space
        H
        >>> q.args
        (0,)
        >>> q.is_commutative
        False
        r   )
_eval_argslenr   default_argsr   __new___eval_hilbert_spacer    )clsargskwargsinstr   r   r   r)   h   s   zQExpr.__new__c                 O   s"   t j| g|R i |}||_|S )a  Create new instance of this class with hilbert_space and args.

        This is used to bypass the more complex logic in the ``__new__``
        method in cases where you already have the exact ``hilbert_space``
        and ``args``. This should be used when you are positive these
        arguments are valid, in their final, proper form and want to optimize
        the creation of the object.
        )r   r)   r    )r+   r    r,   old_assumptionsobjr   r   r   _new_rawargs   s   zQExpr._new_rawargsc                 C   s&   t | jdkr| t|  S | jS )a  The label is the unique set of identifiers for the object.

        Usually, this will include all of the information about the state
        *except* the time (in the case of time-dependent objects).

        This must be a tuple, rather than a Tuple.
        r   )r'   r,   r&   listr(   r#   r   r   r   label   s   	zQExpr.labelc                 C   s   dS )NTr   r#   r   r   r   is_symbolic   s   zQExpr.is_symbolicc                 C      t d)a6  If no arguments are specified, then this will return a default set
        of arguments to be run through the constructor.

        NOTE: Any classes that override this MUST return a tuple of arguments.
        Should be overridden by subclasses to specify the default arguments for kets and operators
        z$No default arguments for this class!NotImplementedErrorr#   r   r   r   r(      s   zQExpr.default_argsc                 C   s4   t | }|d u rt t| }t|tr| j|_|S r"   )r   _eval_adjointr)   r	   r   r   r    )r$   r0   r   r   r   r8      s   

zQExpr._eval_adjointc                 C   s   t |S )zrProcess the args passed to the __new__ method.

        This simply runs args through _qsympify_sequence.
        )r   )r+   r,   r   r   r   r&      s   zQExpr._eval_argsc                 C   s   ddl m} | S )z:Compute the Hilbert space instance from the args.
        r   )HilbertSpace)sympy.physics.quantum.hilbertr9   )r+   r,   r9   r   r   r   r*      s   zQExpr._eval_hilbert_spacec                 G   s0   g }|D ]}| |j|g|R   q||S r"   )append_printjoin)r$   r   sepprinterr,   r   r   r   r   r   _print_sequence   s   
zQExpr._print_sequencec                 G   sV   |j |d g|R  }|dd  D ]}t|| }t||j |g|R   }q|S )Nr      )r<   r   right)r$   r   r>   r?   r,   pformr   r   r   r   _print_sequence_pretty   s
   zQExpr._print_sequence_prettyc                 C   sB   t |d|   }t |d|   }t ||dt jiS )N binding)r   leftwidthrB   belowPOW)r$   abtopbotr   r   r   _print_subscript_pretty   s   zQExpr._print_subscript_prettyc                 C   s   || S r"   r   )r$   rK   rL   r   r   r   _print_superscript_pretty      zQExpr._print_superscript_pretty()c                 C   s   t |j||d S )N)rG   rB   )r   parens)r$   rC   rG   rB   r   r   r   _print_parens_pretty      zQExpr._print_parens_prettyc                 G      | j | j| j|g|R  S )zPrints the label of the QExpr

        This method prints self.label, using self._label_separator to separate
        the elements. This method should not be overridden, instead, override
        _print_contents to change printing behavior.
        r@   r3   _label_separatorr$   r?   r,   r   r   r   _print_label   s
   
zQExpr._print_labelc                 G   s   | j | jd|g|R  S )N,)r@   r3   rZ   r   r   r   _print_label_repr  s
   zQExpr._print_label_reprc                 G   rW   r"   )rD   r3   rY   rZ   r   r   r   _print_label_pretty	  
   
zQExpr._print_label_prettyc                 G   rW   r"   rX   rZ   r   r   r   _print_label_latex  r_   zQExpr._print_label_latexc                 G      | j |g|R  S )aS  Printer for contents of QExpr

        Handles the printing of any unique identifying contents of a QExpr to
        print as its contents, such as any variables or quantum numbers. The
        default is to print the label, which is almost always the args. This
        should not include printing of any brackets or parentheses.
        )r[   rZ   r   r   r   _print_contents  s   zQExpr._print_contentsc                 G   ra   r"   )r^   rZ   r   r   r   _print_contents_pretty  rV   zQExpr._print_contents_prettyc                 G   ra   r"   )r`   rZ   r   r   r   _print_contents_latex"  rV   zQExpr._print_contents_latexc                 G   ra   )a  Default printing behavior of QExpr objects

        Handles the default printing of a QExpr. To add other things to the
        printing of the object, such as an operator name to operators or
        brackets to states, the class should override the _print/_pretty/_latex
        functions directly and make calls to _print_contents where appropriate.
        This allows things like InnerProduct to easily control its printing the
        printing of contents.
        )rb   rZ   r   r   r   	_sympystr'  s   
zQExpr._sympystrc                 G   s&   | j j}| j|g|R  }d||f S )Nz%s(%s))	__class__r   r]   )r$   r?   r,   	classnamer3   r   r   r   
_sympyrepr3  s   zQExpr._sympyreprc                 G   s   | j |g|R  }|S r"   )rc   )r$   r?   r,   rC   r   r   r   _pretty8  s   zQExpr._prettyc                 G   ra   r"   )rd   rZ   r   r   r   _latex<  rV   zQExpr._latexc                 K   r5   )Nz)This object does not have a default basisr6   )r$   optionsr   r   r   _represent_default_basisC  rQ   zQExpr._represent_default_basisN)basisc                K   sJ   |du r| j di |}n
t| d|fi |}|dd}| ||}|S )a`  Represent this object in a given basis.

        This method dispatches to the actual methods that perform the
        representation. Subclases of QExpr should define various methods to
        determine how the object will be represented in various bases. The
        format of these methods is::

            def _represent_BasisName(self, basis, **options):

        Thus to define how a quantum object is represented in the basis of
        the operator Position, you would define::

            def _represent_Position(self, basis, **options):

        Usually, basis object will be instances of Operator subclasses, but
        there is a chance we will relax this in the future to accommodate other
        types of basis sets that are not associated with an operator.

        If the ``format`` option is given it can be ("sympy", "numpy",
        "scipy.sparse"). This will ensure that any matrices that result from
        representing the object are returned in the appropriate matrix format.

        Parameters
        ==========

        basis : Operator
            The Operator whose basis functions will be used as the basis for
            representation.
        options : dict
            A dictionary of key/value pairs that give options and hints for
            the representation, such as the number of basis functions to
            be used.
        N
_representformatsympyr   )rl   dispatch_methodget_format_represent)r$   rm   rk   r   ro   r   r   r   rn   F  s   "zQExpr._representc                 C   sR   |dkrt |tst|S |dkrt |tst|S |dkr't |ts't|S |S )Nrp   numpyzscipy.sparse)r   r   r   r
   r   r   r   )r$   r   ro   r   r   r   rs   r  s   zQExpr._format_represent)rR   rS   )%r   r   r   __doc__	__slots__is_commutativerY   propertyr%   r)   classmethodr1   r3   r4   r(   r8   r&   r*   r@   rD   rO   rP   rU   r[   r]   r^   r`   rb   rc   rd   re   rh   ri   rj   rl   rn   rs   r   r   r   r   r   U   sP    
%





	

,c                 C   s   |   \}}t|}||fS )z1Split into commutative and non-commutative parts.)args_cncr2   )ec_partnc_partr   r   r   split_commutative_parts~  s   r~   c                 C   s<   g }g }| j D ]}t|ts|| q|| q||fS )z=Split an expression into Expr and noncommutative QExpr parts.)r,   r   r   r;   )r{   	expr_part
qexpr_partargr   r   r   split_qexpr_parts  s   

r   c                 K   sV   d||j jf }t| |r t| |}||fi |}|dur |S td| j j||f )z)Dispatch a method to the proper handlers.z%s_%sNz%s.%s cannot handle: %r)rf   r   hasattrgetattrr7   )r$   basenamer   rk   method_namefr   r   r   r   rq     s   

rq   N)sympy.core.exprr   sympy.core.symbolr   sympy.core.sympifyr   sympy.matrices.denser    sympy.printing.pretty.stringpictr   sympy.core.containersr   sympy.utilities.iterablesr   sympy.physics.quantum.daggerr	   !sympy.physics.quantum.matrixutilsr
   r   r   r   r   __all__	Exceptionr   r   r   r   r~   r   rq   r   r   r   r   <module>   s*    
  +