示例#1
0
def test_j2():
    j, m = symbols('j m')
    assert Commutator(J2, Jz).doit() == 0
    assert qapply(J2 * JzKet(1, 1)) == 2 * hbar**2 * JzKet(1, 1)
    assert qapply(J2 * JzKet(
        j, m)) == j**2 * hbar**2 * JzKet(j, m) + j * hbar**2 * JzKet(j, m)
    assert J2.matrix_element(1, 1, 1, 1) == 2 * hbar**2
示例#2
0
def test_entropy():
    up = JzKet(S(1) / 2, S(1) / 2)
    down = JzKet(S(1) / 2, -S(1) / 2)
    d = Density((up, S(1) / 2), (down, S(1) / 2))

    # test for density object
    ent = entropy(d)
    assert entropy(d) == log(2) / 2
    assert d.entropy() == log(2) / 2

    np = import_module('numpy', min_module_version='1.4.0')
    if np:
        #do this test only if 'numpy' is available on test machine
        np_mat = represent(d, format='numpy')
        ent = entropy(np_mat)
        assert isinstance(np_mat, np.matrixlib.defmatrix.matrix)
        assert ent.real == 0.69314718055994529
        assert ent.imag == 0

    scipy = import_module('scipy', __import__kwargs={'fromlist': ['sparse']})
    if scipy and np:
        #do this test only if numpy and scipy are available
        mat = represent(d, format="scipy.sparse")
        assert isinstance(mat, scipy_sparse_matrix)
        assert ent.real == 0.69314718055994529
        assert ent.imag == 0
示例#3
0
def test_jx():
    assert Commutator(Jx, Jz).doit() == -I * hbar * Jy
    assert qapply(Jx * JzKet(1, 1)) == sqrt(2) * hbar * JzKet(1, 0) / 2
    assert Jx.rewrite('plusminus') == (Jminus + Jplus) / 2
    assert represent(Jx, basis=Jz,
                     j=1) == (represent(Jplus, basis=Jz, j=1) +
                              represent(Jminus, basis=Jz, j=1)) / 2
示例#4
0
def test_entropy():
    up = JzKet(S.Half, S.Half)
    down = JzKet(S.Half, Rational(-1, 2))
    d = Density((up, S.Half), (down, S.Half))

    # test for density object
    ent = entropy(d)
    assert entropy(d) == log(2) / 2
    assert d.entropy() == log(2) / 2

    np = import_module("numpy", min_module_version="1.4.0")
    if np:
        # do this test only if 'numpy' is available on test machine
        np_mat = represent(d, format="numpy")
        ent = entropy(np_mat)
        assert isinstance(np_mat, np.matrixlib.defmatrix.matrix)
        assert ent.real == 0.69314718055994529
        assert ent.imag == 0

    scipy = import_module("scipy", import_kwargs={"fromlist": ["sparse"]})
    if scipy and np:
        # do this test only if numpy and scipy are available
        mat = represent(d, format="scipy.sparse")
        assert isinstance(mat, scipy_sparse_matrix)
        assert ent.real == 0.69314718055994529
        assert ent.imag == 0
示例#5
0
def test_spin():
    assert operators_to_state(set([J2Op, JxOp])) == JxKet
    assert operators_to_state(set([J2Op, JyOp])) == JyKet
    assert operators_to_state(set([J2Op, JzOp])) == JzKet
    #FIXME ajgpitch 22 Sept 2019:
    # JxKet() etc seems to require positional arguments: 'j' and 'm'
    # So these tests cannot work
    # Probably easy to work out what 'j' and 'm' should be from the physics
    assert operators_to_state(set([J2Op(), JxOp()])) == JxKet()
    assert operators_to_state(set([J2Op(), JyOp()])) == JyKet()
    assert operators_to_state(set([J2Op(), JzOp()])) == JzKet()

    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])

    #FIXME ajgpitch 22 Sept 2019:
    # JxKet() etc seems to require positional arguments: 'j' and 'm'
    # So these tests cannot work
    # Probably easy to work out what 'j' and 'm' should be from the physics
    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()])
def test_issue3044():
    expr1 = TensorProduct(Jz * JzKet(S(2), S(-1)) / sqrt(2),
                          Jz * JzKet(S(1) / 2,
                                     S(1) / 2))
    result = Mul(S(-1), S(1) / 4, (2**(S(1) / 2)), hbar**2)
    result *= TensorProduct(JzKet(2, -1), JzKet(S(1) / 2, S(1) / 2))
    assert qapply(expr1) == result
示例#7
0
def test_tensorproduct():
    tp = TensorProduct(JzKet(1, 1), JzKet(1, 0))
    assert str(tp) == '|1,1>x|1,0>'
    assert pretty(tp) == '|1,1>x |1,0>'
    assert upretty(tp) == '❘1,1⟩⨂ ❘1,0⟩'
    assert latex(tp) == \
        r'{{\left|1,1\right\rangle }}\otimes {{\left|1,0\right\rangle }}'
    sT(tp, "TensorProduct(JzKet(Integer(1),Integer(1)), JzKet(Integer(1),Integer(0)))")
示例#8
0
def test_tensorproduct():
    tp = TensorProduct(JzKet(1, 1), JzKet(1, 0))
    assert str(tp) == "|1,1>x|1,0>"
    assert pretty(tp) == "|1,1>x |1,0>"
    assert upretty(tp) == u"❘1,1⟩⨂ ❘1,0⟩"
    assert (latex(tp) ==
            r"{{\left|1,1\right\rangle }}\otimes {{\left|1,0\right\rangle }}")
    sT(
        tp,
        "TensorProduct(JzKet(Integer(1),Integer(1)), JzKet(Integer(1),Integer(0)))"
    )
示例#9
0
def test_innerproduct():
    j, m = symbols("j m")
    assert InnerProduct(JzBra(1, 1), JzKet(1, 1)).doit() == 1
    assert InnerProduct(JzBra(S(1) / 2,
                              S(1) / 2), JzKet(S(1) / 2,
                                               -S(1) / 2)).doit() == 0
    assert InnerProduct(JzBra(j, m), JzKet(j, m)).doit() == 1
    assert InnerProduct(JzBra(1, 0), JyKet(1, 1)).doit() == I / sqrt(2)
    assert InnerProduct(JxBra(S(1) / 2,
                              S(1) / 2),
                        JzKet(S(1) / 2,
                              S(1) / 2)).doit() == -sqrt(2) / 2
    assert InnerProduct(JyBra(1, 1), JzKet(1, 1)).doit() == S(1) / 2
    assert InnerProduct(JxBra(1, -1), JyKet(1, 1)).doit() == 0
示例#10
0
def test_op_to_state():
    assert operators_to_state(XOp) == XKet()
    assert operators_to_state(PxOp) == PxKet()
    assert operators_to_state(Operator) == Ket()

    assert operators_to_state(set([J2Op, JxOp])) == JxKet()
    assert operators_to_state(set([J2Op, JyOp])) == JyKet()
    assert operators_to_state(set([J2Op, JzOp])) == JzKet()
    assert operators_to_state(set([J2Op(), JxOp()])) ==  JxKet()
    assert operators_to_state(set([J2Op(), JyOp()])) ==  JyKet()
    assert operators_to_state(set([J2Op(), JzOp()])) ==  JzKet()

    assert state_to_operators(operators_to_state(XOp("Q"))) == XOp("Q")
    assert state_to_operators(operators_to_state(XOp())) == XOp()

    raises(NotImplementedError, 'operators_to_state(XKet)')
示例#11
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)')
示例#12
0
def test_spin():
    assert operators_to_state(set([J2Op, JxOp])) == JxKet()
    assert operators_to_state(set([J2Op, JyOp])) == JyKet()
    assert operators_to_state(set([J2Op, JzOp])) == JzKet()
    assert operators_to_state(set([J2Op(), JxOp()])) == JxKet()
    assert operators_to_state(set([J2Op(), JyOp()])) == JyKet()
    assert operators_to_state(set([J2Op(), JzOp()])) == JzKet()

    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()])
示例#13
0
def test_outer_product():
    k = Ket('k')
    b = Bra('b')
    op = OuterProduct(k, b)

    assert isinstance(op, OuterProduct)
    assert isinstance(op, Operator)

    assert op.ket == k
    assert op.bra == b
    assert op.label == (k, b)
    assert op.is_commutative is False

    op = k*b

    assert isinstance(op, OuterProduct)
    assert isinstance(op, Operator)

    assert op.ket == k
    assert op.bra == b
    assert op.label == (k, b)
    assert op.is_commutative is False

    op = 2*k*b

    assert op == Mul(Integer(2), k, b)

    op = 2*(k*b)

    assert op == Mul(Integer(2), OuterProduct(k, b))

    assert Dagger(k*b) == OuterProduct(Dagger(b), Dagger(k))
    assert Dagger(k*b).is_commutative is False

    #test the _eval_trace
    assert Tr(OuterProduct(JzKet(1, 1), JzBra(1, 1))).doit() == 1

    # test scaled kets and bras
    assert OuterProduct(2 * k, b) == 2 * OuterProduct(k, b)
    assert OuterProduct(k, 2 * b) == 2 * OuterProduct(k, b)

    # test sums of kets and bras
    k1, k2 = Ket('k1'), Ket('k2')
    b1, b2 = Bra('b1'), Bra('b2')
    assert (OuterProduct(k1 + k2, b1) ==
            OuterProduct(k1, b1) + OuterProduct(k2, b1))
    assert (OuterProduct(k1, b1 + b2) ==
            OuterProduct(k1, b1) + OuterProduct(k1, b2))
    assert (OuterProduct(1 * k1 + 2 * k2, 3 * b1 + 4 * b2) ==
            3 * OuterProduct(k1, b1) +
            4 * OuterProduct(k1, b2) +
            6 * OuterProduct(k2, b1) +
            8 * OuterProduct(k2, b2))
示例#14
0
def test_spin():
    assert operators_to_state({J2Op, JxOp}) == JxKet()
    assert operators_to_state({J2Op, JyOp}) == JyKet()
    assert operators_to_state({J2Op, JzOp}) == JzKet()
    assert operators_to_state({J2Op(), JxOp()}) == JxKet()
    assert operators_to_state({J2Op(), JyOp()}) == JyKet()
    assert operators_to_state({J2Op(), JzOp()}) == JzKet()

    assert state_to_operators(JxKet) == {J2Op(), JxOp()}
    assert state_to_operators(JyKet) == {J2Op(), JyOp()}
    assert state_to_operators(JzKet) == {J2Op(), JzOp()}
    assert state_to_operators(JxBra) == {J2Op(), JxOp()}
    assert state_to_operators(JyBra) == {J2Op(), JyOp()}
    assert state_to_operators(JzBra) == {J2Op(), JzOp()}

    assert state_to_operators(JxKet()) == {J2Op(), JxOp()}
    assert state_to_operators(JyKet()) == {J2Op(), JyOp()}
    assert state_to_operators(JzKet()) == {J2Op(), JzOp()}
    assert state_to_operators(JxBra()) == {J2Op(), JxOp()}
    assert state_to_operators(JyBra()) == {J2Op(), JyOp()}
    assert state_to_operators(JzBra()) == {J2Op(), JzOp()}
示例#15
0
def test_eval_trace():
    up = JzKet(S(1) / 2, S(1) / 2)
    down = JzKet(S(1) / 2, -S(1) / 2)
    d = Density((up, 0.5), (down, 0.5))

    t = Tr(d)
    assert t.doit() == 1

    #test dummy time dependent states
    class TestTimeDepKet(TimeDepKet):
        def _eval_trace(self, bra, **options):
            return 1

    x, t = symbols('x t')
    k1 = TestTimeDepKet(0, 0.5)
    k2 = TestTimeDepKet(0, 1)
    d = Density([k1, 0.5], [k2, 0.5])
    assert d.doit() == (0.5 * OuterProduct(k1, k1.dual) +
                        0.5 * OuterProduct(k2, k2.dual))

    t = Tr(d)
    assert t.doit() == 1
示例#16
0
def test_outer_product():
    k = Ket('k')
    b = Bra('b')
    op = OuterProduct(k, b)

    assert isinstance(op, OuterProduct)
    assert isinstance(op, Operator)

    assert op.ket == k
    assert op.bra == b
    assert op.label == (k, b)
    assert op.is_commutative is False

    op = k * b

    assert isinstance(op, OuterProduct)
    assert isinstance(op, Operator)

    assert op.ket == k
    assert op.bra == b
    assert op.label == (k, b)
    assert op.is_commutative is False

    op = 2 * k * b

    assert op == Mul(Integer(2), k, b)

    op = 2 * (k * b)

    assert op == Mul(Integer(2), OuterProduct(k, b))

    assert Dagger(k * b) == OuterProduct(Dagger(b), Dagger(k))
    assert Dagger(k * b).is_commutative is False

    #test the _eval_trace
    assert Tr(OuterProduct(JzKet(1, 1), JzBra(1, 1))).doit() == 1
示例#17
0
def test_rewrite():
    # Rewrite to same basis
    assert JxBra(1, 1).rewrite('Jx') == JxBra(1, 1)
    assert JxKet(1, 1).rewrite('Jx') == JxKet(1, 1)
    # Rewriting to different basis
    assert JxKet(1, 1).rewrite('Jy') == I * JyKet(1, 1)
    assert JxKet(1, 0).rewrite('Jy') == JyKet(1, 0)
    assert JxKet(1, -1).rewrite('Jy') == -I * JyKet(1, -1)
    assert JxKet(1, 1).rewrite(
        'Jz') == JzKet(1, 1) / 2 + JzKet(1, 0) / sqrt(2) + JzKet(1, -1) / 2
    assert JxKet(1, 0).rewrite(
        'Jz') == -sqrt(2) * JzKet(1, 1) / 2 + sqrt(2) * JzKet(1, -1) / 2
    assert JxKet(1, -1).rewrite(
        'Jz') == JzKet(1, 1) / 2 - JzKet(1, 0) / sqrt(2) + JzKet(1, -1) / 2
    assert JyKet(1, 1).rewrite('Jx') == -I * JxKet(1, 1)
    assert JyKet(1, 0).rewrite('Jx') == JxKet(1, 0)
    assert JyKet(1, -1).rewrite('Jx') == I * JxKet(1, -1)
    assert JyKet(1, 1).rewrite('Jz') == JzKet(1, 1) / 2 + sqrt(2) * I * JzKet(
        1, 0) / 2 - JzKet(1, -1) / 2
    assert JyKet(1, 0).rewrite(
        'Jz') == sqrt(2) * I * JzKet(1, 1) / 2 + sqrt(2) * I * JzKet(1, -1) / 2
    assert JyKet(1,
                 -1).rewrite('Jz') == -JzKet(1, 1) / 2 + sqrt(2) * I * JzKet(
                     1, 0) / 2 + JzKet(1, -1) / 2
    assert JzKet(1, 1).rewrite(
        'Jx') == JxKet(1, 1) / 2 - sqrt(2) * JxKet(1, 0) / 2 + JxKet(1, -1) / 2
    assert JzKet(1, 0).rewrite(
        'Jx') == sqrt(2) * JxKet(1, 1) / 2 - sqrt(2) * JxKet(1, -1) / 2
    assert JzKet(1, -1).rewrite(
        'Jx') == JxKet(1, 1) / 2 + sqrt(2) * JxKet(1, 0) / 2 + JxKet(1, -1) / 2
    assert JzKet(1, 1).rewrite('Jy') == JyKet(1, 1) / 2 - sqrt(2) * I * JyKet(
        1, 0) / 2 - JyKet(1, -1) / 2
    assert JzKet(1, 0).rewrite('Jy') == -sqrt(2) * I * JyKet(
        1, 1) / 2 - sqrt(2) * I * JyKet(1, -1) / 2
    assert JzKet(1,
                 -1).rewrite('Jy') == -JyKet(1, 1) / 2 - sqrt(2) * I * JyKet(
                     1, 0) / 2 + JyKet(1, -1) / 2
    # Innerproducts of rewritten states
    assert qapply(JxBra(1, 1) * JxKet(1, 1).rewrite('Jy')).doit() == 1
    assert qapply(JxBra(1, 0) * JxKet(1, 0).rewrite('Jy')).doit() == 1
    assert qapply(JxBra(1, -1) * JxKet(1, -1).rewrite('Jy')).doit() == 1
    assert qapply(JxBra(1, 1) * JxKet(1, 1).rewrite('Jz')).doit() == 1
    assert qapply(JxBra(1, 0) * JxKet(1, 0).rewrite('Jz')).doit() == 1
    assert qapply(JxBra(1, -1) * JxKet(1, -1).rewrite('Jz')).doit() == 1
    assert qapply(JyBra(1, 1) * JyKet(1, 1).rewrite('Jx')).doit() == 1
    assert qapply(JyBra(1, 0) * JyKet(1, 0).rewrite('Jx')).doit() == 1
    assert qapply(JyBra(1, -1) * JyKet(1, -1).rewrite('Jx')).doit() == 1
    assert qapply(JyBra(1, 1) * JyKet(1, 1).rewrite('Jz')).doit() == 1
    assert qapply(JyBra(1, 0) * JyKet(1, 0).rewrite('Jz')).doit() == 1
    assert qapply(JyBra(1, -1) * JyKet(1, -1).rewrite('Jz')).doit() == 1
    assert qapply(JyBra(1, 1) * JyKet(1, 1).rewrite('Jz')).doit() == 1
    assert qapply(JyBra(1, 0) * JyKet(1, 0).rewrite('Jz')).doit() == 1
    assert qapply(JyBra(1, -1) * JyKet(1, -1).rewrite('Jz')).doit() == 1
    assert qapply(JzBra(1, 1) * JzKet(1, 1).rewrite('Jy')).doit() == 1
    assert qapply(JzBra(1, 0) * JzKet(1, 0).rewrite('Jy')).doit() == 1
    assert qapply(JzBra(1, -1) * JzKet(1, -1).rewrite('Jy')).doit() == 1
    assert qapply(JxBra(1, 1) * JxKet(1, 0).rewrite('Jy')).doit() == 0
    assert qapply(JxBra(1, 1) * JxKet(1, -1).rewrite('Jy')) == 0
    assert qapply(JxBra(1, 1) * JxKet(1, 0).rewrite('Jz')).doit() == 0
    assert qapply(JxBra(1, 1) * JxKet(1, -1).rewrite('Jz')) == 0
    assert qapply(JyBra(1, 1) * JyKet(1, 0).rewrite('Jx')).doit() == 0
    assert qapply(JyBra(1, 1) * JyKet(1, -1).rewrite('Jx')) == 0
    assert qapply(JyBra(1, 1) * JyKet(1, 0).rewrite('Jz')).doit() == 0
    assert qapply(JyBra(1, 1) * JyKet(1, -1).rewrite('Jz')) == 0
    assert qapply(JzBra(1, 1) * JzKet(1, 0).rewrite('Jx')).doit() == 0
    assert qapply(JzBra(1, 1) * JzKet(1, -1).rewrite('Jx')) == 0
    assert qapply(JzBra(1, 1) * JzKet(1, 0).rewrite('Jy')).doit() == 0
    assert qapply(JzBra(1, 1) * JzKet(1, -1).rewrite('Jy')) == 0
    assert qapply(JxBra(1, 0) * JxKet(1, 1).rewrite('Jy')) == 0
    assert qapply(JxBra(1, 0) * JxKet(1, -1).rewrite('Jy')) == 0
    assert qapply(JxBra(1, 0) * JxKet(1, 1).rewrite('Jz')) == 0
    assert qapply(JxBra(1, 0) * JxKet(1, -1).rewrite('Jz')) == 0
    assert qapply(JyBra(1, 0) * JyKet(1, 1).rewrite('Jx')) == 0
    assert qapply(JyBra(1, 0) * JyKet(1, -1).rewrite('Jx')) == 0
    assert qapply(JyBra(1, 0) * JyKet(1, 1).rewrite('Jz')) == 0
    assert qapply(JyBra(1, 0) * JyKet(1, -1).rewrite('Jz')) == 0
    assert qapply(JzBra(1, 0) * JzKet(1, 1).rewrite('Jx')) == 0
    assert qapply(JzBra(1, 0) * JzKet(1, -1).rewrite('Jx')) == 0
    assert qapply(JzBra(1, 0) * JzKet(1, 1).rewrite('Jy')) == 0
    assert qapply(JzBra(1, 0) * JzKet(1, -1).rewrite('Jy')) == 0
    assert qapply(JxBra(1, -1) * JxKet(1, 1).rewrite('Jy')) == 0
    assert qapply(JxBra(1, -1) * JxKet(1, 0).rewrite('Jy')).doit() == 0
    assert qapply(JxBra(1, -1) * JxKet(1, 1).rewrite('Jz')) == 0
    assert qapply(JxBra(1, -1) * JxKet(1, 0).rewrite('Jz')).doit() == 0
    assert qapply(JyBra(1, -1) * JyKet(1, 1).rewrite('Jx')) == 0
    assert qapply(JyBra(1, -1) * JyKet(1, 0).rewrite('Jx')).doit() == 0
    assert qapply(JyBra(1, -1) * JyKet(1, 1).rewrite('Jz')) == 0
    assert qapply(JyBra(1, -1) * JyKet(1, 0).rewrite('Jz')).doit() == 0
    assert qapply(JzBra(1, -1) * JzKet(1, 1).rewrite('Jx')) == 0
    assert qapply(JzBra(1, -1) * JzKet(1, 0).rewrite('Jx')).doit() == 0
    assert qapply(JzBra(1, -1) * JzKet(1, 1).rewrite('Jy')) == 0
    assert qapply(JzBra(1, -1) * JzKet(1, 0).rewrite('Jy')).doit() == 0
示例#18
0
def test_jz():
    assert Commutator(Jz, Jminus).doit() == -hbar * Jminus
    assert qapply(Jz * JzKet(2, 1)) == hbar * JzKet(2, 1)
    assert Jz.rewrite('plusminus')
示例#19
0
def test_jy():
    assert Commutator(Jy, Jz).doit() == I * hbar * Jx
    assert qapply(Jy * JzKet(1, 1)) == I * sqrt(2) * hbar * JzKet(1, 0) / 2
    assert Jy.rewrite('plusminus') == (Jplus - Jminus) / (2 * I)
    assert represent(Jy, basis=Jz) == (represent(Jplus, basis=Jz) -
                                       represent(Jminus, basis=Jz)) / (2 * I)
示例#20
0
def test_spin():
    lz = JzOp('L')
    ket = JzKet(1, 0)
    bra = JzBra(1, 0)
    cket = JzKetCoupled(1, 0, (1, 2))
    cbra = JzBraCoupled(1, 0, (1, 2))
    cket_big = JzKetCoupled(1, 0, (1, 2, 3))
    cbra_big = JzBraCoupled(1, 0, (1, 2, 3))
    rot = Rotation(1, 2, 3)
    bigd = WignerD(1, 2, 3, 4, 5, 6)
    smalld = WignerD(1, 2, 3, 0, 4, 0)
    assert str(lz) == 'Lz'
    ascii_str = \
"""\
L \n\
 z\
"""
    ucode_str = \
u"""\
L \n\
 z\
"""
    assert pretty(lz) == ascii_str
    assert upretty(lz) == ucode_str
    assert latex(lz) == 'L_z'
    sT(lz, "JzOp(Symbol('L'))")
    assert str(J2) == 'J2'
    ascii_str = \
"""\
 2\n\
J \
"""
    ucode_str = \
u"""\
 2\n\
J \
"""
    assert pretty(J2) == ascii_str
    assert upretty(J2) == ucode_str
    assert latex(J2) == r'J^2'
    sT(J2, "J2Op(Symbol('J'))")
    assert str(Jz) == 'Jz'
    ascii_str = \
"""\
J \n\
 z\
"""
    ucode_str = \
u"""\
J \n\
 z\
"""
    assert pretty(Jz) == ascii_str
    assert upretty(Jz) == ucode_str
    assert latex(Jz) == 'J_z'
    sT(Jz, "JzOp(Symbol('J'))")
    assert str(ket) == '|1,0>'
    assert pretty(ket) == '|1,0>'
    assert upretty(ket) == u'❘1,0⟩'
    assert latex(ket) == r'{\left|1,0\right\rangle }'
    sT(ket, "JzKet(Integer(1),Integer(0))")
    assert str(bra) == '<1,0|'
    assert pretty(bra) == '<1,0|'
    assert upretty(bra) == u'⟨1,0❘'
    assert latex(bra) == r'{\left\langle 1,0\right|}'
    sT(bra, "JzBra(Integer(1),Integer(0))")
    assert str(cket) == '|1,0,j1=1,j2=2>'
    assert pretty(cket) == '|1,0,j1=1,j2=2>'
    assert upretty(cket) == u'❘1,0,j₁=1,j₂=2⟩'
    assert latex(cket) == r'{\left|1,0,j_{1}=1,j_{2}=2\right\rangle }'
    sT(
        cket,
        "JzKetCoupled(Integer(1),Integer(0),Tuple(Integer(1), Integer(2)),Tuple(Tuple(Integer(1), Integer(2), Integer(1))))"
    )
    assert str(cbra) == '<1,0,j1=1,j2=2|'
    assert pretty(cbra) == '<1,0,j1=1,j2=2|'
    assert upretty(cbra) == u'⟨1,0,j₁=1,j₂=2❘'
    assert latex(cbra) == r'{\left\langle 1,0,j_{1}=1,j_{2}=2\right|}'
    sT(
        cbra,
        "JzBraCoupled(Integer(1),Integer(0),Tuple(Integer(1), Integer(2)),Tuple(Tuple(Integer(1), Integer(2), Integer(1))))"
    )
    assert str(cket_big) == '|1,0,j1=1,j2=2,j3=3,j(1,2)=3>'
    # TODO: Fix non-unicode pretty printing
    # i.e. j1,2 -> j(1,2)
    assert pretty(cket_big) == '|1,0,j1=1,j2=2,j3=3,j1,2=3>'
    assert upretty(cket_big) == u'❘1,0,j₁=1,j₂=2,j₃=3,j₁,₂=3⟩'
    assert latex(cket_big) == \
        r'{\left|1,0,j_{1}=1,j_{2}=2,j_{3}=3,j_{1,2}=3\right\rangle }'
    sT(
        cket_big,
        "JzKetCoupled(Integer(1),Integer(0),Tuple(Integer(1), Integer(2), Integer(3)),Tuple(Tuple(Integer(1), Integer(2), Integer(3)), Tuple(Integer(1), Integer(3), Integer(1))))"
    )
    assert str(cbra_big) == '<1,0,j1=1,j2=2,j3=3,j(1,2)=3|'
    assert pretty(cbra_big) == u'<1,0,j1=1,j2=2,j3=3,j1,2=3|'
    assert upretty(cbra_big) == u'⟨1,0,j₁=1,j₂=2,j₃=3,j₁,₂=3❘'
    assert latex(cbra_big) == \
        r'{\left\langle 1,0,j_{1}=1,j_{2}=2,j_{3}=3,j_{1,2}=3\right|}'
    sT(
        cbra_big,
        "JzBraCoupled(Integer(1),Integer(0),Tuple(Integer(1), Integer(2), Integer(3)),Tuple(Tuple(Integer(1), Integer(2), Integer(3)), Tuple(Integer(1), Integer(3), Integer(1))))"
    )
    assert str(rot) == 'R(1,2,3)'
    assert pretty(rot) == 'R (1,2,3)'
    assert upretty(rot) == u'ℛ (1,2,3)'
    assert latex(rot) == r'\mathcal{R}\left(1,2,3\right)'
    sT(rot, "Rotation(Integer(1),Integer(2),Integer(3))")
    assert str(bigd) == 'WignerD(1, 2, 3, 4, 5, 6)'
    ascii_str = \
"""\
 1         \n\
D   (4,5,6)\n\
 2,3       \
"""
    ucode_str = \
u"""\
 1         \n\
D   (4,5,6)\n\
 2,3       \
"""
    assert pretty(bigd) == ascii_str
    assert upretty(bigd) == ucode_str
    assert latex(bigd) == r'D^{1}_{2,3}\left(4,5,6\right)'
    sT(
        bigd,
        "WignerD(Integer(1), Integer(2), Integer(3), Integer(4), Integer(5), Integer(6))"
    )
    assert str(smalld) == 'WignerD(1, 2, 3, 0, 4, 0)'
    ascii_str = \
"""\
 1     \n\
d   (4)\n\
 2,3   \
"""
    ucode_str = \
u"""\
 1     \n\
d   (4)\n\
 2,3   \
"""
    assert pretty(smalld) == ascii_str
    assert upretty(smalld) == ucode_str
    assert latex(smalld) == r'd^{1}_{2,3}\left(4\right)'
    sT(
        smalld,
        "WignerD(Integer(1), Integer(2), Integer(3), Integer(0), Integer(4), Integer(0))"
    )
from sympy.physics.quantum.constants import hbar
from sympy.physics.quantum.dagger import Dagger
from sympy.physics.quantum.gate import H
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
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
示例#22
0
def test_jminus():
    assert qapply(Jminus * JzKet(1, -1)) == 0
    assert Jminus.matrix_element(1, 0, 1, 1) == sqrt(2) * hbar
    assert Jminus.rewrite('xyz') == Jx - I * Jy
示例#23
0
def test_jplus():
    assert Commutator(Jplus, Jminus).doit() == 2 * hbar * Jz
    assert qapply(Jplus * JzKet(1, 1)) == 0
    assert Jplus.matrix_element(1, 1, 1, 1) == 0
    assert Jplus.rewrite('xyz') == Jx + I * Jy
示例#24
0
def test_sympy__physics__quantum__spin__JzKet():
    from sympy.physics.quantum.spin import JzKet
    assert _test_args(JzKet(1, 0))
示例#25
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())))"
    )
示例#26
0
def test_fidelity():
    #test with kets
    up = JzKet(S(1) / 2, S(1) / 2)
    down = JzKet(S(1) / 2, -S(1) / 2)
    updown = (S(1) / sqrt(2)) * up + (S(1) / sqrt(2)) * down

    #check with matrices
    up_dm = represent(up * Dagger(up))
    down_dm = represent(down * Dagger(down))
    updown_dm = represent(updown * Dagger(updown))

    assert abs(fidelity(up_dm, up_dm) - 1) < 1e-3
    assert fidelity(up_dm, down_dm) < 1e-3
    assert abs(fidelity(up_dm, updown_dm) - (S(1) / sqrt(2))) < 1e-3
    assert abs(fidelity(updown_dm, down_dm) - (S(1) / sqrt(2))) < 1e-3

    #check with density
    up_dm = Density([up, 1.0])
    down_dm = Density([down, 1.0])
    updown_dm = Density([updown, 1.0])

    assert abs(fidelity(up_dm, up_dm) - 1) < 1e-3
    assert abs(fidelity(up_dm, down_dm)) < 1e-3
    assert abs(fidelity(up_dm, updown_dm) - (S(1) / sqrt(2))) < 1e-3
    assert abs(fidelity(updown_dm, down_dm) - (S(1) / sqrt(2))) < 1e-3

    #check mixed states with density
    updown2 = (sqrt(3) / 2) * up + (S(1) / 2) * down
    d1 = Density([updown, 0.25], [updown2, 0.75])
    d2 = Density([updown, 0.75], [updown2, 0.25])
    assert abs(fidelity(d1, d2) - 0.991) < 1e-3
    assert abs(fidelity(d2, d1) - fidelity(d1, d2)) < 1e-3

    #using qubits/density(pure states)
    state1 = Qubit('0')
    state2 = Qubit('1')
    state3 = (S(1) / sqrt(2)) * state1 + (S(1) / sqrt(2)) * state2
    state4 = (sqrt(S(2) / 3)) * state1 + (S(1) / sqrt(3)) * state2

    state1_dm = Density([state1, 1])
    state2_dm = Density([state2, 1])
    state3_dm = Density([state3, 1])

    assert fidelity(state1_dm, state1_dm) == 1
    assert fidelity(state1_dm, state2_dm) == 0
    assert abs(fidelity(state1_dm, state3_dm) - 1 / sqrt(2)) < 1e-3
    assert abs(fidelity(state3_dm, state2_dm) - 1 / sqrt(2)) < 1e-3

    #using qubits/density(mixed states)
    d1 = Density([state3, 0.70], [state4, 0.30])
    d2 = Density([state3, 0.20], [state4, 0.80])
    assert abs(fidelity(d1, d1) - 1) < 1e-3
    assert abs(fidelity(d1, d2) - 0.996) < 1e-3
    assert abs(fidelity(d1, d2) - fidelity(d2, d1)) < 1e-3

    #TODO: test for invalid arguments
    # non-square matrix
    mat1 = [[0, 0], [0, 0], [0, 0]]

    mat2 = [[0, 0], [0, 0]]
    raises(ValueError, lambda: fidelity(mat1, mat2))

    # unequal dimensions
    mat1 = [[0, 0], [0, 0]]
    mat2 = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
    raises(ValueError, lambda: fidelity(mat1, mat2))

    # unsupported data-type
    x, y = 1, 2  # random values that is not a matrix
    raises(ValueError, lambda: fidelity(x, y))
示例#27
0
def test_doit():

    x, y = symbols('x y')
    A, B, C, D, E, F = symbols('A B C D E F', commutative=False)
    d = Density([XKet(), 0.5], [PxKet(), 0.5])
    assert (0.5 * (PxKet() * Dagger(PxKet())) + 0.5 *
            (XKet() * Dagger(XKet()))) == d.doit()

    # check for kets with expr in them
    d_with_sym = Density([XKet(x * y), 0.5], [PxKet(x * y), 0.5])
    assert (0.5 * (PxKet(x * y) * Dagger(PxKet(x * y))) + 0.5 *
            (XKet(x * y) * Dagger(XKet(x * y)))) == d_with_sym.doit()

    d = Density([(A + B) * C, 1.0])
    assert d.doit() == (1.0 * A * C * Dagger(C) * Dagger(A) +
                        1.0 * A * C * Dagger(C) * Dagger(B) +
                        1.0 * B * C * Dagger(C) * Dagger(A) +
                        1.0 * B * C * Dagger(C) * Dagger(B))

    #  With TensorProducts as args
    # Density with simple tensor products as args
    t = TensorProduct(A, B, C)
    d = Density([t, 1.0])
    assert d.doit() == \
        1.0 * TensorProduct(A*Dagger(A), B*Dagger(B), C*Dagger(C))

    # Density with multiple Tensorproducts as states
    t2 = TensorProduct(A, B)
    t3 = TensorProduct(C, D)

    d = Density([t2, 0.5], [t3, 0.5])
    assert d.doit() == (0.5 * TensorProduct(A * Dagger(A), B * Dagger(B)) +
                        0.5 * TensorProduct(C * Dagger(C), D * Dagger(D)))

    #Density with mixed states
    d = Density([t2 + t3, 1.0])
    assert d.doit() == (1.0 * TensorProduct(A * Dagger(A), B * Dagger(B)) +
                        1.0 * TensorProduct(A * Dagger(C), B * Dagger(D)) +
                        1.0 * TensorProduct(C * Dagger(A), D * Dagger(B)) +
                        1.0 * TensorProduct(C * Dagger(C), D * Dagger(D)))

    #Density operators with spin states
    tp1 = TensorProduct(JzKet(1, 1), JzKet(1, -1))
    d = Density([tp1, 1])

    # full trace
    t = Tr(d)
    assert t.doit() == 1

    #Partial trace on density operators with spin states
    t = Tr(d, [0])
    assert t.doit() == JzKet(1, -1) * Dagger(JzKet(1, -1))
    t = Tr(d, [1])
    assert t.doit() == JzKet(1, 1) * Dagger(JzKet(1, 1))

    # with another spin state
    tp2 = TensorProduct(JzKet(S(1) / 2, S(1) / 2), JzKet(S(1) / 2, -S(1) / 2))
    d = Density([tp2, 1])

    #full trace
    t = Tr(d)
    assert t.doit() == 1

    #Partial trace on density operators with spin states
    t = Tr(d, [0])
    assert t.doit() == JzKet(S(1) / 2, -S(1) / 2) * Dagger(
        JzKet(S(1) / 2, -S(1) / 2))
    t = Tr(d, [1])
    assert t.doit() == JzKet(S(1) / 2,
                             S(1) / 2) * Dagger(JzKet(S(1) / 2,
                                                      S(1) / 2))
示例#28
0
def test_issue3044():
    expr1 = TensorProduct(Jz * JzKet(S(2), S.NegativeOne) / sqrt(2),
                          Jz * JzKet(S.Half, S.Half))
    result = Mul(S.NegativeOne, Rational(1, 4), 2**S.Half, hbar**2)
    result *= TensorProduct(JzKet(2, -1), JzKet(S.Half, S.Half))
    assert qapply(expr1) == result
示例#29
0
def test_represent():
    # Spin operators
    assert represent(Jx) == hbar * Matrix([[0, 1], [1, 0]]) / 2
    assert represent(
        Jx,
        j=1) == hbar * sqrt(2) * Matrix([[0, 1, 0], [1, 0, 1], [0, 1, 0]]) / 2
    assert represent(Jy) == hbar * I * Matrix([[0, -1], [1, 0]]) / 2
    assert represent(Jy, j=1) == hbar * I * sqrt(2) * Matrix(
        [[0, -1, 0], [1, 0, -1], [0, 1, 0]]) / 2
    assert represent(Jz) == hbar * Matrix([[1, 0], [0, -1]]) / 2
    assert represent(Jz,
                     j=1) == hbar * Matrix([[1, 0, 0], [0, 0, 0], [0, 0, -1]])
    # Spin states
    # Jx basis
    assert represent(JxKet(S(1) / 2, S(1) / 2), basis=Jx) == Matrix([1, 0])
    assert represent(JxKet(S(1) / 2, -S(1) / 2), basis=Jx) == Matrix([0, 1])
    assert represent(JxKet(1, 1), basis=Jx) == Matrix([1, 0, 0])
    assert represent(JxKet(1, 0), basis=Jx) == Matrix([0, 1, 0])
    assert represent(JxKet(1, -1), basis=Jx) == Matrix([0, 0, 1])
    assert represent(JyKet(S(1) / 2,
                           S(1) / 2),
                     basis=Jx) == Matrix([exp(-I * pi / 4), 0])
    assert represent(JyKet(S(1) / 2, -S(1) / 2),
                     basis=Jx) == Matrix([0, exp(I * pi / 4)])
    assert represent(JyKet(1, 1), basis=Jx) == Matrix([-I, 0, 0])
    assert represent(JyKet(1, 0), basis=Jx) == Matrix([0, 1, 0])
    assert represent(JyKet(1, -1), basis=Jx) == Matrix([0, 0, I])
    assert represent(JzKet(S(1) / 2,
                           S(1) / 2),
                     basis=Jx) == sqrt(2) * Matrix([-1, 1]) / 2
    assert represent(JzKet(S(1) / 2, -S(1) / 2),
                     basis=Jx) == sqrt(2) * Matrix([-1, -1]) / 2
    assert represent(JzKet(1, 1), basis=Jx) == Matrix([1, -sqrt(2), 1]) / 2
    assert represent(JzKet(1, 0), basis=Jx) == sqrt(2) * Matrix([1, 0, -1]) / 2
    assert represent(JzKet(1, -1), basis=Jx) == Matrix([1, sqrt(2), 1]) / 2
    # Jy basis
    assert represent(JxKet(S(1) / 2,
                           S(1) / 2),
                     basis=Jy) == Matrix([exp(-3 * I * pi / 4), 0])
    assert represent(JxKet(S(1) / 2, -S(1) / 2),
                     basis=Jy) == Matrix([0, exp(3 * I * pi / 4)])
    assert represent(JxKet(1, 1), basis=Jy) == Matrix([I, 0, 0])
    assert represent(JxKet(1, 0), basis=Jy) == Matrix([0, 1, 0])
    assert represent(JxKet(1, -1), basis=Jy) == Matrix([0, 0, -I])
    assert represent(JyKet(S(1) / 2, S(1) / 2), basis=Jy) == Matrix([1, 0])
    assert represent(JyKet(S(1) / 2, -S(1) / 2), basis=Jy) == Matrix([0, 1])
    assert represent(JyKet(1, 1), basis=Jy) == Matrix([1, 0, 0])
    assert represent(JyKet(1, 0), basis=Jy) == Matrix([0, 1, 0])
    assert represent(JyKet(1, -1), basis=Jy) == Matrix([0, 0, 1])
    assert represent(JzKet(S(1) / 2,
                           S(1) / 2),
                     basis=Jy) == sqrt(2) * Matrix([-1, I]) / 2
    assert represent(JzKet(S(1) / 2, -S(1) / 2),
                     basis=Jy) == sqrt(2) * Matrix([I, -1]) / 2
    assert represent(JzKet(1, 1),
                     basis=Jy) == Matrix([1, -I * sqrt(2), -1]) / 2
    assert represent(JzKet(1, 0),
                     basis=Jy) == Matrix([-sqrt(2) * I, 0, -sqrt(2) * I]) / 2
    assert represent(JzKet(1, -1),
                     basis=Jy) == Matrix([-1, -sqrt(2) * I, 1]) / 2
    # Jz basis
    assert represent(JxKet(S(1) / 2, S(1) / 2)) == sqrt(2) * Matrix([1, 1]) / 2
    assert represent(JxKet(S(1) / 2,
                           -S(1) / 2)) == sqrt(2) * Matrix([-1, 1]) / 2
    assert represent(JxKet(1, 1)) == Matrix([1, sqrt(2), 1]) / 2
    assert represent(JxKet(1, 0)) == sqrt(2) * Matrix([-1, 0, 1]) / 2
    assert represent(JxKet(1, -1)) == Matrix([1, -sqrt(2), 1]) / 2
    assert represent(JyKet(S(1) / 2,
                           S(1) / 2)) == sqrt(2) * Matrix([-1, -I]) / 2
    assert represent(JyKet(S(1) / 2,
                           -S(1) / 2)) == sqrt(2) * Matrix([-I, -1]) / 2
    assert represent(JyKet(1, 1)) == Matrix([1, sqrt(2) * I, -1]) / 2
    assert represent(JyKet(1, 0)) == sqrt(2) * Matrix([I, 0, I]) / 2
    assert represent(JyKet(1, -1)) == Matrix([-1, sqrt(2) * I, 1]) / 2
    assert represent(JzKet(S(1) / 2, S(1) / 2)) == Matrix([1, 0])
    assert represent(JzKet(S(1) / 2, -S(1) / 2)) == Matrix([0, 1])
    assert represent(JzKet(1, 1)) == Matrix([1, 0, 0])
    assert represent(JzKet(1, 0)) == Matrix([0, 1, 0])
    assert represent(JzKet(1, -1)) == Matrix([0, 0, 1])
示例#30
0
def test_innerproduct():
    x = symbols('x')
    ip1 = InnerProduct(Bra(), Ket())
    ip2 = InnerProduct(TimeDepBra(), TimeDepKet())
    ip3 = InnerProduct(JzBra(1, 1), JzKet(1, 1))
    ip4 = InnerProduct(JzBraCoupled(1, 1, (1, 1)), JzKetCoupled(1, 1, (1, 1)))
    ip_tall1 = InnerProduct(Bra(x / 2), Ket(x / 2))
    ip_tall2 = InnerProduct(Bra(x), Ket(x / 2))
    ip_tall3 = InnerProduct(Bra(x / 2), Ket(x))
    assert str(ip1) == '<psi|psi>'
    assert pretty(ip1) == '<psi|psi>'
    assert upretty(ip1) == u'⟨ψ❘ψ⟩'
    assert latex(
        ip1) == r'\left\langle \psi \right. {\left|\psi\right\rangle }'
    sT(ip1, "InnerProduct(Bra(Symbol('psi')),Ket(Symbol('psi')))")
    assert str(ip2) == '<psi;t|psi;t>'
    assert pretty(ip2) == '<psi;t|psi;t>'
    assert upretty(ip2) == u'⟨ψ;t❘ψ;t⟩'
    assert latex(ip2) == \
        r'\left\langle \psi;t \right. {\left|\psi;t\right\rangle }'
    sT(
        ip2,
        "InnerProduct(TimeDepBra(Symbol('psi'),Symbol('t')),TimeDepKet(Symbol('psi'),Symbol('t')))"
    )
    assert str(ip3) == "<1,1|1,1>"
    assert pretty(ip3) == '<1,1|1,1>'
    assert upretty(ip3) == u'⟨1,1❘1,1⟩'
    assert latex(ip3) == r'\left\langle 1,1 \right. {\left|1,1\right\rangle }'
    sT(
        ip3,
        "InnerProduct(JzBra(Integer(1),Integer(1)),JzKet(Integer(1),Integer(1)))"
    )
    assert str(ip4) == "<1,1,j1=1,j2=1|1,1,j1=1,j2=1>"
    assert pretty(ip4) == '<1,1,j1=1,j2=1|1,1,j1=1,j2=1>'
    assert upretty(ip4) == u'⟨1,1,j₁=1,j₂=1❘1,1,j₁=1,j₂=1⟩'
    assert latex(ip4) == \
        r'\left\langle 1,1,j_{1}=1,j_{2}=1 \right. {\left|1,1,j_{1}=1,j_{2}=1\right\rangle }'
    sT(
        ip4,
        "InnerProduct(JzBraCoupled(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(ip_tall1) == '<x/2|x/2>'
    ascii_str = \
"""\
 / | \\ \n\
/ x|x \\\n\
\\ -|- /\n\
 \\2|2/ \
"""
    ucode_str = \
u"""\
 ╱ │ ╲ \n\
╱ x│x ╲\n\
╲ ─│─ ╱\n\
 ╲2│2╱ \
"""
    assert pretty(ip_tall1) == ascii_str
    assert upretty(ip_tall1) == ucode_str
    assert latex(ip_tall1) == \
        r'\left\langle \frac{1}{2} x \right. {\left|\frac{1}{2} x\right\rangle }'
    sT(
        ip_tall1,
        "InnerProduct(Bra(Mul(Rational(1, 2), Symbol('x'))),Ket(Mul(Rational(1, 2), Symbol('x'))))"
    )
    assert str(ip_tall2) == '<x|x/2>'
    ascii_str = \
"""\
 / | \\ \n\
/  |x \\\n\
\\ x|- /\n\
 \\ |2/ \
"""
    ucode_str = \
u"""\
 ╱ │ ╲ \n\
╱  │x ╲\n\
╲ x│─ ╱\n\
 ╲ │2╱ \
"""
    assert pretty(ip_tall2) == ascii_str
    assert upretty(ip_tall2) == ucode_str
    assert latex(ip_tall2) == \
        r'\left\langle x \right. {\left|\frac{1}{2} x\right\rangle }'
    sT(ip_tall2,
       "InnerProduct(Bra(Symbol('x')),Ket(Mul(Rational(1, 2), Symbol('x'))))")
    assert str(ip_tall3) == '<x/2|x>'
    ascii_str = \
"""\
 / | \\ \n\
/ x|  \\\n\
\\ -|x /\n\
 \\2| / \
"""
    ucode_str = \
u"""\
 ╱ │ ╲ \n\
╱ x│  ╲\n\
╲ ─│x ╱\n\
 ╲2│ ╱ \
"""
    assert pretty(ip_tall3) == ascii_str
    assert upretty(ip_tall3) == ucode_str
    assert latex(ip_tall3) == \
        r'\left\langle \frac{1}{2} x \right. {\left|x\right\rangle }'
    sT(ip_tall3,
       "InnerProduct(Bra(Mul(Rational(1, 2), Symbol('x'))),Ket(Symbol('x')))")