We consider the problem of constructing optimal sparse controllers. It is known that a property called quadratic invariance of the constraint set is important, and results in the constrained minimum-norm problem being soluble via convex programming. We provide an explicit method of computing $\Htwo$-optimal controllers subject to quadratically invariant sparsity constraints, along with a computational test for quadratic invariance. As a consequence, we show that block diagonal constraints are never quadratically invariant unless the plant is block diagonal as well.