Exemple #1
0
def squeezer_matrix(r, theta, D, batched=False):
    """creates the single mode squeeze matrix"""
    r = tf.cast(r, tf.float64)
    if not batched:
        r = tf.expand_dims(r, 0)  # introduce artificial batch dimension
    r = tf.reshape(r, [-1, 1, 1, 1])
    theta = tf.cast(theta, def_type)
    theta = tf.reshape(theta, [-1, 1, 1, 1])

    rng = np.arange(D)
    n = np.reshape(rng, [-1, D, 1, 1])
    m = np.reshape(rng, [-1, 1, D, 1])
    k = np.reshape(rng, [-1, 1, 1, D])

    phase = tf.exp(1j * theta * (n - m) / 2)
    signs = squeeze_parity(D).reshape([1, D, 1, D])
    mask = np.logical_and((m + n) % 2 == 0, k <= np.minimum(
        m, n))  # kills off terms where the sum index k goes past min(m,n)
    k_terms = signs * \
                        tf.pow(tf.sinh(r) / 2, mask * (n + m - 2 * k) / 2) * mask / \
                        tf.pow(tf.cosh(r), (n + m + 1) / 2) * \
                        tf.exp(0.5 * tf.lgamma(tf.cast(m + 1, tf.float64)) + \
                               0.5 * tf.lgamma(tf.cast(n + 1, tf.float64)) - \
                               tf.lgamma(tf.cast(k + 1, tf.float64)) -
                               tf.lgamma(tf.cast((m - k) / 2 + 1, tf.float64)) - \
                               tf.lgamma(tf.cast((n - k) / 2 + 1, tf.float64))
                              )
    output = tf.reduce_sum(phase * tf.cast(k_terms, def_type), axis=-1)

    if not batched:
        # remove extra batch dimension
        output = tf.squeeze(output, 0)
    return output
 def test_squeeze_parity(self):
     self.logTestName()
     parity = so.squeeze_parity(8)
     self.assertAllAlmostEqual(parity, squeeze_parity_8, delta=self.tol)
 def test_squeeze_parity(self, tol):
     """Test the squeeze parity function returns the correct result"""
     parity = so.squeeze_parity(8)
     assert np.allclose(parity, SQUEEZE_PARITY_8, atol=tol, rtol=0)