Exemplo n.º 1
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()])
Exemplo n.º 2
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)')
Exemplo n.º 3
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)')
Exemplo n.º 4
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()}
Exemplo n.º 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()
    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()])
Exemplo n.º 6
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
Exemplo n.º 7
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])
Exemplo n.º 8
0
def test_sympy__physics__quantum__spin__JxKet():
    from sympy.physics.quantum.spin import JxKet
    assert _test_args(JxKet(1, 0))