def test_cplex_ising_direct(self): algo = get_algorithm_instance('CPLEX.Ising') algo.init_args(self.algo_input.qubit_op, display=0) result = algo.run() self.assertEqual(result['energy'], -20.5) x_dict = result['x_sol'] x = np.array([x_dict[i] for i in sorted(x_dict.keys())]) np.testing.assert_array_equal(maxcut.get_graph_solution(x), [1, 0, 1, 1]) self.assertEqual(maxcut.maxcut_value(x, self.w), 24)
def test_cplex_ising_direct(self): try: algo = CPLEX_Ising(self.algo_input.qubit_op, display=0) result = algo.run() self.assertEqual(result['energy'], -20.5) x_dict = result['x_sol'] x = np.array([x_dict[i] for i in sorted(x_dict.keys())]) np.testing.assert_array_equal(maxcut.get_graph_solution(x), [1, 0, 1, 1]) self.assertEqual(maxcut.maxcut_value(x, self.w), 24) except AquaError as e: self.skipTest(str(e))
def test_cplex_ising_via_run_algorithm(self): params = { 'problem': { 'name': 'ising' }, 'algorithm': { 'name': 'CPLEX.Ising', 'display': 0 } } result = run_algorithm(params, self.algo_input) self.assertEqual(result['energy'], -20.5) x_dict = result['x_sol'] x = np.array([x_dict[i] for i in sorted(x_dict.keys())]) np.testing.assert_array_equal(maxcut.get_graph_solution(x), [1, 0, 1, 1]) self.assertEqual(maxcut.maxcut_value(x, self.w), 24)
def test_qaoa(self, w, p, solutions): self.log.debug('Testing {}-step QAOA with MaxCut on graph\n{}'.format( p, w)) np.random.seed(0) optimizer = get_optimizer_instance('COBYLA') qubitOp, offset = maxcut.get_maxcut_qubitops(w) qaoa = get_algorithm_instance('QAOA.Variational') qaoa.setup_quantum_backend(backend='statevector_simulator', shots=100) qaoa.init_args(qubitOp, 'matrix', p, optimizer) result = qaoa.run() x = maxcut.sample_most_likely(result['eigvecs'][0]) graph_solution = maxcut.get_graph_solution(x) self.log.debug('energy: {}'.format(result['energy'])) self.log.debug('time: {}'.format(result['eval_time'])) self.log.debug('maxcut objective: {}'.format(result['energy'] + offset)) self.log.debug('solution: {}'.format(graph_solution)) self.log.debug('solution objective: {}'.format( maxcut.maxcut_value(x, w))) self.assertIn(''.join([str(int(i)) for i in graph_solution]), solutions)
def test_qaoa(self, w, p, solutions): self.log.debug('Testing {}-step QAOA with MaxCut on graph\n{}'.format( p, w)) np.random.seed(0) backend = Aer.get_backend('statevector_simulator') optimizer = COBYLA() qubitOp, offset = maxcut.get_maxcut_qubitops(w) qaoa = QAOA(qubitOp, optimizer, p, operator_mode='matrix') quantum_instance = QuantumInstance(backend) result = qaoa.run(quantum_instance) x = maxcut.sample_most_likely(result['eigvecs'][0]) graph_solution = maxcut.get_graph_solution(x) self.log.debug('energy: {}'.format(result['energy'])) self.log.debug('time: {}'.format(result['eval_time'])) self.log.debug('maxcut objective: {}'.format(result['energy'] + offset)) self.log.debug('solution: {}'.format(graph_solution)) self.log.debug('solution objective: {}'.format( maxcut.maxcut_value(x, w))) self.assertIn(''.join([str(int(i)) for i in graph_solution]), solutions)