def single_displacement_matrix(r, phi, cutoff, dtype=def_type.as_numpy_dtype): """creates a single mode displacement matrix""" r = r.numpy() phi = phi.numpy() gate = displacement_tw(r, phi, cutoff, dtype) # NOTE: tested when testing the gate and its gradients; also tested independently in thewalrus def grad(dy): # pragma: no cover Dr, Dphi = grad_displacement_tw(gate, r, phi) grad_r = tf.math.real(tf.reduce_sum(dy * tf.math.conj(Dr))) grad_phi = tf.math.real(tf.reduce_sum(dy * tf.math.conj(Dphi))) return grad_r, grad_phi, None return gate, grad
def displacement(r, phi, trunc): r"""The displacement operator :math:`D(\alpha)`. Uses the `displacement operation from The Walrus`_ to calculate the displacement. .. _`displacement operation from The Walrus`: https://the-walrus.readthedocs.io/en/latest/code/api/thewalrus.fock_gradients.displacement.html Args: r (float): the displacement amplitude phi (float): the displacement angle trunc (int): the Fock cutoff """ ret = displacement_tw(r, phi, cutoff=trunc) return ret