def test_array_multiply(self): array1 = Array.create('x', (2, 2), 'BINARY') array2 = Array.create('y', (2, 2), 'BINARY') expected = Array([[ Binary('x[0][0]') * Binary('y[0][0]'), Binary('x[0][1]') * Binary('y[0][1]') ], [ Binary('x[1][0]') * Binary('y[1][0]'), Binary('x[1][1]') * Binary('y[1][1]') ]]) self.assertTrue(array1 * array2 == expected) expected2 = Array([[Binary('x[0][0]') * 2, Binary('x[0][1]') * 2], [Binary('x[1][0]') * 2, Binary('x[1][1]') * 2]]) self.assertTrue(array1 * 2 == expected2) self.assertTrue(2 * array1 == expected2) self.assertTrue(array1 * (2 * np.ones((2, 2))) == expected2)
def test_array_add(self): array1 = Array.create('x', (2, 2), 'BINARY') array2 = Array.create('y', (2, 2), 'BINARY') expected = Array([[ Binary('x[0][0]') + Binary('y[0][0]'), Binary('x[0][1]') + Binary('y[0][1]') ], [ Binary('x[1][0]') + Binary('y[1][0]'), Binary('x[1][1]') + Binary('y[1][1]') ]]) self.assertTrue(array1 + array2 == expected) expected2 = Array([[Binary('x[0][0]') + 1, Binary('x[0][1]') + 1], [Binary('x[1][0]') + 1, Binary('x[1][1]') + 1]]) self.assertTrue(array1 + 1 == expected2) self.assertTrue(1 + array1 == expected2) self.assertTrue(array1 + np.ones((2, 2)) == expected2) self.assertRaises(TypeError, lambda: array1 + "str") array3 = Array.create('z', (2, 3), 'BINARY') self.assertRaises(ValueError, lambda: array1 + array3)
def test_array_subtract(self): array1 = Array.create('x', (2, 2), 'BINARY') array2 = Array.create('y', (2, 2), 'BINARY') expected = Array([[ Binary('x[0][0]') - Binary('y[0][0]'), Binary('x[0][1]') - Binary('y[0][1]') ], [ Binary('x[1][0]') - Binary('y[1][0]'), Binary('x[1][1]') - Binary('y[1][1]') ]]) self.assertTrue(array1 - array2 == expected) expected2 = Array([[Binary('x[0][0]') - 1, Binary('x[0][1]') - 1], [Binary('x[1][0]') - 1, Binary('x[1][1]') - 1]]) self.assertTrue(array1 - 1 == expected2) expected3 = Array([[1 - Binary('x[0][0]'), 1 - Binary('x[0][1]')], [1 - Binary('x[1][0]'), 1 - Binary('x[1][1]')]]) self.assertTrue(1 - array1 == expected3) self.assertTrue(array1 - np.ones((2, 2)) == expected2)
def test_equality_of_express(self): a, b = Binary("a"), Binary("b") exp = a * b + 2 * a - 1 expected_exp = AddList([Mul(a, b), Num(-1.0), Mul(a, 2)]) self.assertTrue(exp == expected_exp)
from pyqubo import Binary, Constraint, Placeholder from dimod import ExactSolver # Flagpole problem from Denny's slide 2 # On slide 3, he uses an auxiliary variable p = xy # what other kind of variables are there? p, x, y, z = Binary("p"), Binary("x"), Binary("y"), Binary("z") lamda = Placeholder("lamda") H = (p * z) + (lamda * Constraint((3 * p) + (x * y) - (2 * p * (x + y)), label="subst")) # Can I find the minimum valid energy? # Can I find the maximum valid energy? # and the minimum invalid energy? # what do they require? model = H.compile() bqm = model.to_dimod_bqm(feed_dict={"lamda":1.4}) exact_response = ExactSolver().sample(bqm) for smpl, energy in exact_response.data(['sample', 'energy']): sol, broken, eny = model.decode_solution(smpl, vartype='BINARY', feed_dict={"lamda":0.7}) if not broken: print(" Good ", sol, eny) if broken: print(" Bad ", sol, eny)
def test_array_equality(self): array1 = Array([[Binary('x0'), Binary('x1')], [Binary('x2'), Binary('x3')]]) array2 = Array([[Binary('x0'), Binary('x1')], [Binary('x2'), Binary('x3')]]) array3 = Array([[Binary('x0'), Binary('x1')], [Binary('x2'), Binary('x4')]]) self.assertTrue(array1 == array2) self.assertTrue(array1 != array3) self.assertTrue(not array1 == Binary('x2'))
import dimod from pyqubo import Binary exactsolver = dimod.ExactSolver() # Binary variables x = Binary('x') y = Binary('y') z = Binary('z') w = Binary('w') # Hamiltonian H = x + y - (2 * x * y) - 1 H += (2 * y * z) - y - z H += z + w - (2 * z * w) - 1 # Compile the model, and turn it into a QUBO object model = H.compile() Q = model.to_qubo() bqm = dimod.BinaryQuadraticModel.from_qubo(Q[0], offset=Q[1]) # solve the problem results = exactsolver.sample(bqm) # print the results for smpl, energy in results.data(['sample', 'energy']): print(smpl, energy)
def test_equality_of_express_with_placeholder(self): a, b, p = Binary("a"), Binary("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_repr(self): a, b, p = Binary("a"), Binary("b"), Placeholder("p") exp = a + p - 1 + Constraint(a * b, label="const") expected = "(Binary(a)+Placeholder(p)+Num(-1)+Const(const, (Binary(a)*Binary(b))))" self.assertTrue(repr(exp) == expected)
def test_equality_sub(self): a, b = Binary("a"), Binary("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_equality_sub2(self): a, b = Binary("a"), Binary("b") exp = a - b - 1 expected_exp = AddList([a, Num(-1.0), Mul(b, -1)]) self.assertTrue(exp == expected_exp)
pickle.dump(samples, f) # I am lazy and took these values from a CS 381 problem vals = [1, 6, 18, 22, 28] weights = [1, 2, 5, 6, 7] W = 11 A = 100 # We need B*max(vals) < A so the H_B doesn't "overpower" H_A B = 2 * 28 # y_k is 1 if the final weight is k and 0 otherwise final_weight_term = 0 for i in range(W): final_weight_term += Binary(f'y_{i}') final_weight_term = A * (1 - final_weight_term)**2 # we want to weight of the objects to sum to the final weight: ft = 0 st = 0 final_enforcement_term = 0 for i in range(len(vals)): st += weights[i] * Binary(f'x_{i}') for i in range(W): ft += (i + 1) * Binary(f'y_{i}') - st # need n to start from 1 not 0! final_enforcement_term = A * (ft)**2 H_A = final_weight_term + final_enforcement_term
tasks = np.array(tasks) return tasks, velocity #read problem(infomation of tasks) arg = sys.argv[1] tasks, velocity = read_distances(arg) n = len(velocity) row = n column = n * 2 # creating instance of problem x = [] for i in range(row * column): x.append(Binary('x[{}]'.format(i))) x = Array(np.reshape(np.array(x), (1, row * column))) #Starting point of arm start = [0, 0] #Weight(distance of movement) w0 = [] w = [[] for i in range(column)] wf = [] for i in range(column): w0.append(dist_cal(start, tasks[i])) for j in range(column): if i % 2 == 0: w[i].append(dist_cal(tasks[i + 1], tasks[j]))
import dimod import minorminer from dwave.system import DWaveSampler from dwave.system.composites import AutoEmbeddingComposite, FixedEmbeddingComposite from pyqubo import Binary import matplotlib.pyplot as plt import sys dwaveSampler = DWaveSampler() dimodSampler = dimod.ExactSolver() ## EXAMPLE Problem -> Replace with general form binArr = [] index = 0 for i in range(0, 5): binArr.append(Binary('s' + str(i + 1))) s1, s2, s3, s4, s5 = binArr[0], binArr[1], binArr[2], binArr[3], binArr[4] H = (s1 + 2 * s2 + 3 * s3 + 4 * s4 + 5 * s5 - 8 * 3)**2 + (s1 + s2 + s3 + s4 + s5 - 3 * 3)**2 def get_embedding_with_short_chain(J: dict, tries: int = 5, processor: list = None, verbose=False) -> dict: '''Try a few probabilistic embeddings and return the one with the shortest chain length :param J: Couplings :param tries: Number of probabilistic embeddings :param verbose: Whether to print out diagnostic information
def test_array_dot(self): # inner product of 1-D arrays array_a = Array([Binary('a'), Binary('b')]) array_b = Array([Binary('c'), Binary('d')]) expected = ((Binary('a') * Binary('c')) + (Binary('b') * Binary('d'))) self.assertTrue(expected == array_a.dot(array_b)) # dot product of 1-D array and N-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(expected == array_a.dot(array_b)) # both are 2-D arrays 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(expected == array_a.dot(array_b)) # array_a is a N-D array and array_b is an M-D array (where N, M>=2) array_a = Array.create('a', shape=(3, 2, 4), vartype='BINARY') array_b = Array.create('b', shape=(5, 4, 3), vartype='BINARY') i, j, k, m = (1, 1, 3, 2) self.assertTrue( array_a.dot(array_b)[i, j, k, m] == sum(array_a[i, j, :] * array_b[k, :, m])) # array_a is 1-D array and array_b is a list array_a = Array([Binary('a'), Binary('b')]) array_b = [3, 4] self.assertTrue((Binary('a') * 3 + (Binary('b') * 4)) == array_a.dot(array_b)) # test validation self.assertRaises(TypeError, lambda: array_a.dot(1))
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")))
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_num(self): self.assertTrue(Num(1) == Num(1)) self.assertFalse(Num(1) == Num(2)) self.assertFalse(Num(1) == Binary("a"))
def test_array_create_binary(self): array = Array.create('x', shape=(3, 3, 3), vartype='BINARY') self.assertTrue(array.shape == (3, 3, 3)) self.assertTrue(array[0][0][0] == Binary('x[0][0][0]'))
def test_equality(self): xor1 = XorConst(Binary("a"), Binary("b"), Binary("c"), label="xor") xor2 = XorConst(Binary("a"), Binary("b"), Binary("c"), label="xor") xor3 = XorConst(Binary("b"), Binary("c"), Binary("a"), label="xor") or1 = OrConst(Binary("a"), Binary("b"), Binary("c"), label="xor") self.assertTrue(xor1 + 1 == xor2 + 1) self.assertTrue(xor1 == xor2) self.assertFalse(xor1 == or1) self.assertFalse(xor1 == xor3)