def test_save_load(self, tmpdir, tol):
        """test saving and loading of squeezoing factors"""
        factors_in = so.generate_squeeze_factors(4)
        so.save_squeeze_factors(factors_in, directory=str(tmpdir))
        factors_out = so.load_squeeze_factors(4, directory=str(tmpdir))

        assert np.allclose(factors_out, SQUEEZE_FACTOR_4, atol=tol, rtol=0)
Exemple #2
0
    def test_save_load_squeeze_factors(self):
        factors_in = so.generate_squeeze_factors(4)
        so.save_squeeze_factors(factors_in, directory="./")
        factors_out = so.load_squeeze_factors(4, directory="./")

        self.assertAllAlmostEqual(factors_out,
                                  squeeze_factor_4,
                                  delta=self.tol)
Exemple #3
0
def squeezing(r, theta, trunc, save=False, directory=None):
    r"""The squeezing operator :math:`S(re^{i\theta})`.

    Args:
            r (float): the magnitude of the squeezing in the
                    x direction
            theta (float): the squeezing angle
            trunc (int): the Fock cutoff
    """
    # pylint: disable=duplicate-code
    if r == 0:
        # return the identity
        ret = np.eye(trunc, dtype=def_type)
    else:
        # broadcast the index arrays
        dim_array = np.arange(trunc)
        N = dim_array.reshape((-1, 1, 1))
        n = dim_array.reshape((1, -1, 1))
        k = dim_array.reshape((1, 1, -1))

        try:
            prefac = so.load_squeeze_factors(trunc, directory)
        except FileNotFoundError:
            prefac = so.generate_squeeze_factors(trunc)
            if save:
                so.save_bs_factors(prefac, directory)

        # we only perform the sum when n+N is divisible by 2
        # in which case we sum 0 <= k <= min(N,n)
        # mask = np.logical_and((n+N)%2 == 0, k <= np.minimum(N, n))
        mask = np.logical_and((n + N) % 2 == 0, k <= np.minimum(N, n))

        # perform the summation over k
        scale = mask * np.power(sinh(r) / 2,
                                mask * (N + n - 2 * k) / 2) / (cosh(r)**(
                                    (N + n + 1) / 2))
        ph = exp(1j * theta * (N - n) / 2)
        ret = np.sum(scale * ph * prefac, axis=-1)

    return ret