Finding the square root of a special matrix Let $n\in \mathbb N$ be a natural number, $x_1,\cdots.x_n$ be formal variables. Consider the following $n\times n$-matrix $M_n:=diag\{x_1^2+\cdots+x_n^2,\cdots,x_1^2+\cdots+x_n^2\}$, can we find a solution $A=(a_{ij})$ such that 


*

*(Square root): $A^2=M_n$.

*(Polynomial): $a_{ij}\in \mathbb F[x_1,\cdots,x_n]$ be a polynomial, where $\mathbb F$ is a number field such as $\mathbb Q, \mathbb R$ or $\mathbb C$.(However this condition is not essential and we may use formal series $\mathbb F[[x_1,\cdots,x_n]]$.)


Of course, we do not order $A$ is diagonal. Moreover, as Will pointed out there is no solution for odd cases when $n\geq3$ since the determinant is not a square. For $n=2$ we have an example 
$$
\begin{pmatrix}
x_1^2+x_2^2 &\\
&x_1^2+x_2^2
\end{pmatrix}=\begin{pmatrix}\pm x_1 & x_2\\
x_2& \mp x_1\end{pmatrix}^2
.$$
 A: As Will noted, if $n>1$, one must have $n$ even for there to be any solutions.  (This holds even if the $a_{ij}$ are allowed to be formal power series in the $x_i$, since ${x_1}^2+\cdots+{x_n}^2$ is not a square even in this larger ring when $n>1$.)  (Of course, the $n=1$ case is trivial, so we can set that aside.)
Conversely, when $n=2m$, there is always a solution; just take
$$
A = \begin{pmatrix}0_m & I_m \\ ({x_1}^2+\cdots+{x_n}^2)I_m & 0_m\end{pmatrix}.
$$
However, I suspect that the OP actually wanted $A = A(x)$ to satisfy $A(0)=0_n$, and this is much more restrictive.  
Thus, assume that $A(x)$ has no constant term, i.e., $A(0)=0_n$.  Then the problem comes down to a question about Clifford algebras:  Write $A = A^i\,x_i + R_2(x)$ where $R_2(x)$ vanishes to order $2$ in $x$ and the $A^i$ are $n$-by-$n$ matrices with entries in $\mathbb{F}$.  Then the equation $A^2=M = ({x_1}^2+\cdots+{x_n}^2)I_n$ implies, in particular, that $A^iA^j+A^jA^i = 2\delta^{ij}I_n$.  These are the defining equations of a Clifford algebra over $\mathbb{F}$.  
For simplicity, take $\mathbb{F}=\mathbb{C}$.  The algebra $\mathbb{C}\ell_n$ generated over $\mathbb{C}$ by $n$ generators $J^i$ subject only to to the relations $J^iJ^j+J^jJ^i = 2\delta^{ij} 1$, is known to have dimension $2^n$ and, since $n$ is even, it is also known to  be isomorphic to $M_N(\mathbb{C})$, the algebra of $N$-by-$N$ matrices with complex entries, where $N = 2^{n/2}$. (See any book on Clifford algebras or simply consult the Wikipedia page on Clifford algebras.)  
The assignment $J^i\mapsto A^i$ induces a homomorphism $a:\mathbb{C}\ell_n (= M_N(\mathbb{C}))\to M_n(\mathbb{C})$ of algebras with unit, and, by the usual theory of matrix algebras, this can only happen if $N$ divides $n$, i.e., if $2^{n/2}$ divides $n$.  Of course, this only holds when $n=2$ and $n=4$.  
Conversely, when $n=2$ or $n=4$, the unital algebra $\mathbb{C}\ell_n$ is isomorphic to $M_n(\mathbb{C})$, so the desired $A^i$ do exist satisfying $A^iA^j+A^jA^i = 2\delta^{ij}I_n$ (and they are unique up to conjugation), and we can simply take $A = A^i\,x_i$ (i.e., $R_2(x)=0$) to get a solution.  For example, when $n=4$, one could take
$$
A = \begin{pmatrix}0&0&x_1+i\,x_2& x_3+i\,x_4\\0&0&-(x_3-i\,x_4)&x_1-i\,x_2\\
x_1-i\,x_2& -(x_3+i\,x_4)&0&0\\x_3-i\,x_4&x_1+i\,x_2&0&0\end{pmatrix}.
$$
(Of course, there are also many solutions with $R_2$ not vanishing identically.)
Obviously, if a solution of this kind existed with $\mathbb{F}= \mathbb{Q}$ or $\mathbb{R}$, then we could complexify and get a solution with $\mathbb{F}=\mathbb{C}$, so the only dimensions in which solutions of this kind could possibly exist are $n=2$ or $n=4$.  The case $n=2$ clearly does work in these cases, and, indeed, the OP gives a solution. Unfortunately, when $\mathbb{F}=\mathbb{Q}$ or $\mathbb{R}$, the case $n=4$ is impossible, because, when the ground field is $\mathbb{R}$, the algebra generated by the $J^i$ subject to the above relations is isomorphic to $M_2(\mathbb{H})$, and this algebra does not have a nontrivial homomorphism to $M_4(\mathbb{R})$.
A: As Will notes, this does not really work for odd $n \gt 1$ as the determinant of $M$ is not a square. On the other hand, if $n$ is even then the determinant of $M$ is a square. 
Note that your solution to $A^2=M$ for $n=2$ has $A$ symmetric and thus is also a solution of $AA^t=M$. I think $AA^t=M$ is the more promising equation to generalize. 
In either version, when $n=2m$, the determinant of $A$ would have to be $(x_1^2+x_2^2+\cdots +x_n^2)^m.$ It is hard to imagine this happening without the entries of $A$ all being of the form $\pm x_i.$ That is the only case I will consider.
I don't think there is a solution to $A^2=M$ for $n=4.$ However, $$A=\begin{pmatrix}
x_1 & x_2 &x_3 &x_4\\
x_2 & -x_1 &x_4 & -x_3 \\
x_4 &x_3&-x_2&-x_1\\
x_3&-x_4&-x_1&x_2
\end{pmatrix}$$
does give $AA^t=M.$
If all the $x_i$ are set equal to $1$ then $A$ would become a matrix with entries $\pm 1$ such that $A^2=nI_n$ or $AA^t=nI_n$, depending on which version one is trying to solve. In the $AA^t$ case one would have a Hadamard matrix which means that $n=1,2$ or a multiple of $4.$
I suppose having entries of the form $\zeta x_i$ with $\zeta$ a root of unity could also be worth considering. In that case it would be natural to look at the conjugate transpose (which is, of course, just the transpose for real matrices) or perhaps $A^2=M$ where $A^t$ is the conjugate of $A.$
Later
I should say that I don't think there are any solutions for $n=4$ to $A^2=M$ , of the type I mentioned. I didn't  look for one with entries of the form $\frac{x_1 \pm x_2 \pm x_3 \pm x_4}{2}.$ That seems promising. Of the type I mentioned (over $\mathbb{Z}$) one can see that $A$ would have to be symmetric and one can assume that the first row and column are $x_1\ x_2\ x_3\ x_4.$ This forces the second row and column to be $x_2\ -x_1\ \pm x_4\ \mp x_3.$ There are a few ways to finish off the remaining four entries and none worked. If $Q=A_4(x_1,x-2,x_3,x_4)$ is the matrix I gave, then I might expect a solution for $n=8$ of the form  $$A=\begin{pmatrix}
Q & R\\
S &T
\end{pmatrix}$$
Where $R=S=A_4(x_5,x_6,x_7,x_8)$ and $T=-Q$ or $-Q^t$. That would give the right sign pattern. I did not get that to work. Note that we can shuffle the rows and multiply any of them by $-1$ without affecting $AA^t=M.$ All that matters is that distinct rows are orthogonal. And we never want to get a term of $\pm x_i^2$ as an off diagonal term in $M.$ Hence each $x_i$ should appear once in each row or column and we can assume that the first row and column are $x_1\ x_2\ \cdots x_8.$ So I suppose I could swap rows $3$ and $4$ in my solution for $n=4.$
A: I don't really understand this... when $n=3,$ the determinant of $M$ is not a square by nature; it is a square times $x_1^2 + x_2^2 + x_3^2.$ If we assign integer values such that $x_1^2 + x_2^2 + x_3^2 = m^2$ for odd $m,$ then this can be written as $P^T P$ where $P$ has integer entries. The construction is due to Gordon Pall, about 1945, and uses the quaternions; first you write $m = a^2 + b^2 + c^2 + d^2.$
