def test_even_validation(self):
     """Test that the graph_embed decomposition raises exception if not even number of rows"""
     A = np.random.random([5, 5]) + 1j * np.random.random([5, 5])
     A += A.T
     with pytest.raises(ValueError,
                        match="must have an even number of rows/columns"):
         dec.williamson(A)
예제 #2
0
    def test_williamson_BM_random_circuit_pure(self):
        self.logTestName()
        for k in range(nsamples):
            n = 3
            U2 = haar_measure(n)
            state = gaussiancircuit.GaussianModes(n, hbar=2)
            for i in range(n):
                state.squeeze(np.log(0.2 * i + 2), 0, i)
            state.apply_u(U2)

            V = state.scovmatxp()

            D, S = dec.williamson(V)
            omega = dec.sympmat(n)

            self.assertAlmostEqual(np.linalg.norm(S @ omega @ S.T - omega), 0)
            self.assertAlmostEqual(np.linalg.norm(S @ D @ S.T - V), 0)

            O, s, Oo = dec.bloch_messiah(S)
            self.assertAlmostEqual(
                np.linalg.norm(np.transpose(O) @ omega @ O - omega), 0)
            self.assertAlmostEqual(
                np.linalg.norm(np.transpose(O) @ O - np.identity(2 * n)), 0)

            self.assertAlmostEqual(
                np.linalg.norm(np.transpose(Oo) @ omega @ Oo - omega), 0)
            self.assertAlmostEqual(
                np.linalg.norm(np.transpose(Oo) @ Oo - np.identity(2 * n)), 0)
            self.assertAlmostEqual(
                np.linalg.norm(np.transpose(s) @ omega @ s - omega), 0)
            self.assertAlmostEqual(np.linalg.norm(O @ s @ Oo - S), 0)
    def test_pure_state(self, create_cov, hbar, tol):
        """Test pure state"""
        n = 3
        O = omega(n)

        cov, _ = create_cov(np.zeros([n]))

        Db, S = dec.williamson(cov)
        nbar = np.diag(Db) / hbar - 0.5

        # check decomposition is correct
        assert np.allclose(S @ Db @ S.T, cov, atol=tol, rtol=0)
        # check nbar = 0
        assert np.allclose(nbar, 0, atol=tol, rtol=0)
        # check S is symplectic
        assert np.allclose(S @ O @ S.T, O, atol=tol, rtol=0)
    def test_mixed_state(self, create_cov, hbar, tol):
        """Test mixed state"""
        n = 3
        O = omega(n)
        nbar_in = np.abs(np.random.random(n))

        cov, _ = create_cov(nbar_in)

        Db, S = dec.williamson(cov)
        nbar = np.diag(Db) / hbar - 0.5

        # check decomposition is correct
        assert np.allclose(S @ Db @ S.T, cov, atol=tol, rtol=0)
        # check nbar
        assert np.allclose(sorted(nbar[:n]), sorted(nbar_in), atol=tol, rtol=0)
        # check S is symplectic
        assert np.allclose(S @ O @ S.T, O, atol=tol, rtol=0)
 def test_vacuum_state(self, tol, hbar):
     """Test vacuum state"""
     V = np.identity(4)
     Db, S = dec.williamson(V)
     assert np.allclose(Db, np.identity(4), atol=tol, rtol=0)
     assert np.allclose(S, np.identity(4), atol=tol, rtol=0)
 def test_positive_definite_validation(self):
     """Test that the graph_embed decomposition raises exception if not positive definite"""
     A = np.diag([-2, 0.1, 2, 3])
     with pytest.raises(ValueError,
                        match="matrix is not positive definite"):
         dec.williamson(A)
 def test_symmetric_validation(self):
     """Test that the graph_embed decomposition raises exception if not symmetric"""
     A = np.random.random([5, 5]) + 1j * np.random.random([5, 5])
     with pytest.raises(ValueError, match="matrix is not symmetric"):
         dec.williamson(A)