# Trouble when implement a self designed gradient in qiskit

everyone! I have trouble when implement a self-designed gradient to the VQE algorithm. The following code will pop the 'ListOp' object is not callable exception to the last line of the code when executed. Can anybody tell me how to fix this?

H2 = (-1.052373245772859 * I ^ I) + \
(0.39793742484318045 * I ^ Z) + \
(-0.39793742484318045 * Z ^ I) + \
(-0.01128010425623538 * Z ^ Z) + \
(0.18093119978423156 * X ^ X)

optimizer = AQGD(maxiter=10)
var_form = EfficientSU2(2, su2_gates=['ry', 'rz'], entanglement='linear', reps=2)
op1 = ~StateFn(H2) @ CircuitStateFn(primitive=var_form, coeff=1.)
op2 = ~StateFn((H2 @ H2).reduce()) @ CircuitStateFn(primitive=var_form, coeff=1.)
op = 3 * op1 - 4 * op2

quantum_instance=Aer.get_backend('aer_simulator_statevector'))
result = vqe.compute_minimum_eigenvalue(operator=H2)

• I find that if I change the optimizer to CG, then the code could be excecuted without any error, but I do not know why. Can someone explain why this happens? Jul 13 at 8:41
• Hi @ironmanaudi! Your question is already quite nice but here are some tips to increase your chances to get an answer: 1. include a MWE (minimal working example), in this case the code provided is at least missing the imports. People should be able to copy paste your code an see the exact same error as you. 2. Include the full error message, removing the personal parts (mostly in paths) if needed. By "full" I mean the full stack-trace. 3. instead of posting more information in a comment, edit your question. Jul 13 at 9:04

As the exception says, the gradient must be callable. The convert method return a ListOp object. From the documentation, to get a callable function, you can use gradient_wrapper method. From your code, simply substitute the grad with:
grad = Gradient().gradient_wrapper(operator = op, bind_params = list(var_form.parameters))
After looking at the source code and some experiments, to answer why if you change the optimizer to CG, the code could still be executed without error, it is because the gradient is ignored. Some optimizer like CG, L_BFGS_B, SLSQP, etc, use scipy.optimize.minimize, and if you look at the source code (line 572-574), the gradient (jac variable in the source code) will be ignored because it isn't a callable. You can even use arbitrary input for the gradient and it will still work, for example:
vqe = VQE(var_form, CG(maxiter=10), gradient="this_is_a_string",