def test_commutation(): n, m = symbols("n,m", above_fermi=True) c = Commutator(B(0), Bd(0)) assert c == 1 c = Commutator(Bd(0), B(0)) assert c == -1 c = Commutator(B(n), Bd(0)) assert c == KroneckerDelta(n, 0) c = Commutator(B(0), Bd(0)) e = simplify(apply_operators(c * BKet([n]))) assert e == BKet([n]) c = Commutator(B(0), B(1)) e = simplify(apply_operators(c * BKet([n, m]))) assert e == 0 c = Commutator(F(m), Fd(m)) assert c == +1 - 2 * NO(Fd(m) * F(m)) c = Commutator(Fd(m), F(m)) assert c.expand() == -1 + 2 * NO(Fd(m) * F(m)) C = Commutator X, Y, Z = symbols('X,Y,Z', commutative=False) assert C(C(X, Y), Z) != 0 assert C(C(X, Z), Y) != 0 assert C(Y, C(X, Z)) != 0 i, j, k, l = symbols('i,j,k,l', below_fermi=True) a, b, c, d = symbols('a,b,c,d', above_fermi=True) p, q, r, s = symbols('p,q,r,s') D = KroneckerDelta assert C(Fd(a), F(i)) == -2 * NO(F(i) * Fd(a)) assert C(Fd(j), NO(Fd(a) * F(i))).doit(wicks=True) == -D(j, i) * Fd(a) assert C(Fd(a) * F(i), Fd(b) * F(j)).doit(wicks=True) == 0
def test_create(): i, j, n, m = var('i j n m') o = Bd(i) assert isinstance(o, CreateBoson) o = o.subs(i, j) assert o.atoms(Symbol) == set([j]) o = Bd(0) assert o.apply_operator(Ket([n])) == sqrt(n + 1) * Ket([n + 1]) o = Bd(n) assert o.apply_operator(Ket([n])) == o * Ket([n])
def test_create_b(): i, j, n, m = symbols("i,j,n,m") o = Bd(i) assert isinstance(o, CreateBoson) o = o.subs(i, j) assert o.atoms(Symbol) == {j} o = Bd(0) assert o.apply_operator(BKet([n])) == sqrt(n + 1) * BKet([n + 1]) o = Bd(n) assert o.apply_operator(BKet([n])) == o * BKet([n])
def test_create(): i, j, n, m = symbols('i,j,n,m') o = Bd(i) assert latex(o) == "b^\\dagger_{i}" assert isinstance(o, CreateBoson) o = o.subs(i, j) assert o.atoms(Symbol) == {j} o = Bd(0) assert o.apply_operator(BKet([n])) == sqrt(n + 1)*BKet([n + 1]) o = Bd(n) assert o.apply_operator(BKet([n])) == o*BKet([n])
def test_commutation(): n, m = symbols("n,m", above_fermi=True) c = Commutator(B(0), Bd(0)) assert c == 1 c = Commutator(Bd(0), B(0)) assert c == -1 c = Commutator(B(n), Bd(0)) assert c == KroneckerDelta(n, 0) c = Commutator(B(0), B(0)) assert c == 0 c = Commutator(B(0), Bd(0)) e = simplify(apply_operators(c * BKet([n]))) assert e == BKet([n]) c = Commutator(B(0), B(1)) e = simplify(apply_operators(c * BKet([n, m]))) assert e == 0 c = Commutator(F(m), Fd(m)) assert c == +1 - 2 * NO(Fd(m) * F(m)) c = Commutator(Fd(m), F(m)) assert c.expand() == -1 + 2 * NO(Fd(m) * F(m)) C = Commutator X, Y, Z = symbols("X,Y,Z", commutative=False) assert C(C(X, Y), Z) != 0 assert C(C(X, Z), Y) != 0 assert C(Y, C(X, Z)) != 0 i, j, k, l = symbols("i,j,k,l", below_fermi=True) a, b, c, d = symbols("a,b,c,d", above_fermi=True) p, q, r, s = symbols("p,q,r,s") D = KroneckerDelta assert C(Fd(a), F(i)) == -2 * NO(F(i) * Fd(a)) assert C(Fd(j), NO(Fd(a) * F(i))).doit(wicks=True) == -D(j, i) * Fd(a) assert C(Fd(a) * F(i), Fd(b) * F(j)).doit(wicks=True) == 0 c1 = Commutator(F(a), Fd(a)) assert Commutator.eval(c1, c1) == 0 c = Commutator(Fd(a) * F(i), Fd(b) * F(j)) assert latex(c) == r"\left[a^\dagger_{a} a_{i},a^\dagger_{b} a_{j}\right]" assert ( repr(c) == "Commutator(CreateFermion(a)*AnnihilateFermion(i),CreateFermion(b)*AnnihilateFermion(j))" ) assert ( str(c) == "[CreateFermion(a)*AnnihilateFermion(i),CreateFermion(b)*AnnihilateFermion(j)]" )
def test_commutation(): c = commutator(B(0), Bd(0)) e = simplify(apply_operators(c * Ket([n]))) assert e == Ket([n]) c = commutator(B(0), B(1)) e = simplify(apply_operators(c * Ket([n, m]))) assert e == 0
def test_matrix_elements(): b = VarBosonicBasis(5) o = B(0) m = matrix_rep(o, b) for i in range(4): assert m[i, i + 1] == sqrt(i + 1) o = Bd(0) m = matrix_rep(o, b) for i in range(4): assert m[i + 1, i] == sqrt(i + 1)
def test_sho(): n, m = symbols("n,m") h_n = Bd(n) * B(n) * (n + S.Half) H = Sum(h_n, (n, 0, 5)) o = H.doit(deep=False) b = FixedBosonicBasis(2, 6) m = matrix_rep(o, b) # We need to double check these energy values to make sure that they # are correct and have the proper degeneracies! diag = [1, 2, 3, 3, 4, 5, 4, 5, 6, 7, 5, 6, 7, 8, 9, 6, 7, 8, 9, 10, 11] for i in range(len(diag)): assert diag[i] == m[i, i]
def test_create(): i, j, n, m = symbols('i j n m') o = Bd(i) assert isinstance(o, CreateBoson) o = o.subs(i, j) assert o.atoms(Symbol) == set([j]) o = Bd(0) assert o.apply_operator(BKet([n])) == sqrt(n+1)*BKet([n+1]) o = Bd(n) assert o.apply_operator(BKet([n])) == o*BKet([n])
def test_dagger(): i, j, n, m = symbols('i,j,n,m') assert Dagger(1) == 1 assert Dagger(1.0) == 1.0 assert Dagger(2 * I) == -2 * I assert Dagger(Rational(1, 2) * I / 3.0) == -Rational(1, 2) * I / 3.0 assert Dagger(BKet([n])) == BBra([n]) assert Dagger(B(0)) == Bd(0) assert Dagger(Bd(0)) == B(0) assert Dagger(B(n)) == Bd(n) assert Dagger(Bd(n)) == B(n) assert Dagger(B(0) + B(1)) == Bd(0) + Bd(1) assert Dagger(n * m) == Dagger(n) * Dagger(m) # n, m commute assert Dagger(B(n) * B(m)) == Bd(m) * Bd(n) assert Dagger(B(n)**10) == Dagger(B(n))**10
def test_dagger(): i, j, n, m = symbols("i,j,n,m") assert Dagger(1) == 1 assert Dagger(1.0) == 1.0 assert Dagger(2 * I) == -2 * I assert Dagger(S.Half * I / 3.0) == I * Rational(-1, 2) / 3.0 assert Dagger(BKet([n])) == BBra([n]) assert Dagger(B(0)) == Bd(0) assert Dagger(Bd(0)) == B(0) assert Dagger(B(n)) == Bd(n) assert Dagger(Bd(n)) == B(n) assert Dagger(B(0) + B(1)) == Bd(0) + Bd(1) assert Dagger(n * m) == Dagger(n) * Dagger(m) # n, m commute assert Dagger(B(n) * B(m)) == Bd(m) * Bd(n) assert Dagger(B(n) ** 10) == Dagger(B(n)) ** 10 assert Dagger("a") == Dagger(Symbol("a")) assert Dagger(Dagger("a")) == Symbol("a")
def test_NO(): i, j, k, l = symbols("i j k l", below_fermi=True) a, b, c, d = symbols("a b c d", above_fermi=True) p, q, r, s = symbols("p q r s", cls=Dummy) assert NO(Fd(p) * F(q) + Fd(a) * F(b)) == NO(Fd(p) * F(q)) + NO(Fd(a) * F(b)) assert NO(Fd(i) * NO(F(j) * Fd(a))) == NO(Fd(i) * F(j) * Fd(a)) assert NO(1) == 1 assert NO(i) == i assert NO(Fd(a) * Fd(b) * (F(c) + F(d))) == NO(Fd(a) * Fd(b) * F(c)) + NO( Fd(a) * Fd(b) * F(d) ) assert NO(Fd(a) * F(b))._remove_brackets() == Fd(a) * F(b) assert NO(F(j) * Fd(i))._remove_brackets() == F(j) * Fd(i) assert NO(Fd(p) * F(q)).subs(Fd(p), Fd(a) + Fd(i)) == NO(Fd(a) * F(q)) + NO( Fd(i) * F(q) ) assert NO(Fd(p) * F(q)).subs(F(q), F(a) + F(i)) == NO(Fd(p) * F(a)) + NO( Fd(p) * F(i) ) expr = NO(Fd(p) * F(q))._remove_brackets() assert wicks(expr) == NO(expr) assert NO(Fd(a) * F(b)) == -NO(F(b) * Fd(a)) no = NO(Fd(a) * F(i) * F(b) * Fd(j)) l1 = [ind for ind in no.iter_q_creators()] assert l1 == [0, 1] l2 = [ind for ind in no.iter_q_annihilators()] assert l2 == [3, 2] no = NO(Fd(a) * Fd(i)) assert no.has_q_creators == 1 assert no.has_q_annihilators == -1 assert str(no) == ":CreateFermion(a)*CreateFermion(i):" assert repr(no) == "NO(CreateFermion(a)*CreateFermion(i))" assert latex(no) == r"\left\{a^\dagger_{a} a^\dagger_{i}\right\}" raises(NotImplementedError, lambda: NO(Bd(p) * F(q)))
def test_issue_19661(): a = Symbol('0') assert latex(Commutator( Bd(a)**2, B(a))) == '- \\left[b_{0},{b^\\dagger_{0}}^{2}\\right]'
def test_number_operator(): o = Bd(0) * B(0) e = apply_operators(o * Ket([n])) assert e == n * Ket([n])
def test_basic_apply(): e = B(0) * Ket([n]) assert apply_operators(e) == sqrt(n) * Ket([n - 1]) e = Bd(0) * Ket([n]) assert apply_operators(e) == sqrt(n + 1) * Ket([n + 1])
def test_basic_apply(): n = symbols("n") e = B(0) * BKet([n]) assert apply_operators(e) == sqrt(n) * BKet([n - 1]) e = Bd(0) * BKet([n]) assert apply_operators(e) == sqrt(n + 1) * BKet([n + 1])
def test_complex_apply(): n, m = symbols("n,m") o = Bd(0) * B(0) * Bd(1) * B(0) e = apply_operators(o * BKet([n, m])) answer = sqrt(n) * sqrt(m + 1) * (-1 + n) * BKet([-1 + n, 1 + m]) assert expand(e) == expand(answer)
def test_number_operator(): n = symbols("n") o = Bd(0) * B(0) e = apply_operators(o * BKet([n])) assert e == n * BKet([n])