I'm trying to understand how the gradients are calculated for any given circuit using qiskits CircuitQNN and NeuralNetworkClassifier. I've been looking trough the source on github but haven't found any real "definition" per se, and going through the code leaves much to be desired, atleast from my understand. So, how does it work, or where can I read up on it?


Qiskit implements the parameter shift rule and the linear combination of unitaries to calculate the gradients for a QNN. These techniques are described in detail in Section 3 of this paper.

If we calculate the gradients of the probabilities to measure one of the $2^n$ basis states, the circuit implementing the gradient is sampled $M$ (generally smaller than $2^n$ to avoid exponential cost) times and the gradients are constructed from the shot histogram.

If, on the other hand, we are interested in the gradient of an expectation value, we just need to replace the circuit preparing our state by it's gradient circuit. In this case, Qiskit also supports finite difference gradients.

See also this Qiskit tutorial for more info on the code.


Your Answer

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

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