def test_show_no_block(self): # exclude potential server start-up time from timing tests below app_server.ensure_started() # show shouldn't block regardless of problem inspector opening or not with self.assertMaxRuntime(2000): show(self.response, block=Block.NEVER)
def visualize(self): "Run the D-Wave Problem Inspector on the first raw result." try: inspector.show(self.raw_results[0]) except: self.problem.qmasm.abend( 'Failed to run the D-Wave Problem Inspector')
def test_show_block_timeout(self): # exclude potential server start-up time from timing tests below app_server.ensure_started() # show blocks until timeout with self.assertMaxRuntime(2000): show(self.response, block=Block.ONCE, timeout=1) with self.assertMaxRuntime(2000): show(self.response, block=True, timeout=1)
def test_show_block_once(self): # exclude potential server start-up time from timing tests below app_server.ensure_started() # show should block until first access with ThreadPoolExecutor(max_workers=1) as executor: with self.assertMaxRuntime(2000): # notify the inspector server the `problem_id` has been "viewed" by # our mock browser/viewer (async) fut = executor.submit(self.notify_problem_loaded, problem_id=self.problem_id) show(self.response, block=Block.ONCE) fut.result()
def dwave_single_run(self): """Function to run the problem with the dwave hardware. Args: H (symbol): hamiltonian where the solution is encoded. sym (list): symbols to perfom the substitution. Returns: best_sample (list): bit positions of the best found solution. best_energy (float): energy value associated to the found minimum. """ response = self.run_dwave(self.H, self.sym) best_sample, best_energy = self.get_energies() if self.inspect: insp.show(response) if best_energy == 0: print("Solution found!\n") return best_sample, best_energy
nodes = ['w', 'x', 'y', 'z'] # ------- Set up our QUBO dictionary ------- # Initialize our Q matrix Q = defaultdict(int) # Update Q matrix for every edge in the graph # for i, j in G.edges: # Q[(i,i)]+= -1 # Q[(j,j)]+= -1 # Q[(i,j)]+= 2 Q[('x', 'x')] = -3 Q[('x', 'y')] = 4.2 Q[('y', 'z')] = 11 Q[('y', 'y')] = 6.9 Q[('x', 'z')] = -4 Q[('w', 'w')] = 8 Q[('x', 'w')] = 2 # 3. Instantiate a solver sampler = EmbeddingComposite(DWaveSampler(solver={'qpu': True})) # 4. Solve the problem sampleset = sampler.sample_qubo(Q, chain_strength=8, num_reads=100) inspector.show(sampleset) # 5. Interpret the results - print the results with the lowest energy print(sampleset.lowest())
if neighbours == None: n_vertices, neighbours = load_problem(filename) # 2. Define problem h = [0 for x in range(n_vertices)] J = dict((tuple(neighbour), 10) for neighbour in neighbours) # print(J) # 3. Instantiate solver sampler = EmbeddingComposite(DWaveSampler(token=token)) # 4. Sample problem solution = sampler.sample_ising(h, J, chain_strength=50, num_reads=50) # 5. Use response best_solution = [int(solution.first.sample[x]) for x in range(n_vertices)] # print( best_solution ) if local: return solution else: return {'solution': best_solution} if __name__ == "__main__": insp.show(main('', local=True))
J_PeriodicBC[(0, num_qubits - 1)] = fm_coupler_strength # J_PeriodicBC[(num_qubits-1,0)] = fm_coupler_strength # HW 3.e Chimera graph J_Chimera = {} left = [i for i in range(num_qubits) if not i % 2] right = [i for i in range(num_qubits) if i % 2] for i in left: for j in right: J_Chimera[(i, j)] = fm_coupler_strength # HW 3.f Chimera graph #J_Chimera[(0,2)] = fm_coupler_strength # HW 3.g Clique J_Clique = {} qubit_list = list(range(num_qubits)) for i in range(num_qubits): qubit_list.remove(i) for j in qubit_list: J_Clique[(i, j)] = fm_coupler_strength # Submit the problem to the QPU sampler = EmbeddingComposite(DWaveSampler(solver={'qpu': True})) response = sampler.sample_ising(h, J_Chimera, num_reads=num_reads) # Show the problem visualization on the QPU inspector.show(response) print("QPU response") print(response)
def dwave_iterative(self, iterations, threshold = 10, inspect=False): """Iterative method that fixes qubits that are thought to be found in the best position. Args: iterations (int): number of repetitions for the ancilla fixing. threshold (int): number of low energy solutions to check for same value. Returns: result (list): reconstructed best solution found after iterating. best_energy (float): energy of the system using the result output. w (int): iteration when the solution is first found. """ bits = self.bits H = self.H sym = list(self.sym) fix = [] out = [] for w in range(iterations): response = self.run_dwave(H, self.sym) if inspect: insp.show(response) record = response.record order = np.argsort(record['energy']) best_sample, best_energy = self.get_energies(response) if best_energy == 0: print(f'Solution found! Final iteration: {w}\n') break print('Beginning fixing variables with the same value:\n') c = bits for j in range(bits, len(self.sym)): if j in fix: continue else: a = True b = record.sample[order[0]][c] for k in range(min(len(record.sample), threshold)): if b != record.sample[order[k]][c]: a = False break if a: fix.append(j) out.append(b) c += 1 print(f'The same value was found in positions {fix} \n') print(f'with values {out}.\n') for j in range(len(fix)): H = H.subs(self.sym[fix[j]], out[j]) #sym.pop(fix[j]) print(f'Total number of qubits needed for the next step: {len(self.sym)-len(fix)}.\n') print('Reconstructing state...\n') result = [] c = 0 for i in range(len(self.sym)): if i in fix: result.append(out[fix.index(i)]) else: result.append(best_sample[c]) c += 1 print(f'Reconstructed result:\n') print(f'Relevant bits: {result[:bits]}\n') print(f'Ancillas: {result[bits:]}\n') best_energy = self.H.subs(((self.sym[i], result[i]) for i in range(len(self.sym)))) print(f'With total energy: {best_energy}\n') return result, best_energy, w
if want_Chimera: topology = 'chimera' dwave_sampler = DWaveSampler(solver={'topology__type': topology}) # To use 2000Q QPU else: topology = 'pegasus' dwave_sampler = DWaveSampler(solver={'topology__type': topology}) # To use Advantage QPU import time start_time = time.time() computation = EmbeddingComposite(dwave_sampler).sample_qubo(quboDict, label='Test Topology: {0}, Nodes: {1}, ' 'Num_reads: {2}'.format(topology, NUM_NODES, num_reads), num_reads=num_reads) print('Execution time for {0} nodes: {1} milliseconds'.format(NUM_NODES, (time.time() - start_time)*1000)) # Print results # print("Solution: sample={.samples.first}".format(final_state)) # print(computation) print(computation.first.energy) show(computation) with open('resultsQuantum{0}.csv'.format(topology), 'w') as file_CSV: writer = csv.writer(file_CSV) rowData = ['Energy: {0}'.format(computation.first.energy), 'Lowest energy solution: {0}'.format(computation.first)] writer.writerow(rowData) # show(quboDict,computation,sampler)
""" Follow the four scenarios listed in task 1 by changing the variables in this file qubit_1 qubit_2 O -------- O h_1, J_val, h_2 """ qubit_1 = 0 qubit_2 = 1 h_1 = 0 h_2 = 0 J_val = -1 h = {qubit_1: h_1, qubit_2: h_2} J = {(qubit_1, qubit_2): J_val} sampler = EmbeddingComposite(DWaveSampler(solver=dict(qpu=True))) response = sampler.sample_ising(h, J, num_reads=1000, num_spin_reversal_transforms=0) print(response.aggregate()) dwi.show(response)