Пример #1
0
def test_ascii_symbolic_labels():
    """Test ascii representation of symbols with symbolic labels"""
    i = IdxSym('i')
    j = IdxSym('j')
    hs0 = LocalSpace(0)
    hs1 = LocalSpace(1)
    Psi = IndexedBase('Psi')
    assert ascii(BasisKet(FockIndex(2 * i), hs=hs0)) == '|2*i>^(0)'
    assert ascii(KetSymbol(StrLabel(2 * i), hs=hs0)) == '|2*i>^(0)'
    assert (ascii(KetSymbol(StrLabel(Psi[i, j]),
                            hs=hs0 * hs1)) == '|Psi_ij>^(0*1)')
    expr = BasisKet(FockIndex(i), hs=hs0) * BasisKet(FockIndex(j), hs=hs1)
    assert ascii(expr) == '|i,j>^(0*1)'
    assert ascii(Bra(BasisKet(FockIndex(2 * i), hs=hs0))) == '<2*i|^(0)'
    assert (ascii(LocalSigma(FockIndex(i), FockIndex(j),
                             hs=hs0)) == '|i><j|^(0)')
    expr = CoherentStateKet(symbols('alpha'), hs=1).to_fock_representation()
    assert (ascii(expr) == 'exp(-alpha*conjugate(alpha)/2) * '
            '(Sum_{n in H_1} alpha**n/sqrt(n!) * |n>^(1))')

    tls = SpinSpace(label='s', spin='1/2', basis=('down', 'up'))
    Sig = IndexedBase('sigma')
    n = IdxSym('n')
    Sig_n = OperatorSymbol(StrLabel(Sig[n]), hs=tls)
    assert ascii(Sig_n, show_hs_label=False) == 'sigma_n'
Пример #2
0
def test_tex_symbolic_labels():
    """Test tex representation of symbols with symbolic labels"""
    i = IdxSym('i')
    j = IdxSym('j')
    hs0 = LocalSpace(0)
    hs1 = LocalSpace(1)
    Psi = IndexedBase('Psi')
    with configure_printing(tex_use_braket=True):
        assert latex(BasisKet(FockIndex(2 * i), hs=hs0)) == r'\Ket{2 i}^{(0)}'
        assert latex(KetSymbol(StrLabel(2 * i), hs=hs0)) == r'\Ket{2 i}^{(0)}'
        assert (latex(KetSymbol(StrLabel(Psi[i, j]), hs=hs0 *
                                hs1)) == r'\Ket{\Psi_{i j}}^{(0 \otimes 1)}')
        expr = BasisKet(FockIndex(i), hs=hs0) * BasisKet(FockIndex(j), hs=hs1)
        assert latex(expr) == r'\Ket{i,j}^{(0 \otimes 1)}'
        assert (latex(Bra(BasisKet(FockIndex(2 * i),
                                   hs=hs0))) == r'\Bra{2 i}^{(0)}')
        assert (latex(LocalSigma(FockIndex(i), FockIndex(j),
                                 hs=hs0)) == r'\Ket{i}\!\Bra{j}^{(0)}')
        alpha = symbols('alpha')
        expr = CoherentStateKet(alpha, hs=1).to_fock_representation()
        assert (latex(expr) == r'e^{- \frac{\alpha \overline{\alpha}}{2}} '
                r'\left(\sum_{n \in \mathcal{H}_{1}} '
                r'\frac{\alpha^{n}}{\sqrt{n!}} \Ket{n}^{(1)}\right)')
        assert (latex(
            expr, conjg_style='star') == r'e^{- \frac{\alpha {\alpha}^*}{2}} '
                r'\left(\sum_{n \in \mathcal{H}_{1}} '
                r'\frac{\alpha^{n}}{\sqrt{n!}} \Ket{n}^{(1)}\right)')

    tls = SpinSpace(label='s', spin='1/2', basis=('down', 'up'))
    Sig = IndexedBase('sigma')
    n = IdxSym('n')
    Sig_n = OperatorSymbol(StrLabel(Sig[n]), hs=tls)
    assert latex(Sig_n, show_hs_label=False) == r'\hat{\sigma}_{n}'
Пример #3
0
def test_evaluate_symbolic_labels():
    """Test the behavior of the `substitute` method for evaluation of symbolic
    labels"""
    i, j = symbols('i j', cls=IdxSym)
    A = IndexedBase('A')

    lbl = FockIndex(i + j)
    assert lbl.substitute({i: 1, j: 2}) == 3
    assert lbl.substitute({i: 1}) == FockIndex(1 + j)
    assert lbl.substitute({j: 2}) == FockIndex(i + 2)
    assert lbl.substitute({i: 1}).substitute({j: 2}) == 3
    assert lbl.substitute({}) == lbl

    lbl = StrLabel(A[i, j])
    assert lbl.substitute({i: 1, j: 2}) == 'A_12'
    assert lbl.substitute({i: 1}) == StrLabel(A[1, j])
    assert lbl.substitute({j: 2}) == StrLabel(A[i, 2])
    assert lbl.substitute({i: 1}).substitute({j: 2}) == 'A_12'
    assert lbl.substitute({}) == lbl

    hs = SpinSpace('s', spin=3)
    lbl = SpinIndex(i + j, hs)
    assert lbl.substitute({i: 1, j: 2}) == '+3'
    assert lbl.substitute({i: 1}) == SpinIndex(1 + j, hs)
    assert lbl.substitute({j: 2}) == SpinIndex(i + 2, hs)
    assert lbl.substitute({i: 1}).substitute({j: 2}) == '+3'
    assert lbl.substitute({}) == lbl

    hs = SpinSpace('s', spin='3/2')
    lbl = SpinIndex((i + j) / 2, hs=hs)
    assert lbl.substitute({i: 1, j: 2}) == '+3/2'
    assert lbl.substitute({i: 1}) == SpinIndex((1 + j) / 2, hs)
    assert lbl.substitute({j: 2}) == SpinIndex((i + 2) / 2, hs)
    assert lbl.substitute({i: 1}).substitute({j: 2}) == '+3/2'
    assert lbl.substitute({}) == lbl
Пример #4
0
def test_indexed_hs_not_disjoint():
    i, j = symbols('i, j', cls=IdxSym)
    hs_i = LocalSpace(StrLabel(i))
    hs_j = LocalSpace(StrLabel(j))
    assert not hs_i.isdisjoint(hs_i)
    assert not hs_i.isdisjoint(hs_j)
    expr = Create(hs=hs_j) * Destroy(hs=hs_i)
    assert expr.args == (Create(hs=hs_j), Destroy(hs=hs_i))
Пример #5
0
def test_quantum_symbols_with_indexedhs():
    """Test the free_symbols method for objects that have a Hilbert space with a
    sybmolic label, for the example of an OperatorSymbol"""
    i, j = symbols('i, j', cls=IdxSym)
    hs_i = LocalSpace(StrLabel(i))
    hs_j = LocalSpace(StrLabel(j))
    A = OperatorSymbol("A", hs=hs_i * hs_j)
    assert A.free_symbols == {i, j}
    expr = Create(hs=hs_i) * Destroy(hs=hs_i)
    assert expr.free_symbols == {
        i,
    }
Пример #6
0
def test_indexed_sum_over_scalartimes():
    """Test ScalarIndexedSum over a term that is an ScalarTimes instance"""
    i, j = symbols('i, j', cls=IdxSym)
    hs = LocalSpace(1, dimension=2)
    Psi_i = KetSymbol(StrLabel(IndexedBase('Psi')[i]), hs=hs)
    Psi_j = KetSymbol(StrLabel(IndexedBase('Psi')[j]), hs=hs)
    term = KroneckerDelta(i, j) * BraKet(Psi_i, Psi_j)
    assert isinstance(term, ScalarTimes)
    i_range = IndexOverFockSpace(i, hs)
    j_range = IndexOverFockSpace(j, hs)
    sum = ScalarIndexedSum.create(term, ranges=(i_range, j_range))
    assert sum == hs.dimension
Пример #7
0
def test_sum_instantiator():
    """Test use of Sum instantiator."""
    i = IdxSym('i')
    j = IdxSym('j')
    ket_i = BasisKet(FockIndex(i), hs=0)
    ket_j = BasisKet(FockIndex(j), hs=0)
    A_i = OperatorSymbol(StrLabel(IndexedBase('A')[i]), hs=0)
    hs0 = LocalSpace('0')

    sum = Sum(i)(ket_i)
    ful = KetIndexedSum(ket_i, ranges=IndexOverFockSpace(i, hs=hs0))
    assert sum == ful
    assert sum == Sum(i, hs0)(ket_i)
    assert sum == Sum(i, hs=hs0)(ket_i)

    sum = Sum(i, 1, 10)(ket_i)
    ful = KetIndexedSum(ket_i, ranges=IndexOverRange(i, 1, 10))
    assert sum == ful
    assert sum == Sum(i, 1, 10, 1)(ket_i)
    assert sum == Sum(i, 1, to=10, step=1)(ket_i)
    assert sum == Sum(i, 1, 10, step=1)(ket_i)

    sum = Sum(i, (1, 2, 3))(ket_i)
    ful = KetIndexedSum(ket_i, ranges=IndexOverList(i, (1, 2, 3)))
    assert sum == KetIndexedSum(ket_i, ranges=IndexOverList(i, (1, 2, 3)))
    assert sum == Sum(i, [1, 2, 3])(ket_i)

    sum = Sum(i)(Sum(j)(ket_i * ket_j.dag()))
    ful = OperatorIndexedSum(
        ket_i * ket_j.dag(),
        ranges=(IndexOverFockSpace(i, hs0), IndexOverFockSpace(j, hs0)),
    )
    assert sum == ful
Пример #8
0
def test_unicode_hilbert_elements():
    """Test the unicode representation of "atomic" Hilbert space algebra
    elements"""
    assert unicode(LocalSpace(1)) == 'ℌ₁'
    assert unicode(LocalSpace(1, dimension=2)) == 'ℌ₁'
    assert unicode(LocalSpace(1, basis=('g', 'e'))) == 'ℌ₁'
    assert unicode(LocalSpace('local')) == 'ℌ_local'
    assert unicode(LocalSpace('kappa')) == 'ℌ_κ'
    assert unicode(TrivialSpace) == 'ℌ_null'
    assert unicode(FullSpace) == 'ℌ_total'
    assert unicode(LocalSpace(StrLabel(IdxSym('i')))) == 'ℌ_i'
Пример #9
0
def test_tex_hilbert_elements():
    """Test the tex representation of "atomic" Hilbert space algebra
    elements"""
    assert latex(LocalSpace(1)) == r'\mathcal{H}_{1}'
    assert latex(LocalSpace(1, dimension=2)) == r'\mathcal{H}_{1}'
    assert latex(LocalSpace(1, basis=(r'g', 'e'))) == r'\mathcal{H}_{1}'
    assert latex(LocalSpace('local')) == r'\mathcal{H}_{\text{local}}'
    assert latex(LocalSpace('kappa')) == r'\mathcal{H}_{\kappa}'
    assert latex(TrivialSpace) == r'\mathcal{H}_{\text{null}}'
    assert latex(FullSpace) == r'\mathcal{H}_{\text{total}}'
    assert latex(LocalSpace(StrLabel(IdxSym('i')))) == r'\mathcal{H}_{i}'
Пример #10
0
def test_two_hs_symbol_sum():
    """Test sum_{ij} a_{ij} Psi_{ij}"""
    i = IdxSym('i')
    j = IdxSym('j')
    a = IndexedBase('a')
    hs1 = LocalSpace('1', dimension=3)
    hs2 = LocalSpace('2', dimension=3)
    hs = hs1 * hs2
    Psi = IndexedBase('Psi')
    a_ij = a[i, j]
    Psi_ij = Psi[i, j]
    KetPsi_ij = KetSymbol(StrLabel(Psi_ij), hs=hs)
    term = a_ij * KetPsi_ij

    expr1 = KetIndexedSum(
        term,
        ranges=(IndexOverFockSpace(i, hs=hs1), IndexOverFockSpace(j, hs=hs2)),
    )

    expr2 = KetIndexedSum(term,
                          ranges=(IndexOverRange(i, 0,
                                                 2), IndexOverRange(j, 0, 2)))

    assert expr1.term.free_symbols == set(
        [i, j, symbols('a'), symbols('Psi'), a_ij, Psi_ij])
    assert expr1.free_symbols == set(
        [symbols('a'), symbols('Psi'), a_ij, Psi_ij])
    assert expr1.variables == [i, j]

    assert (
        ascii(expr1) == 'Sum_{i in H_1} Sum_{j in H_2} a_ij * |Psi_ij>^(1*2)')
    assert unicode(expr1) == '∑_{i ∈ ℌ₁} ∑_{j ∈ ℌ₂} a_ij |Ψ_ij⟩^(1⊗2)'
    assert (latex(expr1) ==
            r'\sum_{i \in \mathcal{H}_{1}} \sum_{j \in \mathcal{H}_{2}} '
            r'a_{i j} \left\lvert \Psi_{i j} \right\rangle^{(1 \otimes 2)}')

    assert ascii(expr2) == 'Sum_{i,j=0}^{2} a_ij * |Psi_ij>^(1*2)'
    assert unicode(expr2) == '∑_{i,j=0}^{2} a_ij |Ψ_ij⟩^(1⊗2)'
    assert (latex(expr2) == r'\sum_{i,j=0}^{2} a_{i j} '
            r'\left\lvert \Psi_{i j} \right\rangle^{(1 \otimes 2)}')

    assert expr1.doit() == expr2.doit()
    assert expr1.doit() == KetPlus(
        a[0, 0] * KetSymbol('Psi_00', hs=hs),
        a[0, 1] * KetSymbol('Psi_01', hs=hs),
        a[0, 2] * KetSymbol('Psi_02', hs=hs),
        a[1, 0] * KetSymbol('Psi_10', hs=hs),
        a[1, 1] * KetSymbol('Psi_11', hs=hs),
        a[1, 2] * KetSymbol('Psi_12', hs=hs),
        a[2, 0] * KetSymbol('Psi_20', hs=hs),
        a[2, 1] * KetSymbol('Psi_21', hs=hs),
        a[2, 2] * KetSymbol('Psi_22', hs=hs),
    )
Пример #11
0
def test_ascii_hilbert_elements():
    """Test the ascii representation of "atomic" Hilbert space algebra
    elements"""
    assert ascii(LocalSpace(1)) == 'H_1'
    assert ascii(LocalSpace(1, dimension=2)) == 'H_1'
    assert ascii(LocalSpace(1, basis=('g', 'e'))) == 'H_1'
    assert ascii(LocalSpace('local')) == 'H_local'
    assert ascii(LocalSpace('kappa')) == 'H_kappa'
    with pytest.raises(ValueError):
        LocalSpace(r'\kappa')
    assert ascii(TrivialSpace) == 'H_null'
    assert ascii(FullSpace) == 'H_total'
    assert ascii(LocalSpace(StrLabel(IdxSym('i')))) == 'H_i'
Пример #12
0
def test_unicode_symbolic_labels():
    """Test unicode representation of symbols with symbolic labels"""
    i = IdxSym('i')
    j = IdxSym('j')
    hs0 = LocalSpace(0)
    hs1 = LocalSpace(1)
    Psi = IndexedBase('Psi')
    assert unicode(BasisKet(FockIndex(2 * i), hs=hs0)) == '|2 i⟩⁽⁰⁾'
    assert unicode(KetSymbol(StrLabel(2 * i), hs=hs0)) == '|2 i⟩⁽⁰⁾'
    assert (unicode(KetSymbol(StrLabel(Psi[i, j]),
                              hs=hs0 * hs1)) == '|Ψ_ij⟩^(0⊗1)')
    expr = BasisKet(FockIndex(i), hs=hs0) * BasisKet(FockIndex(j), hs=hs1)
    assert unicode(expr) == '|i,j⟩^(0⊗1)'
    assert unicode(Bra(BasisKet(FockIndex(2 * i), hs=hs0))) == '⟨2 i|⁽⁰⁾'
    assert (unicode(LocalSigma(FockIndex(i), FockIndex(j),
                               hs=hs0)) == '|i⟩⟨j|⁽⁰⁾')
    expr = CoherentStateKet(symbols('alpha'), hs=1).to_fock_representation()
    assert unicode(expr) == 'exp(-α α ⃰/2) (∑_{n ∈ ℌ₁} αⁿ/√n! |n⟩⁽¹⁾)'

    tls = SpinSpace(label='s', spin='1/2', basis=('down', 'up'))
    Sig = IndexedBase('sigma')
    n = IdxSym('n')
    Sig_n = OperatorSymbol(StrLabel(Sig[n]), hs=tls)
    assert unicode(Sig_n, show_hs_label=False) == 'σ̂ₙ'
Пример #13
0
def test_partial_expansion():
    """Test partially executing the sum (only for a subset of summation
    indices)"""
    i = IdxSym('i')
    j = IdxSym('j')
    k = IdxSym('k')
    hs = LocalSpace('0', dimension=2)
    Psi = IndexedBase('Psi')

    def r(index_symbol):
        return IndexOverFockSpace(index_symbol, hs=hs)

    psi_ijk = KetSymbol(StrLabel(Psi[i, j, k]), hs=hs)

    def psi(i_val, j_val, k_val):
        return psi_ijk.substitute({i: i_val, j: j_val, k: k_val})

    expr = KetIndexedSum(psi_ijk, ranges=(r(i), r(j), r(k)))

    expr_expanded = expr.doit(indices=[i])
    assert expr_expanded == KetIndexedSum(psi(0, j, k) + psi(1, j, k),
                                          ranges=(r(j), r(k)))

    expr_expanded = expr.doit(indices=[j])
    assert expr_expanded == KetIndexedSum(psi(i, 0, k) + psi(i, 1, k),
                                          ranges=(r(i), r(k)))

    assert expr.doit(indices=[j]) == expr.doit(indices=['j'])

    expr_expanded = expr.doit(indices=[i, j])
    assert expr_expanded == KetIndexedSum(psi(0, 0, k) + psi(1, 0, k) +
                                          psi(0, 1, k) + psi(1, 1, k),
                                          ranges=r(k))

    assert expr.doit(indices=[i, j]) == expr.doit(indices=[j, i])

    assert expr.doit(indices=[i, j, k]) == expr.doit()

    with pytest.raises(ValueError):
        expr.doit(indices=[i], max_terms=10)
Пример #14
0
        {},
        OperatorPlus(-OpA, -OpB, -OpC),
    ),
    (
        ScalarTimesOperator,
        'R004',
        (2 * gamma, Half * OpA),
        {},
        ScalarTimesOperator(gamma, OpA),
    ),
    # State Algebra
    # ...
    (
        KetIndexedSum,
        'R001',
        (KetSymbol(StrLabel(i), hs=0) - KetSymbol(StrLabel(i), hs=0), ),
        dict(ranges=(IndexOverFockSpace(i, hs=LocalSpace(0)), )),
        ZeroKet,
    ),
    (
        KetIndexedSum,
        'R002',
        (symbols('a') * BasisKet(FockIndex(i), hs=0), ),
        dict(ranges=(IndexOverRange(i, 0, 1), )),
        symbols('a') * KetIndexedSum(BasisKet(FockIndex(i), hs=0),
                                     ranges=(IndexOverRange(i, 0, 1), )),
    ),
]


@pytest.mark.parametrize("cls, rule, args, kwargs, expected", TESTS)
Пример #15
0
 def A(i, j):
     return OperatorSymbol(StrLabel(IndexedBase('A')[i, j]), hs=0)
Пример #16
0
 def Psi(*args):
     return KetSymbol(StrLabel(Indexed(IndexedBase('Psi'), *args)), hs=0)
Пример #17
0
 def A(i):
     if isinstance(i, IdxSym):
         return OperatorSymbol(StrLabel(Indexed('A', i)), hs=0)
     else:
         return OperatorSymbol("A_%s" % i, hs=0)