3
$\begingroup$

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
grad = Gradient().convert(operator = op, params = list(var_form.parameters))

vqe = VQE(var_form, optimizer, gradient=grad,
            quantum_instance=Aer.get_backend('aer_simulator_statevector'))
result = vqe.compute_minimum_eigenvalue(operator=H2)
$\endgroup$
2
  • $\begingroup$ 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? $\endgroup$ Jul 13, 2021 at 8:41
  • $\begingroup$ 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. $\endgroup$ Jul 13, 2021 at 9:04

1 Answer 1

1
$\begingroup$

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",
            quantum_instance=Aer.get_backend('aer_simulator_statevector'))
$\endgroup$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.