def ising_ground_truth(cf, info_mtx, fig_save_path=""): print("Running the Ground Truth...") ising_model = Ising_model(cf, info_mtx) dim = info_mtx.shape[0] if cf.pb_type == "maxcut": # 1/2\sum_edges 1-node1*node2 = 1/2*num_edges - 1/2*\sum_edges node1*node2 graph = ising_model.graph start_time = time.time() result = ising.search(graph, num_states=3, show_progress=True, chunk_size=0) end_time = time.time() energy = result.energies[0] state = decode_state(result.states[0], dim, list(range(dim))) num_edges = info_mtx.sum() / 2 score = num_edges / 2 - energy score1 = ising_model(state) if abs((score - score1) / score) > 1e-2: raise ("Mismatched energy - result1={}, result2={}".format( score, score1)) # plot the graph # Laplacian matrices are real and symmetric, so we can use eigh, # the variation on eig specialized for Hermetian matrices. G = laplacian_to_graph(info_mtx) # Laplacian matrices are real and symmetric, so we can use eigh, # the variation on eig specialized for Hermetian matrices. # method from maxCutPy gt.execution_time(mc.local_consistent_max_cut, 1, G) score1 = gc.cut_edges(G) if abs((score - score1)) != 0: raise ("Mismatched maxcut - result1={}, result2={}".format( score, score1)) nbunch = G.nodes() for i in nbunch: G.node[i]['partition'] = state[i] gd.draw_cut_graph(G) plt.savefig(fig_save_path) plt.close() if cf.pb_type == "spinglass": # \sum_edges J_ij*node1*node2 graph = ising_model.graph start_time = time.time() result = ising.search(graph, num_states=3, show_progress=True, chunk_size=0) end_time = time.time() energy = result.energies[0] state = decode_state(result.states[0], dim, list(range(dim))) energy1 = ising_model(state) if abs((energy - energy1) / energy) > 1e-2: raise ("Mismatched energy - result1={}, result2={}".format( energy, energy1)) score = energy time_elapsed = end_time - start_time return score, state, time_elapsed
def main(no_bits, num_states, log_level): """Entry point of this script.""" # pylint: disable=invalid-name logging.basicConfig(level=log_level, format=LOGFORMAT) logger = logging.getLogger('testrun') Jh = random.rand(no_bits, no_bits) * 10 - 5 # pylint: disable=no-member Jh = Jh + Jh.T logger.info('Running pure Python implementation') py_result = search_py(Jh, num_states) logger.info('Running ising GPU implementation') gpu = ising.search(Jh, num_states=num_states, method='GPU', energies_only=True, chunk_size=no_bits) logger.info('Running ising CPU implementation') cpu = ising.search(Jh, num_states=num_states, method='CPU', energies_only=True, chunk_size=no_bits - 2) sep = '+' + '+'.join(20 * '-' for _ in range(3)) + '+' print(sep) print('|{0:20}|{1:20}|{2:20}|'.format(' Python', ' ising (GPU)', ' ising (CPU)')) print(sep) for py_en, gpu_en, cpu_en in zip(next(iter(py_result)), gpu.energies, cpu.energies): print('|{0:20.5f}|{1:20.5f}|{2:20.5f}|'.format(py_en, gpu_en, cpu_en)) print(num_states)
def main(num_spins, num_states, omp_threads): """An entry point of the script.""" graph = [] for i in range(num_spins): for j in range(i, num_spins): graph.append((i, j, random.rand() * 10 - 5)) print('Running ising GPU implementation') gpu = ising.search(graph, num_states=num_states, method='GPU', show_progress=True, chunk_size=20) print('Running ising CPU implementation') cpu = ising.search(graph, num_states=num_states, method='CPU', show_progress=True, chunk_size=20, omp_threads=omp_threads) sep = '+' + '+'.join(20 * '-' for _ in range(2)) + '+' print('\n' + sep) print('|{0:20}|{1:20}|'.format(' ising.search (GPU)', ' ising.search (CPU)')) print(sep) for gpu_en, cpu_en in zip(gpu.energies, cpu.energies): print('|{0:20.5f}|{1:20.5f}|'.format(gpu_en, cpu_en))
def main(num_spins, num_states, omp_threads): """An entry point of the script.""" graph = random.rand(num_spins, num_spins) * 10 - 5 # pylint: disable=no-member print('Running search with CPU implementation - computing only energies.') result_no_states = ising.search(graph, num_states=num_states, method='CPU', show_progress=True, energies_only=True, omp_threads=omp_threads, chunk_size=20) print( 'Running search with CPU implementation - computing both states and energies.' ) result_with_states = ising.search(graph, num_states=num_states, method='CPU', show_progress=True, chunk_size=20, omp_threads=omp_threads) sep = '+' + '+'.join(20 * '-' for _ in range(2)) + '+' print('\n' + sep) print('|{0:20}|{1:20}|'.format(' ising.search (GPU)', ' ising.search (CPU)')) print(sep) for energy1, energy2 in zip(result_no_states.energies, result_with_states.energies): print('|{0:20.5f}|{1:20.5f}|'.format(energy1, energy2))
def main(num_spins, num_states, omp_threads): """An entry point of the script.""" graph = random.rand(num_spins, num_spins) * 10 - 5 # pylint: disable=no-member print('Running search with GPU implementation.') gpu = ising.search(graph, num_states=num_states, method='GPU', # chunk_size=26, energies_only=False, show_progress=True) print('Running search with CPU implementation.') cpu = ising.search(graph, num_states=num_states, method='CPU', show_progress=True, chunk_size=27, energies_only=False, omp_threads=omp_threads) sep = '+' + '+'.join(20 * '-' for _ in range(2)) + '+' print('\n' + sep) print('|{0:20}|{1:20}|'.format(' ising.search (GPU)', ' ising.search (CPU)')) print(sep) for gpu_en, cpu_en in zip(gpu.energies, cpu.energies): print('|{0:20.5f}|{1:20.5f}|'.format(gpu_en, cpu_en))
def main(num_spins, num_states, omp_threads): """An entry point of the script.""" graph = random.rand(num_spins, num_spins) * 10 - 5 # pylint: disable=no-member print('Running ising.search CPU implementation') cpu = ising.search(graph, num_states=num_states, method='CPU', show_progress=False, energies_only=False, omp_threads=omp_threads) sep = '+' + 22 * '-' + '+' print('\n' + sep) print('| {0:20} |'.format('Energy')) print(sep) for cpu_en in islice(cpu.energies, 10): print('|{0:22.5f}|'.format(cpu_en))
def main(num_spins, num_states): """An entry point of the script.""" graph = random.rand(num_spins, num_spins) * 10 - 5 # pylint: disable=no-member print('Running search with GPU implementation.') gpu = ising.search(graph, num_states=num_states, method='GPU', energies_only=False, show_progress=True) sep = '+' + 20 * '-' + '+' print('\n' + sep) print('|{0:20}|'.format(' Energy')) print(sep) for gpu_en in gpu.energies: print('|{0:20.5f}|'.format(gpu_en))
def plot_graph(cf, L, save_path): if cf.pb_type == "maxcut": seed = cf.random_seed # Laplacian matrices are real and symmetric, so we can use eigh, # the variation on eig specialized for Hermetian matrices. # method from maxCutPy time = gt.execution_time(mc.local_consistent_max_cut, 1, G) result = gc.cut_edges(G) return time, result if cf.pb_type == "spinglass": def decode_state(state_repr, no_spins, labels): state = {} for i in range(no_spins): state[labels[no_spins - (i + 1)]] = 1 if state_repr % 2 else -1 state_repr //= 2 return state def check(dic, graph): total = 0 for edge, energy in graph.items(): total += energy * dic[edge[0]] * dic[edge[1]] return total J = L graph = {} shape = J.shape size = int(shape[0] * shape[1]) for i in range(shape[0]): for j in range(shape[1]): graph[(i, j)] = J[i, j] result = ising.search(graph, num_states=4) print(result.energies, result.states) print(decode_state(result.states[0], size, list(range(size)))) print( check(decode_state(result.states[0], size, list(range(size))), graph))
def main(): graph = {(1, 1): -1, (1, 2): -0.2} solution = search(graph, num_states=4) print('Energy | State') for energy, state in zip(solution.energies, solution.states): print('{0:6.2f} | {1}'.format(energy, state))