def test_1x2_tf(self, hbar, tol): """Test a 1x2 lattice Bose-Hubbard model using TF""" try: import tensorflow as tf except (ImportError, ModuleNotFoundError): pytest.skip("TensorFlow not installed.") if tf.__version__[:3] != "1.3": pytest.skip("Incorrect TensorFlow version") sf.hbar = hbar prog = sf.Program(2) H = bose_hubbard(1, 2, self.J, self.U) with prog.context as q: Fock(2) | q[0] BoseHubbardPropagation(H, self.t, self.k) | q eng = sf.Engine("tf", backend_options={"cutoff_dim": 7}) state = eng.run(prog).state Hm = -self.J*np.sqrt(2)*np.array([[0, 1, 0], [1, 0, 1], [0, 1, 0]]) \ + self.U*np.diag([1, 0, 1]) init_state = np.array([1, 0, 0]) exp = np.abs(np.dot(expm(-1j * self.t * Hm), init_state))**2 assert np.allclose(state.fock_prob([2, 0]), exp[0], rtol=tol) assert np.allclose(state.fock_prob([1, 1]), exp[1], rtol=tol) assert np.allclose(state.fock_prob([0, 2]), exp[2], rtol=tol)
def test_hbar_outside_context(self): """Tests setting hbar outside the engine""" self.eng.reset() q = self.eng.register HBH = BoseHubbardPropagation(self.Hquad, self.t, self.k, hbar=self.hbar) with self.eng: # pylint: disable=pointless-statement Fock(2) | q[0] HBH | q state = self.eng.run('fock', cutoff_dim=7) Hm = -self.J*np.sqrt(2)*np.array([[0, 1, 0], [1, 0, 1], [0, 1, 0]]) \ + self.U*np.diag([1, 0, 1]) init_state = np.array([1, 0, 0]) exp = np.abs(np.dot(expm(-1j * self.t * Hm), init_state))**2 self.assertTrue( np.allclose(state.fock_prob([2, 0]), exp[0], rtol=self.tol)) self.assertTrue( np.allclose(state.fock_prob([1, 1]), exp[1], rtol=self.tol)) self.assertTrue( np.allclose(state.fock_prob([0, 2]), exp[2], rtol=self.tol))
def test_circulant(self): """Test a 3-cycle Bose-Hubbard model in local mode""" self.logTestName() self.eng.reset() q = self.eng.register # tunnelling terms H = BosonOperator('0 1^', -self.J) + BosonOperator('0^ 1', -self.J) H += BosonOperator('0 2^', -self.J) + BosonOperator('0^ 2', -self.J) H += BosonOperator('1 2^', -self.J) + BosonOperator('1^ 2', -self.J) # on-site interactions H += BosonOperator('0^ 0 0^ 0', 0.5 * self.U) - BosonOperator( '0^ 0', 0.5 * self.U) H += BosonOperator('1^ 1 1^ 1', 0.5 * self.U) - BosonOperator( '1^ 1', 0.5 * self.U) H += BosonOperator('2^ 2 2^ 2', 0.5 * self.U) - BosonOperator( '2^ 2', 0.5 * self.U) with self.eng: Fock(2) | q[0] BoseHubbardPropagation(H, self.t, 50) | q state = self.eng.run('fock', cutoff_dim=3) # 2D hamiltonian A = np.array([[0, 1, 1], [1, 0, 1], [1, 1, 0]]) A2 = np.kron(A, np.identity(3)) + np.kron(np.identity(3), A) # change of basis matrix B = np.zeros([6, 9]) B[0, 0] = B[5, 8] = B[3, 4] = 1 B[1, 1] = B[1, 3] = 1 / np.sqrt(2) B[2, 2] = B[2, 6] = 1 / np.sqrt(2) B[4, 5] = B[4, 7] = 1 / np.sqrt(2) # create Hamiltonian and add interaction term Hm = B @ (self.J * A2) @ B.T Hm[0, 0] = Hm[3, 3] = Hm[5, 5] = self.U init_state = np.zeros([6]) init_state[0] = 1 exp = np.abs(np.dot(expm(-1j * self.t * Hm), init_state))**2 tol = 1e-1 self.assertTrue( np.allclose(state.fock_prob([2, 0, 0]), exp[0], rtol=tol)) self.assertTrue( np.allclose(state.fock_prob([1, 1, 0]), exp[1], rtol=tol)) self.assertTrue( np.allclose(state.fock_prob([1, 0, 1]), exp[2], rtol=tol)) self.assertTrue( np.allclose(state.fock_prob([0, 2, 0]), exp[3], rtol=tol)) self.assertTrue( np.allclose(state.fock_prob([0, 1, 1]), exp[4], rtol=tol)) self.assertTrue( np.allclose(state.fock_prob([0, 0, 2]), exp[5], rtol=tol))
def test_1x2_no_onsite(self, eng, tol): """Test a 1x2 lattice Bose-Hubbard model""" prog = sf.Program(2) H = bose_hubbard(1, 2, self.J, 0) with prog.context as q: Fock(2) | q[0] BoseHubbardPropagation(H, self.t, self.k) | q state = eng.run(prog).state Hm = -self.J * np.sqrt(2) * np.array([[0, 1, 0], [1, 0, 1], [0, 1, 0]]) init_state = np.array([1, 0, 0]) exp = np.abs(np.dot(expm(-1j * self.t * Hm), init_state))**2 assert np.allclose(state.fock_prob([2, 0]), exp[0], rtol=tol) assert np.allclose(state.fock_prob([1, 1]), exp[1], rtol=tol) assert np.allclose(state.fock_prob([0, 2]), exp[2], rtol=tol)
def test_global(self, eng, tol): """Test a 1x2 lattice Bose-Hubbard model in global mode""" prog = sf.Program(3) with prog.context as q: Sgate(0.1) | q[2] Fock(2) | q[0] BoseHubbardPropagation(self.H, self.t, self.k, mode='global') | q state = eng.run(prog, run_options={"modes": [0, 1]}).state Hm = -self.J*np.sqrt(2)*np.array([[0, 1, 0], [1, 0, 1], [0, 1, 0]]) \ + self.U*np.diag([1, 0, 1]) init_state = np.array([1, 0, 0]) exp = np.abs(np.dot(expm(-1j * self.t * Hm), init_state))**2 assert np.allclose(state.fock_prob([2, 0]), exp[0], rtol=tol) assert np.allclose(state.fock_prob([1, 1]), exp[1], rtol=tol) assert np.allclose(state.fock_prob([0, 2]), exp[2], rtol=tol)
def test_1x2_quadrature_operators(self, eng, tol): """Test a 1x2 lattice Bose-Hubbard model by passing quadrature operators instead of ladder operators""" prog = sf.Program(2) H = get_quad_operator(bose_hubbard(1, 2, self.J, self.U), hbar=2) with prog.context as q: Fock(2) | q[0] BoseHubbardPropagation(H, self.t, self.k) | q state = eng.run(prog).state Hm = -self.J*np.sqrt(2)*np.array([[0, 1, 0], [1, 0, 1], [0, 1, 0]]) \ + self.U*np.diag([1, 0, 1]) init_state = np.array([1, 0, 0]) exp = np.abs(np.dot(expm(-1j * self.t * Hm), init_state))**2 assert np.allclose(state.fock_prob([2, 0]), exp[0], rtol=tol) assert np.allclose(state.fock_prob([1, 1]), exp[1], rtol=tol) assert np.allclose(state.fock_prob([0, 2]), exp[2], rtol=tol)
def test_three_mode_fock_state_mean_photon(self, setup_eng, tol): """Tests that the mean photon numbers of a three-mode system, where one mode is prepared in a Fock state, are correct.""" eng, prog = setup_eng(3) with prog.context as q: Fock(1) | q[1] result = eng.run(prog) state = result.state nbar0, var_n0 = state.mean_photon(0) nbar1, var_n1 = state.mean_photon(1) nbar2, var_n2 = state.mean_photon(2) assert np.allclose(nbar0, 0.0, atol=tol, rtol=0) assert np.allclose(nbar1, 1.0, atol=tol, rtol=0) assert np.allclose(nbar2, 0.0, atol=tol, rtol=0) assert np.allclose(var_n0, 0.0, atol=tol, rtol=0) assert np.allclose(var_n1, 0.0, atol=tol, rtol=0) assert np.allclose(var_n2, 0.0, atol=tol, rtol=0)
def test_local(self): """Test a 1x2 lattice Bose-Hubbard model in local mode""" self.eng.reset() q = self.eng.register with self.eng: Sgate(0.1) | q[1] Fock(2) | q[0] BoseHubbardPropagation(self.H, self.t, self.k) | (q[0], q[2]) state = self.eng.run('fock', cutoff_dim=7, modes=[0, 2]) Hm = -self.J*np.sqrt(2)*np.array([[0, 1, 0], [1, 0, 1], [0, 1, 0]]) \ + self.U*np.diag([1, 0, 1]) init_state = np.array([1, 0, 0]) exp = np.abs(np.dot(expm(-1j * self.t * Hm), init_state))**2 self.assertTrue( np.allclose(state.fock_prob([2, 0]), exp[0], rtol=self.tol)) self.assertTrue( np.allclose(state.fock_prob([1, 1]), exp[1], rtol=self.tol)) self.assertTrue( np.allclose(state.fock_prob([0, 2]), exp[2], rtol=self.tol))
def test_1x2(self): """Test a 1x2 lattice Bose-Hubbard model""" self.eng.reset() q = self.eng.register H = bose_hubbard(1, 2, self.J, self.U) with self.eng: Fock(2) | q[0] BoseHubbardPropagation(H, self.t, self.k) | q state = self.eng.run('fock', cutoff_dim=7) Hm = -self.J*np.sqrt(2)*np.array([[0, 1, 0], [1, 0, 1], [0, 1, 0]]) \ + self.U*np.diag([1, 0, 1]) init_state = np.array([1, 0, 0]) exp = np.abs(np.dot(expm(-1j * self.t * Hm), init_state))**2 self.assertTrue( np.allclose(state.fock_prob([2, 0]), exp[0], rtol=self.tol)) self.assertTrue( np.allclose(state.fock_prob([1, 1]), exp[1], rtol=self.tol)) self.assertTrue( np.allclose(state.fock_prob([0, 2]), exp[2], rtol=self.tol))