def test_grad_squeezing(): """Tests the value of the analytic gradient for the Sgate against finite differences""" cutoff = 4 r = 1.0 theta = np.pi / 8 T = squeezing(r, theta, cutoff) Dr, Dtheta = grad_squeezing(T, r, theta) dr = 0.001 dtheta = 0.001 Drp = squeezing((r + dr), theta, cutoff) Drm = squeezing((r - dr), theta, cutoff) Dthetap = squeezing(r, theta + dtheta, cutoff) Dthetam = 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_squeezing_values(tol): """Tests the correct construction of the single mode squeezing operation""" r = 0.5 theta = 0.7 cutoff = 5 # This data is obtained by using qutip # np.array(squeeze(40,0.5*np.exp(1j*0.7)).data.todense())[0:5,0:5] expected = np.array([ [ 0.94171062 + 0.0j, 0.0 + 0.0j, 0.23535661 - 0.19823814j, 0.0 + 0.0j, 0.02093159 - 0.12135894j, ], [ 0.0 + 0.0j, 0.83512676 + 0.0j, 0.0 + 0.0j, 0.36151137 - 0.30449682j, 0.0 + 0.0j ], [ -0.23535661 - 0.19823814j, 0.0 + 0.0j, 0.64005396 + 0.0j, 0.0 + 0.0j, 0.42261153 - 0.35596078j, ], [ 0.0 + 0.0j, -0.36151137 - 0.30449682j, 0.0 + 0.0j, 0.38926873 + 0.0j, 0.0 + 0.0j ], [ 0.02093159 + 0.12135894j, 0.0 + 0.0j, -0.42261153 - 0.35596078j, 0.0 + 0.0j, 0.12407853 + 0.0j, ], ]) T = squeezing(r, theta, cutoff) assert np.allclose(T, expected, atol=tol, rtol=0)