Quantum algorithms for spectral sums