Example #1
0
 def setUp(self):
     super().setUp()
     self.seed = 80598
     aqua_globals.random_seed = self.seed
     self.num_nodes = 3
     self.ins = tsp.random_tsp(self.num_nodes)
     self.qubit_op, self.offset = tsp.get_operator(self.ins)
Example #2
0
    def calculate(self, G, cost_matrix, starting_node=0):

        # Create nodes array for the TSP solver in Qiskit
        coords = []
        for node in G.nodes:
            coords.append(G.nodes[node]['pos'])

        tsp_instance = tsp.TspData(name="TSP",
                                   dim=len(G.nodes),
                                   coord=coords,
                                   w=cost_matrix)

        qubitOp, offset = tsp.get_operator(tsp_instance)

        backend = Aer.get_backend('qasm_simulator')
        quantum_instance = QuantumInstance(backend)

        optimizer = COBYLA(maxiter=300, rhobeg=3, tol=1.5)

        # Define Minimum Eigen Solvers
        minimum_eigen_solver = QAOA(quantum_instance=quantum_instance,
                                    optimizer=optimizer,
                                    operator=qubitOp)
        #minimum_eigen_solver = VQE(quantum_instance=quantum_instance, optimizer=optimizer, operator=qubitOp)
        exact_mes = NumPyMinimumEigensolver()

        # Create the optimizers so we can plug our Quadratic Program
        minimum_eigen_optimizer = MinimumEigenOptimizer(minimum_eigen_solver)
        #vqe = MinimumEigenOptimizer(vqe_mes)
        exact = MinimumEigenOptimizer(exact_mes)
        rqaoa = RecursiveMinimumEigenOptimizer(
            min_eigen_optimizer=minimum_eigen_optimizer,
            min_num_vars=1,
            min_num_vars_optimizer=exact)

        # Create QUBO based on qubitOp from the TSP
        qp = QuadraticProgram()
        qp.from_ising(qubitOp, offset, linear=True)

        result = rqaoa.solve(qp)

        if (tsp.tsp_feasible(result.x)):
            z = tsp.get_tsp_solution(result.x)
            print('solution:', z)
            return z
        else:
            print('no solution:', result.x)
            return []
Example #3
0
    def calculate(self, G, cost_matrix, starting_node = 0):

        # Create nodes array for the TSP solver in Qiskit
        coords = []
        for node in G.nodes:
            coords.append(G.nodes[node]['pos'])

        tsp_instance = tsp.TspData(name = "TSP", dim = len(G.nodes), coord = coords, w = cost_matrix)

        qubitOp, offset = tsp.get_operator(tsp_instance)
        print("Qubits needed: ", qubitOp.num_qubits)
        #print(qubitOp.print_details())

        #backend = Aer.get_backend('statevector_simulator')
        backend = Aer.get_backend('qasm_simulator')

        # Use real backend
        #IBMQ.load_account()
        #provider = IBMQ.get_provider('ibm-q')
        #from qiskit.providers.ibmq import least_busy
        #backend = least_busy(provider.backends(filters=lambda x: x.configuration().n_qubits > qubitOp.num_qubits and not x.configuration().simulator ))
        #print(backend.name())

        quantum_instance = QuantumInstance(backend)

        optimizer = SPSA(maxiter=400)
        #optimizer = COBYLA(maxiter=200, rhobeg=0.3, tol=0.1, disp=True)
        ry = TwoLocal(qubitOp.num_qubits, 'ry', 'cz', reps=4, entanglement='full')
        ra = RealAmplitudes(qubitOp.num_qubits, reps=2)
        vqe = VQE(operator=qubitOp, var_form=ry, optimizer=optimizer, quantum_instance=quantum_instance)

        result = vqe.run(quantum_instance)

        x = sample_most_likely(result.eigenstate)

        if(tsp.tsp_feasible(x)):
            z = tsp.get_tsp_solution(x)
            print('solution:', z)
            return z
        else:
            print('no solution:', x)
            return []
Example #4
0
    def calculate(self, G, cost_matrix, starting_node = 0):

        # Create nodes array for the TSP solver in Qiskit
        G.nodes[0]['pos']
        coords = []
        for node in G.nodes:
            coords.append(G.nodes[0]['pos'])

        tsp_instance = tsp.TspData(name = "TSP", dim = len(G.nodes), coord = coords, w = cost_matrix)

        qubitOp, offset = tsp.get_operator(tsp_instance)


        ee = NumPyEigensolver(qubitOp, k=1)
        result = ee.run()


        x = sample_most_likely(result['eigenstates'][0])

        return tsp.get_tsp_solution(x)
Example #5
0
    def calculate(self, G, cost_matrix, starting_node = 0):

        # Create nodes array for the TSP solver in Qiskit
        coords = []
        for node in G.nodes:
            coords.append(G.nodes[node]['pos'])

        tsp_instance = tsp.TspData(name = "TSP", dim = len(G.nodes), coord = coords, w = cost_matrix)

        qubitOp, offset = tsp.get_operator(tsp_instance)
        print("Qubits needed: ", qubitOp.num_qubits)
        #print(qubitOp.print_details())

        #backend = Aer.get_backend('statevector_simulator')
        backend = Aer.get_backend('qasm_simulator')

        # Create QUBO based on qubitOp from the TSP
        qp = QuadraticProgram()
        qp.from_ising(qubitOp, offset, linear=True)

        admm_params = ADMMParameters(
                            rho_initial=1001,
                            beta=1000,
                            factor_c=900,
                            maxiter=100,
                            three_block=True, tol=1.e-6)

        qubo_optimizer = MinimumEigenOptimizer(QAOA(quantum_instance=backend))
        convex_optimizer = CobylaOptimizer()
        admm = ADMMOptimizer(params=admm_params,
                             qubo_optimizer=qubo_optimizer,
                             continuous_optimizer=convex_optimizer)

        quantum_instance = QuantumInstance(backend)

        result = admm.solve(qp)
        print(result)
Example #6
0
    default_axes = plt.axes(frameon=True)
    nx.draw_networkx(G2,
                     node_color=colors,
                     edge_color='b',
                     node_size=600,
                     alpha=.8,
                     ax=default_axes,
                     pos=pos)
    edge_labels = nx.get_edge_attributes(G2, 'weight')
    nx.draw_networkx_edge_labels(G2,
                                 pos,
                                 font_color='b',
                                 edge_labels=edge_labels)


qubitOp, offset = tsp.get_operator(ins)
print('Offset:', offset)
print('Ising Hamiltonian:')
print(qubitOp.print_details())

qp = QuadraticProgram()
qp.from_ising(qubitOp, offset, linear=True)
qp.to_docplex().prettyprint()

result = exact.solve(qp)
print(result)

ee = NumPyMinimumEigensolver(qubitOp)
result = ee.run()

# print('energy:', result.eigenvalue.real)