예제 #1
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
예제 #2
0
def test_unicode_ket_operations():
    """Test the unicode representation of ket operations"""
    hs1 = LocalSpace('q_1', basis=('g', 'e'))
    hs2 = LocalSpace('q_2', basis=('g', 'e'))
    ket_g1 = BasisKet('g', hs=hs1)
    ket_e1 = BasisKet('e', hs=hs1)
    ket_g2 = BasisKet('g', hs=hs2)
    ket_e2 = BasisKet('e', hs=hs2)
    psi1 = KetSymbol("Psi_1", hs=hs1)
    psi2 = KetSymbol("Psi_2", hs=hs1)
    phi = KetSymbol("Phi", hs=hs2)
    A = OperatorSymbol("A_0", hs=hs1)
    gamma = symbols('gamma', positive=True)
    alpha = symbols('alpha')
    beta = symbols('beta')
    phase = exp(-I * gamma)
    i = IdxSym('i')
    assert unicode(psi1 + psi2) == '|Ψ₁⟩^(q₁) + |Ψ₂⟩^(q₁)'
    assert unicode(psi1 * phi) == '|Ψ₁⟩^(q₁) ⊗ |Φ⟩^(q₂)'
    assert unicode(phase * psi1) == 'exp(-ⅈ γ) |Ψ₁⟩^(q₁)'
    assert unicode((alpha + 1) * KetSymbol('Psi', hs=0)) == '(α + 1) |Ψ⟩⁽⁰⁾'
    assert (unicode(
        A * psi1) == 'A\u0302_0^(q\u2081) |\u03a8\u2081\u27e9^(q\u2081)')
    #        Â_0^(q₁) |Ψ₁⟩^(q₁)
    assert unicode(BraKet(psi1, psi2)) == '⟨Ψ₁|Ψ₂⟩^(q₁)'
    expr = BraKet(KetSymbol('Psi_1', alpha, hs=hs1),
                  KetSymbol('Psi_2', beta, hs=hs1))
    assert unicode(expr) == '⟨Ψ₁(α)|Ψ₂(β)⟩^(q₁)'
    assert unicode(ket_e1.dag() * ket_e1) == '1'
    assert unicode(ket_g1.dag() * ket_e1) == '0'
    assert unicode(KetBra(psi1, psi2)) == '|Ψ₁⟩⟨Ψ₂|^(q₁)'
    expr = KetBra(KetSymbol('Psi_1', alpha, hs=hs1),
                  KetSymbol('Psi_2', beta, hs=hs1))
    assert unicode(expr) == '|Ψ₁(α)⟩⟨Ψ₂(β)|^(q₁)'
    bell1 = (ket_e1 * ket_g2 - I * ket_g1 * ket_e2) / sqrt(2)
    bell2 = (ket_e1 * ket_e2 - ket_g1 * ket_g2) / sqrt(2)
    assert unicode(bell1) == '1/√2 (|eg⟩^(q₁⊗q₂) - ⅈ |ge⟩^(q₁⊗q₂))'
    assert (unicode(BraKet.create(
        bell1,
        bell2)) == r'1/2 (⟨eg|^(q₁⊗q₂) + ⅈ ⟨ge|^(q₁⊗q₂)) (|ee⟩^(q₁⊗q₂) - '
            r'|gg⟩^(q₁⊗q₂))')
    assert (unicode(KetBra.create(
        bell1,
        bell2)) == r'1/2 (|eg⟩^(q₁⊗q₂) - ⅈ |ge⟩^(q₁⊗q₂))(⟨ee|^(q₁⊗q₂) - '
            r'⟨gg|^(q₁⊗q₂))')
    assert (unicode(
        KetBra.create(bell1, bell2),
        show_hs_label=False) == r'1/2 (|eg⟩ - ⅈ |ge⟩)(⟨ee| - ⟨gg|)')
    expr = KetBra(KetSymbol('Psi', hs=0), BasisKet(FockIndex(i), hs=0))
    assert unicode(expr) == "|Ψ⟩⟨i|⁽⁰⁾"
    expr = KetBra(BasisKet(FockIndex(i), hs=0), KetSymbol('Psi', hs=0))
    assert unicode(expr) == "|i⟩⟨Ψ|⁽⁰⁾"
    expr = BraKet(KetSymbol('Psi', hs=0), BasisKet(FockIndex(i), hs=0))
    assert unicode(expr) == "⟨Ψ|i⟩⁽⁰⁾"
    expr = BraKet(BasisKet(FockIndex(i), hs=0), KetSymbol('Psi', hs=0))
    assert unicode(expr) == "⟨i|Ψ⟩⁽⁰⁾"
def test_operator_kronecker_sum():
    """Test that Kronecker delta are eliminiated from indexed sums over
    operators"""
    i = IdxSym('i')
    j = IdxSym('j')
    alpha = symbols('alpha')
    delta_ij = KroneckerDelta(i, j)
    delta_0i = KroneckerDelta(0, i)
    delta_1j = KroneckerDelta(1, j)
    delta_0j = KroneckerDelta(0, j)
    delta_1i = KroneckerDelta(1, i)

    def A(i, j):
        return OperatorSymbol(StrLabel(IndexedBase('A')[i, j]), hs=0)

    term = delta_ij * A(i, j)
    sum = OperatorIndexedSum.create(term,
                                    ranges=(IndexOverList(i, (1, 2)),
                                            IndexOverList(j, (1, 2))))
    assert sum == OperatorIndexedSum.create(A(i, i),
                                            ranges=(IndexOverList(i,
                                                                  (1, 2)), ))
    assert sum.doit() == (OperatorSymbol("A_11", hs=0) +
                          OperatorSymbol("A_22", hs=0))

    term = alpha * delta_ij * A(i, j)
    range_i = IndexOverList(i, (1, 2))
    range_j = IndexOverList(j, (1, 2))
    sum = OperatorIndexedSum.create(term, ranges=(range_i, range_j))
    assert isinstance(sum, ScalarTimesOperator)
    expected = alpha * OperatorIndexedSum.create(
        A(i, i), ranges=(IndexOverList(i, (1, 2)), ))
    assert sum == expected

    hs = LocalSpace('0', basis=('g', 'e'))
    i_range = IndexOverFockSpace(i, hs)
    j_range = IndexOverFockSpace(j, hs)
    sig_ij = LocalSigma(FockIndex(i), FockIndex(j), hs=hs)
    sig_0j = LocalSigma('g', FockIndex(j), hs=hs)
    sig_i1 = LocalSigma(FockIndex(i), 'e', hs=hs)

    term = delta_0i * delta_1j * sig_ij

    sum = OperatorIndexedSum.create(term, ranges=(i_range, ))
    expected = delta_1j * sig_0j
    assert sum == expected

    sum = OperatorIndexedSum.create(term, ranges=(j_range, ))
    expected = delta_0i * sig_i1
    assert sum == expected

    term = (delta_0i * delta_1j + delta_0j * delta_1i) * sig_ij
    sum = OperatorIndexedSum.create(term, ranges=(i_range, j_range))
    expected = LocalSigma('g', 'e', hs=hs) + LocalSigma('e', 'g', hs=hs)
    assert sum == expected
예제 #4
0
def test_qubit_state_bra():
    """Test  sum_i alpha_i <i| for TLS"""
    i = IdxSym('i')
    alpha = IndexedBase('alpha')
    alpha_i = alpha[i]
    hs_tls = LocalSpace('tls', basis=('g', 'e'))

    term = alpha_i * BasisKet(FockIndex(i), hs=hs_tls).dag()

    expr = KetIndexedSum.create(term, ranges=IndexOverFockSpace(i, hs=hs_tls))

    assert IndexOverFockSpace(i, hs=hs_tls) in expr.ket.kwargs['ranges']

    assert ascii(expr) == "Sum_{i in H_tls} alpha_i * <i|^(tls)"

    assert expr.ket.term.free_symbols == set([i, symbols('alpha'), alpha_i])
    assert expr.free_symbols == set([symbols('alpha'), alpha_i])
    assert expr.ket.variables == [i]
    assert expr.space == hs_tls
    assert len(expr.ket.args) == 1
    assert len(expr.ket.operands) == 1
    assert len(expr.ket.kwargs) == 1
    assert expr.ket.args[0] == term.ket
    assert expr.ket.term == term.ket
    assert len(expr.kwargs) == 0
    expr_expand = Bra.create(expr.ket.doit().substitute({
        alpha[0]: alpha['g'],
        alpha[1]: alpha['e']
    }))
    assert expr_expand == (alpha['g'] * BasisKet('g', hs=hs_tls).dag() +
                           alpha['e'] * BasisKet('e', hs=hs_tls).dag())
    assert ascii(expr_expand) == 'alpha_e * <e|^(tls) + alpha_g * <g|^(tls)'
예제 #5
0
def test_create_on_fock_expansion():
    """Test ``Create * sum_i alpha_i |i> = sqrt(i+1) * alpha_i * |i+1>``"""
    i = IdxSym('i')
    alpha = IndexedBase('alpha')
    hs = LocalSpace('0', dimension=3)

    expr = Create(hs=hs) * KetIndexedSum(
        alpha[i] * BasisKet(FockIndex(i), hs=hs),
        ranges=IndexOverFockSpace(i, hs),
    )

    assert expr == KetIndexedSum(
        sympy.sqrt(i + 1) * alpha[i] * BasisKet(FockIndex(i + 1), hs=hs),
        ranges=IndexOverFockSpace(i, hs),
    )

    assert expr.doit() == (alpha[0] * BasisKet(1, hs=hs) +
                           sympy.sqrt(2) * alpha[1] * BasisKet(2, hs=hs))
예제 #6
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'
예제 #7
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}'
예제 #8
0
def test_braket_indexed_sum():
    """Test braket product of sums"""
    i = IdxSym('i')
    hs = LocalSpace(1, dimension=5)
    alpha = IndexedBase('alpha')

    psi = KetSymbol('Psi', hs=hs)

    psi1 = KetIndexedSum(
        alpha[1, i] * BasisKet(FockIndex(i), hs=hs),
        ranges=IndexOverFockSpace(i, hs),
    )

    psi2 = KetIndexedSum(
        alpha[2, i] * BasisKet(FockIndex(i), hs=hs),
        ranges=IndexOverFockSpace(i, hs),
    )

    expr = Bra.create(psi1) * psi2
    assert expr.space == TrivialSpace
    assert expr == ScalarIndexedSum.create(
        alpha[1, i].conjugate() * alpha[2, i],
        ranges=(IndexOverFockSpace(i, hs), ),
    )
    assert BraKet.create(psi1, psi2) == expr

    expr = psi.dag() * psi2
    assert expr == ScalarIndexedSum(
        alpha[2, i] * BraKet(psi, BasisKet(FockIndex(i), hs=hs)),
        ranges=IndexOverFockSpace(i, hs),
    )
    assert BraKet.create(psi, psi2) == expr

    expr = psi1.dag() * psi
    assert expr == ScalarIndexedSum(
        alpha[1, i].conjugate() * BraKet(BasisKet(FockIndex(i), hs=hs), psi),
        ranges=IndexOverFockSpace(i, hs),
    )
    assert BraKet.create(psi1, psi) == expr
예제 #9
0
def test_tls_norm():
    """Test that calculating the norm of a TLS state results in 1"""
    hs = LocalSpace('tls', dimension=2)
    i = IdxSym('i')

    ket_i = BasisKet(FockIndex(i), hs=hs)
    nrm = BraKet.create(ket_i, ket_i)
    assert nrm == 1

    psi = KetIndexedSum((1 / sympy.sqrt(2)) * ket_i,
                        ranges=IndexOverFockSpace(i, hs))
    nrm = BraKet.create(psi, psi)
    assert nrm == 1
예제 #10
0
def test_ket_indexed_sum_simplify_scalar():
    """Test calling the simplify_scalar method of an KetIndexedSum."""
    # This tests originates from some broken behavior when IndexedSum received
    # `ranges` as a positional argument instead of a keyword argument.
    a, b, ϕ = symbols('a, b, phi')
    factor = (a + b) * sympy.exp(I * ϕ)
    factor_expand = factor.expand()
    hs = LocalSpace(0)
    n = symbols('n', cls=IdxSym)
    psi_n = hs.basis_state(FockIndex(n))
    expr = KetIndexedSum(factor * psi_n,
                         ranges=(IndexOverFockSpace(n, hs=hs), ))
    expr_expand = expr.simplify_scalar(sympy.expand)
    expected = factor_expand * KetIndexedSum(
        psi_n, ranges=(IndexOverFockSpace(n, hs=hs), ))
    assert expr_expand != expected.term  # happened when ranges was an argument
    assert expr_expand == expected
예제 #11
0
def test_ketbra_indexed_sum():
    """Test ketbra product of sums"""
    i = IdxSym('i')
    hs = LocalSpace(1, dimension=5)
    alpha = IndexedBase('alpha')

    psi = KetSymbol('Psi', hs=hs)

    psi1 = KetIndexedSum(
        alpha[1, i] * BasisKet(FockIndex(i), hs=hs),
        ranges=IndexOverFockSpace(i, hs),
    )

    psi2 = KetIndexedSum(
        alpha[2, i] * BasisKet(FockIndex(i), hs=hs),
        ranges=IndexOverFockSpace(i, hs),
    )

    expr = psi1 * psi2.dag()
    assert expr.space == hs
    expected = OperatorIndexedSum(
        alpha[2, i.prime].conjugate() * alpha[1, i] *
        KetBra.create(BasisKet(FockIndex(i), hs=hs),
                      BasisKet(FockIndex(i.prime), hs=hs)),
        ranges=(IndexOverFockSpace(i, hs), IndexOverFockSpace(i.prime, hs)),
    )
    assert expr == expected
    assert KetBra.create(psi1, psi2) == expr

    expr = psi * psi2.dag()
    assert expr.space == hs
    expected = OperatorIndexedSum(
        alpha[2, i].conjugate() *
        KetBra.create(psi, BasisKet(FockIndex(i), hs=hs)),
        ranges=IndexOverFockSpace(i, hs),
    )
    assert expr == expected
    assert KetBra.create(psi, psi2) == expr

    expr = psi1 * psi.dag()
    assert expr.space == hs
    expected = OperatorIndexedSum(
        alpha[1, i] * KetBra.create(BasisKet(FockIndex(i), hs=hs), psi),
        ranges=IndexOverFockSpace(i, hs),
    )
    assert expr == expected
    assert KetBra.create(psi1, psi) == expr
예제 #12
0
def test_tensor_indexed_sum():
    """Test tensor product of sums"""
    i = IdxSym('i')
    hs1 = LocalSpace(1)
    hs2 = LocalSpace(2)
    alpha = IndexedBase('alpha')

    psi1 = KetIndexedSum(
        alpha[1, i] * BasisKet(FockIndex(i), hs=hs1),
        ranges=IndexOverFockSpace(i, hs1),
    )

    psi2 = KetIndexedSum(
        alpha[2, i] * BasisKet(FockIndex(i), hs=hs2),
        ranges=IndexOverFockSpace(i, hs2),
    )

    expr = psi1 * psi2
    assert expr.space == hs1 * hs2
    rhs = KetIndexedSum(
        alpha[1, i] * alpha[2, i.prime] *
        (BasisKet(FockIndex(i), hs=hs1) *
         BasisKet(FockIndex(i.prime), hs=hs2)),
        ranges=(IndexOverFockSpace(i, hs1), IndexOverFockSpace(i.prime, hs2)),
    )
    assert expr == rhs
    psi0 = KetSymbol('Psi', hs=0)
    psi3 = KetSymbol('Psi', hs=3)
    expr2 = psi0 * psi1 * psi2 * psi3
    rhs = KetIndexedSum(
        alpha[1, i] * alpha[2, i.prime] *
        (psi0 * BasisKet(FockIndex(i), hs=hs1) *
         BasisKet(FockIndex(i.prime), hs=hs2) * psi3),
        ranges=(IndexOverFockSpace(i, hs1), IndexOverFockSpace(i.prime, hs2)),
    )
    assert expr2 == rhs
    assert TensorKet.create(psi0, psi1, psi2, psi3) == expr2
예제 #13
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) == 'σ̂ₙ'
예제 #14
0
        {},
        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)
def test_rule(cls, rule, args, kwargs, expected, caplog):
    """Check that for the given `cls` and `rule` name (which must be a key in
    ``cls._rules`` or ``cls._binary_rules``), if we instantiate
    ``cls(*args, **kwargs)``, `rule` is applied and we obtain the `expected`
    result.

    In order to review the log of how all test expressions are created, call
예제 #15
0
def test_ascii_ket_operations():
    """Test the ascii representation of ket operations"""
    hs1 = LocalSpace('q_1', basis=('g', 'e'))
    hs2 = LocalSpace('q_2', basis=('g', 'e'))
    ket_g1 = BasisKet('g', hs=hs1)
    ket_e1 = BasisKet('e', hs=hs1)
    ket_g2 = BasisKet('g', hs=hs2)
    ket_e2 = BasisKet('e', hs=hs2)
    psi1 = KetSymbol("Psi_1", hs=hs1)
    psi2 = KetSymbol("Psi_2", hs=hs1)
    psi2 = KetSymbol("Psi_2", hs=hs1)
    psi3 = KetSymbol("Psi_3", hs=hs1)
    phi = KetSymbol("Phi", hs=hs2)
    A = OperatorSymbol("A_0", hs=hs1)
    gamma = symbols('gamma', positive=True)
    alpha = symbols('alpha')
    beta = symbols('beta')
    phase = exp(-I * gamma)
    i = IdxSym('i')
    assert ascii(psi1 + psi2) == '|Psi_1>^(q_1) + |Psi_2>^(q_1)'
    assert (ascii(psi1 - psi2 +
                  psi3) == '|Psi_1>^(q_1) - |Psi_2>^(q_1) + |Psi_3>^(q_1)')
    with pytest.raises(UnequalSpaces):
        psi1 + phi
    with pytest.raises(AttributeError):
        (psi1 * phi).label
    assert ascii(psi1 * phi) == '|Psi_1>^(q_1) * |Phi>^(q_2)'
    with pytest.raises(OverlappingSpaces):
        psi1 * psi2
    assert ascii(phase * psi1) == 'exp(-I*gamma) * |Psi_1>^(q_1)'
    assert (ascii(
        (alpha + 1) * KetSymbol('Psi', hs=0)) == '(alpha + 1) * |Psi>^(0)')
    assert ascii(A * psi1) == 'A_0^(q_1) |Psi_1>^(q_1)'
    with pytest.raises(SpaceTooLargeError):
        A * phi
    assert ascii(BraKet(psi1, psi2)) == '<Psi_1|Psi_2>^(q_1)'
    expr = BraKet(KetSymbol('Psi_1', alpha, hs=hs1),
                  KetSymbol('Psi_2', beta, hs=hs1))
    assert ascii(expr) == '<Psi_1(alpha)|Psi_2(beta)>^(q_1)'
    assert ascii(psi1.dag() * psi2) == '<Psi_1|Psi_2>^(q_1)'
    assert ascii(ket_e1.dag() * ket_e1) == '1'
    assert ascii(ket_g1.dag() * ket_e1) == '0'
    assert ascii(KetBra(psi1, psi2)) == '|Psi_1><Psi_2|^(q_1)'
    expr = KetBra(KetSymbol('Psi_1', alpha, hs=hs1),
                  KetSymbol('Psi_2', beta, hs=hs1))
    assert ascii(expr) == '|Psi_1(alpha)><Psi_2(beta)|^(q_1)'
    bell1 = (ket_e1 * ket_g2 - I * ket_g1 * ket_e2) / sqrt(2)
    bell2 = (ket_e1 * ket_e2 - ket_g1 * ket_g2) / sqrt(2)
    assert ascii(bell1) == '1/sqrt(2) * (|eg>^(q_1*q_2) - I * |ge>^(q_1*q_2))'
    assert ascii(bell2) == '1/sqrt(2) * (|ee>^(q_1*q_2) - |gg>^(q_1*q_2))'
    expr = BraKet.create(bell1, bell2)
    expected = (
        r'1/2 * (<eg|^(q_1*q_2) + I * <ge|^(q_1*q_2)) * (|ee>^(q_1*q_2) '
        r'- |gg>^(q_1*q_2))')
    assert ascii(expr) == expected
    assert (ascii(KetBra.create(bell1, bell2)) ==
            '1/2 * (|eg>^(q_1*q_2) - I * |ge>^(q_1*q_2))(<ee|^(q_1*q_2) '
            '- <gg|^(q_1*q_2))')
    expr = KetBra(KetSymbol('Psi', hs=0), BasisKet(FockIndex(i), hs=0))
    assert ascii(expr) == "|Psi><i|^(0)"
    expr = KetBra(BasisKet(FockIndex(i), hs=0), KetSymbol('Psi', hs=0))
    assert ascii(expr) == "|i><Psi|^(0)"
    expr = BraKet(KetSymbol('Psi', hs=0), BasisKet(FockIndex(i), hs=0))
    assert ascii(expr) == "<Psi|i>^(0)"
    expr = BraKet(BasisKet(FockIndex(i), hs=0), KetSymbol('Psi', hs=0))
    assert ascii(expr) == "<i|Psi>^(0)"
예제 #16
0
def test_qubit_state():
    """Test  sum_i alpha_i |i> for TLS"""
    i = IdxSym('i')
    alpha = IndexedBase('alpha')
    alpha_i = alpha[i]
    hs_tls = LocalSpace('tls', basis=('g', 'e'))

    term = alpha_i * BasisKet(FockIndex(i), hs=hs_tls)

    expr1 = KetIndexedSum.create(term, ranges=IndexOverFockSpace(i, hs=hs_tls))

    expr2 = KetIndexedSum.create(term, ranges=IndexOverList(i, [0, 1]))

    expr3 = KetIndexedSum.create(term,
                                 ranges=IndexOverRange(i, start_from=0, to=1))

    assert IndexOverFockSpace(i, hs=hs_tls) in expr1.kwargs['ranges']

    assert ascii(expr1) == "Sum_{i in H_tls} alpha_i * |i>^(tls)"
    assert unicode(expr1) == "∑_{i ∈ ℌ_tls} α_i |i⟩⁽ᵗˡˢ⁾"
    assert (
        srepr(expr1) ==
        "KetIndexedSum(ScalarTimesKet(ScalarValue(Indexed(IndexedBase(Symbol('alpha')), IdxSym('i', integer=True))), BasisKet(FockIndex(IdxSym('i', integer=True)), hs=LocalSpace('tls', basis=('g', 'e')))), ranges=(IndexOverFockSpace(IdxSym('i', integer=True), LocalSpace('tls', basis=('g', 'e'))),))"
    )
    with configure_printing(tex_use_braket=True):
        assert (latex(expr1) ==
                r'\sum_{i \in \mathcal{H}_{tls}} \alpha_{i} \Ket{i}^{(tls)}')

    assert ascii(expr2) == 'Sum_{i in {0,1}} alpha_i * |i>^(tls)'
    assert unicode(expr2) == '∑_{i ∈ {0,1}} α_i |i⟩⁽ᵗˡˢ⁾'
    assert (
        srepr(expr2) ==
        "KetIndexedSum(ScalarTimesKet(ScalarValue(Indexed(IndexedBase(Symbol('alpha')), IdxSym('i', integer=True))), BasisKet(FockIndex(IdxSym('i', integer=True)), hs=LocalSpace('tls', basis=('g', 'e')))), ranges=(IndexOverList(IdxSym('i', integer=True), (0, 1)),))"
    )
    with configure_printing(tex_use_braket=True):
        assert (
            latex(expr2) == r'\sum_{i \in \{0,1\}} \alpha_{i} \Ket{i}^{(tls)}')

    assert ascii(expr3) == 'Sum_{i=0}^{1} alpha_i * |i>^(tls)'
    assert unicode(expr3) == '∑_{i=0}^{1} α_i |i⟩⁽ᵗˡˢ⁾'
    assert (
        srepr(expr3) ==
        "KetIndexedSum(ScalarTimesKet(ScalarValue(Indexed(IndexedBase(Symbol('alpha')), IdxSym('i', integer=True))), BasisKet(FockIndex(IdxSym('i', integer=True)), hs=LocalSpace('tls', basis=('g', 'e')))), ranges=(IndexOverRange(IdxSym('i', integer=True), 0, 1),))"
    )
    with configure_printing(tex_use_braket=True):
        assert latex(expr3) == r'\sum_{i=0}^{1} \alpha_{i} \Ket{i}^{(tls)}'

    for expr in (expr1, expr2, expr3):
        assert expr.term.free_symbols == set([i, symbols('alpha'), alpha_i])
        assert expr.term.bound_symbols == set()
        assert expr.free_symbols == set([symbols('alpha'), alpha_i])
        assert expr.variables == [i]
        assert expr.bound_symbols == set([i])
        assert len(expr) == len(expr.ranges[0]) == 2
        assert 0 in expr.ranges[0]
        assert 1 in expr.ranges[0]
        assert expr.space == hs_tls
        assert len(expr.args) == 1
        assert len(expr.kwargs) == 1
        assert len(expr.operands) == 1
        assert expr.args[0] == term
        assert expr.term == term
        expr_expand = expr.doit().substitute({
            alpha[0]: alpha['g'],
            alpha[1]: alpha['e']
        })
        assert expr_expand == (alpha['g'] * BasisKet('g', hs=hs_tls) +
                               alpha['e'] * BasisKet('e', hs=hs_tls))
        assert (
            ascii(expr_expand) == 'alpha_e * |e>^(tls) + alpha_g * |g>^(tls)')

    with pytest.raises(TypeError) as exc_info:
        KetIndexedSum.create(alpha_i * BasisKet(i, hs=hs_tls),
                             IndexOverFockSpace(i, hs=hs_tls))
    assert "label_or_index must be an instance of" in str(exc_info.value)
예제 #17
0
def test_tex_ket_operations():
    """Test the tex representation of ket operations"""
    hs1 = LocalSpace('q_1', basis=('g', 'e'))
    hs2 = LocalSpace('q_2', basis=('g', 'e'))
    ket_g1 = BasisKet('g', hs=hs1)
    ket_e1 = BasisKet('e', hs=hs1)
    ket_g2 = BasisKet('g', hs=hs2)
    ket_e2 = BasisKet('e', hs=hs2)
    psi1 = KetSymbol("Psi_1", hs=hs1)
    psi2 = KetSymbol("Psi_2", hs=hs1)
    psi2 = KetSymbol("Psi_2", hs=hs1)
    psi3 = KetSymbol("Psi_3", hs=hs1)
    phi = KetSymbol("Phi", hs=hs2)
    A = OperatorSymbol("A_0", hs=hs1)
    gamma = symbols('gamma', positive=True)
    alpha = symbols('alpha')
    beta = symbols('beta')
    phase = exp(-I * gamma)
    i = IdxSym('i')
    assert (latex(psi1 +
                  psi2) == r'\left\lvert \Psi_{1} \right\rangle^{(q_{1})} + '
            r'\left\lvert \Psi_{2} \right\rangle^{(q_{1})}')
    assert (latex(psi1 - psi2 +
                  psi3) == r'\left\lvert \Psi_{1} \right\rangle^{(q_{1})} - '
            r'\left\lvert \Psi_{2} \right\rangle^{(q_{1})} + '
            r'\left\lvert \Psi_{3} \right\rangle^{(q_{1})}')
    assert (latex(
        psi1 * phi) == r'\left\lvert \Psi_{1} \right\rangle^{(q_{1})} \otimes '
            r'\left\lvert \Phi \right\rangle^{(q_{2})}')
    assert (latex(phase * psi1) ==
            r'e^{- i \gamma} \left\lvert \Psi_{1} \right\rangle^{(q_{1})}')
    assert (latex((alpha + 1) * KetSymbol('Psi', hs=0)) ==
            r'\left(\alpha + 1\right) \left\lvert \Psi \right\rangle^{(0)}')
    assert (latex(
        A * psi1
    ) == r'\hat{A}_{0}^{(q_{1})} \left\lvert \Psi_{1} \right\rangle^{(q_{1})}')
    braket = BraKet(psi1, psi2)
    assert (
        latex(braket, show_hs_label='subscript') ==
        r'\left\langle \Psi_{1} \middle\vert \Psi_{2} \right\rangle_{(q_{1})}')
    assert (latex(braket, show_hs_label=False) ==
            r'\left\langle \Psi_{1} \middle\vert \Psi_{2} \right\rangle')
    expr = BraKet(KetSymbol('Psi_1', alpha, hs=hs1),
                  KetSymbol('Psi_2', beta, hs=hs1))
    assert (latex(expr) ==
            r'\left\langle \Psi_{1}\left(\alpha\right) \middle\vert '
            r'\Psi_{2}\left(\beta\right) \right\rangle^{(q_{1})}')
    assert (latex(
        ket_e1 *
        ket_e2) == r'\left\lvert ee \right\rangle^{(q_{1} \otimes q_{2})}')
    assert latex(ket_e1.dag() * ket_e1) == r'1'
    assert latex(ket_g1.dag() * ket_e1) == r'0'
    ketbra = KetBra(psi1, psi2)
    assert (latex(ketbra) == r'\left\lvert \Psi_{1} \middle\rangle\!'
            r'\middle\langle \Psi_{2} \right\rvert^{(q_{1})}')
    assert (latex(
        ketbra,
        show_hs_label='subscript') == r'\left\lvert \Psi_{1} \middle\rangle\!'
            r'\middle\langle \Psi_{2} \right\rvert_{(q_{1})}')
    assert (latex(
        ketbra,
        show_hs_label=False) == r'\left\lvert \Psi_{1} \middle\rangle\!'
            r'\middle\langle \Psi_{2} \right\rvert')
    expr = KetBra(KetSymbol('Psi_1', alpha, hs=hs1),
                  KetSymbol('Psi_2', beta, hs=hs1))
    assert (
        latex(expr) ==
        r'\left\lvert \Psi_{1}\left(\alpha\right) \middle\rangle\!'
        r'\middle\langle \Psi_{2}\left(\beta\right) \right\rvert^{(q_{1})}')
    bell1 = (ket_e1 * ket_g2 - I * ket_g1 * ket_e2) / sqrt(2)
    bell2 = (ket_e1 * ket_e2 - ket_g1 * ket_g2) / sqrt(2)
    assert (latex(bell1) ==
            r'\frac{1}{\sqrt{2}} \left(\left\lvert eg \right\rangle^{(q_{1} '
            r'\otimes q_{2})} - i \left\lvert ge \right\rangle'
            r'^{(q_{1} \otimes q_{2})}\right)')
    assert (latex(bell2) ==
            r'\frac{1}{\sqrt{2}} \left(\left\lvert ee \right\rangle^{(q_{1} '
            r'\otimes q_{2})} - \left\lvert gg \right\rangle'
            r'^{(q_{1} \otimes q_{2})}\right)')
    assert (latex(bell2, show_hs_label=False) ==
            r'\frac{1}{\sqrt{2}} \left(\left\lvert ee \right\rangle - '
            r'\left\lvert gg \right\rangle\right)')
    assert BraKet.create(bell1, bell2).expand() == 0
    assert (latex(BraKet.create(
        bell1, bell2)) == r'\frac{1}{2} \left(\left\langle eg \right\rvert'
            r'^{(q_{1} \otimes q_{2})} + i \left\langle ge \right\rvert'
            r'^{(q_{1} \otimes q_{2})}\right) '
            r'\left(\left\lvert ee \right\rangle^{(q_{1} \otimes q_{2})} '
            r'- \left\lvert gg \right\rangle^{(q_{1} \otimes q_{2})}\right)')
    assert (
        latex(KetBra.create(
            bell1, bell2)) == r'\frac{1}{2} \left(\left\lvert eg \right\rangle'
        r'^{(q_{1} \otimes q_{2})} - i \left\lvert ge \right\rangle'
        r'^{(q_{1} \otimes q_{2})}\right)\left(\left\langle ee \right\rvert'
        r'^{(q_{1} \otimes q_{2})} - \left\langle gg \right\rvert'
        r'^{(q_{1} \otimes q_{2})}\right)')
    with configure_printing(tex_use_braket=True):
        expr = KetBra(KetSymbol('Psi', hs=0), BasisKet(FockIndex(i), hs=0))
        assert latex(expr) == r'\Ket{\Psi}\!\Bra{i}^{(0)}'
        expr = KetBra(BasisKet(FockIndex(i), hs=0), KetSymbol('Psi', hs=0))
        assert latex(expr) == r'\Ket{i}\!\Bra{\Psi}^{(0)}'
        expr = BraKet(KetSymbol('Psi', hs=0), BasisKet(FockIndex(i), hs=0))
        assert latex(expr) == r'\Braket{\Psi | i}^(0)'
        expr = BraKet(BasisKet(FockIndex(i), hs=0), KetSymbol('Psi', hs=0))
        assert latex(expr) == r'\Braket{i | \Psi}^(0)'
예제 #18
0
def test_scalar_indexed_sum(braket):
    """Test instantiation and behavior of a ScalarIndexedSum"""
    i = IdxSym('i')
    ip = i.prime
    ipp = ip.prime
    alpha = IndexedBase('alpha')
    a = symbols('a')
    hs = LocalSpace(0)
    ket_sum = KetIndexedSum(
        alpha[1, i] * BasisKet(FockIndex(i), hs=hs),
        ranges=(IndexOverRange(i, 1, 2), ),
    )
    bra = KetSymbol('Psi', hs=hs).dag()
    expr = bra * ket_sum
    half = sympify(1) / 2
    assert isinstance(expr, ScalarIndexedSum)
    assert isinstance(expr.term, ScalarTimes)
    assert expr.term == bra * ket_sum.term
    assert expr.ranges == ket_sum.ranges
    assert expr.doit() == (alpha[1, 1] * bra * BasisKet(1, hs=hs) +
                           alpha[1, 2] * bra * BasisKet(2, hs=hs))

    expr = ScalarIndexedSum.create(i, ranges=(IndexOverRange(i, 1, 2), ))
    assert expr == ScalarIndexedSum(i, ranges=(IndexOverRange(i, 1, 2), ))
    assert isinstance(expr.doit(), ScalarValue)
    assert expr.doit() == 3

    assert expr.real == expr
    assert expr.imag == Zero
    assert expr.conjugate() == expr

    assert 3 * expr == expr * 3 == Sum(i, 1, 2)(3 * i)
    assert a * expr == expr * a == Sum(i, 1, 2)(a * i)
    assert braket * expr == ScalarTimes(braket, Sum(i, 1, 2)(i))
    assert expr * braket == ScalarTimes(braket, Sum(i, 1, 2)(i))
    assert (2 * i) * expr == 2 * expr * i
    assert (2 * i) * expr == Sum(i, 1, 2)(2 * i * i.prime)

    assert expr * expr == ScalarIndexedSum(
        ScalarValue(i * ip),
        ranges=(IndexOverRange(i, 1, 2), IndexOverRange(ip, 1, 2)),
    )

    sum3 = expr**3
    assert sum3 == ScalarIndexedSum(
        ScalarValue(i * ip * ipp),
        ranges=(
            IndexOverRange(i, 1, 2),
            IndexOverRange(ip, 1, 2),
            IndexOverRange(ipp, 1, 2),
        ),
    )

    assert expr**0 is One
    assert expr**1 is expr
    assert (expr**alpha).exp == alpha
    assert expr**-1 == 1 / expr
    assert (1 / expr).exp == -1
    assert (expr**-alpha).exp == -alpha

    sqrt_sum = sqrt(expr)
    assert sqrt_sum == ScalarPower(expr, ScalarValue(half))

    expr = ScalarIndexedSum.create(I * i, ranges=(IndexOverRange(i, 1, 2), ))
    assert expr.real == Zero
    assert expr.imag == ScalarIndexedSum.create(i,
                                                ranges=(IndexOverRange(
                                                    i, 1, 2), ))
    assert expr.conjugate() == -expr
예제 #19
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