Esempio n. 1
0
def test_inifile_do_not_mix(datadir):
    with pytest.raises(TypeError) as exc_info:
        init_printing(
            str_format='ascii',
            repr_format='ascii',
            inifile=join(datadir, 'printing.ini'),
        )
        assert ("The `inifile` argument cannot be combined with any other "
                "keyword arguments" in str(exc_info.value))
Esempio n. 2
0
    def _repr_latex_(self):
        """TeX representation for IPython notebook.

        Generates the TeX representation and surround it with dollar signs.
        """
        # This method will be replaced by init_printing()
        from qalgebra.printing import init_printing

        init_printing()
        return self._repr_latex_()
Esempio n. 3
0
def test_custom_repr():
    A = OperatorSymbol('A', hs=1)
    assert repr(A) in ['Â⁽¹⁾', 'A^(1)']
    init_printing(repr_format='srepr', reset=True)
    assert repr(A) == "OperatorSymbol('A', hs=LocalSpace('1'))"
    init_printing(reset=True)
    assert repr(A) in ['Â⁽¹⁾', 'A^(1)']
    with configure_printing(repr_format='srepr'):
        assert repr(A) == "OperatorSymbol('A', hs=LocalSpace('1'))"
    assert repr(A) in ['Â⁽¹⁾', 'A^(1)']
Esempio n. 4
0
def test_exception_teardown():
    """Test that teardown works when breaking out due to an exception"""

    class ConfigurePrintingException(Exception):
        pass

    init_printing(show_hs_label=True, repr_format='ascii')
    try:
        with configure_printing(show_hs_label=False, repr_format='srepr'):
            raise ConfigurePrintingException
    except ConfigurePrintingException:
        A = OperatorSymbol('A', hs=1)
        assert repr(A) == 'A^(1)'
    finally:
        # Even if this failed we don't want to make a mess for other tests
        init_printing(reset=True)
Esempio n. 5
0
    def __str__(self):
        # This method will be replaced by init_printing()
        from qalgebra.printing import init_printing

        init_printing()
        return str(self)
Esempio n. 6
0
def test_custom_options():
    """Test giving options to print routines or using configure_printing"""
    A = OperatorSymbol('A', hs=1)
    CNOT = OperatorSymbol('CNOT', hs=1)
    sig = LocalSigma(0, 1, hs=1)
    ket = CoherentStateKet(symbols('alpha'), hs=1)

    assert ascii(A) == r'A^(1)'
    assert ascii(A, show_hs_label=False) == 'A'
    with pytest.raises(TypeError) as exc_info:
        ascii(A, some_bogus_option=False)
    assert "not a valid setting" in str(exc_info.value)
    assert ascii(sig) == r'|0><1|^(1)'
    assert ascii(ket) == r'|alpha=alpha>^(1)'
    assert unicode(A) == r'Â⁽¹⁾'
    assert unicode(sig) == r'|0⟩⟨1|⁽¹⁾'
    assert unicode(ket) == r'|α=α⟩⁽¹⁾'
    assert latex(A) == r'\hat{A}^{(1)}'
    assert (
        latex(sig)
        == r'\left\lvert 0 \middle\rangle\!\middle\langle 1 \right\rvert^{(1)}'
    )
    assert latex(ket) == r'\left\lvert \alpha=\alpha \right\rangle^{(1)}'

    with configure_printing(
        unicode_op_hats=False, tex_op_macro=r'\Op{{{name}}}'
    ):
        assert unicode(A) == r'A⁽¹⁾'
        assert latex(A) == r'\Op{A}^{(1)}'

    with configure_printing(show_hs_label=False):
        assert ascii(A) == r'A'
        assert ascii(sig) == r'|0><1|'
        assert ascii(ket) == r'|alpha=alpha>'
        assert unicode(A) == r'Â'
        assert unicode(sig) == r'|0⟩⟨1|'
        assert unicode(ket) == r'|α=α⟩'
        assert latex(A) == r'\hat{A}'
        assert latex(A, show_hs_label=True) == r'\hat{A}^{(1)}'
        assert latex(A) == r'\hat{A}'
        assert (
            latex(sig)
            == r'\left\lvert 0 \middle\rangle\!\middle\langle 1 \right\rvert'
        )
        assert latex(ket) == r'\left\lvert \alpha=\alpha \right\rangle'

    assert latex(CNOT) == r'\text{CNOT}^{(1)}'
    with configure_printing(tex_textop_macro=r'\Op{{{name}}}'):
        assert latex(CNOT) == r'\Op{CNOT}^{(1)}'

    init_printing(show_hs_label=False)
    assert unicode(A) == r'Â'
    assert latex(A) == r'\hat{A}'
    with configure_printing(
        unicode_op_hats=False, tex_op_macro=r'\Op{{{name}}}'
    ):
        assert unicode(A) == r'A'
        assert latex(A) == r'\Op{A}'
        with configure_printing(tex_op_macro=r'\op{{{name}}}'):
            assert unicode(A) == r'A'
            assert latex(A) == r'\op{A}'
            with configure_printing(tex_use_braket=True):
                assert latex(sig) == r'\Ket{0}\!\Bra{1}'
    assert unicode(A) == r'Â'
    assert latex(A) == r'\hat{A}'
    init_printing(reset=True)

    assert ascii(A) == r'A^(1)'
    assert ascii(sig) == r'|0><1|^(1)'
    assert ascii(ket) == r'|alpha=alpha>^(1)'
    assert unicode(A) == r'Â⁽¹⁾'
    assert unicode(sig) == r'|0⟩⟨1|⁽¹⁾'
    assert unicode(ket) == r'|α=α⟩⁽¹⁾'
    assert latex(A) == r'\hat{A}^{(1)}'
    assert (
        latex(sig)
        == r'\left\lvert 0 \middle\rangle\!\middle\langle 1 \right\rvert^{(1)}'
    )
    assert latex(ket) == r'\left\lvert \alpha=\alpha \right\rangle^{(1)}'
Esempio n. 7
0
def test_invalid_section(datadir):
    with pytest.raises(ValueError) as exc_info:
        init_printing(inifile=join(datadir, 'invalid_section.ini'))
    assert "Invalid section sympy" in str(exc_info.value)
    init_printing(reset=True)
Esempio n. 8
0
def test_initfile(datadir):
    psi = (BasisKet(0, hs=1) + BasisKet(1, hs=1)) / sqrt(2)
    x = symbols('x')
    sig = LocalSigma(0, 1, hs=1)

    init_printing(str_format='unicode', repr_format='unicode')

    str(psi) == unicode(psi)
    repr(psi) == unicode(psi)
    assert isinstance(ascii.printer, QalgebraAsciiPrinter)
    assert isinstance(ascii.printer._sympy_printer, SympyStrPrinter)
    assert isinstance(unicode.printer, QalgebraUnicodePrinter)
    assert ascii(psi) == '1/sqrt(2) * (|0>^(1) + |1>^(1))'
    assert unicode(psi) == '1/√2 (|0⟩⁽¹⁾ + |1⟩⁽¹⁾)'
    assert (latex(psi) ==
            r'\frac{1}{\sqrt{2}} \left(\left\lvert 0 \right\rangle^{(1)} + '
            r'\left\lvert 1 \right\rangle^{(1)}\right)')
    assert (
        latex(atan(x) * sig) ==
        r'\operatorname{atan}{\left(x \right)} \left\lvert 0 \middle\rangle\!\middle\langle 1 \right\rvert^{(1)}'
    )

    with configure_printing(inifile=join(datadir, 'printing.ini')):
        assert (Printer._global_settings['val1'] ==
                '1 # inline comments are not allowd')
        assert (Printer._global_settings['val2'] ==
                '1 ; with either prefix character')
        assert 'show_hs_label' in Printer._global_settings
        assert 'sig_as_ketbra' in Printer._global_settings
        assert 'use_unicode' in Printer._global_settings
        assert len(Printer._global_settings) == 5
        str(psi) == ascii(psi)
        repr(psi) == unicode(psi)
        assert isinstance(ascii.printer, QalgebraAsciiTestPrinter)
        assert isinstance(ascii.printer._sympy_printer, StrPrinter)
        assert isinstance(unicode.printer, QalgebraUnicodePrinter)
        assert ascii(psi) == 'sqrt(2)/2 * (|0>^(1) + |1>^(1))'
        assert unicode(psi) == '1/√2 (|0⟩₍₁₎ + |1⟩₍₁₎)'
        assert (
            latex(psi) == r'\frac{1}{\sqrt{2}} \left(\Ket{0} + \Ket{1}\right)')
        assert (latex(atan(x) *
                      sig) == r'\arctan{\left(x \right)} \Op{\sigma}_{0,1}')

    assert 'use_unicode' in Printer._global_settings
    assert len(Printer._global_settings) == 1

    str(psi) == unicode(psi)
    repr(psi) == unicode(psi)
    assert isinstance(ascii.printer, QalgebraAsciiPrinter)
    assert isinstance(ascii.printer._sympy_printer, SympyStrPrinter)
    assert isinstance(unicode.printer, QalgebraUnicodePrinter)
    assert ascii(psi) == '1/sqrt(2) * (|0>^(1) + |1>^(1))'
    assert unicode(psi) == '1/√2 (|0⟩⁽¹⁾ + |1⟩⁽¹⁾)'
    assert (latex(psi) ==
            r'\frac{1}{\sqrt{2}} \left(\left\lvert 0 \right\rangle^{(1)} + '
            r'\left\lvert 1 \right\rangle^{(1)}\right)')
    assert (latex(
        atan(x) *
        sig) == r'\operatorname{atan}{\left(x \right)} \left\lvert 0 '
            r'\middle\rangle\!\middle\langle 1 \right\rvert^{(1)}')

    init_printing(reset=True)
Esempio n. 9
0
def test_invalid_options(datadir):
    with pytest.raises(TypeError) as exc_info:
        init_printing(inifile=join(datadir, 'invalid_value.ini'))
    assert ("some_bogus_setting is not a valid setting for either "
            "QalgebraAsciiTestPrinter or StrPrinter" in str(exc_info.value))
    init_printing(reset=True)