def binary_clustering(feature_vecs, feature_index, **kwargs): h = {} J = {} cluster1 = [] #stores the indices for the first cluster cluster2 = [] #stores the indices for the second cluster #will recognize tabu or hybrid if 'sampler' in kwargs: sampler_type = kwargs['sampler'] else: sampler_type = "tabu" if 'time_limit' in kwargs: time_limit = kwargs['time_limit'] else: time_limit = 20 sampler = TabuSampler() sampler1 = LeapHybridSampler() for i in feature_index: for j in feature_index: if i < j: J[(i, j)] = np.linalg.norm(feature_vecs[i] - feature_vecs[j]) #Now use a sampler to solve it if sampler_type == "tabu": print("Choosing TABU sampler") sampler = TabuSampler() sampleset = sampler.sample_ising(h, J, num_reads=1, timeout=time_limit * 1000) bin_cluster = sampleset.first[0] else: print("Choosing the hybrid sampler") sampler1 = LeapHybridSampler() sampleset = sampler1.sample_ising(h, J, time_limit=time_limit) bin_cluster = sampleset.first[0] # Run the problem on the sampler and print the results for key in bin_cluster: #put in cluster 1 if -1, else 2 if bin_cluster[key] == -1: cluster1.append(key) elif bin_cluster[key] == 1: cluster2.append(key) return cluster1, cluster2
# hybrid solver test with 10 edges, 4 vertices import dimod from dwave.system import LeapHybridSampler import numpy as np h = np.loadtxt('h.txt') J = np.loadtxt('J.txt') sampler = LeapHybridSampler() results = sampler.sample_ising(h, J, num_reads=100) out = open('Outputs.txt', 'w') for campione in results.record: for Nple in campione: out.write(str(Nple) + "\t") out.write("\n") out.close() print(results.first)