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. 2
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))
Esempio n. 3
0
 def test_quad_form(self, hbar):
     """Test it has the correct form using quadrature operators"""
     H, _ = rotation(self.phi, mode=1, hbar=hbar)
     H = normal_ordered(get_quad_operator(H, hbar=hbar), hbar=hbar)
     expected = QuadOperator('q1 q1', -1 / hbar)
     expected += QuadOperator('p1 p1', -1 / hbar)
     expected += QuadOperator('', 1)
     assert H == expected
Esempio n. 4
0
    def test_rotation_coeff(self, hbar):
        """Test quadratic coefficients for Rgate"""
        # one mode
        H, _ = rotation(0.23, hbar=hbar)
        res, d = quadratic_coefficients(get_quad_operator(H, hbar))
        expected = -np.diag(np.array([1, 1]))
        assert np.allclose(res, expected)
        assert np.allclose(d, np.zeros([2]))

        # two modes
        H, _ = rotation(0.23, mode=1, hbar=hbar)
        res, d = quadratic_coefficients(get_quad_operator(H, hbar))
        expected = np.zeros([4, 4])
        expected[1, 1] = -1
        expected[3, 3] = -1
        assert np.allclose(res, expected)
        assert np.allclose(d, np.zeros([4]))
Esempio n. 5
0
    def test_rotation_coeff(self):
        """Test quadratic coefficients for Rgate"""
        self.logTestName()
        # one mode
        H, _ = rotation(0.23, hbar=self.hbar)
        res, d = quadratic_coefficients(get_quad_operator(H, self.hbar))
        expected = -np.diag(np.array([1, 1]))
        self.assertTrue(np.allclose(res, expected))
        self.assertTrue(np.allclose(d, np.zeros([2])))

        # two modes
        H, _ = rotation(0.23, mode=1, hbar=self.hbar)
        res, d = quadratic_coefficients(get_quad_operator(H, self.hbar))
        expected = np.zeros([4, 4])
        expected[1, 1] = -1
        expected[3, 3] = -1
        self.assertTrue(np.allclose(res, expected))
        self.assertTrue(np.allclose(d, np.zeros([4])))
Esempio n. 6
0
 def test_quad_form(self):
     """Test it has the correct form using quadrature operators"""
     self.logTestName()
     H, _ = rotation(self.phi, mode=1, hbar=self.hbar)
     H = normal_ordered(get_quad_operator(H, hbar=self.hbar),
                        hbar=self.hbar)
     expected = QuadOperator('q1 q1', -0.5)
     expected += QuadOperator('p1 p1', -0.5)
     expected += QuadOperator('', 1)
     self.assertEqual(H, expected)
Esempio n. 7
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))
    def test_single_mode_gate(self, hbar):
        """Test Rgate gives correct means and cov in global mode"""
        H, t = rotation(self.phi, mode=1, hbar=hbar)
        resD, resV = self.H_circuit(H, t)

        gate = Rgate(self.phi)
        expD, expV = self.ref_circuit(gate, 1)

        # test the covariance matrix
        assert np.allclose(resV, expV)
        # test the vector of means
        assert np.allclose(resD, expD)
Esempio n. 9
0
    def test_single_mode_gate(self):
        """Test Rgate gives correct means and cov in global mode"""
        self.eng.reset()
        q = self.eng.register

        H, t = rotation(self.phi, mode=1, hbar=self.hbar)
        resD, resV = self.H_circuit(H, t)

        gate = Rgate(self.phi)
        expD, expV = self.ref_circuit(gate, q[1])

        # test the covariance matrix
        self.assertTrue(np.allclose(resV, expV))
        # test the vector of means
        self.assertTrue(np.allclose(resD, expD))
Esempio n. 10
0
 def test_identity(self):
     """Test alpha=0 gives identity"""
     _, r = rotation(0)
     assert r == 0
Esempio n. 11
0
 def test_coefficients(self):
     """Test coefficients are correct"""
     self.logTestName()
     H, _ = rotation(self.phi, hbar=self.hbar)
     self.assertEqual(H, -BosonOperator('0^ 0') * self.hbar)
Esempio n. 12
0
 def test_time(self):
     """Test time parameter is correct"""
     self.logTestName()
     _, r = rotation(self.phi, hbar=self.hbar)
     self.assertEqual(r, self.phi)
Esempio n. 13
0
 def test_gaussian(self):
     """Test output is gaussian"""
     self.logTestName()
     H, _ = rotation(self.phi, hbar=self.hbar)
     res = get_quad_operator(H, hbar=self.hbar).is_gaussian()
     self.assertTrue(res)
Esempio n. 14
0
 def test_hermitian(self):
     """Test output is hermitian"""
     self.logTestName()
     H, _ = rotation(self.phi, hbar=self.hbar)
     self.assertTrue(is_hermitian(H))
     self.assertTrue(is_hermitian(get_quad_operator(H)))
Esempio n. 15
0
 def test_identity(self):
     """Test alpha=0 gives identity"""
     self.logTestName()
     _, r = rotation(0)
     self.assertEqual(r, 0)
Esempio n. 16
0
 def test_hermitian(self, hbar):
     """Test output is hermitian"""
     H, _ = rotation(self.phi, hbar=hbar)
     assert is_hermitian(H)
     assert is_hermitian(get_quad_operator(H))
Esempio n. 17
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. 18
0
 def test_gaussian(self, hbar):
     """Test output is gaussian"""
     H, _ = rotation(self.phi, hbar=hbar)
     res = get_quad_operator(H, hbar=hbar).is_gaussian()
     assert res
Esempio n. 19
0
 def test_time(self, hbar):
     """Test time parameter is correct"""
     _, r = rotation(self.phi, hbar=hbar)
     assert r == self.phi
Esempio n. 20
0
 def test_coefficients(self, hbar):
     """Test coefficients are correct"""
     H, _ = rotation(self.phi, hbar=hbar)
     assert H == -BosonOperator('0^ 0') * hbar