Exemplo n.º 1
0
def main():
   #matrix = [[1, -1/3], [-1/3, 1]]
   matrix = [[15, 9, 5, -3],
            [9, 15, 3, -5],
            [5, 3, 15, -9],
            [-3, -5, -9, 15]]
   #vector = [1, 0]
   vector = [1/2,1/2,1/2,1/2]
   orig_size = len(vector)
   matrix, vector, truncate_powerdim, truncate_hermitian = HHL.matrix_resize(matrix, vector)
# Initialize eigenvalue finding module
   eigs = create_eigs(matrix, 3, 50, 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)
   circuit_drawer(algo.construct_circuit(),output='mpl',filename='./hhlnew.png')
   result = algo.run(QuantumInstance(Aer.get_backend('statevector_simulator')))

   print("Solution:\t\t", np.round(result['solution'], 5))

   result_ref = NumPyLSsolver(matrix, vector).run()
   print("Classical Solution:\t", np.round(result_ref['solution'], 5))

   print("Probability:\t\t %f" % result['probability_result'])
   fidelity(result['solution'], result_ref['solution'])
Exemplo n.º 2
0
    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)
Exemplo n.º 3
0
    def test_hhl_diagonal(self, vector, use_circuit_library):
        """ hhl diagonal test """
        self.log.debug(
            'Testing HHL simple test in mode Lookup with statevector simulator'
        )

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

        # 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, 3, False, use_circuit_library)
        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)
        if not use_circuit_library:
            warnings.filterwarnings('ignore', category=DeprecationWarning)
        hhl_result = algo.run(
            QuantumInstance(BasicAer.get_backend('statevector_simulator'),
                            seed_simulator=aqua_globals.random_seed,
                            seed_transpiler=aqua_globals.random_seed))
        if not use_circuit_library:
            warnings.filterwarnings('always', category=DeprecationWarning)

        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"])
Exemplo n.º 4
0
    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)
Exemplo n.º 5
0
    def test_hhl_diagonal_negative(self, vector):
        """ hhl diagonal negative test """
        self.log.debug(
            'Testing HHL simple test in mode Lookup with statevector simulator'
        )

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

        # 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=5)

        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)
Exemplo n.º 6
0
    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"])
Exemplo n.º 7
0
    def test_hhl_diagonal_qasm(self, vector):
        """ hhl diagonal qasm test """
        self.log.debug('Testing HHL simple test with qasm 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,
                              scale=0.5,
                              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('qasm_simulator'),
                            shots=1000,
                            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=1)

        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"])
Exemplo n.º 8
0
    def test_hhl_non_hermitian(self):
        """ hhl non hermitian test """
        self.log.debug('Testing HHL with simple non-hermitian matrix')

        matrix = [[1, 1], [2, 1]]
        vector = [1, 0]

        # 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, 6, 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 result
        fidelity = state_fidelity(ref_normed, hhl_normed)
        self.assertGreater(fidelity, 0.8)

        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"])
Exemplo n.º 9
0
    def test_hhl_random_hermitian(self):
        """ hhl random hermitian test """
        self.log.debug('Testing HHL with random hermitian matrix')

        n = 2
        matrix = rmg.random_hermitian(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, 4, False)
        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)
        warnings.filterwarnings('ignore', category=DeprecationWarning)
        hhl_result = algo.run(
            QuantumInstance(BasicAer.get_backend('statevector_simulator'),
                            seed_simulator=aqua_globals.random_seed,
                            seed_transpiler=aqua_globals.random_seed))
        warnings.filterwarnings('always', category=DeprecationWarning)

        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)
Exemplo n.º 10
0
    def solve(self, matrix, vector):
        orig_size = len(vector)
        matrix, vector, truncate_powerdim, truncate_hermitian = HHL.matrix_resize(
            matrix, vector)

        # Initialize eigenvalue finding module
        eigs = self.create_eigs(matrix, self.num_ancillae,
                                self.num_time_slices, self.negative_evals)
        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)

        result = algo.run(
            QuantumInstance(self.backend,
                            optimization_level=self.optimization_level))
        return result['solution']
                   expansion_mode='suzuki',
                   expansion_order=2,
                   evo_time=None,
                   negative_evals=negative_evals,
                   ne_qfts=ne_qfts)


def fidelity(hhl, ref):
    solution_hhl_normed = hhl / np.linalg.norm(hhl)
    solution_ref_normed = ref / np.linalg.norm(ref)
    fidelity = state_fidelity(solution_hhl_normed, solution_ref_normed)
    print("fidelity %f" % fidelity)


orig_size = len(vector)
matrix, vector, truncate_powerdim, truncate_hermitian = HHL.matrix_resize(
    matrix, vector)

# Initialize eigenvalue finding module
eigs = 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)
result = algo.run(