コード例 #1
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, 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)
コード例 #2
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)
コード例 #3
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)
コード例 #4
0
    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)
コード例 #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)
コード例 #6
0
    def test_random_unitary_phase_end(self, tol):
        """This test checks the rectangular decomposition with phases at the end.

        A random unitary is drawn from the Haar measure, then is decomposed
        using Eq. 5 of the rectangular decomposition procedure of Clements et al,
        i.e., moving all the phases to the end of the interferometer. The
        resulting beamsplitters are multiplied together. Test passes if the
        product matches the drawn unitary.
        """
        n = 20
        U = haar_measure(n)

        tlist, diags, _ = dec.rectangular_phase_end(U)

        qrec = np.identity(n)

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

        qrec = np.diag(diags) @ qrec

        assert np.allclose(U, qrec, atol=tol, rtol=0)
コード例 #7
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)
コード例 #8
0
    def test_clements_phase_end_random_unitary(self):
        """This test checks the rectangular decomposition with phases at the end.

        A random unitary is drawn from the Haar measure, then is decomposed
        using Eq. 5 of the rectangular decomposition procedure of Clements et al
        , i.e., moving all the phases to the end of the interferometer. 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)

            new_tlist, new_diags = dec.clements_phase_end(U)

            U_rec = np.identity(n)
            for i in new_tlist:
                U_rec = dec.T(*i) @ U_rec
            U_rec = np.diag(new_diags) @ U_rec
            self.assertAlmostEqual(np.linalg.norm(U_rec - U),
                                   0,
                                   delta=self.tol)