def test_product_of_scalars(): f1 = SingleVarFunctionScalar("f", "x") f2 = SingleVarFunctionScalar("f", "y") d = DeltaFunction("x", "y") expr = f1 * f2.conjugate() * d print(expr) assert set(expr) == set([f1, f2.conjugate(), d])
def test_integrate_single_function(): f = SingleVarFunctionScalar("f", "x") d = DeltaFunction("x", "y") expr = f * d res = integrate(expr, "x") print(res) assert res == SingleVarFunctionScalar("f", "y")
def test_integrate_same_function(): fx = SingleVarFunctionScalar("f", "x") fy = SingleVarFunctionScalar("f", "y").conjugate() d = DeltaFunction("x", "y") expr = fx * fy * d res = integrate(expr) print(res) assert res == 1
def test_has_variable(): fx = SingleVarFunctionScalar("f", "x") fy = SingleVarFunctionScalar("f", "y").conjugate() expr = fx * fy res = integrate(expr, "x") print(res) assert not has_variable(res, "x") assert has_variable(res, "y")
def test_integrate_all_vars(): f = SingleVarFunctionScalar("f", "x") g = SingleVarFunctionScalar("g", "y").conjugate() d = DeltaFunction("x", "y") expr = f * g * d res = integrate(expr) print(res) assert res == InnerProductFunction("f", "g")
def test_atoms_product(): a = SingleVarFunctionScalar('a', 'x') b = SingleVarFunctionScalar('b', 'x') prod = 2 * a * b print(prod) atoms = prod.atoms() assert set([2, a, b]) == set(atoms)
def test_atoms_sum(): a = SingleVarFunctionScalar('a', 'x') b = SingleVarFunctionScalar('b', 'x') sm = 2 + a + b print(sm) atoms = sm.atoms() assert set([2, a, b]) == set(atoms)
def test_atoms_nested(): a = SingleVarFunctionScalar('a', 'x') b = SingleVarFunctionScalar('b', 'x') c = SingleVarFunctionScalar('c', 'x') sm = 2 + a + (b * c) print(sm) atoms = sm.atoms() assert set([2, a, b, c]) == set(atoms)
def test_sum_vars(): a = SingleVarFunctionScalar('a', 'x') b = SingleVarFunctionScalar('b', 'x') c = SingleVarFunctionScalar('c', 'y') sm = a + b + c assert get_variables(sm) == set(['x', 'y']) sm = replace_var(sm, 'y', 'x') assert get_variables(sm) == set(['x']) assert not has_variable(sm, 'y')
def test_combine_factors(): a = SingleVarFunctionScalar('a', 'x') b = SingleVarFunctionScalar('b', 'x') c = SingleVarFunctionScalar('c', 'x') prod = a * b * c prod[0] = 2 prod[2] = 5 prod = simplify(prod) assert prod == 10 * b
def test_sum_commutative(): a = SingleVarFunctionScalar('a', 'x') b = SingleVarFunctionScalar('b', 'x') c = SingleVarFunctionScalar('c', 'x') d = SingleVarFunctionScalar('d', 'x') sm1 = a * b + c * d sm2 = d * c + b * a assert sm1 == sm2
def test_iszero_prod(): a = SingleVarFunctionScalar('a', 'x') b = SingleVarFunctionScalar('b', 'x') prod = a * b prod[1] = 1e-17 prod = simplify(prod) assert prod == 0 assert is_zero(prod)
def test_integrate_sum(): f1 = SingleVarFunctionScalar("f", "x") f2 = SingleVarFunctionScalar("g", "x") d = DeltaFunction("x", "y") expr = (f1 + f2) * d res = integrate(expr, "x") print(res) assert isinstance(res, SumOfScalars) assert set(res) == set([ SingleVarFunctionScalar("f", "y"), SingleVarFunctionScalar("g", "y"), ])
def test_simplify(): a = SingleVarFunctionScalar('a', 'x') b = SingleVarFunctionScalar('b', 'x') c = SingleVarFunctionScalar('c', 'x') expr = (a + b) * (c + 0) expr = expr.simplify() print(expr) assert isinstance(expr, SumOfScalars) assert len(expr) == 2 print(expr[0]) assert len(expr[0]) == 2
def test_expand(): a = SingleVarFunctionScalar('a', 'x') b = SingleVarFunctionScalar('b', 'x') c = SingleVarFunctionScalar('c', 'x') d = SingleVarFunctionScalar('d', 'x') expr = (a + b) * (c + d) expr = simplify(expr.expand()) print(expr) assert isinstance(expr, SumOfScalars) assert len(expr) == 4 print(expr[0]) assert len(expr[0]) == 2
def test_sum_scalar(): a = SingleVarFunctionScalar('a', 'x') b = SingleVarFunctionScalar('b', 'x') c = SingleVarFunctionScalar('c', 'x') d = SingleVarFunctionScalar('d', 'x') sm1 = a + b sm2 = c + d tot = sm1 + sm2 assert len(tot) == 4 assert set(tot) == set([a, b, c, d]) tot += 2 assert len(tot) == 5 assert set(tot) == set([2, a, b, c, d])
def test_setitem(): a = SingleVarFunctionScalar('a', 'x') sm = 2 + a sm[1] = 1 print(sm) sm = simplify(sm) assert sm == 3
def get_fock_states(): """Construct the fock states (eq. (42)-(45) in https://arxiv.org/abs/1903.09778)""" bs0 = BaseFockState() bsc = BaseFockState([FockOp("c", "w1")]) bsd = BaseFockState([FockOp("d", "w1")]) phi = SingleVarFunctionScalar("phi", "w1") psi = SingleVarFunctionScalar("psi", "w1") s0 = bs0.to_state() f = 1 / np.sqrt(2) sphi = f * phi * (bsc.to_state() + bsd.to_state()) spsi = f * psi * (bsc.to_state() - bsd.to_state()) bscc = BaseFockState([FockOp("c", "w1"), FockOp("c", "w2")]) bscd = BaseFockState([FockOp("c", "w1"), FockOp("d", "w2")]) bsdc = BaseFockState([FockOp("d", "w1"), FockOp("c", "w2")]) bsdd = BaseFockState([FockOp("d", "w1"), FockOp("d", "w2")]) phipsi = SingleVarFunctionScalar("phi", "w1") * SingleVarFunctionScalar( "psi", "w2") sphipsi = (1 / 2) * phipsi * (bscc.to_state() + bsdc.to_state() - bscd.to_state() - bsdd.to_state()) return s0, sphi, spsi, sphipsi
def test_integrate(): f1 = SingleVarFunctionScalar("f", "x") f2 = SingleVarFunctionScalar("f", "y") d = DeltaFunction("x", "y") expr = f1 * f2.conjugate() * d res = integrate(expr, "x") print(res) assert set(res) == set([ SingleVarFunctionScalar("f", "y", False), SingleVarFunctionScalar("f", "y", True), ])
def test_integrate_equal_terms(): f1 = SingleVarFunctionScalar("f", "x") f2 = SingleVarFunctionScalar("f", "y") f3 = f1 * f2.conjugate() d = DeltaFunction("x", "y") expr = (f3 + f3) * d res = integrate(expr, "x") print(res) assert isinstance(res, ProductOfScalars) assert set(res) == set([ 2, SingleVarFunctionScalar("f", "y", False), SingleVarFunctionScalar("f", "y", True), ])