Example #1
0
def test_differentiation(braket):
    """Test symbolic differentiation of scalars"""
    t = symbols('t', real=True)
    alpha = symbols('alpha')
    expr = ScalarValue(alpha * t**2 / 2 + 2 * t)
    half = sympify(1) / 2
    assert expr.diff(t, 1) == alpha * t + 2
    assert expr.diff(t, 2) == alpha
    assert ScalarValue(2).diff(t, 1) is Zero
    assert ScalarValue(2)._diff(t) is Zero
    assert One.diff(t, 1) is Zero
    assert One._diff(t) is Zero
    assert Zero.diff(t, 1) is Zero
    assert Zero._diff(t) is Zero

    expr = braket * t**2 / 2 + 2 * t
    assert isinstance(expr, Scalar)
    assert expr.diff(t, 1) == braket * t + 2

    expr = sqrt(braket * t)
    assert expr.diff(t, 1) == half * braket * (braket * t)**(-half)
    assert expr.diff(
        t, 2) == -(half * half) * braket**2 * (braket * t)**(-3 * half)

    expr = braket**2
    assert expr.diff(t, 1) is Zero
Example #2
0
def test_sqrt(braket):
    """Test QAlgebra's scalar sqrt"""
    half = sympify(1) / 2
    expr = sqrt(braket)
    assert expr == ScalarPower(braket, ScalarValue(half))

    expr = 1 / sqrt(braket)
    assert expr == ScalarPower(braket, ScalarValue(-half))

    braket_abssq = braket * braket.dag()
    expr = sqrt(braket_abssq)
    assert expr**2 == braket_abssq

    assert sqrt(half) == sympy_sqrt(half)
    assert isinstance(sqrt(half), ScalarValue)
    v = sqrt(ScalarValue(half))
    assert isinstance(v, ScalarValue)
    assert v == sympy_sqrt(half)
    v = sqrt(2)
    assert v == sympy_sqrt(2)
    assert isinstance(v, ScalarValue)
    v = sqrt(0.5)
    assert v == np.sqrt(0.5)
    assert isinstance(v, ScalarValue)
    assert sqrt(-1) == sqrt(-One) == sqrt(-sympify(1)) == ScalarValue(I)
    assert isinstance(sqrt(-1), ScalarValue)
    assert sqrt(One) is One
    assert sqrt(sympify(1)) is One
    assert sqrt(Zero) is Zero
    assert sqrt(sympify(0)) is Zero
    with pytest.raises(TypeError):
        assert sqrt(None) is Zero
Example #3
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
Example #4
0
def test_divide_state_by_sqrt_2():
    """Test that we can instantiate a state divided by √2."""
    ket0 = LocalSpace(0).basis_state(0)
    expr1 = ket0 / sqrt(2)
    expr2 = ket0 / sympy_sqrt(2)
    assert expr1 == expr2
Example #5
0
def test_series_expand(braket):
    """Test expansion of scalar into a series"""
    t = symbols('t', real=True)
    alpha = symbols('alpha')
    three = ScalarValue(3)
    expr = ScalarValue(alpha * t**2 / 2 + 2 * t)

    assert expr.series_expand(t, about=0, order=4) == (
        Zero,
        2,
        alpha / 2,
        Zero,
        Zero,
    )

    assert expr.series_expand(t, about=0, order=1) == (Zero, 2)

    terms = expr.series_expand(t, about=1, order=4)
    for term in terms:
        assert isinstance(term, Scalar)
    expr_from_terms = sum([terms[i] * (t - 1)**i for i in range(1, 5)],
                          terms[0])
    assert expr_from_terms.val.expand() == expr

    assert expr.series_expand(alpha, about=0, order=4) == (
        2 * t,
        t**2 / 2,
        Zero,
        Zero,
        Zero,
    )

    assert expr.series_expand(symbols('x'), about=0, order=4) == (
        expr,
        Zero,
        Zero,
        Zero,
        Zero,
    )

    assert three.series_expand(symbols('x'), 0, 2) == (three, Zero, Zero)
    assert Zero.series_expand(symbols('x'), 0, 2) == (Zero, Zero, Zero)
    assert One.series_expand(symbols('x'), 0, 2) == (One, Zero, Zero)

    expr = One / ScalarValue(t)
    with pytest.raises(ValueError) as exc_info:
        expr.series_expand(t, 0, 2)
    assert "singular" in str(exc_info.value)

    expr = sqrt(ScalarValue(t))
    assert expr.series_expand(t, 1, 2) == (One, One / 2, -One / 8)
    with pytest.raises(ValueError) as exc_info:
        expr.series_expand(t, 0, 2)
    assert "singular" in str(exc_info.value)

    expr = braket.bra
    assert expr.series_expand(t, 0, 2) == (
        braket.bra,
        Bra(ZeroKet),
        Bra(ZeroKet),
    )

    expr = braket
    assert expr.series_expand(t, 0, 2) == (braket, Zero, Zero)

    expr = t * braket
    assert expr.series_expand(t, 0, 2) == (Zero, braket, Zero)

    expr = (1 + t * braket)**2
    assert expr.series_expand(t, 0, 2) == (One, 2 * braket, braket**2)

    expr = (1 + t * braket)**(sympify(1) / 2)
    with pytest.raises(ValueError):
        expr.series_expand(t, 0, 2)