An inverse problem of reconstructing the initial condition for a time fractional diffusion equation is investigated.On the basis of the optimal control framework,the uniqueness and first order necessary optimality condition of the minimizer for the objective functional are established,and a time-space spectral method is proposed to numerically solve the resulting minimization problem.The contribution of the paper is threefold:1)a priori error estimate for the spectral approximation is derived;2)a conjugate gradient optimization algorithm is designed to efficiently solve the inverse problem;3)some numerical experiments are carried out to show that the proposed method is capable to find out the optimal initial condition,and that the convergence rate of the method is exponential if the optimal initial condition is smooth.