1
$\begingroup$

I am new to Qiskit and I have seen two types of simple 2X2 $Ax=b$ system solutions. I am wondering what the difference is exactly. They both occur on a simulator backend, but the first gives me a Euclidean norm and the second gives me a horrible state vector answer and associated (very low) probability.

code 1

matrix = np.array([[1, -1/3], [-1/3, 1]])
vector = np.array([1, 0])
naive_hhl_solution = HHL().solve(matrix, vector)

code 2

def create_eigs(matrix, num_ancillae, num_time_slices, negative_evals):
    ne_qfts = [None, None]
    if negative_evals:
        num_ancillae += 1
        ne_qfts = [QFT(num_ancillae - 1), QFT(num_ancillae - 1).inverse()]
    
    #Construct the eigenvalues estimation using the PhaseEstimationCircuit
    return EigsQPE(MatrixOperator(matrix=matrix),
                   QFT(num_ancillae).inverse(),
                   num_time_slices=num_time_slices,
                   num_ancillae=num_ancillae,
                   expansion_mode='suzuki',
                   expansion_order=2,
                   evo_time=None,
                   negative_evals=negative_evals,
                   ne_qfts=ne_qfts)

def HHLsolver(matrix, vector, backend, no_ancillas, no_time_slices):
    orig_size = len(vector_b)
    #adapt the matrix to have dimension 2^k
    matrix, vector, truncate_powerdim, truncate_hermitian = HHL.matrix_resize(matrix_A, vector_b)

    #find eigenvalues of the matrix wih phase estimation (i.e. calc. exponential of A, apply 
    #phase estimation) to get exp(lamba) and then inverse QFT to get lambdas themselves
    eigs = create_eigs(matrix, no_ancillas, no_time_slices, False)
    #num_q – Number of qubits required for the matrix Operator instance
    #num_a – Number of ancillary qubits for Eigenvalues instance
    num_q, num_a = eigs.get_register_sizes()

    #construct circuit for finding reciprocals of eigenvalues
    reci = LookupRotation(negative_evals=eigs._negative_evals, evo_time=eigs._evo_time)

    #preparing init state for HHL, i.e. the state containing vector b
    init_state = Custom(num_q, state_vector=vector)

    #construct circuit for HHL based on matrix A, vector B and reciprocals of eigenvalues
    algo = HHL(matrix, vector, truncate_powerdim, truncate_hermitian, eigs,
               init_state, reci, num_q, num_a, orig_size)
    
     #solution on quantum computer
    result = algo.run(quantum_instance = backend)
    print("Solution:\t\t", np.round(result['solution'], 5))
    print("Probability:\t\t %f" % result['probability_result'])
    print("Quantun result storage:\t",result)

    #refence solution - NumPyLSsolver = Numpy LinearSystem algorithm (classical).
    result_ref = NumPyLSsolver(matrix, vector).run()
    print("Classical Solution:\t", np.round(result_ref['solution'], 5))

matrix_A = np.array([[1, -1/3], [-1/3, 1]])
vector_b = [1, 0]

processor = Aer.get_backend('statevector_simulator')

no_ancillas = 3 #number of ancilla qubits
no_time_slices = 50 #number of timeslices in exponential of matrix A (exp(i*A*t))

HHLsolver(matrix_A, vector_b, processor, no_ancillas, no_time_slices)
$\endgroup$
1
  • 1
    $\begingroup$ It would be helpful if you included the output that each piece of code produces. $\endgroup$ – epelaaez Jun 30 at 22:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.