NISQ algorithm for the matrix elements of a generic observable