Now is a good time to vectorize your code 
What the error means is that autograd doesn’t like how you assign to bits of your variable (which makes it hard to tell what’s going on if things get overwritten). Most of the need to do so will go away anyway when you write the code better.
p_temp = Variable(q_temp.data.clone(), requires_grad=True).cuda()

this will also trip you, as .cuda() counts as computation and you won’t get a grad on p_temp. If you’re on 0.4, use p_temp = q_temp.clone().detach().cuda().requires_grad_() or so.
Best regards
Thomas