def test_invalid_k(self):
        """Tests exception if k<=0 or k is non-integer"""
        with pytest.raises(ValueError):
            BoseHubbardPropagation(self.H, self.t, 0)

        with pytest.raises(ValueError):
            BoseHubbardPropagation(self.H, self.t, -2)

        with pytest.raises(ValueError):
            BoseHubbardPropagation(self.H, self.t, 7.12)
Exemplo n.º 2
0
    def test_invalid_k(self):
        """Tests exception if k<=0 or k is non-integer"""
        with self.assertRaises(ValueError):
            BoseHubbardPropagation(self.H, self.t, 0, hbar=self.hbar)

        with self.assertRaises(ValueError):
            BoseHubbardPropagation(self.H, self.t, -2, hbar=self.hbar)

        with self.assertRaises(ValueError):
            BoseHubbardPropagation(self.H, self.t, 7.12, hbar=self.hbar)
Exemplo n.º 3
0
    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_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)
Exemplo n.º 5
0
    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)
Exemplo n.º 9
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))
Exemplo n.º 10
0
    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))
Exemplo n.º 11
0
 def test_non_hermitian(self):
     """Tests exception if non-Hermitian H"""
     with self.assertRaises(ValueError):
         H = QuadOperator('q0', 1 + 2j)
         BoseHubbardPropagation(H, self.t, self.k, hbar=self.hbar)
Exemplo n.º 12
0
 def test_outside_context_no_hbar(self):
     """Tests exception if outside the engine with no hbar"""
     with self.assertRaises(ValueError):
         BoseHubbardPropagation(self.Hquad, self.t, self.k)
Exemplo n.º 13
0
from openfermion.hamiltonians import bose_hubbard

import strawberryfields as sf
from strawberryfields.ops import *
from sfopenboson.ops import BoseHubbardPropagation

H = bose_hubbard(1, 2, 1, 1.5)

eng, q = sf.Engine(2)
with eng:
    Fock(2) | q[1]
    BoseHubbardPropagation(H, 1.086, 20) | q

state = eng.run('fock', cutoff_dim=3)

print("Prob(2,0) = ", state.fock_prob([2,0]))
print("Prob(1,1) = ", state.fock_prob([1,1]))
print("Prob(0,2) = ", state.fock_prob([0,2]))