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)
def test_graph_partition(self): """ Graph Partition test """ algo = NumPyMinimumEigensolver(self.qubit_op, aux_operators=[]) result = algo.run() x = sample_most_likely(result.eigenstate) # 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)
def test_graph_partition_direct(self): """ Graph Partition Direct test """ algo = ExactEigensolver(self.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)
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)
def test_graph_partition_vqe(self): """ Graph Partition VQE test """ aqua_globals.random_seed = 10598 result = VQE(self.qubit_op, RY(self.qubit_op.num_qubits, depth=5, entanglement='linear'), SPSA(max_trials=300), 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]) # 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)
def _brute_force(self): # use the brute-force way to generate the oracle def bitfield(n, length): result = np.binary_repr(n, length) return [int(digit) for digit in result] # [2:] to chop off the "0b" part nodes = self.num_nodes maximum = 2**nodes minimal_v = np.inf for i in range(maximum): cur = bitfield(i, nodes) how_many_nonzero = np.count_nonzero(cur) if how_many_nonzero * 2 != nodes: # not balanced continue cur_v = graph_partition.objective_value(np.array(cur), self.w) if cur_v < minimal_v: minimal_v = cur_v return minimal_v