# Measure to compare a matrix with a given unitary matrix

I am implementing an optimization algorithm which main goal is to find optimal parameters of some problem for targeted solution. Each step of the optimization gives a complex-valued matrix that defines the solution ($U_1$), so that every time I need to compare the matrix with the targeted one. The targeted matrix is known to be unitary. As per the matrix obtained with optimization ($U_2$), I am considering two cases: 1) it is unitary as well, and 2) it is not unitary, but scaled unitary, i.e. the one obtained by multiplication a unitary by a number.

To compare two matrices (for both cases) the following measure can be suggested: $$F(U_1,U_2)=|\frac{tr(U_1^{\dagger}U_2)}{\sqrt{N{}tr(U_2^{\dagger}U_2)}}|^2$$ where $N$ is the matrices dimension. When $U_1=aU_2$, $F=1$, when $U_1\ne{}aU_2$, $0\le{}F<1$

However, now I want to relax the comparison condition and consider matrices $U_1$ and $U_2$ that differ only by any diagonal $D=\text{diag}(e^{i\varphi_1},\ldots,e^{i\varphi_N})$ also equal, i.e. $U$ and $DU$ are cosidered equal. Therefore, I want to construct a measure capable of comparing matrices provided this relaxed conditions. I am asking you to give me a hint on how to do it.