示例#1
0
def test_scalar_conjugate(braket):
    """Test taking the complex conjugate (adjoint) of a scalar"""
    Psi = KetSymbol("Psi", hs=0)
    Phi = KetSymbol("Phi", hs=0)
    phi = symbols('phi', real=True)
    alpha = symbols('alpha')

    expr = ScalarValue(1 + 1j)
    assert expr.adjoint() == expr.conjugate() == 1 - 1j

    assert braket.adjoint() == BraKet.create(Phi, Psi)

    expr = 1j + braket
    assert expr.adjoint() == expr.conjugate() == braket.adjoint() - 1j

    expr = (1 + 1j) * braket
    assert expr.adjoint() == expr.conjugate() == (1 - 1j) * braket.adjoint()

    expr = braket**(I * phi)
    assert expr.conjugate() == braket.adjoint()**(-I * phi)

    expr = braket**alpha
    assert expr.conjugate() == braket.adjoint()**(alpha.conjugate())
示例#2
0
def test_scalar_real_imag(braket):
    """Test taking the real and imaginary part of a scalar"""
    alpha = symbols('alpha')
    a, b = symbols('a, b', real=True)
    braket_dag = braket.adjoint()

    expr = ScalarValue(1 + 1j)
    assert (expr.real, expr.imag) == (1, 1)

    expr = ScalarValue(a + I * b)
    assert (expr.real, expr.imag) == (a, b)

    expr = ScalarValue(alpha)
    assert (expr.real, expr.imag) == expr.as_real_imag()
    assert (expr.real, expr.imag) == alpha.as_real_imag()

    expr = Zero
    assert (expr.real, expr.imag) == (Zero, Zero)

    expr = One
    assert (expr.real, expr.imag) == (One, Zero)

    assert braket.real == (braket + braket_dag) / 2
    assert braket.imag == (I / 2) * (braket_dag - braket)

    expr = braket + One + I
    assert expr.real.expand().simplify_scalar() == 1 + braket.real.expand()
    assert expr.imag.expand().simplify_scalar() == 1 + braket.imag.expand()

    expr = I * braket
    assert expr.real.expand() == (-I / 2) * braket_dag + (I / 2) * braket
    assert expr.imag.expand() == braket / 2 + braket_dag / 2

    expr = braket**alpha
    assert expr.real == (expr.adjoint() + expr) / 2
    assert expr.imag == (I / 2) * (expr.adjoint() - expr)