Example #1
0
    def test_extract_unitary_1_mode_fock(self):
        """Test that unitary extraction works for 1 mode using the fock backend"""
        self.logTestName()
        eng_extract, q_extract = sf.Engine(num_subsystems=1)

        S = sf.ops.Sgate(0.4, -1.2)
        D = sf.ops.Dgate(2, 0.9)
        K = sf.ops.Kgate(-1.5)

        initial_state = np.random.rand(self.D) + 1j * np.random.rand(
            self.D)  # not a state but it doesn't matter

        with self.eng_sf:
            sf.ops.Ket(initial_state) | self.q_sf
            S | self.q_sf
            D | self.q_sf
            K | self.q_sf

        with eng_extract:
            S | q_extract
            D | q_extract
            K | q_extract

        matrix_extract = extract_unitary(eng_extract, cutoff_dim=self.D)
        final_state_extract = matrix_extract.dot(initial_state)
        final_state_sf = self.eng_sf.run().ket()

        self.assertAllAlmostEqual(final_state_extract,
                                  final_state_sf,
                                  delta=self.tol)
Example #2
0
    def setUp(self):
        super().setUp()
        self.eng, q = sf.Engine(self.num_subsystems, hbar=self.hbar)
        self.eng.backend = self.backend
        self.u1 = random_interferometer(self.num_subsystems)
        self.u2 = random_interferometer(self.num_subsystems)
        self.S = random_symplectic(self.num_subsystems)
        self.V_mixed = random_covariance(self.num_subsystems,
                                         hbar=self.hbar,
                                         pure=False)
        self.V_pure = random_covariance(self.num_subsystems,
                                        hbar=self.hbar,
                                        pure=True)
        self.A = np.array([[
            1.28931633 + 0.75228801j, 1.45557375 + 0.96825143j,
            1.53672608 + 1.465635j
        ],
                           [
                               1.45557375 + 0.96825143j,
                               0.37611686 + 0.84964159j,
                               1.25122856 + 1.28071385j
                           ],
                           [
                               1.53672608 + 1.465635j,
                               1.25122856 + 1.28071385j,
                               1.88217983 + 1.70869293j
                           ]])

        self.A -= np.trace(self.A) * np.identity(3) / 3
Example #3
0
    def test_extract_channel_2_modes(self):
        """Test that channel extraction works for 2 modes"""
        self.logTestName()

        eng_extract, q_extract = sf.Engine(num_subsystems=2)

        S = sf.ops.Sgate(2)
        B = sf.ops.BSgate(2.234, -1.165)

        initial_state = np.random.rand(
            self.D, self.D, self.D,
            self.D) + 1j * np.random.rand(self.D, self.D, self.D, self.D)

        with self.eng_sf:
            sf.ops.DensityMatrix(initial_state) | (self.q_sf[0], self.q_sf[1])
            S | self.q_sf[0]
            B | self.q_sf
            S | self.q_sf[1]
            B | self.q_sf

        with eng_extract:
            S | q_extract[0]
            B | q_extract
            S | q_extract[1]
            B | q_extract

        #vectorize = false
        choi = extract_channel(eng_extract,
                               cutoff_dim=self.D,
                               vectorize_modes=False,
                               representation='choi')
        liouville = extract_channel(eng_extract,
                                    cutoff_dim=self.D,
                                    vectorize_modes=False,
                                    representation='liouville')
        kraus = extract_channel(eng_extract,
                                cutoff_dim=self.D,
                                vectorize_modes=False,
                                representation='kraus')

        final_rho_sf = self.eng_sf.run().dm()

        final_rho_choi = np.einsum('abcdefgh,abcd -> efgh', choi,
                                   initial_state)
        final_rho_liouville = np.einsum('abcdefgh,fbhd -> eagc', liouville,
                                        initial_state)
        final_rho_kraus = np.einsum('abcde,cfeg,ahfig -> bhdi', kraus,
                                    initial_state, np.conj(kraus))

        self.assertAllAlmostEqual(final_rho_choi, final_rho_sf, delta=self.tol)
        self.assertAllAlmostEqual(final_rho_liouville,
                                  final_rho_sf,
                                  delta=self.tol)
        self.assertAllAlmostEqual(final_rho_kraus,
                                  final_rho_sf,
                                  delta=self.tol)
Example #4
0
 def setUp(self):
     super().setUp()
     self.eng, q = sf.Engine(self.num_subsystems, hbar=self.hbar)
     self.eng.backend = self.backend
     self.u1 = random_interferometer(self.num_subsystems)
     self.u2 = random_interferometer(self.num_subsystems)
     self.S = random_symplectic(self.num_subsystems)
     self.V_mixed = random_covariance(self.num_subsystems,
                                      hbar=self.hbar,
                                      pure=False)
     self.V_pure = random_covariance(self.num_subsystems,
                                     hbar=self.hbar,
                                     pure=True)
Example #5
0
    def test_extract_unitary_2_modes_fock(self):
        """Test that unitary extraction works for 2 modes using the fock backend"""
        self.logTestName()

        eng_extract, q_extract = sf.Engine(num_subsystems=2)

        S = sf.ops.Sgate(2)
        B = sf.ops.BSgate(2.234, -1.165)

        initial_state = np.complex64(
            np.random.rand(self.D, self.D) +
            1j * np.random.rand(self.D, self.D))

        with self.eng_sf:
            sf.ops.Ket(initial_state) | (self.q_sf[0], self.q_sf[1])
            S | self.q_sf[0]
            B | self.q_sf
            S | self.q_sf[1]
            B | self.q_sf

        with eng_extract:
            S | q_extract[0]
            B | q_extract
            S | q_extract[1]
            B | q_extract

        # calculate final states for vectorize_mode= True
        matrix_extract = extract_unitary(eng_extract,
                                         cutoff_dim=self.D,
                                         vectorize_modes=True)
        final_state_extract = np.tile(
            matrix_extract.dot(initial_state.reshape([-1])), self.bsize)
        final_state_sf = self.eng_sf.run().ket().reshape([-1])

        # calculate final states for vectorize_mode=False
        array_extract = extract_unitary(eng_extract,
                                        cutoff_dim=self.D,
                                        vectorize_modes=False)
        final_array_extract = np.einsum("abcd,bd->ac", array_extract,
                                        initial_state)
        final_array_sf = self.eng_sf.run().ket()

        self.assertAllAlmostEqual(final_state_extract,
                                  final_state_sf,
                                  delta=self.tol)
        self.assertAllAlmostEqual(final_array_extract,
                                  final_array_sf,
                                  delta=self.tol)
Example #6
0
    def setUp(self):
        """Set up."""
        super().setUp()
        self.eng, q = sf.Engine(self.num_subsystems, hbar=self.hbar)
        self.eng.backend = self.backend

        cutoff = 7
        if self.D < cutoff:
            logging.warning("Provided cutoff value is too small for "
                            "tests in `PolyQuadExpectationMultiModeTests`. "
                            "Cutoff has been increased internally.")
            self.D = cutoff
        self.backend.reset(cutoff_dim=self.D)

        if isinstance(self.backend, backends.TFBackend):
            raise unittest.SkipTest('The poly_quad_expectation method is not yet supported on the TF backend.')
Example #7
0
    def test_extract_channel_1_mode(self):
        """Test that channel extraction works for 1 mode"""
        self.logTestName()
        eng_extract, q_extract = sf.Engine(num_subsystems=1)

        S = sf.ops.Sgate(1.1, -1.4)
        L = sf.ops.LossChannel(0.45)

        initial_state = np.random.rand(self.D, self.D)

        with self.eng_sf:
            sf.ops.DensityMatrix(initial_state) | self.q_sf
            S | self.q_sf
            L | self.q_sf

        with eng_extract:
            S | q_extract
            L | q_extract

        choi = extract_channel(eng_extract,
                               cutoff_dim=self.D,
                               vectorize_modes=True,
                               representation='choi')
        liouville = extract_channel(eng_extract,
                                    cutoff_dim=self.D,
                                    vectorize_modes=True,
                                    representation='liouville')
        kraus = extract_channel(eng_extract,
                                cutoff_dim=self.D,
                                vectorize_modes=True,
                                representation='kraus')

        final_rho_sf = self.eng_sf.run().dm()

        final_rho_choi = np.einsum('abcd,ab -> cd', choi, initial_state)
        final_rho_liouville = np.einsum('abcd,db -> ca', liouville,
                                        initial_state)
        final_rho_kraus = np.einsum('abc,cd,aed -> be', kraus, initial_state,
                                    np.conj(kraus))

        self.assertAllAlmostEqual(final_rho_choi, final_rho_sf, delta=self.tol)
        self.assertAllAlmostEqual(final_rho_liouville,
                                  final_rho_sf,
                                  delta=self.tol)
        self.assertAllAlmostEqual(final_rho_kraus,
                                  final_rho_sf,
                                  delta=self.tol)
Example #8
0
    def test_extract_unitary_2_modes_tf(self):
        """Test that unitary extraction works for 2 modes using the tf backend"""
        self.logTestName()

        eng_extract, q_extract = sf.Engine(num_subsystems=2)

        S = sf.ops.Sgate(2)
        B = sf.ops.BSgate(2.234, -1.165)

        initial_state = np.complex64(
            np.random.rand(self.D, self.D) +
            1j * np.random.rand(self.D, self.D))

        with self.eng_sf:
            sf.ops.Ket(initial_state) | (self.q_sf[0], self.q_sf[1])
            S | self.q_sf[0]
            B | self.q_sf
            S | self.q_sf[1]
            B | self.q_sf

        with eng_extract:
            S | q_extract[0]
            B | q_extract
            S | q_extract[1]
            B | q_extract

        # calculate final states for vectorize_mode= True
        matrix_extract_tf_backend = extract_unitary(eng_extract,
                                                    cutoff_dim=self.D,
                                                    vectorize_modes=True,
                                                    backend='tf')

        with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())
            final_state_tf_backend = sess.run(
                tf.einsum('ab,b', matrix_extract_tf_backend,
                          tf.constant(initial_state.reshape([-1]))))

        final_state_tf_backend = np.tile(final_state_tf_backend, self.bsize)
        final_state_sf = self.eng_sf.run().ket().reshape([-1])

        self.assertAllAlmostEqual(final_state_tf_backend,
                                  final_state_sf,
                                  delta=self.tol)
Example #9
0
    def test_extract_unitary_1_mode_tf(self):
        """Test that unitary extraction works for 1 mode using the tf backend"""
        self.logTestName()
        eng_extract, q_extract = sf.Engine(num_subsystems=1)

        S = sf.ops.Sgate(0.4, -1.2)
        D = sf.ops.Dgate(2, 0.9)
        K = sf.ops.Kgate(-1.5)

        initial_state = np.random.rand(self.D) + 1j * np.random.rand(
            self.D)  # not a state but it doesn't matter

        with self.eng_sf:
            sf.ops.Ket(initial_state) | self.q_sf
            S | self.q_sf
            D | self.q_sf
            K | self.q_sf

        with eng_extract:
            S | q_extract
            D | q_extract
            K | q_extract

        matrix_extract_tf_backend = extract_unitary(eng_extract,
                                                    cutoff_dim=self.D,
                                                    backend='tf')

        with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())
            in_state = tf.constant(initial_state.reshape([-1]),
                                   dtype=tf.complex64)
            final_state_tf_backend = sess.run(
                tf.einsum('ab,b', matrix_extract_tf_backend, in_state))

        final_state_sf = self.eng_sf.run().ket()

        self.assertAllAlmostEqual(final_state_tf_backend,
                                  final_state_sf,
                                  delta=self.tol)
 def setUp(self):
     super().setUp()
     self.eng, q = sf.Engine(self.num_subsystems, hbar=self.hbar)
     self.eng.backend = self.backend
     self.eng.reset()
Example #11
0
 def setUp(self):
     super().setUp()
     self.eng, q = sf.Engine(self.num_subsystems, hbar=self.hbar)
 def setUp(self):
     super().setUp()
     self.eng, q = sf.Engine(self.num_subsystems)
     # attach the backend (NOTE: self.backend is shared between the tests, make sure it is reset before use!)
     self.eng.backend = self.backend
     self.backend.reset(cutoff_dim=self.cutoff)
 def setUp(self):
     super().setUp()
     self.eng, q = sf.Engine(self.num_subsystems)
     self.eng.backend = self.backend
     self.backend.reset(cutoff_dim=self.cutoff)
Example #14
0
    def test_extract_channel_2_modes_vectorize(self):
        """Test that channel extraction works for 2 modes vectorized"""
        self.logTestName()

        eng_extract, q_extract = sf.Engine(num_subsystems=2)

        S = sf.ops.Sgate(2)
        B = sf.ops.BSgate(2.234, -1.165)

        initial_state = np.random.rand(
            self.D, self.D, self.D,
            self.D) + 1j * np.random.rand(self.D, self.D, self.D, self.D)

        with self.eng_sf:
            sf.ops.DensityMatrix(initial_state) | (self.q_sf[0], self.q_sf[1])
            S | self.q_sf[0]
            B | self.q_sf
            S | self.q_sf[1]
            B | self.q_sf

        with eng_extract:
            S | q_extract[0]
            B | q_extract
            S | q_extract[1]
            B | q_extract

        #vectorize = true

        choi = extract_channel(eng_extract,
                               cutoff_dim=self.D,
                               vectorize_modes=True,
                               representation='choi')
        liouville = extract_channel(eng_extract,
                                    cutoff_dim=self.D,
                                    vectorize_modes=True,
                                    representation='liouville')
        kraus = extract_channel(eng_extract,
                                cutoff_dim=self.D,
                                vectorize_modes=True,
                                representation='kraus')

        final_rho_sf = self.eng_sf.run().dm()

        if self.batched:
            final_rho_sf = final_rho_sf[0]

        final_rho_sf = np.einsum('abcd->acbd',
                                 final_rho_sf).reshape(self.D**2, self.D**2)
        initial_state = np.einsum('abcd->acbd',
                                  initial_state).reshape(self.D**2, self.D**2)

        final_rho_choi = np.einsum('abcd,ab -> cd', choi, initial_state)
        final_rho_liouville = np.einsum('abcd,db -> ca', liouville,
                                        initial_state)
        final_rho_kraus = np.einsum('abc,cd,aed -> be', kraus, initial_state,
                                    np.conj(kraus))

        self.assertAllAlmostEqual(final_rho_choi, final_rho_sf, delta=self.tol)
        self.assertAllAlmostEqual(final_rho_liouville,
                                  final_rho_sf,
                                  delta=self.tol)
        self.assertAllAlmostEqual(final_rho_kraus,
                                  final_rho_sf,
                                  delta=self.tol)
Example #15
0
 def setUp(self):
     super().setUp()
     self.eng_sf, self.q_sf = sf.Engine(self.num_subsystems, hbar=self.hbar)
     self.eng_sf.backend = self.backend
     self.backend.reset(cutoff_dim=self.D)