def test_hhl_negative_eigs(self): self.log.debug('Testing HHL with matrix with negative eigenvalues') neg_params = self.params neg_params['eigs']['num_ancillae'] = 4 neg_params['eigs']['negative_evals'] = True neg_params['reciprocal']['negative_evals'] = True n = 2 np.random.seed(0) matrix = rmg.random_diag(n, eigrange=[-1, 1]) vector = random(n) algo_input = LinearSystemInput() algo_input.matrix = matrix algo_input.vector = vector # run ExactLSsolver ref_result = run_algorithm(self.els_params, algo_input) ref_solution = ref_result['solution'] ref_normed = ref_solution/np.linalg.norm(ref_solution) # run hhl hhl_result = run_algorithm(neg_params, algo_input) hhl_solution = hhl_result["solution"] hhl_normed = hhl_solution/np.linalg.norm(hhl_solution) # compare results fidelity = state_fidelity(ref_normed, hhl_normed) np.testing.assert_approx_equal(fidelity, 1, significant=3) self.log.debug('HHL solution vector: {}'.format(hhl_solution)) self.log.debug('algebraic solution vector: {}'.format(ref_normed)) self.log.debug('fidelity HHL to algebraic: {}'.format(fidelity)) self.log.debug('probability of result: {}'.format(hhl_result["probability_result"]))
def test_hhl_diagonal_other_dim(self, n, num_ancillary): self.log.debug('Testing HHL with matrix dimension other than 2**n') dim_params = self.params dim_params['eigs']['num_ancillae'] = num_ancillary dim_params['eigs']['negative_evals'] = True dim_params['reciprocal']['negative_evals'] = True np.random.seed(0) matrix = rmg.random_diag(n, eigrange=[0, 1]) vector = random(n) algo_input = LinearSystemInput() algo_input.matrix = matrix algo_input.vector = vector # run ExactLSsolver ref_result = run_algorithm(self.els_params, algo_input) ref_solution = ref_result['solution'] ref_normed = ref_solution/np.linalg.norm(ref_solution) # run hhl hhl_result = run_algorithm(dim_params, algo_input) hhl_solution = hhl_result['solution'] hhl_normed = hhl_solution/np.linalg.norm(hhl_solution) # compare result fidelity = state_fidelity(ref_normed, hhl_normed) np.testing.assert_approx_equal(fidelity, 1, significant=1) self.log.debug('HHL solution vector: {}'.format(hhl_solution)) self.log.debug('algebraic solution vector: {}'.format(ref_solution)) self.log.debug('fidelity HHL to algebraic: {}'.format(fidelity)) self.log.debug('probability of result: {}'.format(hhl_result["probability_result"]))
def test_hhl_negative_eigs_sv(self): self.log.debug('Testing HHL with matrix with negative eigenvalues') neg_params = self.params neg_params['eigs']['num_ancillae'] = 4 neg_params['eigs']['negative_evals'] = True neg_params['reciprocal']['negative_evals'] = True n = 2 matrix = rmg.random_diag(n, eigrange=[-1, 1]) vector = random(2) algo_input = LinearSystemInput() algo_input.matrix = matrix algo_input.vector = vector # run hhl result = run_algorithm(neg_params, algo_input) hhl_solution = result["solution_hhl"] hhl_normed = hhl_solution / np.linalg.norm(hhl_solution) # linear algebra solution linalg_solution = np.linalg.solve(matrix, vector) linalg_normed = linalg_solution / np.linalg.norm(linalg_solution) # compare result fidelity = abs(linalg_normed.dot(hhl_normed.conj()))**2 np.testing.assert_approx_equal(fidelity, 1, significant=3) self.log.debug('HHL solution vector: {}'.format(hhl_solution)) self.log.debug('algebraic solution vector: {}'.format(linalg_solution)) self.log.debug('fidelity HHL to algebraic: {}'.format(fidelity)) self.log.debug('probability of result: {}'.format( result["probability_result"]))
def test_hhl_negative_eigs(self): """ hhl negative eigs test """ self.log.debug('Testing HHL with matrix with negative eigenvalues') # The following seed was chosen so as to ensure we get a negative eigenvalue # and in case anything changes we assert this after the random matrix is created aqua_globals.random_seed = 27 n = 2 matrix = rmg.random_diag(n, eigrange=[-1, 1]) vector = aqua_globals.random.random(n) self.assertTrue(np.any(matrix < 0), "Random matrix has no negative values") # run NumPyLSsolver ref_result = NumPyLSsolver(matrix, vector).run() ref_solution = ref_result.solution ref_normed = ref_solution / np.linalg.norm(ref_solution) # run hhl orig_size = len(vector) matrix, vector, truncate_powerdim, truncate_hermitian = HHL.matrix_resize( matrix, vector) # Initialize eigenvalue finding module eigs = TestHHL._create_eigs(matrix, 4, True) num_q, num_a = eigs.get_register_sizes() # Initialize initial state module init_state = QuantumCircuit(num_q) init_state.initialize(vector / np.linalg.norm(vector), range(num_q)) # Initialize reciprocal rotation module reci = LookupRotation(negative_evals=eigs._negative_evals, evo_time=eigs._evo_time) algo = HHL(matrix, vector, truncate_powerdim, truncate_hermitian, eigs, init_state, reci, num_q, num_a, orig_size) hhl_result = algo.run( QuantumInstance(BasicAer.get_backend('statevector_simulator'), seed_simulator=aqua_globals.random_seed, seed_transpiler=aqua_globals.random_seed)) hhl_solution = hhl_result.solution hhl_normed = hhl_solution / np.linalg.norm(hhl_solution) # compare results fidelity = state_fidelity(ref_normed, hhl_normed) np.testing.assert_approx_equal(fidelity, 1, significant=3) self.log.debug('HHL solution vector: %s', hhl_solution) self.log.debug('algebraic solution vector: %s', ref_normed) self.log.debug('fidelity HHL to algebraic: %s', fidelity) self.log.debug('probability of result: %s', hhl_result.probability_result)
def test_hhl_diagonal_other_dim(self, n, num_ancillary): """ hhl diagonal other dim test """ self.log.debug('Testing HHL with matrix dimension other than 2**n') matrix = rmg.random_diag(n, eigrange=[0, 1]) vector = aqua_globals.random.random(n) # run NumPyLSsolver ref_result = NumPyLSsolver(matrix, vector).run() ref_solution = ref_result.solution ref_normed = ref_solution / np.linalg.norm(ref_solution) # run hhl orig_size = len(vector) matrix, vector, truncate_powerdim, truncate_hermitian = HHL.matrix_resize( matrix, vector) # Initialize eigenvalue finding module eigs = TestHHL._create_eigs(matrix, num_ancillary, True) num_q, num_a = eigs.get_register_sizes() # Initialize initial state module with warnings.catch_warnings(): warnings.filterwarnings('ignore', category=DeprecationWarning) init_state = Custom(num_q, state_vector=vector) # Initialize reciprocal rotation module reci = LookupRotation(negative_evals=eigs._negative_evals, evo_time=eigs._evo_time) algo = HHL(matrix, vector, truncate_powerdim, truncate_hermitian, eigs, init_state, reci, num_q, num_a, orig_size) hhl_result = algo.run( QuantumInstance(BasicAer.get_backend('statevector_simulator'), seed_simulator=aqua_globals.random_seed, seed_transpiler=aqua_globals.random_seed)) hhl_solution = hhl_result.solution hhl_normed = hhl_solution / np.linalg.norm(hhl_solution) # compare result fidelity = state_fidelity(ref_normed, hhl_normed) np.testing.assert_approx_equal(fidelity, 1.0, significant=1) self.log.debug('HHL solution vector: %s', hhl_solution) self.log.debug('algebraic solution vector: %s', ref_solution) self.log.debug('fidelity HHL to algebraic: %s', fidelity) self.log.debug('probability of result: %s', hhl_result.probability_result)
def test_hhl_negative_eigs(self): """ hhl negative eigs test """ self.log.debug('Testing HHL with matrix with negative eigenvalues') n = 2 matrix = rmg.random_diag(n, eigrange=[-1, 1]) vector = aqua_globals.random.random_sample(n) # run NumPyLSsolver ref_result = NumPyLSsolver(matrix, vector).run() ref_solution = ref_result['solution'] ref_normed = ref_solution / np.linalg.norm(ref_solution) # run hhl orig_size = len(vector) matrix, vector, truncate_powerdim, truncate_hermitian = HHL.matrix_resize( matrix, vector) # Initialize eigenvalue finding module eigs = TestHHL._create_eigs(matrix, 4, True) num_q, num_a = eigs.get_register_sizes() # Initialize initial state module init_state = Custom(num_q, state_vector=vector) # Initialize reciprocal rotation module reci = LookupRotation(negative_evals=eigs._negative_evals, evo_time=eigs._evo_time) algo = HHL(matrix, vector, truncate_powerdim, truncate_hermitian, eigs, init_state, reci, num_q, num_a, orig_size) hhl_result = algo.run( QuantumInstance(BasicAer.get_backend('statevector_simulator'), seed_simulator=aqua_globals.random_seed, seed_transpiler=aqua_globals.random_seed)) hhl_solution = hhl_result["solution"] hhl_normed = hhl_solution / np.linalg.norm(hhl_solution) # compare results fidelity = state_fidelity(ref_normed, hhl_normed) np.testing.assert_approx_equal(fidelity, 1, significant=3) self.log.debug('HHL solution vector: %s', hhl_solution) self.log.debug('algebraic solution vector: %s', ref_normed) self.log.debug('fidelity HHL to algebraic: %s', fidelity) self.log.debug('probability of result: %s', hhl_result["probability_result"])