def test_disp_torontonian_single_mode(scale): """Calculates the probability of clicking for a single mode state""" cv = random_covariance(1) mu = scale * (2 * np.random.rand(2) - 1) prob_click = threshold_detection_prob(mu, cv, np.array([1])) expected = 1 - density_matrix_element(mu, cv, [0], [0]) assert np.allclose(prob_click, expected)
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_probs_sum_to_1(n, scale): """test that threshold probabilities sum to 1""" cov = random_covariance(n) mu = scale * (2 * np.random.rand(2 * n) - 1) p_total = 0 for det_pattern in product([0, 1], repeat=n): p = threshold_detection_prob(mu, cov, det_pattern) p_total += p assert np.isclose(p_total, 1)
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 test_disp_torontonian_two_mode(scale): """Calculates the probability of clicking for a two mode state""" cv = random_covariance(2) mu = scale * (2 * np.random.rand(4) - 1) prob_click = threshold_detection_prob(mu, cv, [1, 1]) mu0, cv0 = reduced_gaussian(mu, cv, [0]) mu1, cv1 = reduced_gaussian(mu, cv, [1]) expected = ( 1 - density_matrix_element(mu0, cv0, [0], [0]) - density_matrix_element(mu1, cv1, [0], [0]) + density_matrix_element(mu, cv, [0, 0], [0, 0]) ) assert np.allclose(expected, prob_click)
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)