예제 #1
0
    def basis_state(self, index_or_label):
        """Return the basis state with the given index or label.

        Raises:
            .BasisNotSetError: if the Hilbert space has no defined basis
            IndexError: if there is no basis state with the given index
            KeyError: if there is not basis state with the given label
        """
        from qalgebra.core.state_algebra import BasisKet, TensorKet

        if isinstance(index_or_label, int):  # index
            ls_bases = [ls.basis_labels for ls in self.local_factors]
            label_tuple = list(cartesian_product(*ls_bases))[index_or_label]
            try:
                return TensorKet(*[
                    BasisKet(label, hs=ls)
                    for (ls, label) in zip(self.local_factors, label_tuple)
                ])
            except ValueError as exc_info:
                raise IndexError(str(exc_info))
        else:  # label
            local_labels = index_or_label.split(",")
            if len(local_labels) != len(self.local_factors):
                raise KeyError(
                    "label %s for Hilbert space %s must be comma-separated "
                    "concatenation of local labels" % (index_or_label, self))
            try:
                return TensorKet(*[
                    BasisKet(label, hs=ls)
                    for (ls, label) in zip(self.local_factors, local_labels)
                ])
            except ValueError as exc_info:
                raise KeyError(str(exc_info))
예제 #2
0
def disjunct_commutative_test_data():
    A1 = OperatorSymbol("A", hs=1)
    B1 = OperatorSymbol("B", hs=1)
    C1 = OperatorSymbol("C", hs=1)
    A2 = OperatorSymbol("A", hs=2)
    B2 = OperatorSymbol("B", hs=2)
    A3 = OperatorSymbol("A", hs=3)
    B4 = OperatorSymbol("B", hs=4)
    tr_A1 = tr(A1, over_space=1)
    tr_A2 = tr(A2, over_space=2)
    A1_m = OperatorSymbol("A", hs=LocalSpace(1, order_index=2))
    B1_m = OperatorSymbol("B", hs=LocalSpace(1, order_index=2))
    B2_m = OperatorSymbol("B", hs=LocalSpace(2, order_index=1))
    ket_0 = BasisKet(0, hs=1)
    ket_1 = BasisKet(1, hs=1)
    ketbra = KetBra(ket_0, ket_1)
    braket = BraKet(ket_1, ket_1)
    # fmt: off
    return [
        ([B2, B1, A1], [B1, A1, B2]),
        ([B2_m, B1_m, A1_m], [B2_m, B1_m, A1_m]),
        ([B1_m, A1_m, B2_m], [B2_m, B1_m, A1_m]),
        ([B1, A2, C1, tr_A2], [tr_A2, B1, C1, A2]),
        ([A1, B1 + B2], [A1, B1 + B2]),
        ([B1 + B2, A1], [B1 + B2, A1]),
        ([A3 + B4, A1 + A2], [A1 + A2, A3 + B4]),
        ([A1 + A2, A3 + B4], [A1 + A2, A3 + B4]),
        ([B4 + A3, A2 + A1], [A1 + A2, A3 + B4]),
        ([tr_A2, tr_A1], [tr_A1, tr_A2]),
        ([A2, ketbra, A1], [ketbra, A1, A2]),
        ([A2, braket, A1], [braket, A1, A2]),
    ]
예제 #3
0
def full_commutative_test_data():
    A1 = OperatorSymbol("A", hs=1)
    B1 = OperatorSymbol("B", hs=1)
    C1 = OperatorSymbol("C", hs=1)
    A2 = OperatorSymbol("A", hs=2)
    B2 = OperatorSymbol("B", hs=2)
    A3 = OperatorSymbol("A", hs=3)
    B4 = OperatorSymbol("B", hs=3)
    B4 = OperatorSymbol("B", hs=4)
    tr_A1 = tr(A1, over_space=1)
    tr_A2 = tr(A2, over_space=2)
    A1_m = OperatorSymbol("A", hs=LocalSpace(1, order_index=2))
    B1_m = OperatorSymbol("B", hs=LocalSpace(1, order_index=2))
    B2_m = OperatorSymbol("B", hs=LocalSpace(2, order_index=1))
    ket_0 = BasisKet(0, hs=1)
    ket_1 = BasisKet(1, hs=1)
    ketbra = KetBra(ket_0, ket_1)
    braket = BraKet(ket_1, ket_1)
    a = sympy.symbols('a')
    Ph = lambda phi: Phase(phi, hs=1)
    Ph2 = lambda phi: Phase(phi, hs=2)
    D = lambda alpha: Displace(alpha, hs=1)
    # fmt: off
    return [
        ([B2, B1, A1], [A1, B1, B2]),
        ([B2_m, B1_m, A1_m], [B2_m, A1_m, B1_m]),
        ([B1_m, A1_m, B2_m], [B2_m, A1_m, B1_m]),
        ([B1, A2, C1, tr_A2], [tr_A2, B1, C1, A2]),
        ([A1, B1 + B2], [A1, B1 + B2]),
        ([B1 + B2, A1], [A1, B1 + B2]),
        ([A3 + B4, A1 + A2], [A1 + A2, A3 + B4]),
        ([A1 + A2, A3 + B4], [A1 + A2, A3 + B4]),
        ([B4 + A3, A2 + A1], [A1 + A2, A3 + B4]),
        ([tr_A2, tr_A1], [tr_A1, tr_A2]),
        ([A2, ketbra, A1], [ketbra, A1, A2]),
        ([A2, braket, A1], [braket, A1, A2]),
        ([A2, 0.5 * A1, 2 * A1, A1, a * A1,
          -3 * A1], [0.5 * A1, A1, 2 * A1, -3 * A1, a * A1, A2]),
        ([Ph(1), Ph(0.5), D(2), D(0.1)], [D(0.1), D(2),
                                          Ph(0.5),
                                          Ph(1)]),
        ([Ph(1), Ph2(1), Ph(0.5)], [Ph(0.5), Ph(1), Ph2(1)]),
        ([Ph(a), Ph(1)], [Ph(1), Ph(a)]),
    ]
예제 #4
0
    def basis_states(self):
        """Yield an iterator over the states (:class:`.BasisKet` instances)
        that form the canonical basis of the Hilbert space

        Raises:
            BasisNotSetError: if the Hilbert space has no defined basis
        """
        from qalgebra.core.state_algebra import BasisKet  # avoid circ. import

        for label in self.basis_labels:
            yield BasisKet(label, hs=self)
예제 #5
0
    def basis_states(self):
        """Yield an iterator over the states (:class:`.TensorKet` instances)
        that form the canonical basis of the Hilbert space

        Raises:
            .BasisNotSetError: if the Hilbert space has no defined basis
        """
        # fmt: off
        from qalgebra.core.state_algebra import BasisKet, TensorKet  # isort:skip
        # importing locally avoids circular import
        # fmt: on
        ls_bases = [ls.basis_labels for ls in self.local_factors]
        for label_tuple in cartesian_product(*ls_bases):
            yield TensorKet(*[
                BasisKet(label, hs=ls)
                for (ls, label) in zip(self.local_factors, label_tuple)
            ])
예제 #6
0
    def basis_state(self, index_or_label):
        """Return the basis state with the given index or label.

        Raises:
            BasisNotSetError: if the Hilbert space has no defined basis
            IndexError: if there is no basis state with the given index
            KeyError: if there is not basis state with the given label
        """
        from qalgebra.core.state_algebra import BasisKet  # avoid circ. import

        try:
            return BasisKet(index_or_label, hs=self)
        except BasisNotSetError:
            raise
        except ValueError as exc_info:
            if isinstance(index_or_label, int):
                raise IndexError(str(exc_info))
            else:
                raise KeyError(str(exc_info))
예제 #7
0
def test_hs_basis_states():
    """Test that we can obtain the basis states of a Hilbert space"""
    hs0 = LocalSpace('0')
    hs1 = LocalSpace('1', basis=['g', 'e'])
    hs2 = LocalSpace('2', dimension=2)
    hs3 = LocalSpace('3', dimension=2)
    hs4 = LocalSpace('4', dimension=2)
    spin = SpinSpace('s', spin=Rational(3 / 2))

    assert isinstance(hs0.basis_state(0), BasisKet)
    assert isinstance(hs0.basis_state(1), BasisKet)
    with pytest.raises(BasisNotSetError):
        _ = hs0.basis_state('0')
    with pytest.raises(BasisNotSetError):
        _ = hs0.dimension

    g_1, e_1 = hs1.basis_states
    assert hs1.dimension == 2
    assert hs1.basis_labels == ('g', 'e')
    assert g_1 == BasisKet('g', hs=hs1)
    assert e_1 == BasisKet('e', hs=hs1)
    assert g_1 == hs1.basis_state('g')
    assert g_1 == hs1.basis_state(0)
    assert e_1 == hs1.basis_state(1)
    with pytest.raises(IndexError):
        hs1.basis_state(2)
    with pytest.raises(KeyError):
        hs1.basis_state('r')

    zero_2, one_2 = hs2.basis_states
    assert zero_2 == BasisKet(0, hs=hs2)
    assert one_2 == BasisKet(1, hs=hs2)

    hs_prod = hs1 * hs2
    g0, g1, e0, e1 = list(hs_prod.basis_states)
    assert g0 == g_1 * zero_2
    assert g1 == g_1 * one_2
    assert e0 == e_1 * zero_2
    assert e1 == e_1 * one_2
    assert g0 == hs_prod.basis_state(0)
    assert e1 == hs_prod.basis_state(3)
    assert e1 == hs_prod.basis_state('e,1')
    assert hs_prod.basis_labels == ('g,0', 'g,1', 'e,0', 'e,1')
    with pytest.raises(IndexError):
        hs_prod.basis_state(4)
    with pytest.raises(KeyError):
        hs_prod.basis_state('g0')

    hs_prod4 = hs1 * hs2 * hs3 * hs4
    basis = hs_prod4.basis_states
    assert next(basis) == (
        BasisKet('g', hs=hs1)
        * BasisKet(0, hs=hs2)
        * BasisKet(0, hs=hs3)
        * BasisKet(0, hs=hs4)
    )
    assert next(basis) == (
        BasisKet('g', hs=hs1)
        * BasisKet(0, hs=hs2)
        * BasisKet(0, hs=hs3)
        * BasisKet(1, hs=hs4)
    )
    assert next(basis) == (
        BasisKet('g', hs=hs1)
        * BasisKet(0, hs=hs2)
        * BasisKet(1, hs=hs3)
        * BasisKet(0, hs=hs4)
    )

    assert TrivialSpace.dimension == 1
    assert list(TrivialSpace.basis_states) == [
        TrivialKet,
    ]
    assert TrivialSpace.basis_state(0) == TrivialKet

    basis = spin.basis_states
    ket = next(basis)
    assert ket == BasisKet('-3/2', hs=spin)
    with pytest.raises(TypeError):
        ket == BasisKet(0, hs=spin)
    assert next(basis) == SpinBasisKet(-1, 2, hs=spin)
    assert next(basis) == SpinBasisKet(+1, 2, hs=spin)
    assert next(basis) == SpinBasisKet(+3, 2, hs=spin)

    with pytest.raises(BasisNotSetError):
        FullSpace.dimension
    with pytest.raises(BasisNotSetError):
        FullSpace.basis_states
예제 #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)
예제 #9
0
def test_operator_times_order():
    psi0_1 = BasisKet(0, hs=1)
    psi1_1 = BasisKet(1, hs=1)
    psi1_2 = BasisKet(1, hs=2)
    psi1_1_m = BasisKet(1, hs=LocalSpace(1, order_index=2))
    psi1_2_m = BasisKet(1, hs=LocalSpace(2, order_index=1))
    bra0_1 = psi0_1.adjoint()
    bra1_1 = psi1_1.adjoint()
    bra1_2 = psi1_2.adjoint()
    bra1_1_m = psi1_1_m.adjoint()
    bra1_2_m = psi1_2_m.adjoint()

    assert psi1_1 + psi0_1 == psi0_1 + psi1_1
    assert (psi1_1 + psi0_1).operands == (psi0_1, psi1_1)
    assert psi1_1 * psi1_2 == psi1_2 * psi1_1
    assert (psi1_1 * psi1_2).operands == (psi1_1, psi1_2)
    assert (psi1_1_m * psi1_2_m).operands == (psi1_2_m, psi1_1_m)
    assert bra1_1 + bra0_1 == bra0_1 + bra1_1
    assert (bra1_1 + bra0_1).ket.operands == (psi0_1, psi1_1)
    assert bra1_1 * bra1_2 == bra1_2 * bra1_1
    assert (bra1_1 * bra1_2).ket.operands == (psi1_1, psi1_2)
    assert (bra1_1_m * bra1_2_m).ket.operands == (psi1_2_m, psi1_1_m)