def test_cat_state_error(self, setup_eng, cutoff, bsize, pure, tol):
     """Test that the cat_state function raises an error for a complex
     argument"""
     alpha = 0.3j + 0.4
     p = 1
     with pytest.raises(ValueError, match="cannot be complex"):
         ket = utils.cat_state(alpha, p, fock_dim=cutoff)
Esempio n. 2
0
    def test_odd_cat_state(self, tol):
        """test correct even cat state returned"""
        a = 0.212
        cutoff = 10
        p = 1

        state = utils.cat_state(a, p, fock_dim=cutoff)

        n = np.arange(cutoff)
        expected = np.exp(-0.5 * np.abs(a)**2) * a**n / np.sqrt(
            fac(n)) - np.exp(-0.5 * np.abs(-a)**2) * (-a)**n / np.sqrt(fac(n))
        expected /= np.linalg.norm(expected)

        assert np.allclose(state, expected, atol=tol, rtol=0)
Esempio n. 3
0
    def test_odd_cat_state(self, a, cutoff, tol):
        """test correct odd cat state returned"""
        p = 1

        state = utils.cat_state(a, p, fock_dim=cutoff)

        # For the analytic expression, cast the integer parameter to float so
        # that there's no overflow
        a = float(a)
        n = np.arange(cutoff)
        expected = np.exp(-0.5 * np.abs(a)**2) * a**n / np.sqrt(
            fac(n)) - np.exp(-0.5 * np.abs(-a)**2) * (-a)**n / np.sqrt(fac(n))
        expected /= np.linalg.norm(expected)

        assert np.allclose(state, expected, atol=tol, rtol=0)
Esempio n. 4
0
    def test_cat_state(self, setup_eng, hbar, cutoff, bsize, pure, tol):
        """Test cat state function matches Fock backends"""
        eng, prog = setup_eng(1)
        a = 0.32 + 0.1j
        p = 0.43

        with prog.context as q:
            ops.Catstate(a, p) | q[0]

        state = eng.run(prog)
        ket = utils.cat_state(a, p, fock_dim=cutoff)

        if not pure:
            expected = state.dm()
            ket = np.tile(np.outer(ket, ket.conj()), (bsize, 1, 1))
        else:
            expected = state.ket()
            ket = np.tile(ket, (bsize, 1))

        assert np.allclose(expected, ket, atol=tol, rtol=0)