Solution (source code)

= Solution

For a <unitary matrix>, $UU^\dagger=1$, so $(U^\dagger MU)^r=U^\dagger M^rU$. Cyclicity of the <trace> then gives $\operatorname{tr}(U^\dagger M^rU)=\operatorname{tr}(M^r)$ for both powers entering the action. Thus \b[the action is invariant under unitary conjugation].

The <spectral theorem for normal operators> diagonalizes a finite-dimensional <Hermitian matrix> by a <unitary matrix>. Applying the invariance to that diagonal form gives
$$
\boxed{V(M)=\sum_{i=1}^N\left(\frac{\lambda_i^2}{2}+\frac g4\lambda_i^4\right).}
$$
Only the <eigenvalues> enter, not the choice of <eigenvectors>.