Ejemplo n.º 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)
Ejemplo n.º 2
0
    def test_decomposition(self, tol):
        """Test that an interferometer is correctly decomposed"""
        n = 3
        prog = sf.Program(n)
        U = random_interferometer(n)
        BS1, BS2, R = dec.clements(U)

        G = ops.Interferometer(U)
        cmds = G.decompose(prog.register)

        S = np.identity(2 * n)

        # calculating the resulting decomposed symplectic
        for cmd in cmds:
            # all operations should be BSgates or Rgates
            assert isinstance(cmd.op, (ops.BSgate, ops.Rgate))

            # build up the symplectic transform
            modes = [i.ind for i in cmd.reg]

            if isinstance(cmd.op, ops.Rgate):
                S = _rotation(cmd.op.p[0].x, modes, n) @ S

            if isinstance(cmd.op, ops.BSgate):
                S = _beamsplitter(cmd.op.p[0].x, cmd.op.p[1].x, modes, n) @ S

        # the resulting applied unitary
        X1 = S[:n, :n]
        P1 = S[n:, :n]
        U_applied = X1 + 1j * P1

        assert np.allclose(U, U_applied, 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)
Ejemplo n.º 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)
Ejemplo n.º 6
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)
Ejemplo n.º 7
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.clements(A)