Ejemplo n.º 1
0
    def test_hhl_random_hermitian(self):
        self.log.debug('Testing HHL with random hermitian matrix')

        hermitian_params = self.params
        hermitian_params['eigs']['num_ancillae'] = 4

        n = 2
        np.random.seed(0)
        matrix = rmg.random_hermitian(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(hermitian_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=2)

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

        nonherm_params = self.params
        nonherm_params['eigs']['num_ancillae'] = 6
        nonherm_params['eigs']['num_time_slices'] = 80
        nonherm_params['eigs']['negative_evals'] = True
        nonherm_params['reciprocal']['negative_evals'] = True

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

        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(nonherm_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)
        self.assertGreater(fidelity, 0.8)

        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"]))
Ejemplo n.º 3
0
    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"]))
Ejemplo n.º 4
0
    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"]))
Ejemplo n.º 5
0
    def test_hhl_random_hermitian_sv(self):
        self.log.debug('Testing HHL with random hermitian matrix')

        hermitian_params = self.params
        hermitian_params['eigs']['num_ancillae'] = 4

        n = 2
        matrix = rmg.random_hermitian(n, eigrange=[0, 1])
        vector = random(2)

        algo_input = LinearSystemInput()
        algo_input.matrix = matrix
        algo_input.vector = vector

        # run hhl
        result = run_algorithm(hermitian_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=2)

        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"]))
Ejemplo n.º 6
0
    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"]))
Ejemplo n.º 7
0
def build_circuit(matrix, vector_u, vector_p):
    n_io = int(np.log2(np.size(vector_u)))
    # vector_p /= np.linalg.norm(vector_p)
    # Parameters for HHL algorithm
    num_ancillae = 3
    num_time_slices = 50
    params = dict()
    params['problem'] = {'name': 'linear_system'}
    params['backend'] = {
        'provider': 'qiskit.BasicAer',
        'name': 'statevector_simulator'
    }
    params['algorithm'] = {
        'truncate_powerdim': False,
        'truncate_hermitian': False
    }
    params['reciprocal'] = {'name': 'Lookup', 'negative_evals': True}
    params['eigs'] = {
        'expansion_mode': 'suzuki',
        'expansion_order': 2,
        'name': 'EigsQPE',
        'negative_evals': True,
        'num_ancillae': num_ancillae,
        'num_time_slices': num_time_slices
    }
    params['initial_state'] = {'name': 'CUSTOM'}
    params['iqft'] = {'name': 'STANDARD'}
    params['qft'] = {'name': 'STANDARD'}
    algo_input = LinearSystemInput(matrix=matrix, vector=vector_u)
    hhl = HHL.init_params(params, algo_input)
    # Quantum circuit for HHL
    qc = hhl.construct_circuit()
    # HHL solution output register and success bit register
    io = hhl._io_register
    anc1 = hhl._ancilla_register
    # Quantum registers for Swap test
    psas = QuantumRegister(n_io, 'psas')
    anc2 = QuantumRegister(1, 'anc2')
    c1 = ClassicalRegister(1, 'c1')
    c2 = ClassicalRegister(1, 'c2')
    # Add registers to quantum circuit
    qc.add_register(psas, anc2, c1, c2)
    # Initialize vector_p on psas register
    qc.initialize(vector_p, psas)
    # exit(0)
    # Swap test: control swap for dot product
    qc.h(anc2)
    for i in range(n_io):
        qc.cswap(anc2, psas[i], io[i])
    qc.h(anc2)
    # Projection and meassurement
    qc.barrier(anc1)
    qc.barrier(anc2)
    qc.measure(anc1, c1)
    qc.measure(anc2, c2)
    return qc
Ejemplo n.º 8
0
 def test_els_via_run_algorithm(self):
     """ ELS Via Run Algorithm test """
     params = {
         'algorithm': {
             'name': 'ExactLSsolver'
         },
         'problem': {
             'name': 'linear_system'
         }
     }
     result = run_algorithm(params,
                            LinearSystemInput(self.matrix, self.vector))
     np.testing.assert_array_almost_equal(result['solution'], [1, 0])
     np.testing.assert_array_almost_equal(result['eigvals'], [3, -1])
Ejemplo n.º 9
0
 def setUp(self):
     super().setUp()
     self.algo_input = LinearSystemInput()
     self.algo_input.matrix = [[1, 2], [2, 1]]
     self.algo_input.vector = [1, 2]
Ejemplo n.º 10
0
params['eigs'] = {
    'expansion_mode': 'suzuki',
    'expansion_order': 2,
    'name': 'EigsQPE',
    'negative_evals': True,
    'num_ancillae': 2,#6
    'num_time_slices': 2#70
}
params['initial_state'] = {
    'name': 'CUSTOM'
}
params['iqft'] = {
    'name': 'STANDARD'
}
params['qft'] = {
    'name': 'STANDARD'
}

algo_input = LinearSystemInput(matrix=A, vector=b)
hhl = HHL.init_params(params, algo_input)
circ = hhl.construct_circuit()
print(circ)

backend = Aer.get_backend('statevector_simulator')
quantum_instance = QuantumInstance(backend=backend)
result = hhl.run(quantum_instance)
for key in result.keys():
    print(key, result[key])
print("solution ", np.round(result['solution'], 5))