def test_imul_inplace(self): fermion_op = FermionOperator("1^") prev_id = id(fermion_op) fermion_op *= 3. self.assertEqual(id(fermion_op), prev_id) self.assertEqual(fermion_op.terms[((1, 1), )], 3.)
def test_hermitian_conjugated_simple(self): op = FermionOperator('0') op_hc = FermionOperator('0^') self.assertTrue(op_hc.isclose(hermitian_conjugated(op)))
def test_hermitian_conjugated_complex_const(self): op = FermionOperator('2^ 2', 3j) op_hc = FermionOperator('2^ 2', -3j) self.assertTrue(op_hc.isclose(hermitian_conjugated(op)))
def test_hermitian_conjugate_simple(self): op = FermionOperator('1^') op_hc = FermionOperator('1') op = hermitian_conjugated(op) self.assertTrue(op.isclose(op_hc))
def test_hermitian_conjugate_notordered(self): op = FermionOperator('1 3^ 3 3^', 3j) op_hc = -3j * FermionOperator('3 3^ 3 1^') op = hermitian_conjugated(op) self.assertTrue(op.isclose(op_hc))
def test_init_tuple_npcomplex128_coefficient(self): loc_op = ((0, 1), (5, 0), (6, 1)) coefficient = numpy.complex128(-1.123j + 43.7) fermion_op = FermionOperator(loc_op, coefficient) self.assertEqual(len(fermion_op.terms), 1) self.assertEqual(fermion_op.terms[loc_op], coefficient)
def test_pow_neg_error(self): with self.assertRaises(ValueError): FermionOperator()**-1
def test_mul_bad_multiplier(self): op = FermionOperator(((1, 1), (0, 1)), -1j) with self.assertRaises(TypeError): op = op * "0.5"
def test_mul_npfloat64(self): op = FermionOperator(((1, 0), (3, 1)), 0.5) res = op * numpy.float64(0.5) self.assertTrue( res.isclose(FermionOperator(((1, 0), (3, 1)), 0.5 * 0.5)))
def test_mul_by_scalarzero(self): op = FermionOperator(((1, 1), (0, 1)), -1j) * 0 self.assertNotIn(((0, 1), (1, 1)), op.terms) self.assertIn(((1, 1), (0, 1)), op.terms) self.assertEqual(op.terms[((1, 1), (0, 1))], 0.0)
def test_init_defaults(self): loc_op = FermionOperator() self.assertEqual(len(loc_op.terms), 0)
def test_imul_scalar_npcomplex128(self): loc_op = ((1, 0), (2, 1)) multiplier = numpy.complex128(-1.123j + 1.7911) fermion_op = FermionOperator(loc_op) fermion_op *= multiplier self.assertEqual(fermion_op.terms[loc_op], multiplier)
def test_imul_scalar_npfloat64(self): loc_op = ((1, 0), (2, 1)) multiplier = numpy.float64(2.303) fermion_op = FermionOperator(loc_op) fermion_op *= multiplier self.assertEqual(fermion_op.terms[loc_op], multiplier)
def test_imul_scalar_complex(self): loc_op = ((1, 0), (2, 1)) multiplier = 0.6j fermion_op = FermionOperator(loc_op) fermion_op *= multiplier self.assertEqual(fermion_op.terms[loc_op], multiplier)
def test_sub_bad_subtrahend(self): op = FermionOperator((), 1.0) with self.assertRaises(TypeError): op = op - "0.5"
def test_rmul_scalar_complex(self): op = FermionOperator(((1, 1), (3, 0), (8, 1)), 0.5) multiplier = 0.6j res1 = op * multiplier res2 = multiplier * op self.assertTrue(res1.isclose(res2))
def test_isub_bad_addend(self): op = FermionOperator((), 1.0) with self.assertRaises(TypeError): op -= "0.5"
def test_rmul_scalar_npfloat64(self): op = FermionOperator(((1, 1), (3, 0), (8, 1)), 0.5) multiplier = numpy.float64(2.303) res1 = op * multiplier res2 = multiplier * op self.assertTrue(res1.isclose(res2))
def test_pow_one_term(self): coeff = 6.7j ops = ((3, 1), (1, 0), (4, 1)) term = FermionOperator(ops, coeff) self.assertTrue(term.isclose(term**1))
def test_rmul_scalar_npcomplex128(self): op = FermionOperator(((1, 1), (3, 0), (8, 1)), 0.5) multiplier = numpy.complex128(-1.5j + 7.7) res1 = op * multiplier res2 = multiplier * op self.assertTrue(res1.isclose(res2))
def test_pow_nonint_error(self): with self.assertRaises(ValueError): FermionOperator('3 2^')**0.5
def test_rmul_bad_multiplier(self): op = FermionOperator(((1, 1), (3, 0), (8, 1)), 0.5) with self.assertRaises(TypeError): op = "0.5" * op
def test_hermitian_conjugate_complex_const(self): op = FermionOperator('1^ 3', 3j) op_hc = -3j * FermionOperator('3^ 1') op = hermitian_conjugated(op) self.assertTrue(op.isclose(op_hc))
def test_init_tuple_complex_coefficient(self): loc_op = ((0, 1), (5, 0), (6, 1)) coefficient = 0.6j fermion_op = FermionOperator(loc_op, coefficient) self.assertEqual(len(fermion_op.terms), 1) self.assertEqual(fermion_op.terms[loc_op], coefficient)
def test_hermitian_conjugated_empty(self): op = FermionOperator() self.assertTrue(op.isclose(hermitian_conjugated(op)))
def test_itruediv_bad_divisor(self): op = FermionOperator(((1, 1), (3, 0), (8, 1)), 0.5) with self.assertRaises(TypeError): op /= "0.5"
def test_init_str(self): fermion_op = FermionOperator('0^ 5 12^', -1.) correct = ((0, 1), (5, 0), (12, 1)) self.assertIn(correct, fermion_op.terms) self.assertEqual(fermion_op.terms[correct], -1.0)
def test_init_tuple_npfloat64_coefficient(self): loc_op = ((0, 1), (5, 0), (6, 1)) coefficient = numpy.float64(2.303) fermion_op = FermionOperator(loc_op, coefficient) self.assertEqual(len(fermion_op.terms), 1) self.assertEqual(fermion_op.terms[loc_op], coefficient)
def test_hermitian_conjugated_multiterm(self): op = FermionOperator('1^ 2') + FermionOperator('2 3 4') op_hc = FermionOperator('2^ 1') + FermionOperator('4^ 3^ 2^') self.assertTrue(op_hc.isclose(hermitian_conjugated(op)))
def test_isclose_different_num_terms(self): a = FermionOperator(((1, 0), ), -0.1j) a += FermionOperator(((1, 1), ), -0.1j) b = FermionOperator(((1, 0), ), -0.1j) self.assertFalse(b.isclose(a, rel_tol=1e-12, abs_tol=0.05)) self.assertFalse(a.isclose(b, rel_tol=1e-12, abs_tol=0.05))