def test_squeezed_fidelity(self, setup_backend, cutoff, tol, hbar): backend = setup_backend(2) backend.prepare_squeezed_state(r, phi, 0) backend.squeeze(r, phi, 1) state = backend.state() if isinstance(backend, backends.BaseFock): in_state = utils.squeezed_state(r, phi, basis="fock", fock_dim=cutoff, hbar=hbar) else: in_state = utils.squeezed_state(r, phi, basis="gaussian", hbar=hbar) assert np.allclose(state.fidelity(in_state, 0), 1, atol=tol, rtol=0) assert np.allclose(state.fidelity(in_state, 1), 1, atol=tol, rtol=0)
def test_squeezed(self, r, phi, setup_eng, hbar, tol): """Test squeezed function matches Gaussian backends""" eng, prog = setup_eng(1) with prog.context as q: ops.Sgate(r, phi) | q[0] state = eng.run(prog).state mu, cov = utils.squeezed_state(r, phi, basis="gaussian", hbar=hbar) if eng.backend_name == "gassian": mu_exp, cov_exp = state.reduced_gaussian(0) assert np.allclose(mu, mu_exp, atol=tol, rtol=0) assert np.allclose(cov, cov_exp, atol=tol, rtol=0) elif eng.backend_name == "bosonic": _, mu_exp, cov_exp = state.reduced_bosonic(0) assert np.allclose(np.expand_dims(mu, axis=0), mu_exp, atol=tol, rtol=0) assert np.allclose(np.expand_dims(cov, axis=0), cov_exp, atol=tol, rtol=0)
def test_squeezed_state_fock(self, r, phi, cutoff, hbar, tol): """test squeezed state returns correct Fock basis state vector""" state = utils.squeezed_state(r, phi, basis="fock", fock_dim=cutoff, hbar=hbar) n = np.arange(cutoff) kets = (np.sqrt(fac(2 * (n // 2))) / (2 ** (n // 2) * fac(n // 2))) * ( -np.exp(1j * phi) * np.tanh(r) ) ** (n // 2) expected = np.where(n % 2 == 0, np.sqrt(1 / np.cosh(r)) * kets, 0) assert np.allclose(state, expected, atol=tol, rtol=0)
def test_squeezed(self, r, phi, setup_eng, hbar, tol): """Test squeezed function matches Gaussian backends""" eng, prog = setup_eng(1) with prog.context as q: ops.Sgate(r, phi) | q[0] state = eng.run(prog) mu, cov = utils.squeezed_state(r, phi, basis="gaussian", hbar=hbar) mu_exp, cov_exp = state.reduced_gaussian(0) assert np.allclose(mu, mu_exp, atol=tol, rtol=0) assert np.allclose(cov, cov_exp, atol=tol, rtol=0)
def test_squeezed(self, setup_eng, cutoff, hbar, tol): eng, prog = setup_eng(3) r = 0.05 phi = 0 cov = (hbar / 2) * np.diag([np.exp(-2 * r)] * 3 + [np.exp(2 * r)] * 3) in_state = squeezed_state(r, phi, basis="fock", fock_dim=cutoff) with prog.context as q: ops.Gaussian(cov) | q state = eng.run(prog).state assert len(eng.run_progs[-1]) == 3 for n in range(3): assert np.allclose(state.fidelity(in_state, n), 1, atol=tol)
def test_squeezed(self, setup_eng, cutoff, hbar, tol): eng, q = setup_eng(3) r = 0.05 phi = 0 cov = (hbar / 2) * np.diag([np.exp(-2 * r)] * 3 + [np.exp(2 * r)] * 3) in_state = squeezed_state(r, phi, basis="fock", fock_dim=cutoff) 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_squeezed_state_gaussian(self, r, phi, hbar, tol): """test squeezed state returns correct means and covariance""" means, cov = utils.squeezed_state(r, phi, basis="gaussian", hbar=hbar) cov_expected = (hbar / 2) * np.array([ [ np.cosh(2 * r) - np.cos(phi) * np.sinh(2 * r), -2 * np.cosh(r) * np.sin(phi) * np.sinh(r), ], [ -2 * np.cosh(r) * np.sin(phi) * np.sinh(r), np.cosh(2 * r) + np.cos(phi) * np.sinh(2 * r), ], ]) assert np.all(means == np.zeros([2])) assert np.allclose(cov, cov_expected, atol=tol, rtol=0)
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_rotated_squeezed(self, setup_eng, cutoff, hbar, tol): eng, prog = 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)]) cov = xpxp_to_xxpp(block_diag(*[rot(phi) @ v1 @ rot(phi).T] * 3)) with prog.context as q: ops.Gaussian(cov) | q state = eng.run(prog).state assert len(eng.run_progs[-1]) == 3 for n in range(3): assert np.allclose(state.fidelity(in_state, n), 1, atol=tol)
def test_squeezed(self, setup_eng, hbar, cutoff, bsize, pure, tol): """Test squeezed function matches Fock backends""" eng, prog = setup_eng(1) r = 0.112 phi = 0.123 with prog.context as q: ops.Sgate(r, phi) | q[0] state = eng.run(prog).state ket = utils.squeezed_state(r, phi, basis="fock", fock_dim=cutoff, hbar=hbar) if not pure: expected = state.dm() ket = np.tile(np.outer(ket, ket.conj()), (bsize, 1, 1)) else: expected = state.ket() ket = np.tile(ket, (bsize, 1)) assert np.allclose(expected, ket, atol=tol, rtol=0)