I am trying to run Qiskit QAOA algorithm on using aer simulator and device GPU. But I am not sure how to give this backend in the QAOA algorithm structure.

Here is a code:

nqubits = 5
H = QuantumCircuit(nqubits)
start = time.time()

sim = AerSimulator(method='statevector')
sim_gpu = AerSimulator(method='statevector', device='GPU')

for i in range(nqubits):
    H_op = MatrixOp(Operator(H))
    new_H = transpile(H, backend=sim)

    qaoa = QAOA(optimizer= COBYLA(), reps=1, mixer=new_H,

    res = qaoa.compute_minimum_eigenvalue(H_op)

end = time.time()

print("time taken : ", end - start)

I get this error:

QiskitError: 'Circuit execution failed: ERROR: Failed to load circuits: Invalid parameterization: instruction param position out of range'

I tried running the code via estimators but I am unable to see any speed-up.

def cost_func(params, ansatz, hamiltonian, estimator):
    cost = estimator.run(ansatz, hamiltonian, parameter_values=params).result().values[0]
    return cost

# Problem to Hamiltonian operator
hamiltonian = SparsePauliOp.from_list([("IIIZZ", 1), ("IIZIZ", 1), ("IZIIZ", 1),("ZIIIZ", 1)])
# QAOA ansatz circuit
ansatz = QAOAAnsatz(hamiltonian, reps=8)
x0 = 2 * np.pi * np.random.rand(ansatz.num_parameters)

estimator = Estimator(
          "method": "statevector"
    run_options={"shots": 1e4},

estimator_gpu = Estimator(
           "method": "statevector",
           "device": "GPU"
    run_options={"shots": 1e4},

sampler = Sampler(run_options= {"method": "statevector"})

start = time.time()
res = minimize(cost_func, x0, args=(ansatz, hamiltonian, estimator), method="COBYLA")
end = time.time()

print("Time Taken CPU: ", end-start)

start_gpu = time.time()
res_gpu = minimize(cost_func, x0, args=(ansatz, hamiltonian, estimator_gpu), method="COBYLA")
end_gpu = time.time()

print("Time Taken GPU: ", end_gpu-start_gpu)

Time Taken CPU: 2.922945022583008 Time Taken GPU: 2.9809350967407227

What is it that I am doing wrong?



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.