Trace is additive, and ${\rm Tr}(u)={\rm Tr}(u^2)$ for all $u$, so $ax^2+bx$ has the same trace as $(a+b^2)x^2$. Therefore the sum is $2^r$ if $a=b^2$ and zero otherwise.