def test_xmat(n): """test X_n = [[0, I], [I, 0]]""" I = np.identity(n) O = np.zeros_like(I) X = np.block([[O, I], [I, O]]) res = Xmat(n) assert np.all(X == res)
def test_numba_tor(N): """Tests numba implementations of the torontonian against the default implementation""" cov = random_covariance(N) O = Xmat(N) @ Amat(cov) t1 = tor(O) t2 = numba_tor(O) t3 = rec_torontonian(O) assert np.isclose(t1, t2) assert np.isclose(t1, t3)
def test_numba_ltor(N): """Tests numba implementations of the loop torontonian against the default implementation""" alpha = np.random.random(N) + np.random.random(N) * 1j alpha = np.concatenate((alpha, alpha.conj())) cov = random_covariance(N) O = Xmat(N) @ Amat(cov) mu = O @ alpha t1 = ltor(O, mu) t2 = numba_ltor(O, mu) t3 = rec_ltorontonian(O, mu) assert np.isclose(t1, t2) assert np.isclose(t1, t3)
def reference_count_unnormalized(cov, only_modes=None): A = Amat(cov) modes = cov.shape[0] // 2 if only_modes is not None: mask = only_modes + tuple(x + modes for x in only_modes) A = A[mask, :][:, mask] xmat_size = len(only_modes) else: xmat_size = modes O = Xmat(xmat_size) @ A return tor(O).real
def Amat(self): """Constructs the A matrix from Hamilton's paper""" ######### this needs to be conjugated sigmaq = np.concatenate( ( np.concatenate((np.transpose(self.nmat), self.mmat), axis=1), np.concatenate( (np.transpose(np.conjugate(self.mmat)), self.nmat), axis=1), ), axis=0, ) + np.identity(2 * self.nlen) return np.dot(Xmat(self.nlen), np.identity(2 * self.nlen) - np.linalg.inv(sigmaq))
def test_Covmat(): """ Test the Covmat function by checking that its inverse function is Qmat """ n = 1 B = np.random.rand(n, n) + 1j * np.random.rand(n, n) B = B + B.T sc = find_scaling_adjacency_matrix(B, 1) idm = np.identity(2 * n) X = Xmat(n) Bsc = sc * B A = np.block([[Bsc, 0 * Bsc], [0 * Bsc, Bsc.conj()]]) Q = np.linalg.inv(idm - X @ A) cov = Covmat(Q) Qrec = Qmat(cov) assert np.allclose(Q, Qrec)
def test_tor_and_threshold_displacement_prob_agree(n_modes): """Tests that threshold_detection_prob, ltor and the usual tor expression all agree when displacements are zero""" cv = random_covariance(n_modes) mu = np.zeros([2 * n_modes]) Q = Qmat(cv) O = Xmat(n_modes) @ Amat(cv) expected = tor(O) / np.sqrt(np.linalg.det(Q)) prob = threshold_detection_prob(mu, cv, np.array([1] * n_modes)) prob2 = numba_ltor(O, mu) / np.sqrt(np.linalg.det(Q)) prob3 = rec_ltorontonian(O, mu) / np.sqrt(np.linalg.det(Q)) prob4 = numba_vac_prob(mu, Q) * ltor(O, mu) assert np.isclose(expected, prob) assert np.isclose(expected, prob2) assert np.isclose(expected, prob3) assert np.isclose(expected, prob4)