def test_squeezed_coherent_fidelity(self, setup_backend, cutoff, tol, hbar): backend = setup_backend(2) backend.prepare_displaced_squeezed_state(np.abs(a), np.angle(a), r, phi, 0) backend.squeeze(r, phi, 1) backend.displacement(np.abs(a), np.angle(a), 1) state = backend.state() if isinstance(backend, backends.BaseFock): in_state = utils.displaced_squeezed_state(np.abs(a), np.angle(a), r, phi, basis="fock", fock_dim=cutoff, hbar=hbar) else: in_state = utils.displaced_squeezed_state(np.abs(a), np.angle(a), 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_multimode_gaussian_state(self, setup_backend, batch_size, pure, tol): """Test multimode Gaussian state preparation""" N = 4 backend = setup_backend(N) cov = np.diag(np.exp(2 * np.array([-1, -1, 1, 1]))) means = np.zeros([4]) # prepare displaced squeezed states in all modes a = 0.2 + 0.4j r = 0.5 phi = 0.12 for i in range(N): backend.prepare_displaced_squeezed_state(a, r, phi, i) # prepare new squeezed displaced state in mode 1 and 3 backend.prepare_gaussian_state(means, cov, modes=[1, 3]) state = backend.state([1, 3]) # test Gaussian state is correct state = backend.state([1, 3]) assert np.allclose(state.means(), means, atol=tol, rtol=0) assert np.allclose(state.cov(), cov, atol=tol, rtol=0) # test that displaced squeezed states are unchanged ex_means, ex_V = displaced_squeezed_state(a, r, phi, basis="gaussian") for i in [0, 2]: state = backend.state([i]) assert np.allclose(state.means(), ex_means, atol=tol, rtol=0) assert np.allclose(state.cov(), ex_V, atol=tol, rtol=0)
def test_singlemode_gaussian_state(self, setup_backend, batch_size, pure, tol): """Test single mode Gaussian state preparation""" N = 4 backend = setup_backend(N) means = 2 * np.random.random(size=[2]) - 1 cov = random_covariance(1, pure=pure) a = 0.2 + 0.4j r = 1 phi = 0 # circuit is initially in displaced squeezed state for i in range(N): backend.prepare_displaced_squeezed_state(a, r, phi, mode=i) # prepare Gaussian state in mode 1 backend.prepare_gaussian_state(means, cov, modes=1) # test Gaussian state is correct state = backend.state([1]) assert np.allclose(state.means(), means, atol=tol, rtol=0) assert np.allclose(state.cov(), cov, atol=tol, rtol=0) # test that displaced squeezed states are unchanged ex_means, ex_V = displaced_squeezed_state(a, r, phi, basis="gaussian") for i in [0, 2, 3]: state = backend.state([i]) assert np.allclose(state.means(), ex_means, atol=tol, rtol=0) assert np.allclose(state.cov(), ex_V, atol=tol, rtol=0)
def test_displaced_squeezed(self, setup_eng, hbar, cutoff, bsize, pure, tol): """Test displaced squeezed function matches Fock backends""" eng, prog = setup_eng(1) a = 0.32 + 0.1j r = 0.112 phi = 0.123 with prog.context as q: ops.Sgate(r, phi) | q[0] ops.Dgate(np.abs(a), np.angle(a)) | q[0] state = eng.run(prog).state ket = utils.displaced_squeezed_state(np.abs(a), np.angle(a), 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)
def test_displaced_squeezed(self, a, r, phi, setup_eng, hbar, tol): """Test displaced squeezed function matches Gaussian backends""" eng, prog = setup_eng(1) with prog.context as q: ops.Sgate(r, phi) | q[0] ops.Dgate(np.abs(a), np.angle(a)) | q[0] state = eng.run(prog).state mu, cov = utils.displaced_squeezed_state(np.abs(a), np.angle(a), 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_displaced_squeezed_state_fock(self, r_d, phi_d, r_s, phi_s, hbar, cutoff, tol): """test displaced squeezed state returns correct Fock basis state vector""" state = utils.displaced_squeezed_state(r_d, phi_d, r_s, phi_s, basis="fock", fock_dim=cutoff, hbar=hbar) a = r_d * np.exp(1j * phi_d) if r_s == 0: pytest.skip("test only non-zero squeezing") n = np.arange(cutoff) gamma = a * np.cosh(r_s) + np.conj(a) * np.exp( 1j * phi_s) * np.sinh(r_s) coeff = np.diag((0.5 * np.exp(1j * phi_s) * np.tanh(r_s))**(n / 2) / np.sqrt(fac(n) * np.cosh(r_s))) expected = H(gamma / np.sqrt(np.exp(1j * phi_s) * np.sinh(2 * r_s)), coeff) expected *= np.exp(-0.5 * np.abs(a)**2 - 0.5 * np.conj(a)**2 * np.exp(1j * phi_s) * np.tanh(r_s)) assert np.allclose(state, expected, atol=tol, rtol=0)
def test_displaced_squeezed_state_gaussian(self, r_d, phi_d, r_s, phi_s, hbar, tol): """test displaced squeezed state returns correct means and covariance""" means, cov = utils.displaced_squeezed_state(r_d, phi_d, r_s, phi_s, basis="gaussian", hbar=hbar) a = r_d * np.exp(1j * phi_d) means_expected = np.array([[a.real, a.imag]]) * np.sqrt(2 * hbar) cov_expected = (hbar / 2) * np.array([ [ np.cosh(2 * r_s) - np.cos(phi_s) * np.sinh(2 * r_s), -2 * np.cosh(r_s) * np.sin(phi_s) * np.sinh(r_s), ], [ -2 * np.cosh(r_s) * np.sin(phi_s) * np.sinh(r_s), np.cosh(2 * r_s) + np.cos(phi_s) * np.sinh(2 * r_s), ], ]) assert np.allclose(means, means_expected, atol=tol, rtol=0) assert np.allclose(cov, cov_expected, atol=tol, rtol=0)
def test_displaced_squeezed_fock_no_squeezing(self, a, phi, hbar, cutoff, tol): """test displaced squeezed state returns coherent state when there is no squeezing""" state = utils.displaced_squeezed_state(a, 0, phi, basis="fock", fock_dim=cutoff, hbar=hbar) n = np.arange(cutoff) expected = np.exp(-0.5 * np.abs(a) ** 2) * a ** n / np.sqrt(fac(n)) assert np.allclose(state, expected, atol=tol, rtol=0)
def test_displaced_squeezed_fock_no_displacement(self, r, phi, hbar, cutoff, tol): """test displaced squeezed state returns squeezed state when there is no displacement""" state = utils.displaced_squeezed_state(0, 0, 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_displaced_squeezed(self, a, r, phi, setup_eng, hbar, tol): """Test displaced squeezed function matches Gaussian backends""" eng, prog = setup_eng(1) with prog.context as q: ops.Sgate(r, phi) | q[0] ops.Dgate(a) | q[0] state = eng.run(prog) mu, cov = utils.displaced_squeezed_state(a, 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)