Example #1
0
    def test_hhl_diagonal(self, vector):
        """ hhl diagonal test """
        self.log.debug(
            'Testing HHL simple test in mode Lookup with statevector simulator'
        )

        matrix = [[1, 0], [0, 1]]

        # run ExactLSsolver
        ref_result = ExactLSsolver(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, 3, False)
        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=5)

        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"])
Example #2
0
 def test_els_direct(self):
     algo = ExactLSsolver(self.algo_input.matrix, self.algo_input.vector)
     result = algo.run()
     np.testing.assert_array_almost_equal(result['solution'], [1, 0])
     np.testing.assert_array_almost_equal(result['eigvals'], [3, -1])
Example #3
0
 def test_els(self):
     """ ELS test """
     algo = ExactLSsolver(self.matrix, self.vector)
     result = algo.run()
     np.testing.assert_array_almost_equal(result['solution'], [1, 0])
     np.testing.assert_array_almost_equal(result['eigvals'], [3, -1])