Variational quantum linear solver with a dynamic ansatz