def test_rectangular(self, U, tol):
        """This test checks the function :func:`dec.rectangular` for
        various unitary matrices.

        A given unitary (identity or random draw from Haar measure) is
        decomposed using the function :func:`dec.rectangular`
        and the resulting beamsplitters are multiplied together.

        Test passes if the product matches the given unitary.
        """
        nmax, mmax = U.shape
        assert nmax == mmax

        tilist, diags, tlist = dec.rectangular(U)

        qrec = np.identity(nmax)

        for i in tilist:
            qrec = dec.T(*i) @ qrec

        qrec = np.diag(diags) @ qrec

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

        assert np.allclose(U, qrec, atol=tol, rtol=0)
    def test_random_unitary(self, tol):
        """This test checks the rectangular decomposition for a random unitary.

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

        tilist, diags, tlist = dec.rectangular(U)

        qrec = np.identity(n)

        for i in tilist:
            qrec = dec.T(*i) @ qrec

        qrec = np.diag(diags) @ qrec

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

        assert np.allclose(U, qrec, atol=tol, rtol=0)
 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.rectangular(A)