Пример #1
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)
Пример #2
0
 def test_clique_direct(self):
     """ Clique Direct test """
     algo = ExactEigensolver(self.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)
Пример #3
0
 def test_clique(self):
     """ Clique test """
     algo = NumPyMinimumEigensolver(self.qubit_op, aux_operators=[])
     result = algo.run()
     x = sample_most_likely(result.eigenstate)
     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)
Пример #4
0
 def test_clique(self):
     """ Clique test """
     params = {
         'problem': {'name': 'ising'},
         'algorithm': {'name': 'ExactEigensolver'}
     }
     result = run_algorithm(params, EnergyInput(self.qubit_op))
     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)
Пример #5
0
 def test_clique_vqe(self):
     """ VQE Clique test """
     aqua_globals.random_seed = 10598
     result = VQE(self.qubit_op,
                  RY(self.qubit_op.num_qubits, depth=5, entanglement='linear'),
                  COBYLA(),
                  max_evals_grouped=2).run(
                      QuantumInstance(BasicAer.get_backend('statevector_simulator'),
                                      seed_simulator=aqua_globals.random_seed,
                                      seed_transpiler=aqua_globals.random_seed))
     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)
Пример #6
0
    def _brute_force(self):
        # brute-force way: try every possible assignment!
        def bitfield(n, length):
            result = np.binary_repr(n, length)
            return [int(digit) for digit in result]

        nodes = self.num_nodes  # length of the bitstring that represents the assignment
        maximum = 2**nodes
        has_sol = False
        for i in range(maximum):
            cur = bitfield(i, nodes)
            cur_v = clique.satisfy_or_not(np.array(cur), self.w, self.k)
            if cur_v:
                has_sol = True
                break
        return has_sol