Given a unitary $A$ and diagonal matrix $D$, find $B$ so that $AD=DB$. In principle the title says it all. Given some diagonal Matrix $D$ (with ma have 0- and repeated entries) and some unitary matrix $A$, I want to find some other unitary matrix $B$ so that $AD=DB$. What are the conditions that such a matrix $B$ exists and how can I construct it?
 A: Such a $B$ exists if and only if $A$ commutes with $|D|^2$, where $|D|$ denotes the entrywise absolute value of $D$. (This condition is actually equivalent to $A|D|=|D|A$, but this is unimportant here.)
Suppose $AD=DB$ where $B$ is unitary. Then $ADD^\ast A^\ast=DBB^\ast D^\ast$. Hence $A|D|^2A^\ast=|D|^2$, i.e., $A|D|^2=|D|^2A$.
Conversely, suppose $A|D|^2=|D|^2A$. Without loss of generality we may assume that $|D|=d_1I_{n_1}\oplus\cdots\oplus d_kI_{n_k}$, where the $d_i$s are distinct nonnegative real numbers. Hence $D$ must be a block-diagonal matrix in the form of $D_1\oplus\cdots\oplus D_k$, where in each $D_i$ all diagonal elements have modulus $d_i$. It follows that $D=d_1\Lambda_1\oplus\cdots\oplus d_k\Lambda_k$ for some diagonal unitary matrices $\Lambda_1,\ldots,\Lambda_k$. (More specifically, $\Lambda_i=d_i^{-1}D_i$ when $d_i>0$ and we may take $\Lambda_i=I_{n_i}$ when $d_i=0$.) Since $A$ commutes with $|D|^2$, it must be a block-diagonal matrix in the form of $A_1\oplus\cdots\oplus A_k$, where each $A_i$ is $n_i\times n_i$. Therefore $AD=DB$ where $B$ is the unitary matrix $(\Lambda_1^\ast A_1\Lambda_1)\oplus\cdots\oplus(\Lambda_k^\ast A_k\Lambda_k)$.
