Beispiel #1
0
def test_anticommutator():
    A = Operator('A')
    B = Operator('B')
    ac = AntiCommutator(A, B)
    ac_tall = AntiCommutator(A**2, B)
    assert str(ac) == '{A,B}'
    assert pretty(ac) == '{A,B}'
    assert upretty(ac) == '{A,B}'
    assert latex(ac) == r'\left\{A,B\right\}'
    sT(ac, "AntiCommutator(Operator(Symbol('A')),Operator(Symbol('B')))")
    assert str(ac_tall) == '{A**2,B}'
    ascii_str = \
"""\
/ 2  \\\n\
<A ,B>\n\
\\    /\
"""
    ucode_str = \
"""\
⎧ 2  ⎫\n\
⎨A ,B⎬\n\
⎩    ⎭\
"""
    assert pretty(ac_tall) == ascii_str
    assert upretty(ac_tall) == ucode_str
    #FIXME ajgpitch 2019-09-22:
    # It's not clear to me why this would be expected
    # assert latex(ac_tall) == r'\left\{\left(A\right)^{2},B\right\}'
    # This renders fine
    assert latex(ac_tall) == r'\left\{A^{2},B\right\}'
    sT(
        ac_tall,
        "AntiCommutator(Pow(Operator(Symbol('A')), Integer(2)),Operator(Symbol('B')))"
    )
Beispiel #2
0
def test_commutator():
    A = Operator('A')
    B = Operator('B')
    c = Commutator(A, B)
    c_tall = Commutator(A**2, B)
    assert str(c) == '[A,B]'
    assert pretty(c) == '[A,B]'
    assert upretty(c) == '[A,B]'
    assert latex(c) == r'\left[A,B\right]'
    sT(c, "Commutator(Operator(Symbol('A')),Operator(Symbol('B')))")
    assert str(c_tall) == '[A**2,B]'
    ascii_str = \
"""\
[ 2  ]\n\
[A ,B]\
"""
    ucode_str = \
"""\
⎡ 2  ⎤\n\
⎣A ,B⎦\
"""
    assert pretty(c_tall) == ascii_str
    assert upretty(c_tall) == ucode_str
    #FIXME ajgpitch 2019-09-22:
    # It's not clear to me why this would be expected
    # assert latex(ac_tall) == r'\left[\left(A\right)^{2},B\right]'
    # This renders fine
    assert latex(c_tall) == r'\left[A^{2},B\right]'
    sT(
        c_tall,
        "Commutator(Pow(Operator(Symbol('A')), Integer(2)),Operator(Symbol('B')))"
    )
Beispiel #3
0
def test_anticommutator():
    A = Operator("A")
    B = Operator("B")
    ac = AntiCommutator(A, B)
    ac_tall = AntiCommutator(A**2, B)
    assert str(ac) == "{A,B}"
    assert pretty(ac) == "{A,B}"
    assert upretty(ac) == u"{A,B}"
    assert latex(ac) == r"\left\{A,B\right\}"
    sT(ac, "AntiCommutator(Operator(Symbol('A')),Operator(Symbol('B')))")
    assert str(ac_tall) == "{A**2,B}"
    ascii_str = """\
/ 2  \\\n\
<A ,B>\n\
\\    /\
"""
    ucode_str = u("""\
⎧ 2  ⎫\n\
⎨A ,B⎬\n\
⎩    ⎭\
""")
    assert pretty(ac_tall) == ascii_str
    assert upretty(ac_tall) == ucode_str
    assert latex(ac_tall) == r"\left\{A^{2},B\right\}"
    sT(
        ac_tall,
        "AntiCommutator(Pow(Operator(Symbol('A')), Integer(2)),Operator(Symbol('B')))",
    )
Beispiel #4
0
def test_anticommutator():
    A = Operator('A')
    B = Operator('B')
    ac = AntiCommutator(A, B)
    ac_tall = AntiCommutator(A**2, B)
    assert str(ac) == '{A,B}'
    assert pretty(ac) == '{A,B}'
    assert upretty(ac) == u'{A,B}'
    assert latex(ac) == r'\left\{A,B\right\}'
    sT(ac, "AntiCommutator(Operator(Symbol('A')),Operator(Symbol('B')))")
    assert str(ac_tall) == '{A**2,B}'
    ascii_str = \
"""\
/ 2  \\\n\
<A ,B>\n\
\\    /\
"""
    ucode_str = \
u"""\
⎧ 2  ⎫\n\
⎨A ,B⎬\n\
⎩    ⎭\
"""
    assert pretty(ac_tall) == ascii_str
    assert upretty(ac_tall) == ucode_str
    assert latex(ac_tall) == r'\left\{\left(A\right)^{2},B\right\}'
    sT(
        ac_tall,
        "AntiCommutator(Pow(Operator(Symbol('A')), Integer(2)),Operator(Symbol('B')))"
    )
Beispiel #5
0
def test_commutator():
    A = Operator("A")
    B = Operator("B")
    c = Commutator(A, B)
    c_tall = Commutator(A**2, B)
    assert str(c) == "[A,B]"
    assert pretty(c) == "[A,B]"
    assert upretty(c) == u"[A,B]"
    assert latex(c) == r"\left[A,B\right]"
    sT(c, "Commutator(Operator(Symbol('A')),Operator(Symbol('B')))")
    assert str(c_tall) == "[A**2,B]"
    ascii_str = """\
[ 2  ]\n\
[A ,B]\
"""
    ucode_str = u("""\
⎡ 2  ⎤\n\
⎣A ,B⎦\
""")
    assert pretty(c_tall) == ascii_str
    assert upretty(c_tall) == ucode_str
    assert latex(c_tall) == r"\left[A^{2},B\right]"
    sT(
        c_tall,
        "Commutator(Pow(Operator(Symbol('A')), Integer(2)),Operator(Symbol('B')))",
    )
Beispiel #6
0
def test_state_to_op():
    assert state_to_operators(XKet) == XOp()
    assert state_to_operators(PxKet) == PxOp()
    assert state_to_operators(XBra) == XOp()
    assert state_to_operators(PxBra) == PxOp()
    assert state_to_operators(Ket) == Operator()
    assert state_to_operators(Bra) == Operator()

    assert state_to_operators(JxKet) == set([J2Op(), JxOp()])
    assert state_to_operators(JyKet) == set([J2Op(), JyOp()])
    assert state_to_operators(JzKet) == set([J2Op(), JzOp()])
    assert state_to_operators(JxBra) == set([J2Op(), JxOp()])
    assert state_to_operators(JyBra) == set([J2Op(), JyOp()])
    assert state_to_operators(JzBra) == set([J2Op(), JzOp()])

    assert state_to_operators(JxKet()) == set([J2Op(), JxOp()])
    assert state_to_operators(JyKet()) == set([J2Op(), JyOp()])
    assert state_to_operators(JzKet()) == set([J2Op(), JzOp()])
    assert state_to_operators(JxBra()) == set([J2Op(), JxOp()])
    assert state_to_operators(JyBra()) == set([J2Op(), JyOp()])
    assert state_to_operators(JzBra()) == set([J2Op(), JzOp()])

    assert operators_to_state(state_to_operators(XKet("test"))) == XKet("test")
    assert operators_to_state(state_to_operators(XBra("test"))) == XKet("test")
    assert operators_to_state(state_to_operators(XKet())) == XKet()
    assert operators_to_state(state_to_operators(XBra())) == XKet()

    raises(NotImplementedError, 'state_to_operators(XOp)')
Beispiel #7
0
def test_commutator():
    A = Operator('A')
    B = Operator('B')
    c = Commutator(A, B)
    c_tall = Commutator(A**2, B)
    assert str(c) == '[A,B]'
    assert pretty(c) == '[A,B]'
    assert upretty(c) == u'[A,B]'
    assert latex(c) == r'\left[A,B\right]'
    sT(c, "Commutator(Operator(Symbol('A')),Operator(Symbol('B')))")
    assert str(c_tall) == '[A**2,B]'
    ascii_str = \
"""\
[ 2  ]\n\
[A ,B]\
"""
    ucode_str = \
u"""\
⎡ 2  ⎤\n\
⎣A ,B⎦\
"""
    assert pretty(c_tall) == ascii_str
    assert upretty(c_tall) == ucode_str
    assert latex(c_tall) == r'\left[\left(A\right)^{2},B\right]'
    sT(
        c_tall,
        "Commutator(Pow(Operator(Symbol('A')), Integer(2)),Operator(Symbol('B')))"
    )
Beispiel #8
0
def test_operator():
    a = Operator('A')
    b = Operator('B', Symbol('t'), S(1) / 2)
    inv = a.inv()
    f = Function('f')
    x = symbols('x')
    d = DifferentialOperator(Derivative(f(x), x), f(x))
    op = OuterProduct(Ket(), Bra())
    assert str(a) == 'A'
    assert pretty(a) == 'A'
    assert upretty(a) == u'A'
    assert latex(a) == 'A'
    sT(a, "Operator(Symbol('A'))")
    assert str(inv) == 'A**(-1)'
    ascii_str = \
"""\
 -1\n\
A  \
"""
    ucode_str = \
u"""\
 -1\n\
A  \
"""
    assert pretty(inv) == ascii_str
    assert upretty(inv) == ucode_str
    assert latex(inv) == r'\left(A\right)^{-1}'
    sT(inv, "Pow(Operator(Symbol('A')), Integer(-1))")
    assert str(d) == 'DifferentialOperator(Derivative(f(x), x),f(x))'
    ascii_str = \
"""\
                    /d            \\\n\
DifferentialOperator|--(f(x)),f(x)|\n\
                    \dx           /\
"""
    ucode_str = \
u"""\
                    ⎛d            ⎞\n\
DifferentialOperator⎜──(f(x)),f(x)⎟\n\
                    ⎝dx           ⎠\
"""
    assert pretty(d) == ascii_str
    assert upretty(d) == ucode_str
    assert latex(d) == \
        r'DifferentialOperator\left(\frac{\partial}{\partial x} \operatorname{f}{\left (x \right )},\operatorname{f}{\left (x \right )}\right)'
    sT(
        d,
        "DifferentialOperator(Derivative(Function('f')(Symbol('x')), Symbol('x')),Function('f')(Symbol('x')))"
    )
    assert str(b) == 'Operator(B,t,1/2)'
    assert pretty(b) == 'Operator(B,t,1/2)'
    assert upretty(b) == u'Operator(B,t,1/2)'
    assert latex(b) == r'Operator\left(B,t,\frac{1}{2}\right)'
    sT(b, "Operator(Symbol('B'),Symbol('t'),Rational(1, 2))")
    assert str(op) == '|psi><psi|'
    assert pretty(op) == '|psi><psi|'
    assert upretty(op) == u'❘ψ⟩⟨ψ❘'
    assert latex(op) == r'{\left|\psi\right\rangle }{\left\langle \psi\right|}'
    sT(op, "OuterProduct(Ket(Symbol('psi')),Bra(Symbol('psi')))")
Beispiel #9
0
def test_operator():
    a = Operator("A")
    b = Operator("B", Symbol("t"), S.Half)
    inv = a.inv()
    f = Function("f")
    x = symbols("x")
    d = DifferentialOperator(Derivative(f(x), x), f(x))
    op = OuterProduct(Ket(), Bra())
    assert str(a) == "A"
    assert pretty(a) == "A"
    assert upretty(a) == u"A"
    assert latex(a) == "A"
    sT(a, "Operator(Symbol('A'))")
    assert str(inv) == "A**(-1)"
    ascii_str = """\
 -1\n\
A  \
"""
    ucode_str = u("""\
 -1\n\
A  \
""")
    assert pretty(inv) == ascii_str
    assert upretty(inv) == ucode_str
    assert latex(inv) == r"A^{-1}"
    sT(inv, "Pow(Operator(Symbol('A')), Integer(-1))")
    assert str(d) == "DifferentialOperator(Derivative(f(x), x),f(x))"
    ascii_str = """\
                    /d            \\\n\
DifferentialOperator|--(f(x)),f(x)|\n\
                    \\dx           /\
"""
    ucode_str = u("""\
                    ⎛d            ⎞\n\
DifferentialOperator⎜──(f(x)),f(x)⎟\n\
                    ⎝dx           ⎠\
""")
    assert pretty(d) == ascii_str
    assert upretty(d) == ucode_str
    assert (
        latex(d) ==
        r"DifferentialOperator\left(\frac{d}{d x} f{\left(x \right)},f{\left(x \right)}\right)"
    )
    sT(
        d,
        "DifferentialOperator(Derivative(Function('f')(Symbol('x')), Tuple(Symbol('x'), Integer(1))),Function('f')(Symbol('x')))",
    )
    assert str(b) == "Operator(B,t,1/2)"
    assert pretty(b) == "Operator(B,t,1/2)"
    assert upretty(b) == u"Operator(B,t,1/2)"
    assert latex(b) == r"Operator\left(B,t,\frac{1}{2}\right)"
    sT(b, "Operator(Symbol('B'),Symbol('t'),Rational(1, 2))")
    assert str(op) == "|psi><psi|"
    assert pretty(op) == "|psi><psi|"
    assert upretty(op) == u"❘ψ⟩⟨ψ❘"
    assert latex(op) == r"{\left|\psi\right\rangle }{\left\langle \psi\right|}"
    sT(op, "OuterProduct(Ket(Symbol('psi')),Bra(Symbol('psi')))")
Beispiel #10
0
def test_state_to_op():
    assert state_to_operators(XKet) == XOp()
    assert state_to_operators(PxKet) == PxOp()
    assert state_to_operators(XBra) == XOp()
    assert state_to_operators(PxBra) == PxOp()
    assert state_to_operators(Ket) == Operator()
    assert state_to_operators(Bra) == Operator()

    assert operators_to_state(state_to_operators(XKet("test"))) == XKet("test")
    assert operators_to_state(state_to_operators(XBra("test"))) == XKet("test")
    assert operators_to_state(state_to_operators(XKet())) == XKet()
    assert operators_to_state(state_to_operators(XBra())) == XKet()

    raises(NotImplementedError, lambda: state_to_operators(XOp))
Beispiel #11
0
def test_dagger_mul():
    O = Operator('O')
    I = IdentityOperator()
    assert Dagger(O) * O == Dagger(O) * O
    assert Dagger(O) * O * I == Mul(Dagger(O), O) * I
    assert Dagger(O) * Dagger(O) == Dagger(O)**2
    assert Dagger(O) * Dagger(I) == Dagger(O)
Beispiel #12
0
def test_operator():
    A = Operator('A')
    B = Operator('B')
    C = Operator('C')

    assert isinstance(A, Operator)
    assert isinstance(A, QExpr)

    assert A.label == (Symbol('A'), )
    assert A.is_commutative == False
    assert A.hilbert_space == HilbertSpace()

    assert A * B != B * A

    assert (A * (B + C)).expand() == A * B + A * C
    assert ((A + B)**2).expand() == A**2 + A * B + B * A + B**2
Beispiel #13
0
def test_operator():
    A = Operator('A')
    B = Operator('B')
    C = Operator('C')

    assert isinstance(A, Operator)
    assert isinstance(A, QExpr)

    assert A.label == (Symbol('A'),)
    assert A.is_commutative is False
    assert A.hilbert_space == HilbertSpace()

    assert A*B != B*A

    assert (A*(B + C)).expand() == A*B + A*C
    assert ((A + B)**2).expand() == A**2 + A*B + B*A + B**2

    assert t_op.label[0] == Symbol(t_op.default_args()[0])

    assert Operator() == Operator("O")
Beispiel #14
0
def test_identity():
    I = IdentityOperator()
    O = Operator('O')

    assert isinstance(I, IdentityOperator)
    assert isinstance(I, Operator)

    assert I.inv() == I
    assert Dagger(I) == I
    assert qapply(I * O) == O
    assert qapply(O * I) == O

    for n in [2, 3, 5]:
        assert represent(IdentityOperator(n)) == eye(n)
def non_commutative_sympify(expr_string, status, boolean):
    if "^" in expr_string:
        expr_string = expr_string.replace("^", "**")

    if "Ad" in expr_string:
        expr_string = expr_string.replace("Ad", "Dagger")

    fixed_string = ""
    if 'Dagger' in expr_string:
        fixed_string = expr_string.replace("Dagger", "sin")
    else:
        fixed_string = expr_string
    temp_evaluated_expr = parse_expr(fixed_string, evaluate=False)
    if status == '0':
        status1 = False
    elif status == '1':
        status1 = True

    new_locals = {
        sym.name: Symbol(sym.name, commutative=status1)
        for sym in temp_evaluated_expr.atoms(Symbol)
    }

    #new_locals = {}
    #for sym in temp_evaluated_expr.atoms(Symbol):
    #       new_locals.update({sym.name:Operator(sym.name)})

    #{'C': C, 'E': E, 'I': I, 'N': N, 'O': O, 'Q': Q, 'S': S}

    new_locals.update({'U': UnitaryOperator('U')})
    new_locals.update({'c': Symbol('c', commutative=True)})
    new_locals.update({'r': Symbol('r', commutative=True)})
    new_locals.update({'t': Symbol('t', commutative=True)})
    new_locals.update({'W': UnitaryOperator('W')})
    new_locals.update({'V': UnitaryOperator('V')})
    new_locals.update({'u': UnitaryOperator('u')})
    new_locals.update({'w': UnitaryOperator('w')})
    new_locals.update({'v': UnitaryOperator('v')})
    new_locals.update({'H': HermitianOperator('H')})
    new_locals.update({'A': HermitianOperator('A')})
    new_locals.update({'T': HermitianOperator('T')})
    new_locals.update({'C': Operator('C')})
    new_locals.update({'Dagger': Dagger})

    return sympify(expr_string, locals=new_locals, evaluate=boolean)
Beispiel #16
0
def generate_variables(n_vars, hermitian=False, commutative=False, name='x'):
    """Generates a number of commutative or noncommutative variables

    :param n_vars: The number of variables.
    :type n_vars: int.

    :returns: list of :class:`sympy.physics.quantum.operator.Operator` or
              :class:`sympy.physics.quantum.operator.HermitianOperator`
              variables
    """

    variables = []
    for i in range(n_vars):
        if hermitian or commutative:
            variables.append(HermitianOperator('%s%s' % (name, i)))
        else:
            variables.append(Operator('%s%s' % (name, i)))
        variables[i].is_commutative = commutative
    return variables
Beispiel #17
0
def test_identity():
    I = IdentityOperator()
    O = Operator("O")
    x = Symbol("x")

    assert isinstance(I, IdentityOperator)
    assert isinstance(I, Operator)

    assert I * O == O
    assert O * I == O
    assert isinstance(I * I, IdentityOperator)
    assert isinstance(3 * I, Mul)
    assert isinstance(I * x, Mul)
    assert I.inv() == I
    assert Dagger(I) == I
    assert qapply(I * O) == O
    assert qapply(O * I) == O

    for n in [2, 3, 5]:
        assert represent(IdentityOperator(n)) == eye(n)
Beispiel #18
0
def test_sympy__physics__quantum__operator__Operator():
    from sympy.physics.quantum.operator import Operator
    assert _test_args(Operator('A'))
Beispiel #19
0
def test_operator_inv():
    A = Operator('A')
    assert A*A.inv() == 1
    assert A.inv()*A == 1
Beispiel #20
0
def test_operator_dagger():
    A = Operator('A')
    B = Operator('B')
    assert Dagger(A*B) == Dagger(B)*Dagger(A)
    assert Dagger(A + B) == Dagger(A) + Dagger(B)
    assert Dagger(A**2) == Dagger(A)**2
Beispiel #21
0
def test_big_expr():
    f = Function('f')
    x = symbols('x')
    e1 = Dagger(
        AntiCommutator(
            Operator('A') + Operator('B'),
            Pow(DifferentialOperator(Derivative(f(x), x), f(x)), 3)) *
        TensorProduct(Jz**2,
                      Operator('A') + Operator('B'))) * (JzBra(1, 0) + JzBra(
                          1, 1)) * (JzKet(0, 0) + JzKet(1, -1))
    e2 = Commutator(Jz**2,
                    Operator('A') + Operator('B')) * AntiCommutator(
                        Dagger(Operator('C') * Operator('D')),
                        Operator('E').inv()**2) * Dagger(Commutator(Jz, J2))
    e3 = Wigner3j(1, 2, 3, 4, 5, 6) * TensorProduct(
        Commutator(
            Operator('A') + Dagger(Operator('B')),
            Operator('C') + Operator('D')), Jz - J2) * Dagger(
                OuterProduct(Dagger(JzBra(1, 1)), JzBra(
                    1, 0))) * TensorProduct(
                        JzKetCoupled(1, 1,
                                     (1, 1)) + JzKetCoupled(1, 0, (1, 1)),
                        JzKetCoupled(1, -1, (1, 1)))
    e4 = (ComplexSpace(1) * ComplexSpace(2) +
          FockSpace()**2) * (L2(Interval(0, oo)) + HilbertSpace())
    assert str(
        e1
    ) == '(Jz**2)x(Dagger(A) + Dagger(B))*{Dagger(DifferentialOperator(Derivative(f(x), x),f(x)))**3,Dagger(A) + Dagger(B)}*(<1,0| + <1,1|)*(|0,0> + |1,-1>)'
    ascii_str = \
"""\
                 /                                      3        \\                                 \n\
                 |/                                   +\\         |                                 \n\
    2  / +    +\\ <|                    /d            \\ |   +    +>                                 \n\
/J \\ x \\A  + B /*||DifferentialOperator|--(f(x)),f(x)| | ,A  + B |*(<1,0| + <1,1|)*(|0,0> + |1,-1>)\n\
\\ z/             \\\\                    \dx           / /         /                                 \
"""
    ucode_str = \
u"""\
                 ⎧                                      3        ⎫                                 \n\
                 ⎪⎛                                   †⎞         ⎪                                 \n\
    2  ⎛ †    †⎞ ⎨⎜                    ⎛d            ⎞ ⎟   †    †⎬                                 \n\
⎛J ⎞ ⨂ ⎝A  + B ⎠⋅⎪⎜DifferentialOperator⎜──(f(x)),f(x)⎟ ⎟ ,A  + B ⎪⋅(⟨1,0❘ + ⟨1,1❘)⋅(❘0,0⟩ + ❘1,-1⟩)\n\
⎝ z⎠             ⎩⎝                    ⎝dx           ⎠ ⎠         ⎭                                 \
"""
    assert pretty(e1) == ascii_str
    assert upretty(e1) == ucode_str
    assert latex(e1) == \
        r'{\left(J_z\right)^{2}}\otimes \left({A^{\dag} + B^{\dag}}\right) \left\{\left(DifferentialOperator\left(\frac{\partial}{\partial x} \operatorname{f}{\left (x \right )},\operatorname{f}{\left (x \right )}\right)^{\dag}\right)^{3},A^{\dag} + B^{\dag}\right\} \left({\left\langle 1,0\right|} + {\left\langle 1,1\right|}\right) \left({\left|0,0\right\rangle } + {\left|1,-1\right\rangle }\right)'
    sT(
        e1,
        "Mul(TensorProduct(Pow(JzOp(Symbol('J')), Integer(2)), Add(Dagger(Operator(Symbol('A'))), Dagger(Operator(Symbol('B'))))), AntiCommutator(Pow(Dagger(DifferentialOperator(Derivative(Function('f')(Symbol('x')), Symbol('x')),Function('f')(Symbol('x')))), Integer(3)),Add(Dagger(Operator(Symbol('A'))), Dagger(Operator(Symbol('B'))))), Add(JzBra(Integer(1),Integer(0)), JzBra(Integer(1),Integer(1))), Add(JzKet(Integer(0),Integer(0)), JzKet(Integer(1),Integer(-1))))"
    )
    assert str(e2) == '[Jz**2,A + B]*{E**(-2),Dagger(D)*Dagger(C)}*[J2,Jz]'
    ascii_str = \
"""\
[    2      ] / -2  +  +\\ [ 2   ]\n\
[/J \\ ,A + B]*<E  ,D *C >*[J ,J ]\n\
[\\ z/       ] \\         / [    z]\
"""
    ucode_str = \
u"""\
⎡    2      ⎤ ⎧ -2  †  †⎫ ⎡ 2   ⎤\n\
⎢⎛J ⎞ ,A + B⎥⋅⎨E  ,D ⋅C ⎬⋅⎢J ,J ⎥\n\
⎣⎝ z⎠       ⎦ ⎩         ⎭ ⎣    z⎦\
"""
    assert pretty(e2) == ascii_str
    assert upretty(e2) == ucode_str
    assert latex(e2) == \
        r'\left[\left(J_z\right)^{2},A + B\right] \left\{\left(E\right)^{-2},D^{\dag} C^{\dag}\right\} \left[J^2,J_z\right]'
    sT(
        e2,
        "Mul(Commutator(Pow(JzOp(Symbol('J')), Integer(2)),Add(Operator(Symbol('A')), Operator(Symbol('B')))), AntiCommutator(Pow(Operator(Symbol('E')), Integer(-2)),Mul(Dagger(Operator(Symbol('D'))), Dagger(Operator(Symbol('C'))))), Commutator(J2Op(Symbol('J')),JzOp(Symbol('J'))))"
    )
    assert str(e3) == \
        "Wigner3j(1, 2, 3, 4, 5, 6)*[Dagger(B) + A,C + D]x(-J2 + Jz)*|1,0><1,1|*(|1,0,j1=1,j2=1> + |1,1,j1=1,j2=1>)x|1,-1,j1=1,j2=1>"
    ascii_str = \
"""\
          [ +          ]  /   2     \\                                                                 \n\
/1  3  5\\*[B  + A,C + D]x |- J  + J |*|1,0><1,1|*(|1,0,j1=1,j2=1> + |1,1,j1=1,j2=1>)x |1,-1,j1=1,j2=1>\n\
|       |                 \\        z/                                                                 \n\
\\2  4  6/                                                                                             \
"""
    ucode_str = \
u"""\
          ⎡ †          ⎤  ⎛   2     ⎞                                                                 \n\
⎛1  3  5⎞⋅⎣B  + A,C + D⎦⨂ ⎜- J  + J ⎟⋅❘1,0⟩⟨1,1❘⋅(❘1,0,j₁=1,j₂=1⟩ + ❘1,1,j₁=1,j₂=1⟩)⨂ ❘1,-1,j₁=1,j₂=1⟩\n\
⎜       ⎟                 ⎝        z⎠                                                                 \n\
⎝2  4  6⎠                                                                                             \
"""
    assert pretty(e3) == ascii_str
    assert upretty(e3) == ucode_str
    assert latex(e3) == \
        r'\left(\begin{array}{ccc} 1 & 3 & 5 \\ 2 & 4 & 6 \end{array}\right) {\left[B^{\dag} + A,C + D\right]}\otimes \left({- J^2 + J_z}\right) {\left|1,0\right\rangle }{\left\langle 1,1\right|} \left({{\left|1,0,j_{1}=1,j_{2}=1\right\rangle } + {\left|1,1,j_{1}=1,j_{2}=1\right\rangle }}\right)\otimes {{\left|1,-1,j_{1}=1,j_{2}=1\right\rangle }}'
    sT(
        e3,
        "Mul(Wigner3j(Integer(1), Integer(2), Integer(3), Integer(4), Integer(5), Integer(6)), TensorProduct(Commutator(Add(Dagger(Operator(Symbol('B'))), Operator(Symbol('A'))),Add(Operator(Symbol('C')), Operator(Symbol('D')))), Add(Mul(Integer(-1), J2Op(Symbol('J'))), JzOp(Symbol('J')))), OuterProduct(JzKet(Integer(1),Integer(0)),JzBra(Integer(1),Integer(1))), TensorProduct(Add(JzKetCoupled(Integer(1),Integer(0),Tuple(Integer(1), Integer(1)),Tuple(Tuple(Integer(1), Integer(2), Integer(1)))), JzKetCoupled(Integer(1),Integer(1),Tuple(Integer(1), Integer(1)),Tuple(Tuple(Integer(1), Integer(2), Integer(1))))), JzKetCoupled(Integer(1),Integer(-1),Tuple(Integer(1), Integer(1)),Tuple(Tuple(Integer(1), Integer(2), Integer(1))))))"
    )
    assert str(e4) == '(C(1)*C(2)+F**2)*(L2([0, oo))+H)'
    ascii_str = \
"""\
// 1    2\\    x2\\   / 2    \\\n\
\\\\C  x C / + F  / x \L  + H/\
"""
    ucode_str = \
u"""\
⎛⎛ 1    2⎞    ⨂2⎞   ⎛ 2    ⎞\n\
⎝⎝C  ⨂ C ⎠ ⊕ F  ⎠ ⨂ ⎝L  ⊕ H⎠\
"""
    assert pretty(e4) == ascii_str
    assert upretty(e4) == ucode_str
    assert latex(e4) == \
        r'\left(\left(\mathcal{C}^{1}\otimes \mathcal{C}^{2}\right)\oplus {\mathcal{F}}^{\otimes 2}\right)\otimes \left({\mathcal{L}^2}\left( \left[0, \infty\right) \right)\oplus \mathcal{H}\right)'
    sT(
        e4,
        "TensorProductHilbertSpace((DirectSumHilbertSpace(TensorProductHilbertSpace(ComplexSpace(Integer(1)),ComplexSpace(Integer(2))),TensorPowerHilbertSpace(FockSpace(),Integer(2)))),(DirectSumHilbertSpace(L2(Interval(Integer(0), oo, False, True)),HilbertSpace())))"
    )
Beispiel #22
0
def test_operator():
    a = Operator('A')
    b = Operator('B', Symbol('t'), S(1) / 2)
    inv = a.inv()
    f = Function('f')
    x = symbols('x')
    d = DifferentialOperator(Derivative(f(x), x), f(x))
    op = OuterProduct(Ket(), Bra())
    assert str(a) == 'A'
    assert pretty(a) == 'A'
    assert upretty(a) == 'A'
    assert latex(a) == 'A'
    sT(a, "Operator(Symbol('A'))")
    assert str(inv) == 'A**(-1)'
    ascii_str = \
"""\
 -1\n\
A  \
"""
    ucode_str = \
"""\
 -1\n\
A  \
"""
    assert pretty(inv) == ascii_str
    assert upretty(inv) == ucode_str
    #FIXME ajgpitch 2019-09-22
    # It's not clear to me why these extra brackets would be wanted / needed
    #assert latex(inv) == r'\left(A\right)^{-1}'
    # This renders okay
    assert latex(inv) == r'A^{-1}'
    sT(inv, "Pow(Operator(Symbol('A')), Integer(-1))")
    assert str(d) == 'DifferentialOperator(Derivative(f(x), x),f(x))'
    ascii_str = \
"""\
                    /d            \\\n\
DifferentialOperator|--(f(x)),f(x)|\n\
                    \dx           /\
"""
    ucode_str = \
"""\
                    ⎛d            ⎞\n\
DifferentialOperator⎜──(f(x)),f(x)⎟\n\
                    ⎝dx           ⎠\
"""
    assert pretty(d) == ascii_str
    assert upretty(d) == ucode_str
    assert latex(d) == \
        r'DifferentialOperator\left(\frac{d}{d x} f{\left(x \right)},f{\left(x \right)}\right)'
    #FIXME: ajgpitch 2019-09-22
    # Not clear why this is failing
    # `Tuple(Symbol('x'), Integer(1))` seems to enter into srepr(expr)
    # for some reason.
    sT(
        d,
        "DifferentialOperator(Derivative(Function('f')(Symbol('x')), Symbol('x')),Function('f')(Symbol('x')))"
    )
    assert str(b) == 'Operator(B,t,1/2)'
    assert pretty(b) == 'Operator(B,t,1/2)'
    assert upretty(b) == 'Operator(B,t,1/2)'
    assert latex(b) == r'Operator\left(B,t,\frac{1}{2}\right)'
    sT(b, "Operator(Symbol('B'),Symbol('t'),Rational(1, 2))")
    assert str(op) == '|psi><psi|'
    assert pretty(op) == '|psi><psi|'
    assert upretty(op) == '❘ψ⟩⟨ψ❘'
    assert latex(op) == r'{\left|\psi\right\rangle }{\left\langle \psi\right|}'
    sT(op, "OuterProduct(Ket(Symbol('psi')),Bra(Symbol('psi')))")
Beispiel #23
0
def test_operator_dagger():
    A = Operator("A")
    B = Operator("B")
    assert Dagger(A * B) == Dagger(B) * Dagger(A)
    assert Dagger(A + B) == Dagger(A) + Dagger(B)
    assert Dagger(A**2) == Dagger(A)**2
from sympy.physics.quantum.qapply import qapply
from sympy.physics.quantum.spin import Jx, Jy, Jz, Jplus, Jminus, J2, JzKet
from sympy.physics.quantum.tensorproduct import TensorProduct
from sympy.physics.quantum.state import Ket
from sympy.physics.quantum.density import Density
from sympy.physics.quantum.qubit import Qubit
from sympy.physics.quantum.boson import BosonOp, BosonFockKet, BosonFockBra
from sympy.physics.quantum.tensorproduct import TensorProduct

j, jp, m, mp = symbols("j j' m m'")

z = JzKet(1, 0)
po = JzKet(1, 1)
mo = JzKet(1, -1)

A = Operator('A')


class Foo(Operator):
    def _apply_operator_JzKet(self, ket, **options):
        return ket


def test_basic():
    assert qapply(Jz * po) == hbar * po
    assert qapply(Jx * z) == hbar * po / sqrt(2) + hbar * mo / sqrt(2)
    assert qapply((Jplus + Jminus) * z / sqrt(2)) == hbar * po + hbar * mo
    assert qapply(Jz * (po + mo)) == hbar * po - hbar * mo
    assert qapply(Jz * po + Jz * mo) == hbar * po - hbar * mo
    assert qapply(Jminus * Jminus * po) == 2 * hbar**2 * mo
    assert qapply(Jplus**2 * mo) == 2 * hbar**2 * po
from sympy.physics.quantum.cartesian import XOp, XKet, PxOp, PxKet
from sympy.physics.quantum.operatorset import operators_to_state
from sympy.physics.quantum.represent import rep_expectation
from sympy.physics.quantum.operator import Operator
operators_to_state(XOp)
#|x>
operators_to_state(XOp())
#|x>
operators_to_state(PxOp)
#|px>
operators_to_state(PxOp())
#|px>
operators_to_state(Operator)
#|psi>
operators_to_state(Operator())
#|psi>

rep_expectation(XOp())
#x_1*DiracDelta(x_1 - x_2)
rep_expectation(XOp(), basis=PxOp())
#<px_2|*X*|px_1>
rep_expectation(XOp(), basis=PxKet())
#<px_2|*X*|px_1>
def test_issue_6073():
    x, y = symbols('x y', commutative=False)
    A = Ket(x, y)
    B = Operator('B')
    assert qapply(A) == A
    assert qapply(A.dual * B) == A.dual * B
Beispiel #27
0
from sympy.physics.quantum.operator import Operator
from sympy.physics.quantum.qapply import qapply
from sympy.physics.quantum.spin import Jx, Jy, Jz, Jplus, Jminus, J2, JzKet
from sympy.physics.quantum.tensorproduct import TensorProduct
from sympy.physics.quantum.state import Ket
from sympy.physics.quantum.density import Density
from sympy.physics.quantum.qubit import Qubit
from sympy.physics.quantum.boson import BosonOp, BosonFockKet, BosonFockBra

j, jp, m, mp = symbols("j j' m m'")

z = JzKet(1, 0)
po = JzKet(1, 1)
mo = JzKet(1, -1)

A = Operator("A")


class Foo(Operator):
    def _apply_operator_JzKet(self, ket, **options):
        return ket


def test_basic():
    assert qapply(Jz * po) == hbar * po
    assert qapply(Jx * z) == hbar * po / sqrt(2) + hbar * mo / sqrt(2)
    assert qapply((Jplus + Jminus) * z / sqrt(2)) == hbar * po + hbar * mo
    assert qapply(Jz * (po + mo)) == hbar * po - hbar * mo
    assert qapply(Jz * po + Jz * mo) == hbar * po - hbar * mo
    assert qapply(Jminus * Jminus * po) == 2 * hbar**2 * mo
    assert qapply(Jplus**2 * mo) == 2 * hbar**2 * po