Initially I write down a 2x2 unitary (Uni) , make a tensor product 4 times (LocalUni) and apply it to an initial numerical matrix Rho, to obtain RhoLU, that depends on 12 parameters.
This is my code:
Uni[phi_, psi_,theta_] := {{E^(I*phi)*Cos[theta], E^(I*psi)*Sin[theta]}, {-E^(-I*psi)*Sin[theta], E^(-I*phi)*Cos[theta]}};
LocalUni[a1_, a2_, a3_, a4_, b1_, b2_, b3_, b4_, c1_, c2_, c3_, c4_] := KroneckerProduct[Uni[a1, b1, c1], KroneckerProduct[Uni[a2, b2, c2], KroneckerProduct[Uni[a3, b3, c3], Uni[a4, b4, c4]]]]
RhoLU[a1_, a2_, a3_, a4_, b1_, b2_, b3_, b4_, c1_, c2_, c3_, c4_] := LocalUni[a1, a2, a3, a4, b1, b2, b3, b4, c1, c2, c3, c4].Rho.ConjugateTranspose[LocalUni[a1, a2, a3, a4, b1, b2, b3, b4, c1, c2, c3, c4]]
where Rho is just a large matrix with complex entries.
I look at the number of elements that are above the 0.01 threshold in my initial Rho matrix:
Count[Re[Flatten[Rho]], u_ /; Abs[u] > 0.01] + Count[Im[Flatten[Rho]], u_ /; Abs[u] > 0.01]
and I get 239.
Now what I want is to apply NMinimize to this last line , depending on the a1, a2... parameters, such that the total number of elements of the RhoLU matrix (so the Rho matrix after I apply the local unitaries) is as small as possible. What I tried is:
ELM = Count[Re[Flatten[RhoLU[a1, a2, a3, a4, b1, b2, b3, b4, c1, c2, c3, c4]]], u_ /; Abs[u] > 0.01] + Count[Im[Flatten[RhoLU[a1, a2, a3, a4, b1, b2, b3, b4, c1, c2, c3, c4]]],u_ /; Abs[u] > 0.01] ;
LeastElements = NMinimize[ELM,{a1, a2, a3, a4, b1, b2, b3, b4, c1, c2, c3, c4}]
The result is always
{0., a1->0, a2->0 ...}
I find this weird because if you write LocalUni in matrix form for all the parameters set to zero, you get an identity matrix (as you must), thus even if it does nothing and sets everything equal to zero, I should get at least 239, which is the number I got for my initial Rho, without any transformations.