def test_coherent(self, a, setup_eng, hbar, tol): """Test coherent function matches Gaussian backends""" eng, prog = setup_eng(1) with prog.context as q: ops.Dgate(np.abs(a), np.angle(a)) | q[0] state = eng.run(prog).state mu, cov = utils.coherent_state(np.abs(a), np.angle(a), 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_coherent_state_gaussian(self, r_d, phi_d, hbar): """test coherent state returns correct means and covariance""" means, cov = utils.coherent_state(r_d, phi_d, basis="gaussian", hbar=hbar) alpha = r_d * np.exp(1j * phi_d) means_expected = np.array([alpha.real, alpha.imag]) * np.sqrt(2 * hbar) assert np.all(means == means_expected) assert np.all(cov == np.identity(2) * hbar / 2)
def test_coherent_state_fock(self, r_d, phi_d, cutoff, hbar, tol): """test coherent state returns correct Fock basis state vector""" state = utils.coherent_state(r_d, phi_d, basis="fock", fock_dim=cutoff, hbar=hbar) n = np.arange(cutoff) alpha = r_d * np.exp(1j * phi_d) expected = np.exp(-0.5 * np.abs(alpha) ** 2) * alpha ** n / np.sqrt(fac(n)) assert np.allclose(state, expected, atol=tol, rtol=0)
def test_coherent_fidelity(self, setup_backend, cutoff, tol, hbar): backend = setup_backend(2) backend.prepare_coherent_state(np.abs(a), np.angle(a), 0) backend.displacement(np.abs(a), np.angle(a), 1) state = backend.state() if isinstance(backend, backends.BaseFock): in_state = utils.coherent_state( np.abs(a), np.angle(a), basis="fock", fock_dim=cutoff, hbar=hbar ) else: in_state = utils.coherent_state(np.abs(a), np.angle(a), 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) assert np.allclose(state.fidelity_coherent([a, a]), 1, atol=tol, rtol=0)
def test_coherent(self, a, setup_eng, hbar, tol): """Test coherent function matches Gaussian backends""" eng, prog = setup_eng(1) with prog.context as q: ops.Dgate(a) | q[0] state = eng.run(prog) mu, cov = utils.coherent_state(a, 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_coherent(self, setup_eng, hbar, cutoff, bsize, pure, tol): """Test coherent function matches Fock backends""" eng, prog = setup_eng(1) a = 0.32 + 0.1j with prog.context as q: ops.Dgate(a) | q[0] state = eng.run(prog).state ket = utils.coherent_state(a, 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)