Esempio n. 1
0
    def test_quadratic_phase(self):
        """Test quadratic phase gives correct means and cov"""
        self.eng.reset()
        q = self.eng.register

        x = 0.2
        p = 0.3
        s = 0.432
        H, t = quadratic_phase(s)

        with self.eng:
            Xgate(x) | q[0]
            Zgate(p) | q[0]
            GaussianPropagation(H, t) | q[0]

        state = self.eng.run('gaussian')

        # test the covariance matrix
        res = state.cov()
        expected = np.array([[1, s], [s, 1 + s**2]]) * self.hbar / 2
        self.assertTrue(np.allclose(res, expected))

        # test the vector of means
        res = state.means()
        expected = np.array([x, p + s * x])
        self.assertTrue(np.allclose(res, expected))
    def test_quadratic_phase(self, hbar):
        """Test quadratic phase gives correct means and cov"""
        prog = sf.Program(1)
        eng = sf.Engine("gaussian")

        x = 0.2
        p = 0.3
        s = 0.432
        H, t = quadratic_phase(s)

        with prog.context as q:
            Xgate(x) | q[0]
            Zgate(p) | q[0]
            GaussianPropagation(H, t) | q[0]

        state = eng.run(prog).state

        # test the covariance matrix
        res = state.cov()
        expected = np.array([[1, s], [s, 1 + s**2]]) * hbar / 2
        assert np.allclose(res, expected)

        # test the vector of means
        res = state.means()
        expected = np.array([x, p + s * x])
        assert np.allclose(res, expected)
Esempio n. 3
0
    def test_displaced_oscillator(self):
        """Test that a forced quantum oscillator produces the correct
        self.logTestName()
        trajectory in the phase space"""
        H = QuadOperator('q0 q0', 0.5)
        H += QuadOperator('p0 p0', 0.5)
        H -= QuadOperator('q0', self.F)

        res = []
        tlist = np.arange(0, self.t, self.dt)

        for t in tlist:  #pylint: disable=unused-variable
            self.eng.reset()
            q = self.eng.register
            with self.eng:
                Xgate(self.x0) | q[0]
                Zgate(self.p0) | q[0]
                GaussianPropagation(H, self.t) | q

            state = self.eng.run('gaussian')
            res.append(state.means().tolist())

        res = np.array(res)[-1]
        expected = self.displaced_oscillator_soln(self.t)
        self.assertTrue(np.allclose(res, expected))
    def test_squeezing(self, hbar):
        """Test squeezing gives correct means and cov"""
        prog = sf.Program(1)
        eng = sf.Engine("gaussian")

        x = 0.2
        p = 0.3
        r = 0.42
        phi = 0.123
        H, t = squeezing(r, phi, hbar=hbar)

        with prog.context as q:
            Xgate(x) | q[0]
            Zgate(p) | q[0]
            GaussianPropagation(H, t) | q[0]

        state = eng.run(prog).state

        # test the covariance matrix
        res = state.cov()
        S = rot(phi / 2) @ np.diag(np.exp([-r, r])) @ rot(phi / 2).T
        V = S @ S.T * hbar / 2
        assert np.allclose(res, V)

        # test the vector of means
        res = state.means()
        exp = S @ np.array([x, p])
        assert np.allclose(res, exp)
    def test_rotation(self, hbar):
        """Test rotation gives correct means and cov"""
        prog = sf.Program(1)
        eng = sf.Engine("gaussian")

        x = 0.2
        p = 0.3
        phi = 0.123
        H, t = rotation(phi, hbar=hbar)

        with prog.context as q:
            Xgate(x) | q[0]
            Zgate(p) | q[0]
            Sgate(2) | q[0]
            GaussianPropagation(H, t) | q[0]

        state = eng.run(prog).state

        # test the covariance matrix
        res = state.cov()
        V = np.diag([np.exp(-4), np.exp(4)]) * hbar / 2
        expected = rot(phi) @ V @ rot(phi).T
        assert np.allclose(res, expected)

        # test the vector of means
        res = state.means()
        exp = rot(phi) @ np.diag(np.exp([-2, 2])) @ np.array([x, p])
        assert np.allclose(res, exp)
Esempio n. 6
0
    def test_rotation(self):
        """Test rotation gives correct means and cov"""
        self.eng.reset()
        q = self.eng.register

        x = 0.2
        p = 0.3
        phi = 0.123
        H, t = rotation(phi, hbar=self.hbar)

        with self.eng:
            Xgate(x) | q[0]
            Zgate(p) | q[0]
            Sgate(2) | q[0]
            GaussianPropagation(H, t) | q[0]

        state = self.eng.run('gaussian')

        # test the covariance matrix
        res = state.cov()
        V = np.diag([np.exp(-4), np.exp(4)]) * self.hbar / 2
        expected = rot(phi) @ V @ rot(phi).T
        self.assertTrue(np.allclose(res, expected))

        # test the vector of means
        res = state.means()
        exp = rot(phi) @ np.diag(np.exp([-2, 2])) @ np.array([x, p])
        self.assertTrue(np.allclose(res, exp))
    def test_displaced_oscillator(self):
        """Test that a forced quantum oscillator produces the correct
        self.logTestName()
        trajectory in the phase space"""
        H = QuadOperator('q0 q0', 0.5)
        H += QuadOperator('p0 p0', 0.5)
        H -= QuadOperator('q0', self.F)

        res = []
        tlist = np.arange(0, self.t, self.dt)

        eng = sf.Engine("gaussian")

        for idx, t in enumerate(tlist):  #pylint: disable=unused-variable
            prog = sf.Program(1)

            with prog.context as q:
                Xgate(self.x0) | q[0]
                Zgate(self.p0) | q[0]
                GaussianPropagation(H, self.t) | q

            state = eng.run(prog).state
            eng.reset()
            res.append(state.means().tolist())

        res = np.array(res)[-1]
        expected = self.displaced_oscillator_soln(self.t)
        assert np.allclose(res, expected)
Esempio n. 8
0
    def test_squeezing(self):
        """Test squeezing gives correct means and cov"""
        self.eng.reset()
        q = self.eng.register

        x = 0.2
        p = 0.3
        r = 0.42
        phi = 0.123
        H, t = squeezing(r, phi, hbar=self.hbar)

        with self.eng:
            Xgate(x) | q[0]
            Zgate(p) | q[0]
            GaussianPropagation(H, t) | q[0]

        state = self.eng.run('gaussian')

        # test the covariance matrix
        res = state.cov()
        S = rot(phi / 2) @ np.diag(np.exp([-r, r])) @ rot(phi / 2).T
        V = S @ S.T * self.hbar / 2
        self.assertTrue(np.allclose(res, V))

        # test the vector of means
        res = state.means()
        exp = S @ np.array([x, p])
        self.assertTrue(np.allclose(res, exp))
Esempio n. 9
0
    def H_circuit(self, H, t):
        """Test circuit for Gaussian Hamiltonian"""
        self.eng.reset()
        q = self.eng.register
        with self.eng:
            q = init_layer(q)
            GaussianPropagation(H, t) | q

        state = self.eng.run('gaussian')
        return state.means(), state.cov()
Esempio n. 10
0
    def test_outside_context(self):
        """test setting hbar outside of engine context"""
        H, t = rotation(self.phi)
        Ugate = GaussianPropagation(H, t, hbar=self.hbar)
        resD, resV = self.ref_circuit(Ugate)
        expD, expV = self.ref_circuit(Rgate(self.phi))

        # test the covariance matrix
        self.assertTrue(np.allclose(resV, expV))
        # test the vector of means
        self.assertTrue(np.allclose(resD, expD))
Esempio n. 11
0
    def H_circuit(self, H, t):
        """Test circuit for Gaussian Hamiltonian"""
        prog = sf.Program(2)
        eng = sf.Engine("gaussian")

        with prog.context as q:
            q = init_layer(q)
            GaussianPropagation(H, t) | q

        state = eng.run(prog).state
        return state.means(), state.cov()
Esempio n. 12
0
    def H_circuit(self, H, t):
        """Test circuit for Gaussian Hamiltonian"""
        self.eng.reset()
        q = self.eng.register
        with self.eng:
            # pylint: disable=pointless-statement
            q = init_layer(q)
            Xgate(0.1) | q[2]
            Sgate(0.1) | q[2]
            GaussianPropagation(H, t, mode='global') | q

        state = self.eng.run('gaussian')
        return state.means(), state.cov()
Esempio n. 13
0
    def test_singular_coefficients(self):
        """Test that H=p^2/2+q has displacement (q,t)=(-t^2,-t)"""
        self.eng.reset()
        q = self.eng.register

        H = QuadOperator('p0 p0', 0.5) + QuadOperator('q0')

        with self.eng:
            GaussianPropagation(H, self.t) | q[0]

        state = self.eng.run('gaussian')
        res = state.means()
        expected = [-self.t**2 / 2, -self.t]
        self.assertTrue(np.allclose(res, expected))
Esempio n. 14
0
    def H_circuit(self, H, t):
        """Test circuit for Gaussian Hamiltonian"""
        prog = sf.Program(3)
        eng = sf.Engine("gaussian")

        with prog.context as q:
            # pylint: disable=pointless-statement
            q = init_layer(q)
            Xgate(0.1) | q[2]
            Sgate(0.1) | q[2]
            GaussianPropagation(H, t, mode='global') | q

        state = eng.run(prog).state
        return state.means(), state.cov()
Esempio n. 15
0
    def test_singular_coefficients(self):
        """Test that H=p^2/2+q has displacement (q,t)=(-t^2,-t)"""
        prog = sf.Program(1)
        eng = sf.Engine("gaussian")

        H = QuadOperator('p0 p0', 0.5) + QuadOperator('q0')

        with prog.context as q:
            GaussianPropagation(H, self.t) | q[0]

        state = eng.run(prog).state
        res = state.means()
        expected = [-self.t**2 / 2, -self.t]
        assert np.allclose(res, expected)
Esempio n. 16
0
    def test_displacement(self, hbar):
        """Test displacement gives correct means and cov"""
        prog = sf.Program(1)
        eng = sf.Engine("gaussian")

        a = 0.2 + 0.3j
        H, t = displacement(a, hbar=hbar)

        with prog.context as q:
            GaussianPropagation(H, t) | q[0]

        state = eng.run(prog).state

        # test the covariance matrix
        res = state.cov()
        expected = np.identity(2) * hbar / 2
        assert np.allclose(res, expected)

        # test the vector of means
        res = state.means()
        expected = np.array([a.real, a.imag]) * np.sqrt(2 * hbar)
        assert np.allclose(res, expected)
Esempio n. 17
0
    def test_displacement(self):
        """Test displacement gives correct means and cov"""
        self.eng.reset()
        q = self.eng.register

        a = 0.2 + 0.3j
        H, t = displacement(a, hbar=self.hbar)

        with self.eng:
            GaussianPropagation(H, t) | q[0]

        state = self.eng.run('gaussian')

        # test the covariance matrix
        res = state.cov()
        expected = np.identity(2) * self.hbar / 2
        self.assertTrue(np.allclose(res, expected))

        # test the vector of means
        res = state.means()
        expected = np.array([a.real, a.imag]) * np.sqrt(2 * self.hbar)
        self.assertTrue(np.allclose(res, expected))
Esempio n. 18
0
import strawberryfields as sf
from strawberryfields.ops import *
from sfopenboson.ops import GaussianPropagation

# define the Hamiltonian
H = QuadOperator('q0 q0', 0.5) + QuadOperator('p0 p0', 0.5) - QuadOperator(
    'q0', 2)

# create the engine
eng, q = sf.Engine(1, hbar=2)

# set the time-steps
t_vals = np.arange(0, 6, 0.1)
results = np.zeros([2, len(t_vals)])

# evalutate the circuit at each time-step
for step, t in enumerate(t_vals):
    eng.reset()
    with eng:
        Xgate(1) | q[0]
        Zgate(0.5) | q[0]
        GaussianPropagation(H, t) | q

    state = eng.run('gaussian')
    results[:, step] = state.means()

# plot the results
plt.plot(*results)
plt.savefig('forced_oscillator.png')
Esempio n. 19
0
 def test_outside_context_no_hbar(self):
     """test exception if no hbar outside of engine context"""
     with self.assertRaises(ValueError):
         H, t = rotation(self.phi)
         GaussianPropagation(H, t)
Esempio n. 20
0
 def test_non_hermitian(self):
     """test exception if H is not hermitian"""
     with pytest.raises(ValueError):
         H = QuadOperator('q0', 1 + 2j)
         GaussianPropagation(H)
Esempio n. 21
0
 def test_non_hermitian(self):
     """test exception if H is not hermitian"""
     with self.assertRaises(ValueError):
         H = QuadOperator('q0', 1 + 2j)
         GaussianPropagation(H, hbar=2.)