def test_squeezing(self): """Test squeezing gives correct means and cov""" self.eng.reset() q = self.eng.register x = 0.2 p = 0.3 r = 0.42 phi = 0.123 H, t = squeezing(r, phi, hbar=self.hbar) with self.eng: Xgate(x) | q[0] Zgate(p) | q[0] GaussianPropagation(H, t) | q[0] state = self.eng.run('gaussian') # test the covariance matrix res = state.cov() S = rot(phi / 2) @ np.diag(np.exp([-r, r])) @ rot(phi / 2).T V = S @ S.T * self.hbar / 2 self.assertTrue(np.allclose(res, V)) # test the vector of means res = state.means() exp = S @ np.array([x, p]) self.assertTrue(np.allclose(res, exp))
def test_rotation(self): """Test rotation gives correct means and cov""" self.eng.reset() q = self.eng.register x = 0.2 p = 0.3 phi = 0.123 H, t = rotation(phi, hbar=self.hbar) with self.eng: Xgate(x) | q[0] Zgate(p) | q[0] Sgate(2) | q[0] GaussianPropagation(H, t) | q[0] state = self.eng.run('gaussian') # test the covariance matrix res = state.cov() V = np.diag([np.exp(-4), np.exp(4)]) * self.hbar / 2 expected = rot(phi) @ V @ rot(phi).T self.assertTrue(np.allclose(res, expected)) # test the vector of means res = state.means() exp = rot(phi) @ np.diag(np.exp([-2, 2])) @ np.array([x, p]) self.assertTrue(np.allclose(res, exp))
def test_rotation(self, hbar): """Test rotation gives correct means and cov""" prog = sf.Program(1) eng = sf.Engine("gaussian") x = 0.2 p = 0.3 phi = 0.123 H, t = rotation(phi, hbar=hbar) with prog.context as q: Xgate(x) | q[0] Zgate(p) | q[0] Sgate(2) | q[0] GaussianPropagation(H, t) | q[0] state = eng.run(prog).state # test the covariance matrix res = state.cov() V = np.diag([np.exp(-4), np.exp(4)]) * hbar / 2 expected = rot(phi) @ V @ rot(phi).T assert np.allclose(res, expected) # test the vector of means res = state.means() exp = rot(phi) @ np.diag(np.exp([-2, 2])) @ np.array([x, p]) assert np.allclose(res, exp)
def test_squeezing(self, hbar): """Test squeezing gives correct means and cov""" prog = sf.Program(1) eng = sf.Engine("gaussian") x = 0.2 p = 0.3 r = 0.42 phi = 0.123 H, t = squeezing(r, phi, hbar=hbar) with prog.context as q: Xgate(x) | q[0] Zgate(p) | q[0] GaussianPropagation(H, t) | q[0] state = eng.run(prog).state # test the covariance matrix res = state.cov() S = rot(phi / 2) @ np.diag(np.exp([-r, r])) @ rot(phi / 2).T V = S @ S.T * hbar / 2 assert np.allclose(res, V) # test the vector of means res = state.means() exp = S @ np.array([x, p]) assert np.allclose(res, exp)
def test_rotated_squeezed(self, setup_eng, hbar, tol): """Testing a rotated squeezed state""" eng, prog = setup_eng(3) r = 0.1 phi = 0.2312 v1 = (hbar / 2) * np.diag([np.exp(-r), np.exp(r)]) A = changebasis(3) cov = A.T @ block_diag(*[rot(phi) @ v1 @ rot(phi).T] * 3) @ A with prog.context as q: ops.Gaussian(cov, decomp=False) | q state = eng.run(prog).state assert np.allclose(state.cov(), cov, atol=tol)
def test_covariance_rotated_squeezed(self): self.logTestName() q = self.eng.register r = 0.1 phi = 0.2312 v1 = (self.hbar / 2) * np.diag([np.exp(-r), np.exp(r)]) A = changebasis(3) cov = A.T @ block_diag(*[rot(phi) @ v1 @ rot(phi).T] * 3) @ A with self.eng: Gaussian(cov, decomp=False) | q state = self.eng.run() self.assertAllAlmostEqual(state.cov(), cov, delta=self.tol)
def test_rotated_squeezed(self, setup_eng, hbar, tol): """Testing decomposed rotated squeezed state""" eng, q = setup_eng(3) r = 0.1 phi = 0.2312 v1 = (hbar / 2) * np.diag([np.exp(-r), np.exp(r)]) A = changebasis(3) cov = A.T @ block_diag(*[rot(phi) @ v1 @ rot(phi).T] * 3) @ A with eng: ops.Gaussian(cov) | q state = eng.run() assert np.allclose(state.cov(), cov, atol=tol) assert np.all(len(eng.cmd_applied[0]) == 3)
def test_covariance_rotated_squeezed(self): self.eng.reset() q = self.eng.register r = 0.1 phi = 0.2312 v1 = (self.hbar / 2) * np.diag([np.exp(-r), np.exp(r)]) A = changebasis(3) cov = A.T @ block_diag(*[rot(phi) @ v1 @ rot(phi).T] * 3) @ A with self.eng: CovarianceState(cov) | q state = self.eng.run(backend=self.backend_name, cutoff_dim=self.D) self.assertAllAlmostEqual(state.cov(), cov, delta=self.tol) self.assertAllEqual(len(self.eng.cmd_applied), 3)
def test_rotated_squeezed(self, setup_eng, cutoff, hbar, tol): eng, q = setup_eng(3) r = 0.1 phi = 0.2312 in_state = squeezed_state(r, phi, basis="fock", fock_dim=cutoff) v1 = (hbar / 2) * np.diag([np.exp(-2 * r), np.exp(2 * r)]) A = changebasis(3) cov = A.T @ block_diag(*[rot(phi) @ v1 @ rot(phi).T] * 3) @ A with eng: ops.Gaussian(cov) | q state = eng.run() assert len(eng.cmd_applied[0]) == 3 for n in range(3): assert np.allclose(state.fidelity(in_state, n), 1, atol=tol)
def test_covariance_rotated_squeezed(self): self.logTestName() q = self.eng.register r = 0.1 phi = 0.2312 in_state = squeezed_state(r, phi, basis='fock', fock_dim=self.D) v1 = (self.hbar / 2) * np.diag([np.exp(-2 * r), np.exp(2 * r)]) A = changebasis(3) cov = A.T @ block_diag(*[rot(phi) @ v1 @ rot(phi).T] * 3) @ A with self.eng: Gaussian(cov) | q state = self.eng.run(**self.kwargs) self.assertAllEqual(len(self.eng.cmd_applied[0]), 3) for n in range(3): self.assertAllAlmostEqual(state.fidelity(in_state, n), 1, delta=self.tol)