<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">"""Hermitian conjugation."""

from sympy.core import Expr, Mul
from sympy.functions.elementary.complexes import adjoint

__all__ = [
    'Dagger'
]


class Dagger(adjoint):
    """General Hermitian conjugate operation.

    Explanation
    ===========

    Take the Hermetian conjugate of an argument [1]_. For matrices this
    operation is equivalent to transpose and complex conjugate [2]_.

    Parameters
    ==========

    arg : Expr
        The SymPy expression that we want to take the dagger of.

    Examples
    ========

    Daggering various quantum objects:

        &gt;&gt;&gt; from sympy.physics.quantum.dagger import Dagger
        &gt;&gt;&gt; from sympy.physics.quantum.state import Ket, Bra
        &gt;&gt;&gt; from sympy.physics.quantum.operator import Operator
        &gt;&gt;&gt; Dagger(Ket('psi'))
        &lt;psi|
        &gt;&gt;&gt; Dagger(Bra('phi'))
        |phi&gt;
        &gt;&gt;&gt; Dagger(Operator('A'))
        Dagger(A)

    Inner and outer products::

        &gt;&gt;&gt; from sympy.physics.quantum import InnerProduct, OuterProduct
        &gt;&gt;&gt; Dagger(InnerProduct(Bra('a'), Ket('b')))
        &lt;b|a&gt;
        &gt;&gt;&gt; Dagger(OuterProduct(Ket('a'), Bra('b')))
        |b&gt;&lt;a|

    Powers, sums and products::

        &gt;&gt;&gt; A = Operator('A')
        &gt;&gt;&gt; B = Operator('B')
        &gt;&gt;&gt; Dagger(A*B)
        Dagger(B)*Dagger(A)
        &gt;&gt;&gt; Dagger(A+B)
        Dagger(A) + Dagger(B)
        &gt;&gt;&gt; Dagger(A**2)
        Dagger(A)**2

    Dagger also seamlessly handles complex numbers and matrices::

        &gt;&gt;&gt; from sympy import Matrix, I
        &gt;&gt;&gt; m = Matrix([[1,I],[2,I]])
        &gt;&gt;&gt; m
        Matrix([
        [1, I],
        [2, I]])
        &gt;&gt;&gt; Dagger(m)
        Matrix([
        [ 1,  2],
        [-I, -I]])

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Hermitian_adjoint
    .. [2] https://en.wikipedia.org/wiki/Hermitian_transpose
    """

    def __new__(cls, arg):
        if hasattr(arg, 'adjoint'):
            obj = arg.adjoint()
        elif hasattr(arg, 'conjugate') and hasattr(arg, 'transpose'):
            obj = arg.conjugate().transpose()
        if obj is not None:
            return obj
        return Expr.__new__(cls, arg)

    def __mul__(self, other):
        from sympy.physics.quantum import IdentityOperator
        if isinstance(other, IdentityOperator):
            return self

        return Mul(self, other)

adjoint.__name__ = "Dagger"
adjoint._sympyrepr = lambda a, b: "Dagger(%s)" % b._print(a.args[0])
</pre></body></html>