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_array_matmul(self): # the either of the arguments is 1-D array, array_a = Array([[Binary('a'), Binary('b')], [Binary('c'), Binary('d')]]) array_b = Array([Binary('e'), Binary('f')]) expected = Array([ ((Binary('a') * Binary('e')) + (Binary('b') * Binary('f'))), ((Binary('c') * Binary('e')) + (Binary('d') * Binary('f'))) ]) self.assertTrue(array_a.matmul(array_b) == expected) # both arguments are 2-D array array_a = Array([[Binary('a'), Binary('b')], [Binary('c'), Binary('d')]]) array_b = Array([[Binary('e'), Binary('f')], [Binary('g'), Binary('h')]]) expected = Array( [[((Binary('a') * Binary('e')) + (Binary('b') * Binary('g'))), ((Binary('a') * Binary('f')) + (Binary('b') * Binary('h')))], [((Binary('c') * Binary('e')) + (Binary('d') * Binary('g'))), ((Binary('c') * Binary('f')) + (Binary('d') * Binary('h')))]]) self.assertTrue(array_a.matmul(array_b) == expected) # either argument is N-D (where N > 2) array_a = Array.create('a', shape=(2, 2, 3), vartype='BINARY') array_b = Array.create('b', shape=(3, 2), vartype='BINARY') self.assertTrue( (array_a.matmul(array_b))[0] == array_a[0].matmul(array_b)) # array_a is 2-D array and array_b is a list array_a = Array([[Binary('a'), Binary('b')], [Binary('c'), Binary('d')]]) array_b = [3, 4] expected = Array([((Binary('a') * Num(3)) + (Binary('b') * Num(4))), ((Binary('c') * Num(3)) + (Binary('d') * Num(4)))]) self.assertTrue(expected == array_a.matmul(array_b)) # test validation array_a = Array.create('a', shape=(2, 2, 3), vartype='BINARY') array_b = Array.create('b', shape=(3, 3, 2), vartype='BINARY') self.assertRaises(AssertionError, lambda: array_a.matmul(array_b))
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(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_equality_of_num(self): self.assertTrue(Num(1) == Num(1)) self.assertFalse(Num(1) == Num(2)) self.assertFalse(Num(1) == Qbit("a"))
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)