Exemplo n.º 1
0
def test_tensor_key():
    hs_mech = LocalSpace('m', dimension=5)
    hs_opt = LocalSpace('o', dimension=5)
    hs = hs_mech * hs_opt
    ket00 = hs.basis_state(0)
    ket0m = hs_mech.basis_state(0)
    ket0o = hs_opt.basis_state(0)
    qutip_ket00 = convert_to_qutip(ket00)
    qutip_ket0m = convert_to_qutip(ket0m)
    qutip_ket0o = convert_to_qutip(ket0o)
    expected = qutip.tensor(qutip_ket0m, qutip_ket0o)
    assert (qutip_ket00 - expected).norm() < 1e-15
Exemplo n.º 2
0
def test_hs_basis_states():
    """Test that we can obtain the basis states of a Hilbert space"""
    hs0 = LocalSpace('0')
    hs1 = LocalSpace('1', basis=['g', 'e'])
    hs2 = LocalSpace('2', dimension=2)
    hs3 = LocalSpace('3', dimension=2)
    hs4 = LocalSpace('4', dimension=2)
    spin = SpinSpace('s', spin=Rational(3 / 2))

    assert isinstance(hs0.basis_state(0), BasisKet)
    assert isinstance(hs0.basis_state(1), BasisKet)
    with pytest.raises(BasisNotSetError):
        _ = hs0.basis_state('0')
    with pytest.raises(BasisNotSetError):
        _ = hs0.dimension

    g_1, e_1 = hs1.basis_states
    assert hs1.dimension == 2
    assert hs1.basis_labels == ('g', 'e')
    assert g_1 == BasisKet('g', hs=hs1)
    assert e_1 == BasisKet('e', hs=hs1)
    assert g_1 == hs1.basis_state('g')
    assert g_1 == hs1.basis_state(0)
    assert e_1 == hs1.basis_state(1)
    with pytest.raises(IndexError):
        hs1.basis_state(2)
    with pytest.raises(KeyError):
        hs1.basis_state('r')

    zero_2, one_2 = hs2.basis_states
    assert zero_2 == BasisKet(0, hs=hs2)
    assert one_2 == BasisKet(1, hs=hs2)

    hs_prod = hs1 * hs2
    g0, g1, e0, e1 = list(hs_prod.basis_states)
    assert g0 == g_1 * zero_2
    assert g1 == g_1 * one_2
    assert e0 == e_1 * zero_2
    assert e1 == e_1 * one_2
    assert g0 == hs_prod.basis_state(0)
    assert e1 == hs_prod.basis_state(3)
    assert e1 == hs_prod.basis_state('e,1')
    assert hs_prod.basis_labels == ('g,0', 'g,1', 'e,0', 'e,1')
    with pytest.raises(IndexError):
        hs_prod.basis_state(4)
    with pytest.raises(KeyError):
        hs_prod.basis_state('g0')

    hs_prod4 = hs1 * hs2 * hs3 * hs4
    basis = hs_prod4.basis_states
    assert next(basis) == (
        BasisKet('g', hs=hs1)
        * BasisKet(0, hs=hs2)
        * BasisKet(0, hs=hs3)
        * BasisKet(0, hs=hs4)
    )
    assert next(basis) == (
        BasisKet('g', hs=hs1)
        * BasisKet(0, hs=hs2)
        * BasisKet(0, hs=hs3)
        * BasisKet(1, hs=hs4)
    )
    assert next(basis) == (
        BasisKet('g', hs=hs1)
        * BasisKet(0, hs=hs2)
        * BasisKet(1, hs=hs3)
        * BasisKet(0, hs=hs4)
    )

    assert TrivialSpace.dimension == 1
    assert list(TrivialSpace.basis_states) == [
        TrivialKet,
    ]
    assert TrivialSpace.basis_state(0) == TrivialKet

    basis = spin.basis_states
    ket = next(basis)
    assert ket == BasisKet('-3/2', hs=spin)
    with pytest.raises(TypeError):
        ket == BasisKet(0, hs=spin)
    assert next(basis) == SpinBasisKet(-1, 2, hs=spin)
    assert next(basis) == SpinBasisKet(+1, 2, hs=spin)
    assert next(basis) == SpinBasisKet(+3, 2, hs=spin)

    with pytest.raises(BasisNotSetError):
        FullSpace.dimension
    with pytest.raises(BasisNotSetError):
        FullSpace.basis_states