# Section 10: for S with entries of modulus 1, the maximum of Re Tr(S^*U) over the unitary matrices U is Tr|S|, reached at the polar part # of S, and for U unitary the maximum of Re Tr(S^*U) over such S is ||U||_1, reached at the phase matrix of U. The alternating maximization # S -> phase matrix of the polar part of S increases Tr|S|. We run it from random real signs and from random complex phases. # Usage: python3 cooling.py [runs with real signs, default 1000] [runs with complex phases, default 200] import numpy as np, sys rng = np.random.default_rng(7) def polar(S): w, s, vh = np.linalg.svd(S); return w @ vh def phase(U, S): a = np.abs(U); ok = a > 1e-12; return np.where(ok, U/np.where(ok, a, 1), S) # at a zero entry of U any phase will do: keep the old one def cool(S, steps=20000): f = np.linalg.svd(S, compute_uv=False).sum() for k in range(steps): T = phase(polar(S), S); g = np.linalg.svd(T, compute_uv=False).sum() if g <= f + 1e-14: break S, f = T, g return f, k if __name__ == "__main__": runs = int(sys.argv[1]) if len(sys.argv) > 1 else 1000; runs2 = int(sys.argv[2]) if len(sys.argv) > 2 else 200 for n in range(3, 13): for real, count in ((True, runs), (False, runs2)): out = [cool(rng.choice([-1.0, 1.0], size=(n, n)) if real else np.exp(2j*np.pi*rng.random((n, n)))) for _ in range(count)] res = np.array([f for f, k in out])/n**1.5; its = np.array([k for f, k in out]) print("N=%2d %s runs %d: mean %.6f, lowest %.6f, Hadamard %d times, mean number of steps %.0f, largest %d" % (n, "real " if real else "complex", count, res.mean(), res.min(), int((res > 1 - 1e-6).sum()), its.mean(), its.max()), flush=True)