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, tlist, diags = dec.clements(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_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_clements_identity(self):
        n = 20
        U = np.identity(n)
        (tilist, tlist, diags) = dec.clements(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

        self.assertAllAlmostEqual(U, qrec, delta=self.tol)
    def test_clements_random_unitary(self):
        error = np.empty(nsamples)
        for k in range(nsamples):
            n = 20
            V = haar_measure(n)
            (tilist, tlist, diags) = dec.clements(V)
            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

            error[k] = np.linalg.norm(V - qrec)
        self.assertAlmostEqual(error.mean(), 0)
Beispiel #5
0
    def test_clements_identity(self):
        """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.
        """
        self.logTestName()
        n = 20
        U = np.identity(n)
        (tilist, tlist, diags) = dec.clements(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

        self.assertAllAlmostEqual(U, qrec, delta=self.tol)
    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(U)

        qrec = np.diag(diags)

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

        assert np.allclose(U, qrec, atol=tol, rtol=0)
Beispiel #7
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)
Beispiel #8
0
    def test_clements_random_unitary(self):
        """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.
        """
        self.logTestName()
        error = np.empty(nsamples)
        for k in range(nsamples):
            n = 20
            V = haar_measure(n)
            (tilist, tlist, diags) = dec.clements(V)
            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

            error[k] = np.linalg.norm(V - qrec)
        self.assertAlmostEqual(error.mean(), 0)