Ejemplo n.º 1
0
 def test_tsp(self):
     """ TSP test """
     algo = NumPyMinimumEigensolver(self.qubit_op)
     result = algo.run()
     x = sample_most_likely(result.eigenstate)
     order = tsp.get_tsp_solution(x)
     np.testing.assert_array_equal(order, [1, 2, 0])
Ejemplo n.º 2
0
 def test_tsp(self):
     """ TSP test """
     algo = NumPyMinimumEigensolver(self.qubit_op)
     result = algo.run()
     x = sample_most_likely(result.eigenstate)
     # print(self.qubit_op.to_opflow().eval(result.eigenstate).adjoint().eval(result.eigenstate))
     order = tsp.get_tsp_solution(x)
     np.testing.assert_equal(tsp.tsp_value(order, self.ins.w),
                             tsp.tsp_value([1, 2, 0], self.ins.w))
Ejemplo n.º 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)

        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 []
Ejemplo n.º 4
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 []
Ejemplo n.º 5
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)
Ejemplo n.º 6
0
        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)
<<<<<<< HEAD
        x = sample_most_likely(result.x)
=======
        x = sample_most_likely(result.eigenstate)
>>>>>>> 6209be83266d7276df8be29b451094c86b1097f9

        if(tsp.tsp_feasible(x)):
            z = tsp.get_tsp_solution(x)
            print('solution:', z)
            return z
        else:
            print('no solution:', x)
            return []


    def iterations(_eval_count, param_set, means, estimator_error):
        print(_eval_count)
        print(param_set)
        print(means)
        print(estimator_error)
Ejemplo n.º 7
0
 def test_tsp_get_solution(self):
     """ Test tsp.get_tsp_solution()"""
     feasible = [1, 0, 0, 0, 1, 0, 0, 0, 1]
     self.assertListEqual(tsp.get_tsp_solution(feasible), [0, 1, 2])
     infeasible = [1, 0, 0, 1, 1, 0, 0, 0, 0]
     self.assertListEqual(tsp.get_tsp_solution(infeasible), [[0, 1], 1, []])