def test_williamson_BM_random_circuit_pure(self): self.logTestName() for k in range(nsamples): n = 3 U2 = haar_measure(n) state = gaussiancircuit.GaussianModes(n, hbar=2) for i in range(n): state.squeeze(np.log(0.2 * i + 2), 0, i) state.apply_u(U2) V = state.scovmatxp() D, S = dec.williamson(V) omega = dec.sympmat(n) self.assertAlmostEqual(np.linalg.norm(S @ omega @ S.T - omega), 0) self.assertAlmostEqual(np.linalg.norm(S @ D @ S.T - V), 0) O, s, Oo = dec.bloch_messiah(S) self.assertAlmostEqual( np.linalg.norm(np.transpose(O) @ omega @ O - omega), 0) self.assertAlmostEqual( np.linalg.norm(np.transpose(O) @ O - np.identity(2 * n)), 0) self.assertAlmostEqual( np.linalg.norm(np.transpose(Oo) @ omega @ Oo - omega), 0) self.assertAlmostEqual( np.linalg.norm(np.transpose(Oo) @ Oo - np.identity(2 * n)), 0) self.assertAlmostEqual( np.linalg.norm(np.transpose(s) @ omega @ s - omega), 0) self.assertAlmostEqual(np.linalg.norm(O @ s @ Oo - S), 0)
def random_degenerate_symmetric(): iis = [ 1 + np.random.randint(2), 1 + np.random.randint(3), 1 + np.random.randint(3), 1 ] vv = [[i] * iis[i] for i in range(len(iis))] dd = np.array(sum(vv, [])) n = len(dd) U = haar_measure(n) symmat = U @ np.diag(dd) @ np.transpose(U) return symmat
def test_clements_random_unitary(self): error = np.empty(nsamples) for k in range(nsamples): n = 20 V = haar_measure(n) (tilist, tlist, diags) = dec.clements(V) qrec = np.identity(n) for i in tilist: qrec = dec.T(*i) @ qrec qrec = np.diag(diags) @ qrec for i in reversed(tlist): qrec = dec.Ti(*i) @ qrec error[k] = np.linalg.norm(V - qrec) self.assertAlmostEqual(error.mean(), 0)
def test_triangular_decomposition_random_unitary(self): """This test checks the triangular decomposition for a random unitary. A random unitary is drawn from the Haar measure, then is decomposed using the triangular decomposition of Reck et al. and the resulting beamsplitters are multiplied together. Test passes if the product matches the drawn unitary. """ self.logTestName() error = np.empty(nsamples) for k in range(nsamples): n = 20 U = haar_measure(n) tlist, diags = dec.triangular_decomposition(U) U_passive_try = np.diag(diags) for i in tlist: U_passive_try = dec.Ti(*i) @ U_passive_try self.assertAlmostEqual(np.linalg.norm(U_passive_try - U), 0, delta=self.tol)
def test_clements_random_unitary(self): """This test checks the rectangular decomposition for a random unitary. A random unitary is drawn from the Haar measure, then is decomposed via the rectangular decomposition of Clements et al., and the resulting beamsplitters are multiplied together. Test passes if the product matches the drawn unitary. """ self.logTestName() error = np.empty(nsamples) for k in range(nsamples): n = 20 V = haar_measure(n) (tilist, tlist, diags) = dec.clements(V) qrec = np.identity(n) for i in tilist: qrec = dec.T(*i) @ qrec qrec = np.diag(diags) @ qrec for i in reversed(tlist): qrec = dec.Ti(*i) @ qrec error[k] = np.linalg.norm(V - qrec) self.assertAlmostEqual(error.mean(), 0)
def test_clements_phase_end_random_unitary(self): """This test checks the rectangular decomposition with phases at the end. A random unitary is drawn from the Haar measure, then is decomposed using Eq. 5 of the rectangular decomposition procedure of Clements et al , i.e., moving all the phases to the end of the interferometer. The resulting beamsplitters are multiplied together. Test passes if the product matches the drawn unitary. """ self.logTestName() error = np.empty(nsamples) for k in range(nsamples): n = 20 U = haar_measure(n) new_tlist, new_diags = dec.clements_phase_end(U) U_rec = np.identity(n) for i in new_tlist: U_rec = dec.T(*i) @ U_rec U_rec = np.diag(new_diags) @ U_rec self.assertAlmostEqual(np.linalg.norm(U_rec - U), 0, delta=self.tol)
def test_haar_measure(self, n, tol): """test that the haar measure function returns unitary matrices""" U = so.haar_measure(n) assert np.allclose(U @ U.conj().T, np.identity(n), atol=tol, rtol=0)