예제 #1
0
def test_unicode_equation():
    """Test printing of the Eq class"""
    eq_1 = Eq(lhs=OperatorSymbol('H', hs=0), rhs=Create(hs=0) * Destroy(hs=0))
    # fmt: off
    eq = (eq_1.apply_to_lhs(lambda expr: expr + 1).apply_to_rhs(
        lambda expr: expr + 1).apply_to_rhs(lambda expr: expr**2).tag(
            3).transform(lambda eq: eq + 1).tag(4).apply_to_rhs('expand').
          apply_to_lhs(lambda expr: expr**2).tag(5).apply(
              'expand').apply_to_lhs(lambda expr: expr**2).tag(6).apply_to_lhs(
                  'expand').apply_to_rhs(lambda expr: expr + 1))
    # fmt: on
    assert unicode(eq_1) == 'Ĥ⁽⁰⁾ = â^(0)† â⁽⁰⁾'
    assert unicode(eq_1.tag(1)) == 'Ĥ⁽⁰⁾ = â^(0)† â⁽⁰⁾    (1)'
    unicode_lines = unicode(eq, show_hs_label=False).split("\n")
    expected = [
        '                                     Ĥ = â^† â',
        '                                 𝟙 + Ĥ = â^† â',
        '                                       = 𝟙 + â^† â',
        '                                       = (𝟙 + â^† â) (𝟙 + â^† â)        (3)',
        '                                 2 + Ĥ = 𝟙 + (𝟙 + â^† â) (𝟙 + â^† â)    (4)',
        '                                       = 2 + â^† â^† â â + 3 â^† â',
        '                       (2 + Ĥ) (2 + Ĥ) = 2 + â^† â^† â â + 3 â^† â      (5)',
        '                         4 + 4 Ĥ + Ĥ Ĥ = 2 + â^† â^† â â + 3 â^† â',
        '       (4 + 4 Ĥ + Ĥ Ĥ) (4 + 4 Ĥ + Ĥ Ĥ) = 2 + â^† â^† â â + 3 â^† â      (6)',
        '16 + 32 Ĥ + Ĥ Ĥ Ĥ Ĥ + 8 Ĥ Ĥ Ĥ + 24 Ĥ Ĥ = 2 + â^† â^† â â + 3 â^† â',
        '                                       = 3 + â^† â^† â â + 3 â^† â',
    ]
    for i, line in enumerate(unicode_lines):
        assert line == expected[i]

    eq = Eq(OperatorSymbol('H', hs=0), 0, eq_sym_str='→')
    assert unicode(eq, show_hs_label=False) == 'Ĥ → 0'
예제 #2
0
def test_render_head_repr_tuple_list():
    """Test that render_head_repr works for lists and tuples"""
    a, b = symbols('a, b')
    A = OperatorSymbol('A', hs=0)
    B = OperatorSymbol('B', hs=0)

    expr = ((a, 2), (b, 1))
    assert render_head_repr(expr) == "((Symbol('a'), 2), (Symbol('b'), 1))"

    expr = [[a, 2], [b, 1]]
    assert render_head_repr(expr) == "[[Symbol('a'), 2], [Symbol('b'), 1]]"

    expr = [(a, 2), (b, 1)]
    assert render_head_repr(expr) == "[(Symbol('a'), 2), (Symbol('b'), 1)]"

    expr = ([a, 2], [b, 1])
    assert render_head_repr(expr) == "([Symbol('a'), 2], [Symbol('b'), 1])"

    expr = (A, (b, 1))
    assert (render_head_repr(expr) ==
            "(OperatorSymbol('A', hs=LocalSpace('0')), (Symbol('b'), 1))")

    expr = [(a, 2), (A, B)]
    assert (render_head_repr(expr) ==
            "[(Symbol('a'), 2), (OperatorSymbol('A', hs=LocalSpace('0')), "
            "OperatorSymbol('B', hs=LocalSpace('0')))]")
예제 #3
0
def matrix_expr():
    A = OperatorSymbol("A", hs=1)
    B = OperatorSymbol("B", hs=1)
    C = OperatorSymbol("C", hs=1)
    D = OperatorSymbol("D", hs=1)
    gamma = symbols('gamma')
    phase = exp(-I * gamma / 2)
    return Matrix([[phase * A, B], [C, phase.conjugate() * D]])
예제 #4
0
def test_apply_mtd():
    H_0 = OperatorSymbol('H_0', hs=0)
    H = OperatorSymbol('H', hs=0)
    ω, E0 = sympy.symbols('omega, E_0')
    eq0 = Eq(H_0, ω * Create(hs=0) * Destroy(hs=0) + E0, tag='0')
    eq = eq0.apply('substitute', {H_0: H, E0: 0}).tag('new')
    assert eq.lhs == H
    assert eq.rhs == ω * Create(hs=0) * Destroy(hs=0)
    assert eq._tag == 'new'
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
예제 #6
0
def test_eq_sub_eq():
    ω, E0 = sympy.symbols('omega, E_0')
    H_0 = OperatorSymbol('H_0', hs=0)
    H_1 = OperatorSymbol('H_1', hs=0)
    mu = OperatorSymbol('mu', hs=0)
    eq0 = Eq(H_0, ω * Create(hs=0) * Destroy(hs=0) + E0, tag='0')
    eq1 = Eq(H_1, mu + E0, tag='1')
    eq = eq0 - eq1
    assert eq.lhs == H_0 - H_1
    assert eq.rhs == ω * Create(hs=0) * Destroy(hs=0) - mu
    assert eq._tag is None
예제 #7
0
def test_apply():
    """Test the apply method"""

    A = OperatorSymbol('A', hs=0)

    def raise_to_power(x, y):
        return x**y

    def plus_n(expr, *, n):
        return expr + n

    assert A.apply(raise_to_power, 2).apply(plus_n, n=1) == A**2 + 1
예제 #8
0
def test_unicode_operator_operations():
    """Test the unicode representation of operator algebra operations"""
    hs1 = LocalSpace('q_1', dimension=2)
    hs2 = LocalSpace('q_2', dimension=2)
    A = OperatorSymbol("A", hs=hs1)
    B = OperatorSymbol("B", hs=hs1)
    C = OperatorSymbol("C", hs=hs2)
    psi = KetSymbol('Psi', hs=hs1)
    gamma = symbols('gamma', positive=True)
    assert unicode(A + B) == 'A\u0302^(q\u2081) + B\u0302^(q\u2081)'
    #                         Â^(q₁) + B̂^(q₁)
    assert unicode(A * B) == 'A\u0302^(q\u2081) B\u0302^(q\u2081)'
    #                         Â^(q₁) B̂^(q₁)
    assert unicode(A * C) == 'A\u0302^(q\u2081) C\u0302^(q\u2082)'
    #                         Â^(q₁) Ĉ^(q₂)
    assert unicode(2 * A) == '2 A\u0302^(q\u2081)'  # 2 Â^(q₁)
    assert unicode(2j * A) == '2j A\u0302^(q\u2081)'
    #                          2j Â^(q₁)
    assert unicode((1 + 2j) * A) == '(1+2j) A\u0302^(q\u2081)'
    #                              (1+2j) Â^(q₁)
    assert unicode(gamma**2 * A) == '\u03b3\xb2 A\u0302^(q\u2081)'
    #                                γ² Â^(q₁)
    assert unicode(-(gamma**2) / 2 * A) == '-\u03b3\xb2/2 A\u0302^(q\u2081)'
    #                                   -γ²/2 Â^(q₁)
    assert (unicode(tr(
        A * C,
        over_space=hs2)) == 'tr_(q\u2082)[C\u0302^(q\u2082)] A\u0302^(q\u2081)'
            )
    #       tr_(q₂)[Ĉ^(q₂)] Â^(q₁)
    assert unicode(Adjoint(A)) == 'A\u0302^(q\u2081)\u2020'
    #                             Â^(q₁)†
    assert unicode(Adjoint(Create(hs=1))) == 'a\u0302\u207d\xb9\u207e'
    #              â⁽¹⁾
    assert unicode(PseudoInverse(A)) == '(A\u0302^(q\u2081))^+'
    #              (Â^(q₁))^+
    assert unicode(NullSpaceProjector(A)) == 'P\u0302_Ker(A\u0302^(q\u2081))'
    #                                         P̂_Ker(Â^(q₁))
    assert unicode(A - B) == 'A\u0302^(q\u2081) - B\u0302^(q\u2081)'
    #                         Â^(q₁) - B̂^(q₁)
    assert unicode(2 * A - sqrt(gamma) * (B + C)) in [
        '2 A\u0302^(q\u2081) - \u221a\u03b3 (B\u0302^(q\u2081) '
        '+ C\u0302^(q\u2082))',
        '2 A\u0302^(q\u2081) - sqrt(\u03b3) (B\u0302^(q\u2081) '
        '+ C\u0302^(q\u2082))',
    ]
    #       2 Â^(q₁) - √γ (B̂^(q₁) + Ĉ^(q₂))
    assert (unicode(Commutator(A,
                               B)) == '[A\u0302^(q\u2081), B\u0302^(q\u2081)]')
    #       [Â^(q₁), B̂^(q₁)]
    expr = (Commutator(A, B) * psi).dag()
    assert unicode(expr, show_hs_label=False) == r'⟨Ψ| [Â, B̂]^†'
예제 #9
0
def test_ascii_matrix():
    """Test ascii representation of the Matrix class"""
    A = OperatorSymbol("A", hs=1)
    B = OperatorSymbol("B", hs=1)
    C = OperatorSymbol("C", hs=1)
    D = OperatorSymbol("D", hs=1)
    assert (ascii(Matrix([[A, B], [C,
                                   D]])) == '[[A^(1), B^(1)], [C^(1), D^(1)]]')
    assert (ascii(Matrix([A, B, C,
                          D])) == '[[A^(1)], [B^(1)], [C^(1)], [D^(1)]]')
    assert ascii(Matrix([[A, B, C, D]])) == '[[A^(1), B^(1), C^(1), D^(1)]]'
    assert ascii(Matrix([[0, 1], [-1, 0]])) == '[[0, 1], [-1, 0]]'
    assert ascii(Matrix([[], []])) == '[[], []]'
    assert ascii(Matrix([])) == '[[], []]'
예제 #10
0
def matrix_exprs():
    """Prepare a list of Matrix expressions"""
    A = OperatorSymbol("A", hs=1)
    B = OperatorSymbol("B", hs=1)
    C = OperatorSymbol("C", hs=1)
    D = OperatorSymbol("D", hs=1)
    return [
        Matrix([[A, B], [C, D]]),
        Matrix([A, B, C, D]),
        Matrix([[A, B, C, D]]),
        Matrix([[0, 1], [-1, 0]]),
        # Matrix([[], []]),  # see issue #8316 in numpy
        # Matrix([]),
    ]
예제 #11
0
def test_tex_operator_operations():
    """Test the tex representation of operator algebra operations"""
    hs1 = LocalSpace('q_1', dimension=2)
    hs2 = LocalSpace('q_2', dimension=2)
    A = OperatorSymbol("A", hs=hs1)
    B = OperatorSymbol("B", hs=hs1)
    C = OperatorSymbol("C", hs=hs2)
    psi = KetSymbol('Psi', hs=hs1)
    gamma = symbols('gamma', positive=True)
    assert latex(A.dag()) == r'\hat{A}^{(q_{1})\dagger}'
    assert latex(A + B) == r'\hat{A}^{(q_{1})} + \hat{B}^{(q_{1})}'
    assert latex(A * B) == r'\hat{A}^{(q_{1})} \hat{B}^{(q_{1})}'
    assert latex(A * C) == r'\hat{A}^{(q_{1})} \hat{C}^{(q_{2})}'
    assert latex(2 * A) == r'2 \hat{A}^{(q_{1})}'
    assert latex(2j * A) == r'2i \hat{A}^{(q_{1})}'
    assert latex((1 + 2j) * A) == r'(1+2i) \hat{A}^{(q_{1})}'
    assert latex(gamma**2 * A) == r'\gamma^{2} \hat{A}^{(q_{1})}'
    assert (latex(-(gamma**2) / 2 *
                  A) == r'- \frac{\gamma^{2}}{2} \hat{A}^{(q_{1})}')
    assert (latex(tr(
        A * C,
        over_space=hs2)) == r'{\rm tr}_{q_{2}}\left[\hat{C}^{(q_{2})}\right] '
            r'\hat{A}^{(q_{1})}')
    assert latex(Adjoint(A)) == r'\hat{A}^{(q_{1})\dagger}'
    assert (latex(Adjoint(
        A**2)) == r'\left(\hat{A}^{(q_{1})} \hat{A}^{(q_{1})}\right)^\dagger')
    assert (latex(
        Adjoint(A)**2) == r'\hat{A}^{(q_{1})\dagger} \hat{A}^{(q_{1})\dagger}')
    assert latex(Adjoint(Create(hs=1))) == r'\hat{a}^{(1)}'
    assert (latex(Adjoint(A + B)) ==
            r'\left(\hat{A}^{(q_{1})} + \hat{B}^{(q_{1})}\right)^\dagger')
    assert latex(PseudoInverse(A)) == r'\left(\hat{A}^{(q_{1})}\right)^+'
    assert (latex(
        PseudoInverse(A)**2
    ) == r'\left(\hat{A}^{(q_{1})}\right)^+ \left(\hat{A}^{(q_{1})}\right)^+')
    assert (latex(NullSpaceProjector(A)) ==
            r'\hat{P}_{Ker}\left(\hat{A}^{(q_{1})}\right)')
    assert latex(A - B) == r'\hat{A}^{(q_{1})} - \hat{B}^{(q_{1})}'
    assert (latex(A - B + C) ==
            r'\hat{A}^{(q_{1})} - \hat{B}^{(q_{1})} + \hat{C}^{(q_{2})}')
    assert (latex(2 * A - sqrt(gamma) * (B + C)) ==
            r'2 \hat{A}^{(q_{1})} - \sqrt{\gamma} \left(\hat{B}^{(q_{1})} + '
            r'\hat{C}^{(q_{2})}\right)')
    assert (latex(Commutator(
        A, B)) == r'\left[\hat{A}^{(q_{1})}, \hat{B}^{(q_{1})}\right]')
    expr = (Commutator(A, B) * psi).dag()
    assert (latex(expr, show_hs_label=False) ==
            r'\left\langle \Psi \right\rvert \left[\hat{A}, '
            r'\hat{B}\right]^{\dagger}')
예제 #12
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}'
예제 #13
0
def test_ascii_sop_operations():
    """Test the ascii representation of super operator algebra operations"""
    hs1 = LocalSpace('q_1', dimension=2)
    hs2 = LocalSpace('q_2', dimension=2)
    A = SuperOperatorSymbol("A", hs=hs1)
    B = SuperOperatorSymbol("B", hs=hs1)
    C = SuperOperatorSymbol("C", hs=hs2)
    L = SuperOperatorSymbol("L", hs=1)
    M = SuperOperatorSymbol("M", hs=1)
    A_op = OperatorSymbol("A", hs=1)
    gamma = symbols('gamma', positive=True)
    assert ascii(A + B) == 'A^(q_1) + B^(q_1)'
    assert ascii(A * B) == 'A^(q_1) * B^(q_1)'
    assert ascii(A * C) == 'A^(q_1) * C^(q_2)'
    assert ascii(2 * A) == '2 * A^(q_1)'
    assert ascii(2j * A) == '2j * A^(q_1)'
    assert ascii((1 + 2j) * A) == '(1+2j) * A^(q_1)'
    assert ascii(gamma**2 * A) == 'gamma**2 * A^(q_1)'
    assert ascii(-(gamma**2) / 2 * A) == '-gamma**2/2 * A^(q_1)'
    assert ascii(SuperAdjoint(A)) == 'A^(q_1)H'
    assert ascii(SuperAdjoint(A + B)) == '(A^(q_1) + B^(q_1))^H'
    assert ascii(A - B) == 'A^(q_1) - B^(q_1)'
    assert ascii(A - B + C) == 'A^(q_1) - B^(q_1) + C^(q_2)'
    assert (
        ascii(2 * A - sqrt(gamma) *
              (B + C)) == '2 * A^(q_1) - sqrt(gamma) * (B^(q_1) + C^(q_2))')
    assert ascii(SPre(A_op)) == 'SPre(A^(1))'
    assert ascii(SPost(A_op)) == 'SPost(A^(1))'
    assert ascii(SuperOperatorTimesOperator(L, A_op)) == 'L^(1)[A^(1)]'
    assert (ascii(SuperOperatorTimesOperator(
        L,
        sqrt(gamma) * A_op)) == 'L^(1)[sqrt(gamma) * A^(1)]')
    assert (ascii(SuperOperatorTimesOperator(
        (L + 2 * M), A_op)) == '(L^(1) + 2 * M^(1))[A^(1)]')
예제 #14
0
def test_ascii_bra_operations():
    """Test the ascii representation of bra operations"""
    hs1 = LocalSpace('q_1', dimension=2)
    hs2 = LocalSpace('q_2', dimension=2)
    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)
    bra_psi1 = KetSymbol("Psi_1", hs=hs1).dag()
    bra_psi2 = KetSymbol("Psi_2", hs=hs1).dag()
    bra_psi2 = KetSymbol("Psi_2", hs=hs1).dag()
    bra_psi3 = KetSymbol("Psi_3", hs=hs1).dag()
    bra_phi = KetSymbol("Phi", hs=hs2).dag()
    A = OperatorSymbol("A_0", hs=hs1)
    gamma = symbols('gamma', positive=True)
    phase = exp(-I * gamma)
    assert ascii((psi1 + psi2).dag()) == '<Psi_1|^(q_1) + <Psi_2|^(q_1)'
    assert ascii(bra_psi1 + bra_psi2) == '<Psi_1|^(q_1) + <Psi_2|^(q_1)'
    assert (ascii(
        (psi1 - psi2 +
         psi3).dag()) == '<Psi_1|^(q_1) - <Psi_2|^(q_1) + <Psi_3|^(q_1)')
    assert (ascii(bra_psi1 - bra_psi2 +
                  bra_psi3) == '<Psi_1|^(q_1) - <Psi_2|^(q_1) + <Psi_3|^(q_1)')
    assert ascii((psi1 * phi).dag()) == '<Psi_1|^(q_1) * <Phi|^(q_2)'
    assert ascii(bra_psi1 * bra_phi) == '<Psi_1|^(q_1) * <Phi|^(q_2)'
    assert ascii(Bra(phase * psi1)) == 'exp(I*gamma) * <Psi_1|^(q_1)'
    assert ascii((A * psi1).dag()) == '<Psi_1|^(q_1) A_0^(q_1)H'
예제 #15
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'
예제 #16
0
def test_eq_substitute():
    H_0 = OperatorSymbol('H_0', hs=0)
    ω, E0 = sympy.symbols('omega, E_0')
    eq0 = Eq(H_0, ω * Create(hs=0) * Destroy(hs=0) + E0, tag='0')
    eq1 = eq0.apply('substitute', {E0: 0}).reset()
    eq2 = Eq(H_0, ω * Create(hs=0) * Destroy(hs=0))
    assert eq1 == eq2
예제 #17
0
def test_ascii_equation():
    """Test printing of the Eq class"""
    eq_1 = Eq(lhs=OperatorSymbol('H', hs=0), rhs=Create(hs=0) * Destroy(hs=0))
    # fmt: off
    eq = (eq_1.apply_to_lhs(lambda expr: expr + 1).apply_to_rhs(
        lambda expr: expr + 1).apply_to_rhs(lambda expr: expr**2).tag(
            3).transform(lambda eq: eq + 1).tag(4).apply_to_rhs('expand').
          apply_to_lhs(lambda expr: expr**2).tag(5).apply(
              'expand').apply_to_lhs(lambda expr: expr**2).tag(6).apply_to_lhs(
                  'expand').apply_to_rhs(lambda expr: expr + 1))
    # fmt: on
    assert ascii(eq_1) == 'H^(0) = a^(0)H * a^(0)'
    assert ascii(eq_1.tag(1).reset()) == 'H^(0) = a^(0)H * a^(0)    (1)'
    assert ascii(eq, show_hs_label=False).strip() == (r'''
                                                       H = a^H * a
                                                   1 + H = a^H * a
                                                         = 1 + a^H * a
                                                         = (1 + a^H * a) * (1 + a^H * a)          (3)
                                                   2 + H = 1 + (1 + a^H * a) * (1 + a^H * a)      (4)
                                                         = 2 + a^H * a^H * a * a + 3 * a^H * a
                                       (2 + H) * (2 + H) = 2 + a^H * a^H * a * a + 3 * a^H * a    (5)
                                       4 + 4 * H + H * H = 2 + a^H * a^H * a * a + 3 * a^H * a
               (4 + 4 * H + H * H) * (4 + 4 * H + H * H) = 2 + a^H * a^H * a * a + 3 * a^H * a    (6)
16 + 32 * H + H * H * H * H + 8 * H * H * H + 24 * H * H = 2 + a^H * a^H * a * a + 3 * a^H * a
                                                         = 3 + a^H * a^H * a * a + 3 * a^H * a
    '''.strip())
예제 #18
0
def test_eq_copy():
    H_0 = OperatorSymbol('H_0', hs=0)
    ω, E0 = sympy.symbols('omega, E_0')
    eq0 = Eq(H_0, ω * Create(hs=0) * Destroy(hs=0) + E0, tag='0')
    eq = eq0.copy()
    assert eq == eq0
    assert eq is not eq0
예제 #19
0
def sop_exprs():
    """Prepare a list of super operator algebra expressions"""
    hs1 = LocalSpace('q1', dimension=2)
    hs2 = LocalSpace('q2', dimension=2)
    A = SuperOperatorSymbol("A", hs=hs1)
    B = SuperOperatorSymbol("B", hs=hs1)
    C = SuperOperatorSymbol("C", hs=hs2)
    L = SuperOperatorSymbol("L", hs=1)
    M = SuperOperatorSymbol("M", hs=1)
    A_op = OperatorSymbol("A", hs=1)
    gamma = symbols('gamma')
    return [
        SuperOperatorSymbol("A", hs=hs1),
        SuperOperatorSymbol("A_1", hs=hs1 * hs2),
        SuperOperatorSymbol("A", symbols('alpha'), symbols('beta'), hs=hs1),
        IdentitySuperOperator,
        ZeroSuperOperator,
        A + B,
        A * B,
        A * C,
        2 * A,
        (1 + 2j) * A,
        -(gamma**2) / 2 * A,
        SuperAdjoint(A + B),
        2 * A - sqrt(gamma) * (B + C),
        SPre(A_op),
        SPost(A_op),
        SuperOperatorTimesOperator(L,
                                   sqrt(gamma) * A_op),
        SuperOperatorTimesOperator((L + 2 * M), A_op),
    ]
예제 #20
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
예제 #21
0
def test_matrixis_zero():
    """Test that zero-matrices can be identified"""
    m = Matrix([[0, 0], [0, 0]])
    assert m.is_zero

    m = Matrix([[Zero, Zero], [Zero, Zero]])
    assert m.is_zero

    m = Matrix([[ZeroOperator, ZeroOperator], [ZeroOperator, ZeroOperator]])
    assert m.is_zero

    m = Matrix([[ZeroOperator, 0], [Zero, ZeroOperator]])
    assert m.is_zero

    m = Matrix([[0, 0], [1, 0]])
    assert not m.is_zero

    m = Matrix([[Zero, Zero], [One, Zero]])
    assert not m.is_zero

    A = OperatorSymbol("A", hs=0)
    m = Matrix([[ZeroOperator, ZeroOperator], [A, ZeroOperator]])
    assert not m.is_zero

    m = Matrix([[ZeroOperator, 0], [symbols('alpha'), ZeroOperator]])
    assert not m.is_zero
예제 #22
0
def test_ascii_operator_elements():
    """Test the ascii representation of "atomic" operator algebra elements"""
    hs1 = LocalSpace('q1', dimension=2)
    hs2 = LocalSpace('q2', dimension=2)
    alpha, beta = symbols('alpha, beta')
    assert ascii(OperatorSymbol("A", hs=hs1)) == 'A^(q1)'
    A_1 = OperatorSymbol("A_1", hs=1)
    assert ascii(A_1, show_hs_label='subscript') == 'A_1,(1)'
    assert ascii(OperatorSymbol("A", hs=hs1), show_hs_label=False) == 'A'
    assert ascii(OperatorSymbol("A_1", hs=hs1 * hs2)) == 'A_1^(q1*q2)'
    assert ascii(OperatorSymbol("Xi_2", hs=('q1', 'q2'))) == 'Xi_2^(q1*q2)'
    assert ascii(OperatorSymbol("Xi_full", hs=1)) == 'Xi_full^(1)'
    assert ascii(OperatorSymbol("Xi", alpha, beta,
                                hs=1)) == ('Xi^(1)(alpha, beta)')
    with pytest.raises(ValueError):
        OperatorSymbol(r'\Xi^2', hs='a')
    assert ascii(IdentityOperator) == "1"
    assert ascii(ZeroOperator) == "0"
    assert ascii(Create(hs=1)) == "a^(1)H"
    assert ascii(Create(hs=1), show_hs_label=False) == "a^H"
    assert ascii(Create(hs=1), show_hs_label='subscript') == "a_(1)^H"
    assert ascii(Destroy(hs=1)) == "a^(1)"
    fock1 = LocalSpace(1,
                       local_identifiers={
                           'Create': 'b',
                           'Destroy': 'b',
                           'Phase': 'Ph'
                       })
    spin1 = SpinSpace(1,
                      spin=1,
                      local_identifiers={
                          'Jz': 'Z',
                          'Jplus': 'Jp',
                          'Jminus': 'Jm'
                      })
    assert ascii(Create(hs=fock1)) == "b^(1)H"
    assert ascii(Destroy(hs=fock1)) == "b^(1)"
    assert ascii(Jz(hs=SpinSpace(1, spin=1))) == "J_z^(1)"
    assert ascii(Jz(hs=spin1)) == "Z^(1)"
    assert ascii(Jplus(hs=spin1)) == "Jp^(1)"
    assert ascii(Jminus(hs=spin1)) == "Jm^(1)"
    assert ascii(Phase(0.5, hs=1)) == 'Phase^(1)(0.5)'
    assert ascii(Phase(0.5, hs=fock1)) == 'Ph^(1)(0.5)'
    assert ascii(Displace(0.5, hs=1)) == 'D^(1)(0.5)'
    assert ascii(Squeeze(0.5, hs=1)) == 'Squeeze^(1)(0.5)'
    hs_tls = LocalSpace('1', basis=('g', 'e'))
    sig_e_g = LocalSigma('e', 'g', hs=hs_tls)
    assert ascii(sig_e_g) == '|e><g|^(1)'
    assert ascii(sig_e_g, sig_as_ketbra=False) == 'sigma_e,g^(1)'
    sig_e_e = LocalProjector('e', hs=hs_tls)
    assert ascii(sig_e_e, sig_as_ketbra=False) == 'Pi_e^(1)'
    assert (ascii(BasisKet(0, hs=1) * BasisKet(0, hs=2) *
                  BasisKet(0, hs=3)) == '|0,0,0>^(1*2*3)')
    assert ascii(BasisKet(0, hs=hs1) * BasisKet(0, hs=hs2)) == '|00>^(q1*q2)'
    assert (ascii(
        BasisKet(0, hs=LocalSpace(0, dimension=20)) *
        BasisKet(0, hs=LocalSpace(1, dimension=20))) == '|0,0>^(0*1)')
예제 #23
0
def test_repr_latex():
    H_0 = OperatorSymbol('H_0', hs=0)
    ω, E0 = sympy.symbols('omega, E_0')
    Eq.latex_renderer = staticmethod(latex)
    eq0 = Eq(H_0, ω * Create(hs=0) * Destroy(hs=0) + E0, tag='0')
    repr1 = eq0._repr_latex_()
    repr2 = latex(eq0)
    assert repr1 == repr2
예제 #24
0
def test_eq_div_const():
    H_0 = OperatorSymbol('H_0', hs=0)
    ω, E0 = sympy.symbols('omega, E_0')
    eq0 = Eq(H_0, ω * Create(hs=0) * Destroy(hs=0) + E0, tag='0')
    eq = eq0 / 2
    assert eq.lhs == eq0.lhs / 2
    assert eq.rhs == eq0.rhs / 2
    assert eq._tag is None
예제 #25
0
def test_apply_to_lhs():
    H_0 = OperatorSymbol('H_0', hs=0)
    ω, E0 = sympy.symbols('omega, E_0')
    eq0 = Eq(H_0, ω * Create(hs=0) * Destroy(hs=0) + E0, tag='0')
    eq = eq0.apply_to_lhs(lambda expr: expr + E0).tag('new')
    assert eq.lhs == H_0 + E0
    assert eq.rhs == eq0.rhs
    assert eq._tag == 'new'
예제 #26
0
def test_ascii_operator_operations():
    """Test the ascii representation of operator algebra operations"""
    hs1 = LocalSpace('q_1', dimension=2)
    hs2 = LocalSpace('q_2', dimension=2)
    A = OperatorSymbol("A", hs=hs1)
    B = OperatorSymbol("B", hs=hs1)
    C = OperatorSymbol("C", hs=hs2)
    D = OperatorSymbol("D", hs=hs1)
    psi = KetSymbol('Psi', hs=hs1)
    gamma = symbols('gamma', positive=True)
    assert ascii(A + B) == 'A^(q_1) + B^(q_1)'
    assert ascii(A * B) == 'A^(q_1) * B^(q_1)'
    assert ascii(A * C) == 'A^(q_1) * C^(q_2)'
    assert ascii(A * (B + D)) == 'A^(q_1) * (B^(q_1) + D^(q_1))'
    assert ascii(A * (B - D)) == 'A^(q_1) * (B^(q_1) - D^(q_1))'
    assert (ascii(
        (A + B) *
        (-2 * B - D)) == '(A^(q_1) + B^(q_1)) * (-D^(q_1) - 2 * B^(q_1))')
    assert ascii(OperatorTimes(A, -B)) == 'A^(q_1) * (-B^(q_1))'
    assert ascii(OperatorTimes(A, -B), show_hs_label=False) == 'A * (-B)'
    assert ascii(2 * A) == '2 * A^(q_1)'
    assert ascii(2j * A) == '2j * A^(q_1)'
    assert ascii((1 + 2j) * A) == '(1+2j) * A^(q_1)'
    assert ascii(gamma**2 * A) == 'gamma**2 * A^(q_1)'
    assert ascii(-(gamma**2) / 2 * A) == '-gamma**2/2 * A^(q_1)'
    assert ascii(tr(A * C, over_space=hs2)) == 'tr_(q_2)[C^(q_2)] * A^(q_1)'
    expr = A + OperatorPlusMinusCC(B * D)
    assert ascii(expr, show_hs_label=False) == 'A + (B * D + c.c.)'
    expr = A + OperatorPlusMinusCC(B + D)
    assert ascii(expr, show_hs_label=False) == 'A + (B + D + c.c.)'
    expr = A * OperatorPlusMinusCC(B * D)
    assert ascii(expr, show_hs_label=False) == 'A * (B * D + c.c.)'
    assert ascii(Adjoint(A)) == 'A^(q_1)H'
    assert ascii(Adjoint(Create(hs=1))) == 'a^(1)'
    assert ascii(Adjoint(A + B)) == '(A^(q_1) + B^(q_1))^H'
    assert ascii(PseudoInverse(A)) == '(A^(q_1))^+'
    assert ascii(NullSpaceProjector(A)) == 'P_Ker(A^(q_1))'
    assert ascii(A - B) == 'A^(q_1) - B^(q_1)'
    assert ascii(A - B + C) == 'A^(q_1) - B^(q_1) + C^(q_2)'
    expr = 2 * A - sqrt(gamma) * (B + C)
    assert ascii(expr) == '2 * A^(q_1) - sqrt(gamma) * (B^(q_1) + C^(q_2))'
    assert ascii(Commutator(A, B)) == r'[A^(q_1), B^(q_1)]'
    expr = (Commutator(A, B) * psi).dag()
    assert ascii(expr, show_hs_label=False) == r'<Psi| [A, B]^H'
예제 #27
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|Ψ⟩⁽⁰⁾"
예제 #28
0
def test_indented_srepr_tuple_list():
    """Test that indendented srepr of a list or tuple is same as unindented

    Because these occur as kwargs values, they are always printed unindented.
    """
    a, b = symbols('a, b')
    A = OperatorSymbol('A', hs=0)
    B = OperatorSymbol('B', hs=0)
    exprs = [
        ((a, 2), (b, 1)),
        [[a, 2], [b, 1]],
        [(a, 2), (b, 1)],
        ([a, 2], [b, 1]),
        (A, (b, 1)),
        [(a, 2), (A, B)],
    ]
    for expr in exprs:
        assert (srepr(expr, indented=True) == srepr(expr) ==
                render_head_repr(expr))
예제 #29
0
def test_unicode_matrix():
    """Test unicode representation of the Matrix class"""
    A = OperatorSymbol("A", hs=1)
    B = OperatorSymbol("B", hs=1)
    C = OperatorSymbol("C", hs=1)
    D = OperatorSymbol("D", hs=1)
    assert (unicode(Matrix(
        [[A, B],
         [C, D]])) == '[[A\u0302\u207d\xb9\u207e, B\u0302\u207d\xb9\u207e], '
            '[C\u0302\u207d\xb9\u207e, D\u0302\u207d\xb9\u207e]]')
    #  '[[Â⁽¹⁾, B̂⁽¹⁾], [Ĉ⁽¹⁾, D̂⁽¹⁾]]')
    assert (unicode(Matrix(
        [A, B, C,
         D])) == '[[A\u0302\u207d\xb9\u207e], [B\u0302\u207d\xb9\u207e], '
            '[C\u0302\u207d\xb9\u207e], [D\u0302\u207d\xb9\u207e]]')
    #  '[Â⁽¹⁾], [B̂⁽¹⁾], [Ĉ⁽¹⁾], [D̂⁽¹⁾]]')
    assert unicode(Matrix([[0, 1], [-1, 0]])) == '[[0, 1], [-1, 0]]'
    assert unicode(Matrix([[], []])) == '[[], []]'
    assert unicode(Matrix([])) == '[[], []]'
예제 #30
0
def test_tex_matrix():
    """Test tex representation of the Matrix class"""
    A = OperatorSymbol("A", hs=1)
    B = OperatorSymbol("B", hs=1)
    C = OperatorSymbol("C", hs=1)
    D = OperatorSymbol("D", hs=1)
    assert latex(OperatorSymbol("A", hs=1)) == r'\hat{A}^{(1)}'
    assert (latex(Matrix(
        [[A, B], [C,
                  D]])) == r'\begin{pmatrix}\hat{A}^{(1)} & \hat{B}^{(1)} \\'
            r'\hat{C}^{(1)} & \hat{D}^{(1)}\end{pmatrix}')
    assert (latex(Matrix(
        [A, B, C, D])) == r'\begin{pmatrix}\hat{A}^{(1)} \\\hat{B}^{(1)} \\'
            r'\hat{C}^{(1)} \\\hat{D}^{(1)}\end{pmatrix}')
    assert (latex(Matrix(
        [[A, B, C, D]])) == r'\begin{pmatrix}\hat{A}^{(1)} & \hat{B}^{(1)} & '
            r'\hat{C}^{(1)} & \hat{D}^{(1)}\end{pmatrix}')
    assert (latex(Matrix([[0, 1], [-1, 0]
                          ])) == r'\begin{pmatrix}0 & 1 \\-1 & 0\end{pmatrix}')
    assert latex(Matrix([[], []])) == r'\begin{pmatrix} \\\end{pmatrix}'
    assert latex(Matrix([])) == r'\begin{pmatrix} \\\end{pmatrix}'