Qiskit / qiskit

Qiskit is an open-source SDK for working with quantum computers at the level of extended quantum circuits, operators, and primitives.
https://www.ibm.com/quantum/qiskit
Apache License 2.0
5.04k stars 2.32k forks source link

Implementation of VQE on IBMQ, Debugging #7809

Closed andrea2020liuyy closed 1 year ago

andrea2020liuyy commented 2 years ago

Environment

What is happening?

I was trying to implement VQE on IBMQ, but every time I run the cell, it has AlgorithmError. I took a long time on it and right now need help on debugging. Really appreciate the help. Thanks in advance!

How can we reproduce the issue?

# Import libraries
import pandas as pd
import numpy as np
from numpy import linalg as la
import scipy   
from scipy.sparse import coo_matrix
from qiskit import *
from qiskit.opflow import MatrixOp
from qiskit.circuit.library import TwoLocal
from qiskit import Aer
from qiskit.algorithms import VQE
from qiskit.algorithms.optimizers import COBYLA
from qiskit.utils import algorithm_globals, QuantumInstance
from qiskit import IBMQ
from qiskit.providers.ibmq import least_busy
from qiskit.tools.monitor import job_monitor
from qiskit.visualization import plot_histogram

# Create a random Hermitian Operator
n=4
np.random.seed(1)
cond_P =  0.564 
log_cond_P = np.log(cond_P)
exp_vec = np.arange(-log_cond_P/4., log_cond_P * (n + 1)/(4 * (n - 1)), log_cond_P/(2.*(n-1)))
ss = np.exp(exp_vec)
S = np.diag(ss)
U, _ = la.qr(np.random.rand(n, n))
V, _ = la.qr(np.random.rand(n, n))
P = U.dot(S).dot(V.T)
H = P.dot(P.T)

# Map Operator to Circuits
qubitOp = MatrixOp(primitive=H)
var_form_depth = 3
var_form = TwoLocal(qubitOp.num_qubits, ['rz', 'ry'], 'cz', entanglement = 'full', reps = var_form_depth)
qc = QuantumCircuit(qubitOp.num_qubits)
qc += var_form
qc.measure_all()
qc.decompose().draw('mpl')

# Set Simulation
rngseed = 0
algorithm_globals.random_seed = rngseed
counts = [ ]
values = [ ]
def store_intermediate_result(eval_count, parameters, mean, std):
    counts.append(eval_count)
    values.append(mean)
optimizer = COBYLA(maxiter=1000000,tol=0.000001)

# Choose least busy IBMQ to submit job
provider = IBMQ.load_account()
small_devices = provider.backends(filters=lambda x: x.configuration().n_qubits == 5
                                   and not x.configuration().simulator)
new_backend = least_busy(small_devices)
qcomp = provider.get_backend('ibmq_lima')

# Run VQE algorithm on IBMQ
q_instance = QuantumInstance(new_backend, seed_transpiler=rngseed)
vqe_ibm = VQE(tqc, optimizer=optimizer, quantum_instance=q_instance, 
callback=store_intermediate_result)
result_ibm = vqe_ibm.compute_minimum_eigenvalue(operator = qubitOp)
print(result_ibm)
optimal_value = result.optimal_value

What should happen?

It should return a minimum eigenvalue(ground state energy). But no return at all.

Any suggestions?

I've successfully run this on qasm_simulator and other ibmqs, but it has no result return from ibmq. Usually, I'll wait several minutes after submission to get a return (no matter it's the correct answer or AlgorithmError), since the available IBMQs are only 5 and occupied at most times. I hope to get help with debugging (mostly in the last part). Really appreciate it.

gines-carrascal commented 2 years ago

Your code works only changing "tqc" -> "var_form" at the VQE instantiation. You can see the job created:

image

The queue is taking long (more than 20 minutes):

image
Cryoris commented 2 years ago

@andrea2020liuyy does the above reply help? It seems indeed that tqc hasn't been defined. If not, do you mind posting the error that you're getting? That would help us understand what the issue might be 🙂

woodsp-ibm commented 1 year ago

This issue has been open some time with no response from the originator. It was most likely user error as it was indicated `tqc' was undefined above and worked if you altered that. It also is around the now deprecated VQE using QuantumInstance which is soon to be removed so as such I am going to close this.

andrea2020liuyy commented 1 year ago

@Cryoris Thanks, it's working. I forgot to come back to this community and close it. @woodsp-ibm I use the Qiskit RuntimeService estimator program to execute VQE and it works fine as well.