def __init__(self, graph): self.graph = graph self.h, self.J = self.graph.get_data() solver = local_connection.get_solver('c4-sw_optimize') self.blackbox_solver = BlackBoxSolver(solver) self.blackbox_parameter = 1 self.cluster_parameter = 2
def __init__(self, filename): self.clauses = [] self.process_file(filename) solver = local_connection.get_solver('c4-sw_optimize') self.blackbox_solver = BlackBoxSolver(solver) self.blackbox_parameter = 10 self.gamma = 99999
class QNand(object): def __init__(self): solver = local_connection.get_solver('c4-sw_optimize') self.blackbox_solver = BlackBoxSolver(solver) self.evaluator = NANDEvaluator() self.blackbox_parameter = 10 def compute(self, x, y): # map 0/1 to qubit values +/-1 self.evaluator.x = x * 2 - 1 self.evaluator.y = y * 2 - 1 blackbox_answer = self.blackbox_solver.solve( self.evaluator, 3, cluster_num=10, min_iter_inner=self.blackbox_parameter, max_iter_outer=self.blackbox_parameter, unchanged_threshold=self.blackbox_parameter, max_unchanged_objective_outer=self.blackbox_parameter, max_unchanged_objective_inner=self.blackbox_parameter, unchanged_best_threshold=self.blackbox_parameter, verbose=0) # rebuild 0/1 booleans [a,b,h] from [x,y,g] blackbox_answer_bin = [(item + 1) / 2 for item in blackbox_answer] # the answer we desire is the third value in the list [a,b,h] return blackbox_answer_bin[2]
class SATSolver(object): def __init__(self, filename): self.clauses = [] self.process_file(filename) solver = local_connection.get_solver('c4-sw_optimize') self.blackbox_solver = BlackBoxSolver(solver) self.blackbox_parameter = 10 self.gamma = 99999 def process_file(self, filename): file = open(filename, 'r') # retain the clauses as lists of pairs (var_index, value=+/-1) for line in file: tokens = line.split() self.clauses.append([(int(tokens[2 * i]), int(tokens[2 * i + 1])) for i in range(len(tokens) / 2)]) file.close() # determine the number of variables in our list as the largest var index self.nvars = 0 for clause in self.clauses: for pair in clause: if pair[0] > self.nvars: self.nvars = pair[0] self.nvars += 1 def __call__(self, states, num_states): ret = [] for i in range(num_states): # retrieve a candidate solution state = states[i * self.nvars:(i + 1) * self.nvars] # the product of (x_i + x_i+) sat_value = 1 # each clause is a list of (index, x_index+) pairs for clause in self.clauses: clause_sum = 0 # each pair contributes to the satisfiability measure for pair in clause: # pair[1] = x_index+, state[pair[0]] = state[index] = x_index clause_sum += (state[pair[0]] + pair[1])**2 sat_value *= clause_sum # final objective value ret.append(self.gamma - sat_value) return tuple(ret) def solve(self): blackbox_answer = self.blackbox_solver.solve( self, self.nvars, cluster_num=10, min_iter_inner=self.blackbox_parameter, max_iter_outer=self.blackbox_parameter, unchanged_threshold=self.blackbox_parameter, max_unchanged_objective_outer=self.blackbox_parameter, max_unchanged_objective_inner=self.blackbox_parameter, unchanged_best_threshold=self.blackbox_parameter, verbose=0) energy = self.__call__(blackbox_answer, 1)[0] return (energy != self.gamma), blackbox_answer
def solve(self): solver = local_connection.get_solver('c4-sw_optimize') blackbox_parameter = 10 blackbox_solver = BlackBoxSolver(solver) if debug: print 'contacting super black box...' blackbox_answer = blackbox_solver.solve(self, 2 * SIZE, cluster_num = 10, \ min_iter_inner = blackbox_parameter, max_iter_outer= blackbox_parameter, \ unchanged_threshold=blackbox_parameter, max_unchanged_objective_outer=blackbox_parameter, \ max_unchanged_objective_inner = blackbox_parameter, \ unchanged_best_threshold = blackbox_parameter, verbose=0) blackbox_answer_bin = array([(item + 1) / 2 for item in blackbox_answer]) a, b = (binaryToInt(blackbox_answer[0:SIZE]), binaryToInt(blackbox_answer[SIZE:])) if debug: print 'The best bit string we found was:', blackbox_answer_bin print 'the factors are ', a, ' and ', b return (a, b)
class WMISIsingSolver(object): def __init__(self, graph): self.graph = graph self.h, self.J = self.graph.get_data() solver = local_connection.get_solver('c4-sw_optimize') self.blackbox_solver = BlackBoxSolver(solver) self.blackbox_parameter = 1 self.cluster_parameter = 2 def __call__(self, states, num_states): ret = [] state_len = len(states) / num_states for i in range(num_states): state = states[i * state_len:(i + 1) * state_len] # this is the objective function value obj_value = dot(self.h, state) + dot(state, dot(self.J, state)) ret.append(obj_value) return tuple(ret) def compute(self): blackbox_answer = self.blackbox_solver.solve( self, self.graph.nnodes, cluster_num=self.cluster_parameter, min_iter_inner=self.blackbox_parameter, max_iter_outer=self.blackbox_parameter, unchanged_threshold=self.blackbox_parameter, max_unchanged_objective_outer=self.blackbox_parameter, max_unchanged_objective_inner=self.blackbox_parameter, unchanged_best_threshold=self.blackbox_parameter, verbose=0) selected_nodes = filter(lambda node: blackbox_answer[node.index] == 1, self.graph.nodes) total_weight = reduce(lambda x, node: x + node.weight, selected_nodes, 0) return selected_nodes, total_weight
#Here we create the tiles. We only need to do this once. for i in range(len(tile_list)): build_a_piece(tile_list[i], i, tile_list_string) row_map = compute_row_map(n) valid_edges = compute_valid_edges(n, row_map) #This code allows you to test the visualization routine with a "fake" bitstring #mismatches = calculate_mismatches(test_bitstring, tile_list, valid_edges, n) #visualize_board(test_bitstring, tile_list_string, n) #Now we use Blackbox solver to provide the bitstring definition. mismatch_function = MismatchFunction(tile_list, valid_edges, n) blackbox_solver = BlackBoxSolver(solver) blackbox_answer = blackbox_solver.solve(mismatch_function, num_vars, cluster_num = 2, \ min_iter_inner = blackbox_parameter, \ max_iter_outer= blackbox_parameter, \ unchanged_threshold=blackbox_parameter, \ max_unchanged_objective_outer=blackbox_parameter, \ max_unchanged_objective_inner = blackbox_parameter, \ unchanged_best_threshold = blackbox_parameter, verbose=1) print blackbox_answer blackbox_answer_bin = [(item + 1) / 2 for item in blackbox_answer ] # converting to 0/1 from -1/+1 print "The best bit string we found was:", blackbox_answer_bin print "The number of mismatches is:", calculate_mismatches( blackbox_answer_bin, tile_list, valid_edges, n) print "The number of bad tiles is:", calculate_bad_tile_choice(
def convert_answer(self, bb_answer): bin = [(x + 1) / 2 for x in bb_answer] theta0 = self.as_float32(bin[:32]) theta1 = self.as_float32(bin[32:64]) theta2 = self.as_float32(bin[64:96]) theta3 = self.as_float32(bin[96:128]) # theta4 = self.as_float32(bin[128:]) return [theta0, theta1, theta2, theta3] solver = local_connection.get_solver('c4-sw_optimize') blackbox_parameter = 10 obj = QuantumLR("data.txt") blackbox_solver = BlackBoxSolver(solver) print 'contacting super black box...' blackbox_answer = blackbox_solver.solve(obj, obj.stateLen) print blackbox_answer theta = obj.convert_answer(blackbox_answer) print theta print obj.totalCost(theta) r = arange(0, 100, 1) x = linspace(30, 100, 100) y = linspace(30, 100, 100) X, Y = meshgrid(x, y) F = -1.121 + 3.0517576306010596e-05 * X + 0.00390625 * Y + 0.00024414061044808477 * X * Y
else: # the total length of included edges evaluation = dot(edge_lengths, w) # vertices must be present in the selected edge list exactly twice # penalize any non-conforming paths for line in self.index_matrix: evaluation += (self.gamma * (dot(line, w) - 2))**2 # penalize any path longer or shorter than exactly one complete tour evaluation += (self.gamma * (path_length - self.npoints))**2 ret.append(evaluation) return tuple(ret) solver = local_connection.get_solver('c4-sw_optimize') blackbox_solver = BlackBoxSolver(solver) evaluator = TSPEvaluator('simple_tsp.in') print 'The points on the map:' for point in evaluator.points: print point print 'Calling Blackbox...' blackbox_parameter = 10 blackbox_answer = blackbox_solver.solve(evaluator,\ len(evaluator.edges),\ cluster_num = 10,\ min_iter_inner = blackbox_parameter,\ max_iter_outer = blackbox_parameter,\ unchanged_threshold = blackbox_parameter,\ max_unchanged_objective_outer = blackbox_parameter,\
def __init__(self): solver = local_connection.get_solver('c4-sw_optimize') self.blackbox_solver = BlackBoxSolver(solver) self.evaluator = NANDEvaluator() self.blackbox_parameter = 10
#Here we create the tiles. We only need to do this once. for i in range(len(tile_list)): build_a_piece(tile_list[i], i, tile_list_string) row_map = compute_row_map(n) valid_edges = compute_valid_edges(n, row_map) #This code allows you to test the visualization routine with a "fake" bitstring #mismatches = calculate_mismatches(test_bitstring, tile_list, valid_edges, n) #visualize_board(test_bitstring, tile_list_string, n) #Now we use Blackbox solver to provide the bitstring definition. mismatch_function = MismatchFunction(tile_list, valid_edges, n) blackbox_solver = BlackBoxSolver(solver) blackbox_answer = blackbox_solver.solve(mismatch_function, num_vars, cluster_num = 2, \ min_iter_inner = blackbox_parameter, \ max_iter_outer= blackbox_parameter, \ unchanged_threshold=blackbox_parameter, \ max_unchanged_objective_outer=blackbox_parameter, \ max_unchanged_objective_inner = blackbox_parameter, \ unchanged_best_threshold = blackbox_parameter, verbose=1) print blackbox_answer blackbox_answer_bin = [(item+1)/2 for item in blackbox_answer] # converting to 0/1 from -1/+1 print "The best bit string we found was:",blackbox_answer_bin print "The number of mismatches is:", calculate_mismatches(blackbox_answer_bin, tile_list, valid_edges, n) print "The number of bad tiles is:", calculate_bad_tile_choice(blackbox_answer_bin, n, verbose=0)/3 print "The number of bad corners is:", calculate_bad_corner_choice(blackbox_answer_bin)/5 print "The number of bad edges is:", calculate_bad_edge_choice(blackbox_answer_bin)/4
result = result + self.bignum * superprod ret.append(result) return tuple(ret) # additional methods if necessary # ... # in the main part of the program superset = [-7, 4, 2, 5, -8, 0, 1] num_vars = len(superset) solver = local_connection.get_solver('c4-sw_optimize') blackbox_solver = BlackBoxSolver(solver) # initialize variables and all other necessary values # ... obj = MyEvaluatorObject(superset, -1) blackbox_parameter = 10 blackbox_answer = blackbox_solver.solve (obj , num_vars , \ cluster_num = 10, \ min_iter_inner = blackbox_parameter , \ max_iter_outer = blackbox_parameter , \ unchanged_threshold = blackbox_parameter ,\ max_unchanged_objective_outer = blackbox_parameter , \ max_unchanged_objective_inner = blackbox_parameter , \ unchanged_best_threshold = 5,\ verbose =0)