def test_sz_operator(self): op = sz_operator(2) expected = (FermionOperator(((0, 1), (0, 0)), 0.5) - FermionOperator( ((1, 1), (1, 0)), 0.5) + FermionOperator( ((2, 1), (2, 0)), 0.5) - FermionOperator( ((3, 1), (3, 0)), 0.5)) self.assertTrue(op == expected)
def test_relations(self): n_spatial_orbitals = 2 s_plus = s_plus_operator(n_spatial_orbitals) s_minus = s_minus_operator(n_spatial_orbitals) sx = sx_operator(n_spatial_orbitals) sy = sy_operator(n_spatial_orbitals) sz = sz_operator(n_spatial_orbitals) s_squared = s_squared_operator(n_spatial_orbitals) identity = FermionOperator(()) self.assertEqual(normal_ordered(sx), normal_ordered(.5 * (s_plus + s_minus))) self.assertEqual(normal_ordered(sy), normal_ordered((.5 / 1.j) * (s_plus - s_minus))) self.assertEqual(normal_ordered(s_squared), normal_ordered(sx**2 + sy**2 + sz**2)) self.assertEqual( normal_ordered(s_squared), normal_ordered(s_plus * s_minus + sz * (sz - identity))) self.assertEqual(normal_ordered(commutator(s_plus, s_minus)), normal_ordered(2 * sz)) self.assertEqual(normal_ordered(commutator(sx, sy)), normal_ordered(1.j * sz))
def test_invalid_input(self): with self.assertRaises(TypeError): s_minus_operator('a') with self.assertRaises(TypeError): s_plus_operator('a') with self.assertRaises(TypeError): sx_operator('a') with self.assertRaises(TypeError): sy_operator('a') with self.assertRaises(TypeError): sz_operator('a') with self.assertRaises(TypeError): s_squared_operator('a')