importrandomimportnumpyasnpfromdwave.systemimportDWaveSampler,EmbeddingCompositeimportdwave_networkxasdnximportnetworkxasnximportdimod# Number of geolocations (nodes)num_nodes=6# Function to generate random geolocationsdefgenerate_geolocations(num_nodes):geolocations=[(random.uniform(0,100),random.uniform(0,100))for_inrange(num_nodes)]returngeolocations# Function to calculate the Euclidean distance between two pointsdefeuclidean_distance(p1,p2):returnnp.sqrt((p1[0]-p2[0])**2+(p1[1]-p2[1])**2)# Generate geolocationsgeolocations=generate_geolocations(num_nodes)# Create a complete graph with nodes representing geolocationsG=nx.complete_graph(num_nodes)# Add edges with weights as the Euclidean distance between geolocationsfori,p1inenumerate(geolocations):forj,p2inenumerate(geolocations):ifi!=j:G.add_edge(i,j,weight=euclidean_distance(p1,p2))# Set up the QUBO problemlagrange=1.0qubo=dnx.traveling_salesperson_qubo(G,lagrange)# Convert the QUBO to a BQM (Binary Quadratic Model)bqm=dimod.BinaryQuadraticModel.from_qubo(qubo)# Set up the samplersampler=EmbeddingComposite(DWaveSampler())# Solve the TSP problem using D-Wave's quantum annealersampleset=sampler.sample(bqm)# Find the shortest path from the samplesetpath=dnx.traveling_salesperson(G,sampleset)print("Shortest path:",path)print("Total distance:",sampleset.first.energy)
---------------------------------------------------------------------------ModuleNotFoundErrorTraceback (most recent call last)
CellIn[1],line31importrandom2importnumpyasnp----> 3fromdwave.systemimportDWaveSampler,EmbeddingComposite4importdwave_networkxasdnx5importnetworkxasnxModuleNotFoundError: No module named 'dwave'