  Hybrid quantum-classical optimization algorithms represent one of the most
promising application for near-term quantum computers. In these algorithms the
goal is to optimize an observable quantity with respect to some classical
parameters, using feedback from measurements performed on the quantum device.
Here we study the problem of estimating the gradient of the function to be
optimized directly from quantum measurements, generalizing and simplifying some
approaches present in the literature, such as the so-called parameter-shift
rule. We derive a mathematically exact formula that provides a stochastic
algorithm for estimating the gradient of any multi-qubit parametric quantum
evolution, without the introduction of ancillary qubits or the use of
Hamiltonian simulation techniques. The gradient measurement is possible when
the underlying device can realize all Pauli rotations in the expansion of the
Hamiltonian whose coefficients depend on the parameter. Our algorithm continues
to work, although with some approximations, even when all the available quantum
gates are noisy, for instance due to the coupling between the quantum device
and an unknown environment.
