Beispiel #1
0
def test_invalid_spin_basis_ket():
    """Test that trying to instantiate invalid :func:`SpinBasisKet` raises the
    appropriate exceptions"""
    hs1 = SpinSpace('s', spin='3/2')
    hs2 = SpinSpace('s', spin=1)
    with pytest.raises(TypeError) as exc_info:
        SpinBasisKet(1, 2, hs=LocalSpace(1))
    assert "must be a SpinSpace" in str(exc_info.value)
    with pytest.raises(TypeError) as exc_info:
        SpinBasisKet(1, hs=hs1)
    assert "exactly two positional arguments" in str(exc_info.value)
    with pytest.raises(TypeError) as exc_info:
        SpinBasisKet(1, 2, hs=hs2)
    assert "exactly one positional argument" in str(exc_info.value)
    with pytest.raises(ValueError) as exc_info:
        SpinBasisKet(1, 3, hs=hs1)
    assert "must be 2" in str(exc_info.value)
    with pytest.raises(ValueError) as exc_info:
        SpinBasisKet(-5, 2, hs=hs1)
    assert "must be in range" in str(exc_info.value)
    with pytest.raises(ValueError) as exc_info:
        SpinBasisKet(5, 2, hs=hs1)
    assert "must be in range" in str(exc_info.value)
    with pytest.raises(ValueError) as exc_info:
        SpinBasisKet(-5, hs=hs2)
    assert "must be in range" in str(exc_info.value)
    with pytest.raises(ValueError) as exc_info:
        SpinBasisKet(5, hs=hs2)
    assert "must be in range" in str(exc_info.value)
Beispiel #2
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
Beispiel #3
0
def test_tls_invalid_basis():
    """Test that trying to instantiate a TLS with canonical basis labes in the
    wrong order raises a ValueError"""
    with pytest.raises(ValueError) as exc_info:
        SpinSpace('tls', spin='1/2', basis=('up', 'down'))
    assert "Invalid basis" in str(exc_info.value)
    with pytest.raises(ValueError) as exc_info:
        SpinSpace('tls', spin='1/2', basis=('+', '-'))
    assert "Invalid basis" in str(exc_info.value)
Beispiel #4
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)')
Beispiel #5
0
def test_tex_spin_arrows_multi_sigma():
    # when fixed, combine with test_tex_spin_arrows
    tls1 = SpinSpace('1', spin='1/2', basis=("down", "up"))
    tls2 = SpinSpace('2', spin='1/2', basis=("down", "up"))
    tls3 = SpinSpace('3', spin='1/2', basis=("down", "up"))
    sig1 = LocalSigma("up", "down", hs=tls1)
    sig2 = LocalSigma("up", "up", hs=tls2)
    sig3 = LocalSigma("down", "down", hs=tls3)
    assert latex(sig1 * sig2 * sig3) == r''
def operator_exprs():
    """Prepare a list of operator algebra expressions"""
    hs1 = LocalSpace('q1', dimension=2)
    hs2 = LocalSpace('q2', dimension=2)
    A = OperatorSymbol("A", hs=hs1)
    B = OperatorSymbol("B", hs=hs1)
    C = OperatorSymbol("C", hs=hs2)
    a, b = symbols('a, b')
    A_ab = OperatorSymbol("A", a, b, hs=0)
    gamma = symbols('gamma')
    return [
        OperatorSymbol("A", hs=hs1),
        OperatorSymbol("A_1", hs=hs1 * hs2),
        OperatorSymbol("A_1", symbols('alpha'), symbols('beta'), hs=hs1 * hs2),
        A_ab.diff(a, n=2).diff(b),
        A_ab.diff(a, n=2).diff(b).evaluate_at({a: 0}),
        OperatorSymbol("Xi_2", hs=(r'q1', 'q2')),
        OperatorSymbol("Xi_full", hs=1),
        IdentityOperator,
        ZeroOperator,
        Create(hs=1),
        Create(hs=LocalSpace(1, local_identifiers={'Create': 'b'})),
        Destroy(hs=1),
        Destroy(hs=LocalSpace(1, local_identifiers={'Destroy': 'b'})),
        Jz(hs=SpinSpace(1, spin=1)),
        Jz(hs=SpinSpace(1, spin=1, local_identifiers={'Jz': 'Z'})),
        Jplus(hs=SpinSpace(1, spin=1, local_identifiers={'Jplus': 'Jp'})),
        Jminus(hs=SpinSpace(1, spin=1, local_identifiers={'Jminus': 'Jm'})),
        Phase(0.5, hs=1),
        Phase(0.5, hs=LocalSpace(1, local_identifiers={'PhaseCC': 'Ph'})),
        Displace(0.5, hs=1),
        Squeeze(0.5, hs=1),
        LocalSigma('e', 'g', hs=LocalSpace(1, basis=('g', 'e'))),
        LocalSigma('e', 'e', hs=LocalSpace(1, basis=('g', 'e'))),
        A + B,
        A * B,
        A * C,
        2 * A,
        2j * A,
        (1 + 2j) * A,
        gamma**2 * A,
        -(gamma**2) / 2 * A,
        tr(A * C, over_space=hs2),
        Adjoint(A),
        Adjoint(A + B),
        PseudoInverse(A),
        NullSpaceProjector(A),
        A - B,
        2 * A - sqrt(gamma) * (B + C),
        Commutator(A, B),
    ]
Beispiel #7
0
def test_unicode_spin_arrows():
    """Test the representation of spin-1/2 spaces with special labels "down",
    "up" as arrows"""
    tls1 = SpinSpace('1', spin='1/2', basis=("down", "up"))
    tls2 = SpinSpace('2', spin='1/2', basis=("down", "up"))
    tls3 = SpinSpace('3', spin='1/2', basis=("down", "up"))
    down1 = BasisKet('down', hs=tls1)
    up1 = BasisKet('up', hs=tls1)
    down2 = BasisKet('down', hs=tls2)
    up3 = BasisKet('up', hs=tls3)
    assert unicode(down1) == r'|↓⟩⁽¹⁾'
    assert unicode(up1) == r'|↑⟩⁽¹⁾'
    ket = down1 * down2 * up3
    assert unicode(ket) == r'|↓↓↑⟩^(1⊗2⊗3)'
    sig = LocalSigma("up", "down", hs=tls1)
    assert unicode(sig) == r'|↑⟩⟨↓|⁽¹⁾'
Beispiel #8
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'
Beispiel #9
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}'
Beispiel #10
0
def test_spin_pauli_matrices():
    """Test correctness of Pauli matrices on a spin space"""
    hs = SpinSpace("s", spin='1/2', basis=('down', 'up'))
    assert PauliX(hs) == (LocalSigma('down', 'up', hs=hs) +
                          LocalSigma('up', 'down', hs=hs))
    assert PauliX(hs) == PauliX(hs, states=('down', 'up'))
    assert PauliY(hs).expand() == (-I * LocalSigma('down', 'up', hs=hs) +
                                   I * LocalSigma('up', 'down', hs=hs))
    assert PauliY(hs) == PauliY(hs, states=('down', 'up'))
    assert PauliZ(hs) == (LocalProjector('down', hs=hs) -
                          LocalProjector('up', hs=hs))
    assert PauliZ(hs) == PauliZ(hs, states=('down', 'up'))

    hs = SpinSpace("s", spin=1, basis=('-', '0', '+'))
    with pytest.raises(TypeError):
        PauliX(hs, states=(0, 2))
    assert PauliX(hs, states=('-', '+')) == (LocalSigma('-', '+', hs=hs) +
                                             LocalSigma('+', '-', hs=hs))
    assert PauliX(hs) == PauliX(hs, states=('-', '0'))
Beispiel #11
0
def test_tex_spin_arrows():
    """Test the representation of spin-1/2 spaces with special labels "down",
    "up" as arrows"""
    tls1 = SpinSpace('1', spin='1/2', basis=("down", "up"))
    tls2 = SpinSpace('2', spin='1/2', basis=("down", "up"))
    tls3 = SpinSpace('3', spin='1/2', basis=("down", "up"))
    down1 = BasisKet('down', hs=tls1)
    up1 = BasisKet('up', hs=tls1)
    down2 = BasisKet('down', hs=tls2)
    up3 = BasisKet('up', hs=tls3)
    assert latex(down1) == r'\left\lvert \downarrow \right\rangle^{(1)}'
    assert latex(up1) == r'\left\lvert \uparrow \right\rangle^{(1)}'
    ket = down1 * down2 * up3
    assert (
        latex(ket) == r'\left\lvert \downarrow\downarrow\uparrow \right\rangle'
        r'^{(1 \otimes 2 \otimes 3)}')
    sig = LocalSigma("up", "down", hs=tls1)
    assert (latex(sig) == r'\left\lvert \uparrow \middle\rangle\!'
            r'\middle\langle \downarrow \right\rvert^{(1)}')
Beispiel #12
0
def test_spin_basis_ket():
    """Test the properties of BasisKet for the example of a spin system"""
    hs = SpinSpace('s', spin=(3, 2))
    ket_lowest = SpinBasisKet(-3, 2, hs=hs)
    assert ket_lowest.index == 0
    assert ket_lowest.next() == SpinBasisKet(-1, 2, hs=hs)
    assert ket_lowest.next().prev() == ket_lowest
    assert ket_lowest.next(n=2).prev(n=2) == ket_lowest
    assert ket_lowest.prev().is_zero
    assert SpinBasisKet(3, 2, hs=hs).next().is_zero
    assert SpinBasisKet(3, 2, hs=hs).index == 3
Beispiel #13
0
def test_tex_ket_elements():
    """Test the tex representation of "atomic" kets"""
    hs1 = LocalSpace('q1', basis=('g', 'e'))
    hs2 = LocalSpace('q2', basis=('g', 'e'))
    alpha, beta = symbols('alpha, beta')
    psi = KetSymbol('Psi', hs=hs1)
    assert latex(psi) == r'\left\lvert \Psi \right\rangle^{(q_{1})}'
    assert (latex(KetSymbol('Psi', alpha, beta, hs=1)) ==
            r'\left\lvert \Psi\left(\alpha, \beta\right) \right\rangle^{(1)}')
    assert latex(psi, tex_use_braket=True) == r'\Ket{\Psi}^{(q_{1})}'
    assert (latex(psi, tex_use_braket=True,
                  show_hs_label='subscript') == r'\Ket{\Psi}_{(q_{1})}')
    assert (latex(psi, tex_use_braket=True,
                  show_hs_label=False) == r'\Ket{\Psi}')
    assert (latex(KetSymbol('Psi',
                            hs=1)) == r'\left\lvert \Psi \right\rangle^{(1)}')
    assert (latex(KetSymbol(
        'Psi',
        hs=(1, 2))) == r'\left\lvert \Psi \right\rangle^{(1 \otimes 2)}')
    assert (latex(KetSymbol(
        'Psi', hs=hs1 *
        hs2)) == r'\left\lvert \Psi \right\rangle^{(q_{1} \otimes q_{2})}')
    assert (latex(KetSymbol('Psi',
                            hs=1)) == r'\left\lvert \Psi \right\rangle^{(1)}')
    assert latex(ZeroKet) == '0'
    assert latex(TrivialKet) == '1'
    assert (latex(BasisKet(
        'e', hs=hs1)) == r'\left\lvert e \right\rangle^{(q_{1})}')
    hs_tls = LocalSpace('1', basis=('excited', 'ground'))
    assert (latex(BasisKet(
        'excited',
        hs=hs_tls)) == r'\left\lvert \text{excited} \right\rangle^{(1)}')
    assert latex(BasisKet(1, hs=1)) == r'\left\lvert 1 \right\rangle^{(1)}'
    spin = SpinSpace('s', spin=(3, 2))
    assert (latex(SpinBasisKet(
        -3, 2, hs=spin)) == r'\left\lvert -3/2 \right\rangle^{(s)}')
    assert (latex(SpinBasisKet(
        1, 2, hs=spin)) == r'\left\lvert +1/2 \right\rangle^{(s)}')
    assert (latex(SpinBasisKet(-3, 2, hs=spin), tex_frac_for_spin_labels=True)
            == r'\left\lvert -\frac{3}{2} \right\rangle^{(s)}')
    assert (latex(SpinBasisKet(1, 2, hs=spin), tex_frac_for_spin_labels=True)
            == r'\left\lvert +\frac{1}{2} \right\rangle^{(s)}')
    assert (latex(CoherentStateKet(
        2.0, hs=1)) == r'\left\lvert \alpha=2 \right\rangle^{(1)}')
Beispiel #14
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) == 'σ̂ₙ'
def test_invalid_spin_space():
    """Test that a ValueError is raised when trying to instantiate a SpinSpace
    with and invalid `spin` value"""
    with pytest.raises(ValueError) as exc_info:
        SpinSpace(0, spin=0)
    assert "must be greater than zero" in str(exc_info.value)
    with pytest.raises(ValueError) as exc_info:
        SpinSpace(0, spin=-1)
    assert "must be greater than zero" in str(exc_info.value)
    with pytest.raises(ValueError) as exc_info:
        SpinSpace(0, spin=sympy.Rational(2, 3))
    assert "must be an integer or half-integer" in str(exc_info.value)
    with pytest.raises(ValueError) as exc_info:
        SpinSpace(0, spin="one")
    assert "must be an integer or half-integer" in str(exc_info.value)
    with pytest.raises(ValueError) as exc_info:
        SpinSpace(0, spin=IdxSym('i'))
    assert "must be an integer or half-integer" in str(exc_info.value)
    with pytest.raises(TypeError) as exc_info:
        SpinSpace(0)
    assert "required keyword-only argument: 'spin'" in str(exc_info.value)
Beispiel #16
0
def test_tex_operator_elements():
    """Test the tex representation of "atomic" operator algebra elements"""
    hs1 = LocalSpace('q1', dimension=2)
    hs2 = LocalSpace('q2', dimension=2)
    alpha, beta = symbols('alpha, beta')
    fock1 = LocalSpace(1,
                       local_identifiers={
                           'Create': 'b',
                           'Destroy': 'b',
                           'Phase': 'Phi'
                       })
    spin1 = SpinSpace(1,
                      spin=1,
                      local_identifiers={
                          'Jz': 'Z',
                          'Jplus': 'Jp',
                          'Jminus': 'Jm'
                      })
    assert latex(OperatorSymbol("A", hs=hs1)) == r'\hat{A}^{(q_{1})}'
    assert (latex(OperatorSymbol(
        "A_1", hs=hs1 * hs2)) == r'\hat{A}_{1}^{(q_{1} \otimes q_{2})}')
    assert (latex(OperatorSymbol(
        "Xi_2", hs=(r'q1', 'q2'))) == r'\hat{\Xi}_{2}^{(q_{1} \otimes q_{2})}')
    assert (latex(OperatorSymbol("Xi_full",
                                 hs=1)) == r'\hat{\Xi}_{\text{full}}^{(1)}')
    assert latex(
        OperatorSymbol("Xi", alpha, beta,
                       hs=1)) == (r'\hat{\Xi}^{(1)}\left(\alpha, \beta\right)')
    assert latex(IdentityOperator) == r'\mathbb{1}'
    assert latex(IdentityOperator, tex_identity_sym='I') == 'I'
    assert latex(ZeroOperator) == r'\mathbb{0}'
    assert latex(Create(hs=1)) == r'\hat{a}^{(1)\dagger}'
    assert latex(Create(hs=fock1)) == r'\hat{b}^{(1)\dagger}'
    assert latex(Destroy(hs=1)) == r'\hat{a}^{(1)}'
    assert latex(Destroy(hs=fock1)) == r'\hat{b}^{(1)}'
    assert latex(Jz(hs=SpinSpace(1, spin=1))) == r'\hat{J}_{z}^{(1)}'
    assert latex(Jz(hs=spin1)) == r'\hat{Z}^{(1)}'
    assert latex(Jplus(hs=SpinSpace(1, spin=1))) == r'\hat{J}_{+}^{(1)}'
    assert latex(Jplus(hs=spin1)) == r'\text{Jp}^{(1)}'
    assert latex(Jminus(hs=SpinSpace(1, spin=1))) == r'\hat{J}_{-}^{(1)}'
    assert latex(Jminus(hs=spin1)) == r'\text{Jm}^{(1)}'
    assert (latex(Phase(Rational(
        1, 2), hs=1)) == r'\text{Phase}^{(1)}\left(\frac{1}{2}\right)')
    assert latex(Phase(0.5, hs=1)) == r'\text{Phase}^{(1)}\left(0.5\right)'
    assert latex(Phase(0.5, hs=fock1)) == r'\hat{\Phi}^{(1)}\left(0.5\right)'
    assert latex(Displace(0.5, hs=1)) == r'\hat{D}^{(1)}\left(0.5\right)'
    assert latex(Squeeze(0.5, hs=1)) == r'\text{Squeeze}^{(1)}\left(0.5\right)'
    hs_tls = LocalSpace('1', basis=('g', 'e'))
    sig_e_g = LocalSigma('e', 'g', hs=hs_tls)
    assert latex(sig_e_g, sig_as_ketbra=False) == r'\hat{\sigma}_{e,g}^{(1)}'
    assert (
        latex(sig_e_g) ==
        r'\left\lvert e \middle\rangle\!\middle\langle g \right\rvert^{(1)}')
    hs_tls = LocalSpace('1', basis=('excited', 'ground'))
    sig_excited_ground = LocalSigma('excited', 'ground', hs=hs_tls)
    assert (latex(sig_excited_ground, sig_as_ketbra=False) ==
            r'\hat{\sigma}_{\text{excited},\text{ground}}^{(1)}')
    assert (latex(sig_excited_ground) ==
            r'\left\lvert \text{excited} \middle\rangle\!'
            r'\middle\langle \text{ground} \right\rvert^{(1)}')
    hs_tls = LocalSpace('1', basis=('mu', 'nu'))
    sig_mu_nu = LocalSigma('mu', 'nu', hs=hs_tls)
    assert (latex(sig_mu_nu) == r'\left\lvert \mu \middle\rangle\!'
            r'\middle\langle \nu \right\rvert^{(1)}')
    hs_tls = LocalSpace('1', basis=('excited', 'ground'))
    sig_excited_excited = LocalProjector('excited', hs=hs_tls)
    assert (latex(sig_excited_excited,
                  sig_as_ketbra=False) == r'\hat{\Pi}_{\text{excited}}^{(1)}')
    hs_tls = LocalSpace('1', basis=('g', 'e'))
    sig_e_e = LocalProjector('e', hs=hs_tls)
    assert latex(sig_e_e, sig_as_ketbra=False) == r'\hat{\Pi}_{e}^{(1)}'