def test_multiplication(self): # Real expr_r = c_dag(1, "up") self.assertIsInstance(expr_r * c(2, "dn"), ExpressionR) self.assertIsInstance(c(2, "dn") * expr_r, ExpressionR) self.assertEqual(str(ExpressionR() * ExpressionR()), "0") self.assertEqual(str(expr_r * ExpressionR()), "0") self.assertEqual(str(ExpressionR() * expr_r), "0") self.assertEqual(str(expr_r * c(2, "dn")), "1*C+(1,up)C(2,dn)") self.assertEqual(str(c(2, "dn") * expr_r), "-1*C+(1,up)C(2,dn)") expr_r *= c(2, "dn") self.assertEqual(str(expr_r * a(0, "x")), "1*C+(1,up)C(2,dn)A(0,x)") self.assertEqual(str(a(0, "x") * expr_r), "1*C+(1,up)C(2,dn)A(0,x)") self.assertEqual(str(expr_r * c_dag(1, "up")), "0") self.assertEqual(str(c_dag(1, "up") * expr_r), "0") # Real and complex expr1 = make_complex(c_dag(1, "up")) expr2 = c(2, "dn") self.assertIsInstance(expr1 * expr2, ExpressionC) self.assertIsInstance(expr2 * expr1, ExpressionC) self.assertEqual(str(ExpressionC() * ExpressionR()), "(0,0)") self.assertEqual(str(ExpressionR() * ExpressionC()), "(0,0)") self.assertEqual(str(expr1 * ExpressionR()), "(0,0)") self.assertEqual(str(ExpressionR() * expr1), "(0,0)") self.assertEqual(str(expr2 * ExpressionC()), "(0,0)") self.assertEqual(str(ExpressionC() * expr2), "(0,0)") self.assertEqual(str(expr1 * expr2), "(1,0)*C+(1,up)C(2,dn)") self.assertEqual(str(expr2 * expr1), "(-1,0)*C+(1,up)C(2,dn)") expr1 *= expr2 self.assertEqual(str(expr1 * a(0, "x")), "(1,0)*C+(1,up)C(2,dn)A(0,x)") self.assertEqual(str(a(0, "x") * expr1), "(1,0)*C+(1,up)C(2,dn)A(0,x)") self.assertEqual(str(expr1 * make_complex(c_dag(1, "up"))), "(0,0)") self.assertEqual(str(make_complex(c_dag(1, "up")) * expr1), "(0,0)")
def test_inplace_multiplication(self): # Real expr_r = c(2, "dn") expr_r *= c_dag(1, "up") self.assertEqual(str(expr_r), "-1*C+(1,up)C(2,dn)") expr_r *= a(0, "x") self.assertEqual(str(expr_r), "-1*C+(1,up)C(2,dn)A(0,x)") expr_r *= a_dag(0, "y") self.assertEqual(str(expr_r), "-1*C+(1,up)C(2,dn)A+(0,y)A(0,x)") expr_r *= ExpressionR(2) self.assertEqual(str(expr_r), "-2*C+(1,up)C(2,dn)A+(0,y)A(0,x)") expr_r *= ExpressionR() self.assertEqual(str(expr_r), "0") # Complex expr_c = make_complex(c(2, "dn")) expr_c *= make_complex(c_dag(1, "up")) self.assertEqual(str(expr_c), "(-1,0)*C+(1,up)C(2,dn)") expr_c *= make_complex(a(0, "x")) self.assertEqual(str(expr_c), "(-1,0)*C+(1,up)C(2,dn)A(0,x)") expr_c *= make_complex(a_dag(0, "y")) self.assertEqual(str(expr_c), "(-1,0)*C+(1,up)C(2,dn)A+(0,y)A(0,x)") expr_c *= ExpressionC(2) self.assertEqual(str(expr_c), "(-2,0)*C+(1,up)C(2,dn)A+(0,y)A(0,x)") expr_c *= ExpressionC() self.assertEqual(str(expr_c), "(0,0)")
def test_subtraction(self): # Real expr_r = c_dag(1, "up") self.assertIsInstance(expr_r - c(2, "dn"), ExpressionR) self.assertIsInstance(c(2, "dn") - expr_r, ExpressionR) self.assertEqual(str(ExpressionR() - ExpressionR()), "0") self.assertEqual(str(expr_r - ExpressionR()), "1*C+(1,up)") self.assertEqual(str(ExpressionR() - expr_r), "-1*C+(1,up)") self.assertEqual(str(expr_r - c(2, "dn")), "1*C+(1,up) + -1*C(2,dn)") self.assertEqual(str(c(2, "dn") - expr_r), "-1*C+(1,up) + 1*C(2,dn)") expr_r -= c(2, "dn") self.assertEqual(str(expr_r + ExpressionR()), "1*C+(1,up) + -1*C(2,dn)") self.assertEqual(str(ExpressionR() - expr_r), "-1*C+(1,up) + 1*C(2,dn)") self.assertEqual(str(expr_r - a(0, "x")), "1*C+(1,up) + -1*C(2,dn) + -1*A(0,x)") self.assertEqual(str(a(0, "x") - expr_r), "-1*C+(1,up) + 1*C(2,dn) + 1*A(0,x)") self.assertEqual(str(expr_r - c_dag(1, "up")), "-1*C(2,dn)") self.assertEqual(str(c_dag(1, "up") - expr_r), "1*C(2,dn)") self.assertEqual( str((c_dag(1, "up") + c(2, "dn")) - (c(2, "dn") + 2.0)), "-2 + 1*C+(1,up)") # Real and complex expr1 = make_complex(c_dag(1, "up")) expr2 = c(2, "dn") self.assertIsInstance(expr1 - expr2, ExpressionC) self.assertIsInstance(expr2 - expr1, ExpressionC) self.assertEqual(str(ExpressionC() - ExpressionR()), "(0,0)") self.assertEqual(str(ExpressionR() - ExpressionC()), "(0,0)") self.assertEqual(str(expr1 - ExpressionR()), "(1,0)*C+(1,up)") self.assertEqual(str(ExpressionR() - expr1), "(-1,-0)*C+(1,up)") self.assertEqual(str(expr2 - ExpressionC()), "(1,-0)*C(2,dn)") self.assertEqual(str(ExpressionC() - expr2), "(-1,0)*C(2,dn)") self.assertEqual(str(expr1 - expr2), "(1,0)*C+(1,up) + (-1,0)*C(2,dn)") self.assertEqual(str(expr2 - expr1), "(-1,-0)*C+(1,up) + (1,-0)*C(2,dn)") expr1 -= expr2 self.assertEqual(str(expr1 - ExpressionR()), "(1,0)*C+(1,up) + (-1,0)*C(2,dn)") self.assertEqual(str(ExpressionR() - expr1), "(-1,-0)*C+(1,up) + (1,-0)*C(2,dn)") self.assertEqual(str(expr1 - a(0, "x")), "(1,0)*C+(1,up) + (-1,0)*C(2,dn) + (-1,0)*A(0,x)") self.assertEqual(str(a(0, "x") - expr1), "(-1,-0)*C+(1,up) + (1,-0)*C(2,dn) + (1,-0)*A(0,x)") self.assertEqual(str(expr1 - make_complex(c_dag(1, "up"))), "(-1,0)*C(2,dn)") self.assertEqual(str(make_complex(c_dag(1, "up")) - expr1), "(1,0)*C(2,dn)") self.assertEqual( str( make_complex(c_dag(1, "up") + c(2, "dn")) - (c(2, "dn") + 2.0)), "(-2,0) + (1,0)*C+(1,up)")
def test_addition(self): # Real expr_r = c_dag(1, "up") self.assertIsInstance(expr_r + c(2, "dn"), ExpressionR) self.assertIsInstance(c(2, "dn") + expr_r, ExpressionR) self.assertEqual(str(ExpressionR() + ExpressionR()), "0") self.assertEqual(str(expr_r + ExpressionR()), "1*C+(1,up)") self.assertEqual(str(ExpressionR() + expr_r), "1*C+(1,up)") self.assertEqual(str(expr_r + c(2, "dn")), "1*C+(1,up) + 1*C(2,dn)") self.assertEqual(str(c(2, "dn") + expr_r), "1*C+(1,up) + 1*C(2,dn)") expr_r += c(2, "dn") self.assertEqual(str(expr_r + ExpressionR()), "1*C+(1,up) + 1*C(2,dn)") self.assertEqual(str(ExpressionR() + expr_r), "1*C+(1,up) + 1*C(2,dn)") self.assertEqual(str(expr_r + a(0, "x")), "1*C+(1,up) + 1*C(2,dn) + 1*A(0,x)") self.assertEqual(str(a(0, "x") + expr_r), "1*C+(1,up) + 1*C(2,dn) + 1*A(0,x)") self.assertEqual(str(expr_r + (-c_dag(1, "up"))), "1*C(2,dn)") self.assertEqual(str((-c_dag(1, "up")) + expr_r), "1*C(2,dn)") self.assertEqual( str((c_dag(1, "up") + c(2, "dn")) + (c(2, "dn") + 2.0)), "2 + 1*C+(1,up) + 2*C(2,dn)") # Real and complex expr1 = make_complex(c_dag(1, "up")) expr2 = c(2, "dn") self.assertIsInstance(expr1 + expr2, ExpressionC) self.assertIsInstance(expr2 + expr1, ExpressionC) self.assertEqual(str(ExpressionC() + ExpressionR()), "(0,0)") self.assertEqual(str(ExpressionR() + ExpressionC()), "(0,0)") self.assertEqual(str(expr1 + ExpressionR()), "(1,0)*C+(1,up)") self.assertEqual(str(ExpressionR() + expr1), "(1,0)*C+(1,up)") self.assertEqual(str(expr2 + ExpressionC()), "(1,0)*C(2,dn)") self.assertEqual(str(ExpressionC() + expr2), "(1,0)*C(2,dn)") self.assertEqual(str(expr1 + expr2), "(1,0)*C+(1,up) + (1,0)*C(2,dn)") self.assertEqual(str(expr2 + expr1), "(1,0)*C+(1,up) + (1,0)*C(2,dn)") expr1 += expr2 self.assertEqual(str(expr1 + ExpressionR()), "(1,0)*C+(1,up) + (1,0)*C(2,dn)") self.assertEqual(str(ExpressionR() + expr1), "(1,0)*C+(1,up) + (1,0)*C(2,dn)") self.assertEqual(str(expr1 + a(0, "x")), "(1,0)*C+(1,up) + (1,0)*C(2,dn) + (1,0)*A(0,x)") self.assertEqual(str(a(0, "x") + expr1), "(1,0)*C+(1,up) + (1,0)*C(2,dn) + (1,0)*A(0,x)") self.assertEqual(str(expr1 + (-make_complex(c_dag(1, "up")))), "(1,0)*C(2,dn)") self.assertEqual(str((-make_complex(c_dag(1, "up"))) + expr1), "(1,0)*C(2,dn)") self.assertEqual( str( make_complex(c_dag(1, "up") + c(2, "dn")) + (c(2, "dn") + 2.0)), "(2,0) + (1,0)*C+(1,up) + (2,0)*C(2,dn)")
def test_init(self): self.assertEqual(str(ExpressionR()), "0") self.assertEqual(str(ExpressionR(1e-100)), "0") expr_r_const = ExpressionR(2) self.assertEqual(str(expr_r_const), "2") expr_c_const = ExpressionC(2) self.assertEqual(str(expr_c_const), "(2,0)") self.assertEqual(str(ExpressionC(expr_r_const)), "(2,0)") mon = Monomial( [make_fermion(True, 1, "up"), make_fermion(False, 2, "dn")]) self.assertEqual(str(ExpressionR(1e-100, mon)), "0") expr_mon = ExpressionR(3, mon) self.assertEqual(str(expr_mon), "3*C+(1,up)C(2,dn)") expr_mon.clear() self.assertEqual(len(expr_mon), 0)
def test_Heisenberg(self): # Addition of 3D vectors def add(S1, S2): return (S1[0] + S2[0], S1[1] + S2[1], S1[2] + S2[2]) # Dot-product of 3D vectors def dot(S1, S2): return S1[0] * S2[0] + S1[1] * S2[1] + S1[2] * S2[2] # Cross-product of vectors def cross(S1, S2): return (S1[1] * S2[2] - S1[2] * S2[1], S1[2] * S2[0] - S1[0] * S2[2], S1[0] * S2[1] - S1[1] * S2[0]) N = 6 S = [(S_x(i), S_y(i), S_z(i)) for i in range(N)] H = sum(dot(S[i], S[(i + 1) % N]) for i in range(N)) S_tot = (ExpressionC(), ) * 3 for i in range(N): S_tot += add(S_tot, S[i]) # H must commute with the total spin self.assertEqual(len(H * S_tot[0] - S_tot[0] * H), 0) self.assertEqual(len(H * S_tot[1] - S_tot[1] * H), 0) self.assertEqual(len(H * S_tot[2] - S_tot[2] * H), 0) # Q3 is a higher-order integral of motion Q3 = sum( dot(cross(S[i], S[(i + 1) % N]), S[(i + 2) % N]) for i in range(N)) self.assertEqual(len(H * Q3 - Q3 * H), 0)
def test_slater_int(self): spins = ("up", "dn") F0 = 400 F2 = 100 U = F0 + 4 * F2 / 25 J = 3 * F2 / 25 # L = 0 H1 = slater_int(np.array([F0])) self.assertIsInstance(H1, ExpressionR) ref1 = F0 * n(0, "up") * n(0, "dn") self.assertEqual(H1, ref1) # L = 1 orbs = (-1, 0, 1) H2 = slater_int(np.array([F0, F2])) self.assertIsInstance(H2, ExpressionR) N = sum(n(m, "up") + n(m, "dn") for m in orbs) sp = sum(c_dag(m, "up") * c(m, "dn") for m in orbs) sz = sum(0.5 * (n(m, "up") - n(m, "dn")) for m in orbs) s2 = 0.5 * (sp * conj(sp) + conj(sp) * sp) + sz * sz lp = np.sqrt(2) * sum( c_dag(1, s) * c(0, s) + c_dag(0, s) * c(-1, s) for s in spins) lz = sum(n(1, s) - n(-1, s) for s in spins) l2 = 0.5 * (lp * conj(lp) + conj(lp) * lp) + lz * lz ref2 = (4 * J - U / 2) * N + 0.5 * (U - 3 * J) * N * N ref2 += -J * (2 * s2 + 0.5 * l2) self.assertEqual(H2 - ref2, ExpressionR()) H3 = slater_int(1j * np.array([F0, F2])) self.assertIsInstance(H3, ExpressionC) ref3 = 1j * ref2 self.assertEqual(H3 - ref3, ExpressionC()) H4 = slater_int(np.array([F0, F2]), indices_up=[("up", -1), ("up", 0), ("up", 1)], indices_dn=[("dn", -1), ("dn", 0), ("dn", 1)]) self.assertIsInstance(H4, ExpressionR) N = sum(n("up", m) + n("dn", m) for m in orbs) sp = sum(c_dag("up", m) * c("dn", m) for m in orbs) sz = sum(0.5 * (n("up", m) - n("dn", m)) for m in orbs) s2 = 0.5 * (sp * conj(sp) + conj(sp) * sp) + sz * sz lp = np.sqrt(2) * sum( c_dag(s, 1) * c(s, 0) + c_dag(s, 0) * c(s, -1) for s in spins) lz = sum(n(s, 1) - n(s, -1) for s in spins) l2 = 0.5 * (lp * conj(lp) + conj(lp) * lp) + lz * lz ref4 = (4 * J - U / 2) * N + 0.5 * (U - 3 * J) * N * N ref4 += -J * (2 * s2 + 0.5 * l2) self.assertEqual(H4 - ref4, ExpressionR())
def test_inplace_subtraction(self): # Real expr_r = c_dag(1, "up") expr_r -= c(2, "dn") self.assertEqual(str(expr_r), "1*C+(1,up) + -1*C(2,dn)") expr_r -= ExpressionR() self.assertEqual(str(expr_r), "1*C+(1,up) + -1*C(2,dn)") expr_r -= c_dag(1, "up") self.assertEqual(str(expr_r), "-1*C(2,dn)") # Complex expr_c = make_complex(c_dag(1, "up")) expr_c -= c(2, "dn") self.assertEqual(str(expr_c), "(1,0)*C+(1,up) + (-1,0)*C(2,dn)") expr_c -= ExpressionC() self.assertEqual(str(expr_c), "(1,0)*C+(1,up) + (-1,0)*C(2,dn)") expr_c -= c_dag(1, "up") self.assertEqual(str(expr_c), "(-1,0)*C(2,dn)")
def test_inplace_addition(self): # Real expr_r = c_dag(1, "up") expr_r += c(2, "dn") self.assertEqual(str(expr_r), "1*C+(1,up) + 1*C(2,dn)") expr_r += ExpressionR() self.assertEqual(str(expr_r), "1*C+(1,up) + 1*C(2,dn)") expr_r += -c_dag(1, "up") self.assertEqual(str(expr_r), "1*C(2,dn)") # Complex expr_c = make_complex(c_dag(1, "up")) expr_c += c(2, "dn") self.assertEqual(str(expr_c), "(1,0)*C+(1,up) + (1,0)*C(2,dn)") expr_c += ExpressionC() self.assertEqual(str(expr_c), "(1,0)*C+(1,up) + (1,0)*C(2,dn)") expr_c += -c_dag(1, "up") self.assertEqual(str(expr_c), "(1,0)*C(2,dn)")