Example #1
0
    def test_qaoa(self, w, prob, m, solutions):
        """ QAOA test """
        seed = 0
        aqua_globals.random_seed = seed
        os.environ.pop('QISKIT_AQUA_CIRCUIT_CACHE', None)
        self.log.debug('Testing %s-step QAOA with MaxCut on graph\n%s', prob,
                       w)

        backend = BasicAer.get_backend('statevector_simulator')
        optimizer = COBYLA()
        qubit_op, offset = max_cut.get_qubit_op(w)

        qaoa = QAOA(qubit_op, optimizer, prob, mixer=m)
        # TODO: cache fails for QAOA since we construct the evolution circuit via instruction
        quantum_instance = QuantumInstance(backend,
                                           circuit_caching=False,
                                           seed_simulator=seed,
                                           seed_transpiler=seed)

        result = qaoa.run(quantum_instance)
        x = sample_most_likely(result['eigvecs'][0])
        graph_solution = max_cut.get_graph_solution(x)
        self.log.debug('energy:             %s', result['energy'])
        self.log.debug('time:               %s', result['eval_time'])
        self.log.debug('maxcut objective:   %s', result['energy'] + offset)
        self.log.debug('solution:           %s', graph_solution)
        self.log.debug('solution objective: %s', max_cut.max_cut_value(x, w))
        self.assertIn(''.join([str(int(i)) for i in graph_solution]),
                      solutions)
        if quantum_instance.has_circuit_caching:
            self.assertLess(quantum_instance._circuit_cache.misses, 3)
Example #2
0
    def test_qaoa(self, w, prob, m, solutions):
        """ QAOA test """
        seed = 0
        aqua_globals.random_seed = seed
        self.log.debug('Testing %s-step QAOA with MaxCut on graph\n%s', prob,
                       w)

        backend = BasicAer.get_backend('statevector_simulator')
        optimizer = COBYLA()
        qubit_op, offset = max_cut.get_qubit_op(w)

        qaoa = QAOA(qubit_op, optimizer, prob, mixer=m)
        quantum_instance = QuantumInstance(backend,
                                           seed_simulator=seed,
                                           seed_transpiler=seed)

        result = qaoa.run(quantum_instance)
        x = sample_most_likely(result['eigvecs'][0])
        graph_solution = max_cut.get_graph_solution(x)
        self.log.debug('energy:             %s', result['energy'])
        self.log.debug('time:               %s', result['eval_time'])
        self.log.debug('maxcut objective:   %s', result['energy'] + offset)
        self.log.debug('solution:           %s', graph_solution)
        self.log.debug('solution objective: %s', max_cut.max_cut_value(x, w))
        self.assertIn(''.join([str(int(i)) for i in graph_solution]),
                      solutions)
Example #3
0
    def test_graph_partition_vqe(self):
        """ Graph Partition VQE test """
        algorithm_cfg = {
            'name': 'VQE',
            'max_evals_grouped': 2
        }

        optimizer_cfg = {
            'name': 'SPSA',
            'max_trials': 300
        }

        var_form_cfg = {
            'name': 'RY',
            'depth': 5,
            'entanglement': 'linear'
        }

        params = {
            'problem': {'name': 'ising', 'random_seed': 10598},
            'algorithm': algorithm_cfg,
            'optimizer': optimizer_cfg,
            'variational_form': var_form_cfg
        }
        backend = BasicAer.get_backend('statevector_simulator')
        result = run_algorithm(params, self.algo_input, backend=backend)
        x = sample_most_likely(result['eigvecs'][0])
        # check against the oracle
        ising_sol = graph_partition.get_graph_solution(x)
        np.testing.assert_array_equal(ising_sol, [0, 1, 0, 1])
        oracle = self._brute_force()
        self.assertEqual(graph_partition.objective_value(x, self.w), oracle)
Example #4
0
    def test_clique_vqe(self):
        """ VQE Clique test """
        algorithm_cfg = {
            'name': 'VQE',
            'max_evals_grouped': 2
        }

        optimizer_cfg = {
            'name': 'COBYLA'
        }

        var_form_cfg = {
            'name': 'RY',
            'depth': 5,
            'entanglement': 'linear'
        }

        params = {
            'problem': {'name': 'ising', 'random_seed': 10598},
            'algorithm': algorithm_cfg,
            'optimizer': optimizer_cfg,
            'variational_form': var_form_cfg
        }
        backend = BasicAer.get_backend('statevector_simulator')
        result = run_algorithm(params, self.algo_input, backend=backend)
        x = sample_most_likely(result['eigvecs'][0])
        ising_sol = clique.get_graph_solution(x)
        np.testing.assert_array_equal(ising_sol, [1, 1, 1, 1, 1])
        oracle = self._brute_force()
        self.assertEqual(clique.satisfy_or_not(ising_sol, self.w, self.k), oracle)
Example #5
0
    def test_exact_cover_vqe(self):
        """ Exact Cover VQE test """
        algorithm_cfg = {'name': 'VQE', 'max_evals_grouped': 2}

        optimizer_cfg = {'name': 'COBYLA'}

        var_form_cfg = {'name': 'RYRZ', 'depth': 5}

        params = {
            'problem': {
                'name': 'ising',
                'random_seed': 10598
            },
            'algorithm': algorithm_cfg,
            'optimizer': optimizer_cfg,
            'variational_form': var_form_cfg
        }
        backend = BasicAer.get_backend('statevector_simulator')
        result = run_algorithm(params, self.algo_input, backend=backend)
        x = sample_most_likely(result['eigvecs'][0])
        ising_sol = exact_cover.get_solution(x)
        oracle = self._brute_force()
        self.assertEqual(
            exact_cover.check_solution_satisfiability(ising_sol,
                                                      self.list_of_subsets),
            oracle)
Example #6
0
    def test_vertex_cover_vqe(self):
        """ Vertex Cover VQE test """
        algorithm_cfg = {'name': 'VQE', 'max_evals_grouped': 2}

        optimizer_cfg = {'name': 'SPSA', 'max_trials': 200}

        var_form_cfg = {
            'name': 'RYRZ',
            'depth': 3,
        }

        params = {
            'problem': {
                'name': 'ising',
                'random_seed': self.seed
            },
            'algorithm': algorithm_cfg,
            'optimizer': optimizer_cfg,
            'variational_form': var_form_cfg
        }
        backend = BasicAer.get_backend('qasm_simulator')
        result = run_algorithm(params, self.algo_input, backend=backend)
        x = sample_most_likely(result['eigvecs'][0])
        sol = vertex_cover.get_graph_solution(x)
        oracle = self._brute_force()
        self.assertEqual(np.count_nonzero(sol), oracle)
Example #7
0
 def test_set_packing_direct(self):
     """ set packing direct test """
     algo = ExactEigensolver(self.algo_input.qubit_op, k=1, aux_operators=[])
     result = algo.run()
     x = sample_most_likely(result['eigvecs'][0])
     ising_sol = set_packing.get_solution(x)
     np.testing.assert_array_equal(ising_sol, [0, 1, 1])
     oracle = self._brute_force()
     self.assertEqual(np.count_nonzero(ising_sol), oracle)
Example #8
0
 def test_clique_direct(self):
     """ Clique Direct test """
     algo = ExactEigensolver(self.algo_input.qubit_op, k=1, aux_operators=[])
     result = algo.run()
     x = sample_most_likely(result['eigvecs'][0])
     ising_sol = clique.get_graph_solution(x)
     np.testing.assert_array_equal(ising_sol, [1, 1, 1, 1, 1])
     oracle = self._brute_force()
     self.assertEqual(clique.satisfy_or_not(ising_sol, self.w, self.k), oracle)
Example #9
0
 def test_graph_partition_direct(self):
     """ Graph Partition Direct test """
     algo = ExactEigensolver(self.algo_input.qubit_op, k=1, aux_operators=[])
     result = algo.run()
     x = sample_most_likely(result['eigvecs'][0])
     # check against the oracle
     ising_sol = graph_partition.get_graph_solution(x)
     np.testing.assert_array_equal(ising_sol, [0, 1, 0, 1])
     oracle = self._brute_force()
     self.assertEqual(graph_partition.objective_value(x, self.w), oracle)
Example #10
0
 def test_vertex_cover_direct(self):
     """ Vertex Cover Direct test """
     algo = ExactEigensolver(self.algo_input.qubit_op,
                             k=1,
                             aux_operators=[])
     result = algo.run()
     x = sample_most_likely(result['eigvecs'][0])
     sol = vertex_cover.get_graph_solution(x)
     np.testing.assert_array_equal(sol, [0, 1, 1])
     oracle = self._brute_force()
     self.assertEqual(np.count_nonzero(sol), oracle)
Example #11
0
 def test_set_packing(self):
     """ set packing test """
     params = {
         'problem': {'name': 'ising'},
         'algorithm': {'name': 'ExactEigensolver'}
     }
     result = run_algorithm(params, self.algo_input)
     x = sample_most_likely(result['eigvecs'][0])
     ising_sol = set_packing.get_solution(x)
     np.testing.assert_array_equal(ising_sol, [0, 1, 1])
     oracle = self._brute_force()
     self.assertEqual(np.count_nonzero(ising_sol), oracle)
Example #12
0
 def test_graph_partition(self):
     """ Graph Partition test """
     params = {
         'problem': {'name': 'ising'},
         'algorithm': {'name': 'ExactEigensolver'}
     }
     result = run_algorithm(params, self.algo_input)
     x = sample_most_likely(result['eigvecs'][0])
     # check against the oracle
     ising_sol = graph_partition.get_graph_solution(x)
     np.testing.assert_array_equal(ising_sol, [0, 1, 0, 1])
     oracle = self._brute_force()
     self.assertEqual(graph_partition.objective_value(x, self.w), oracle)
Example #13
0
 def test_exact_cover_direct(self):
     """ Exact Cover Direct test """
     algo = ExactEigensolver(self.algo_input.qubit_op,
                             k=1,
                             aux_operators=[])
     result = algo.run()
     x = sample_most_likely(result['eigvecs'][0])
     ising_sol = exact_cover.get_solution(x)
     np.testing.assert_array_equal(ising_sol, [0, 1, 1, 0])
     oracle = self._brute_force()
     self.assertEqual(
         exact_cover.check_solution_satisfiability(ising_sol,
                                                   self.list_of_subsets),
         oracle)
Example #14
0
 def test_exact_cover(self):
     """ Exact Cover test """
     params = {
         'problem': {
             'name': 'ising'
         },
         'algorithm': {
             'name': 'ExactEigensolver'
         }
     }
     result = run_algorithm(params, self.algo_input)
     x = sample_most_likely(result['eigvecs'][0])
     ising_sol = exact_cover.get_solution(x)
     np.testing.assert_array_equal(ising_sol, [0, 1, 1, 0])
     oracle = self._brute_force()
     self.assertEqual(
         exact_cover.check_solution_satisfiability(ising_sol,
                                                   self.list_of_subsets),
         oracle)
Example #15
0
    def test_set_packing_vqe(self):
        """ set packing vqe test """
        try:
            from qiskit import Aer
        except Exception as ex:  # pylint: disable=broad-except
            self.skipTest("Aer doesn't appear to be installed. Error: '{}'".format(str(ex)))
            return

        algorithm_cfg = {
            'name': 'VQE',
            'max_evals_grouped': 2
        }

        optimizer_cfg = {
            'name': 'SPSA',
            'max_trials': 200
        }

        var_form_cfg = {
            'name': 'RY',
            'depth': 5,
            'entanglement': 'linear'
        }

        params = {
            'problem': {'name': 'ising', 'random_seed': 100},
            'algorithm': algorithm_cfg,
            'optimizer': optimizer_cfg,
            'variational_form': var_form_cfg
        }
        backend = Aer.get_backend('qasm_simulator')
        result = run_algorithm(params, self.algo_input, backend=backend)
        x = sample_most_likely(result['eigvecs'][0])
        ising_sol = set_packing.get_solution(x)
        oracle = self._brute_force()
        self.assertEqual(np.count_nonzero(ising_sol), oracle)