def test_grad_two_mode_squeezing(): """Tests the value of the analytic gradient for the S2gate against finite differences""" cutoff = 4 r = 1.0 theta = np.pi / 8 T = two_mode_squeezing(r, theta, cutoff) Dr, Dtheta = grad_two_mode_squeezing(T, r, theta) dr = 0.001 dtheta = 0.001 Drp = two_mode_squeezing(r + dr, theta, cutoff) Drm = two_mode_squeezing(r - dr, theta, cutoff) Dthetap = two_mode_squeezing(r, theta + dtheta, cutoff) Dthetam = two_mode_squeezing(r, theta - dtheta, cutoff) Drapprox = (Drp - Drm) / (2 * dr) Dthetaapprox = (Dthetap - Dthetam) / (2 * dtheta) assert np.allclose(Dr, Drapprox, atol=1e-5, rtol=0) assert np.allclose(Dtheta, Dthetaapprox, atol=1e-5, rtol=0)
def test_two_mode_squeezing_values(tol): """Tests the correct construction of the single mode squeezing operation""" r = 0.5 theta = 0.7 cutoff = 5 T = two_mode_squeezing(r, theta, cutoff) expected = ((np.tanh(r) * np.exp(1j * theta)) ** np.arange(cutoff)) / np.cosh(r) assert np.allclose(np.diag(T[:, :, 0, 0]), expected, atol=tol, rtol=0)
def test_S2_selection_rules(tol): r"""Tests the selection rules of a two mode squeezing operation. If one writes the squeezing gate as :math:`S_2` and its matrix elements as :math:`\langle p_0 p_1|S_2|q_0 q_1 \rangle` then these elements are nonzero if and only if :math:`p_0 - q_0 = p_1 - q_1`. This test checks that this selection rule holds. """ cutoff = 5 s = np.arcsinh(1.0) phi = np.pi / 6 T = two_mode_squeezing(s, phi, cutoff) m = np.arange(cutoff).reshape(-1, 1, 1, 1) n = np.arange(cutoff).reshape(1, -1, 1, 1) k = np.arange(cutoff).reshape(1, 1, -1, 1) l = np.arange(cutoff).reshape(1, 1, 1, -1) # create a copy of T, but replace all elements where # m+n != k+l with 0. S = np.where(m - n != k - l, 0, T) # check that S and T remain equal assert np.allclose(S, T, atol=tol, rtol=0)