def test_equality_of_mul(self): exp1 = Mul(Qbit("a"), Qbit("b")) exp2 = Mul(Qbit("b"), Qbit("a")) exp3 = Mul(Qbit("a"), Qbit("a")) self.assertTrue(exp1 == exp2) self.assertTrue(hash(exp1) == hash(exp2)) self.assertFalse(exp1 == exp3) self.assertTrue(exp1 != Qbit("a"))
def test_equality_of_with_penalty(self): M = 2.0 a, aa, b = OneHotEncInteger("a", 0, 3, M), OneHotEncInteger("a", 0, 3, M),\ OneHotEncInteger("b", 0, 3, M) self.assertTrue(a == aa) self.assertTrue(a != 1) self.assertTrue(hash(a) != hash(b)) exp = a * b + 2 * a - 1 expected_exp = AddList([Mul(a, b), Num(-1.0), Mul(a, 2)]) self.assertTrue(exp == expected_exp)
def test_equality_of_express_with_const(self): a, b = Qbit("a"), Spin("b") exp = a + b - 1 + Constraint(a * b, label="const") expected_exp = AddList( [a, Num(-1.0), b, Constraint(Mul(a, b), label="const")]) self.assertTrue(exp == expected_exp)
def test_equality_of_express(self): a, b = Qbit("a"), Qbit("b") exp = a * b + 2*a - 1 expected_exp = AddList([Mul(a, b), Num(-1.0), Mul(a, 2)]) self.assertTrue(exp == expected_exp)
def test_express_error(self): self.assertRaises(ValueError, lambda: 2 / Qbit("a")) self.assertRaises(ValueError, lambda: Qbit("a") / Qbit("a")) self.assertRaises(ValueError, lambda: Qbit("a") ** 1.5) self.assertRaises(ValueError, lambda: Mul(1, Qbit("b"))) self.assertRaises(ValueError, lambda: Add(1, Qbit("b")))
def test_equality_of_express_with_placeholder(self): a, b, p = Qbit("a"), Qbit("b"), Placeholder("p") exp = a + b - 1 + a * p expected_exp = AddList([a, Num(-1.0), b, Mul(p, a)]) self.assertTrue(exp == expected_exp)
def test_equality_sub2(self): a, b = Qbit("a"), Qbit("b") exp = a-b-1 expected_exp = AddList([a, Num(-1.0), Mul(b, -1)]) self.assertTrue(exp == expected_exp)
def test_equality_sub(self): a, b = Qbit("a"), Qbit("b") exp = 1-a-b expected_exp = AddList([Mul(a, -1), Num(1.0), Mul(b, -1)]) self.assertTrue(exp == expected_exp) self.assertTrue(exp - 0.0 == expected_exp)
def test_express_error(self): self.assertRaises(ValueError, lambda: 2 / Binary("a")) self.assertRaises(ValueError, lambda: Binary("a") / Binary("a")) self.assertRaises(ValueError, lambda: Binary("a")**1.5) self.assertRaises(ValueError, lambda: Mul(1, Binary("b"))) self.assertRaises(ValueError, lambda: Add(1, Binary("b")))