def test_identity(self, tol):
        """This test checks the rectangular decomposition for an identity unitary.

        An identity unitary is decomposed via the rectangular decomposition of
        Clements et al. and the resulting beamsplitters are multiplied together.
        Test passes if the product matches identity.
        """
        # TODO: this test currently uses the T and Ti functions used to compute
        # Clements as the comparison. Probably should be changed.
        n = 20
        U = np.identity(n)

        tlist, diags = dec.triangular_decomposition(U)

        qrec = np.diag(diags)

        for i in tlist:
            qrec = dec.Ti(*i) @ qrec

        assert np.allclose(U, qrec, atol=tol, rtol=0)
Esempio n. 2
0
    def test_triangular_decomposition_random_unitary(self):
        """This test checks the triangular decomposition for a random unitary.

        A random unitary is drawn from the Haar measure, then is decomposed
        using the triangular decomposition of Reck et al. and the resulting beamsplitters are multiplied together. Test passes if the product
        matches the drawn unitary.
        """
        self.logTestName()
        error = np.empty(nsamples)
        for k in range(nsamples):
            n = 20
            U = haar_measure(n)

            tlist, diags = dec.triangular_decomposition(U)

            U_passive_try = np.diag(diags)
            for i in tlist:
                U_passive_try = dec.Ti(*i) @ U_passive_try

            self.assertAlmostEqual(np.linalg.norm(U_passive_try - U),
                                   0,
                                   delta=self.tol)
 def test_unitary_validation(self):
     """Test that an exception is raised if not unitary"""
     A = np.random.random([5, 5]) + 1j * np.random.random([5, 5])
     with pytest.raises(ValueError, match="matrix is not unitary"):
         dec.triangular_decomposition(A)