def test_extract_beamsplitter(self, backend_name, cutoff, tol):
        """test that the beamsplitter gate is correctly extracted"""
        prog = sf.Program(2)
        theta = 0.432
        phi = 0.765
        with prog.context as q:
            ops.BSgate(theta, phi) | q

        U = utils.extract_unitary(prog,
                                  cutoff_dim=cutoff,
                                  backend=backend_name)
        expected = bs_U(theta, phi, cutoff)

        if isinstance(U, tf.Tensor):
            U = U.numpy()

        assert np.allclose(U, expected, atol=tol, rtol=0)
    def test_extract_beamsplitter(self, setup_backend, cutoff, tol):
        """test that the beamsplitter gate is correctly extracted"""
        backend = setup_backend(2)
        eng, q = sf.Engine(2)

        theta = 0.432
        phi = 0.765

        with eng:
            ops.BSgate(theta, phi) | q

        U = utils.extract_unitary(eng,
                                  cutoff_dim=cutoff,
                                  backend=backend._short_name)
        expected = bs_U(np.cos(theta), np.sin(theta), phi, cutoff)

        if isinstance(U, tf.Tensor):
            U = tf.Session().run(U)

        assert np.allclose(U, expected, atol=tol, rtol=0)
예제 #3
0
    def test_extract_beamsplitter(self):
        """test that BSgate is correctly extracted"""
        self.logTestName()
        self.eng_sf.backend = None

        theta = 0.432
        phi = 0.765

        with self.eng_sf:
            BSgate(theta, phi) | self.q_sf

        U = extract_unitary(self.eng_sf,
                            cutoff_dim=self.D,
                            backend=self.backend_name)
        expected = bs_U(np.cos(theta), np.sin(theta), phi, self.D)

        if isinstance(U, tf.Tensor):
            U = tf.Session().run(U)

        self.assertAllAlmostEqual(U, expected, delta=self.tol)