Ejemplo n.º 1
0
 def test_creation(self):
     sv = quantumstate.StateVector((1, 2, 3, 4), 1)
     self.assertEqual(sv.dimensions, (4, ))
     self.assertEqual(sv.time, 1)
     sv = quantumstate.StateVector(numpy.empty((12, 10)))
     self.assertEqual(sv.dimensions, (12, 10))
     self.assertEqual(sv.time, 0)
Ejemplo n.º 2
0
 def test_reducesquare(self):
     sv1 = quantumstate.StateVector((2, 1, 1), norm=True)
     sv2 = quantumstate.StateVector((3, 2, 6, 7), norm=True)
     sv3 = quantumstate.StateVector((1, 0, 3, 4), norm=True)
     sv = sv1**sv2**sv3
     rsv = sv.reducesquare((2, 1))
     self.assertEqual(rsv.shape, (3, 3))
     rsv = sv.reducesquare(2)
     self.assertEqual(rsv.shape, (3, 4, 3, 4))
Ejemplo n.º 3
0
 def test_reduce(self):
     sv1 = quantumstate.StateVector((2, 1, 1), norm=True)
     sv2 = quantumstate.StateVector((3, 2, 6, 7), norm=True)
     sv3 = quantumstate.StateVector((1, 0, 3, 4), norm=True)
     sv = sv1**sv2**sv3
     self.assert_(((sv.reduce((1, 2)) - sv1) < eps).all())
     self.assert_(((sv.reduce((0, 2)) - sv2) < eps).all())
     self.assert_(((sv.reduce((0, 1)) - sv3) < eps).all())
     self.assert_(((sv.reduce(2) - (sv1**sv2)) < eps).all())
Ejemplo n.º 4
0
 def test_diagexpvalue(self):
     sv1 = quantumstate.StateVector((1, 1, 1), norm=True)
     sv2 = quantumstate.StateVector((1, 2, 3, 4), norm=True)
     sv = sv1**sv2
     X = (1, 2, 3)
     ev1_X = sv1.diagexpvalue(X)
     self.assert_(not isinstance(ev1_X, numpy.ndarray))
     self.assert_((ev1_X - 2) < eps)
     ev_X = sv.diagexpvalue(X, indices=(0))
     self.assert_(not isinstance(ev_X, numpy.ndarray))
     self.assert_((ev_X - 2) < eps)
     Y = (0, 1, 2)
     ev = sv1.diagexpvalue((X, Y), multi=True)
     self.assert_(isinstance(ev, expvalues.ExpectationValueCollection))
     self.assertEqual(ev.shape, (2, ))
Ejemplo n.º 5
0
def coherent(alpha=2, N=20):
    r"""
    Generate a coherent StateVector in the Fock space.

    *Usage*
        >>> sv = coherent(alpha=2, N=20)
        >>> print sv
        StateVector(20)

    :param alpha: (optional)
            A complex number specifying the coherent state. (Default is 2)

    :param N: (optional)
            A number determining the dimension of the Fock space. (Default is
            20)

    :returns sv:
            A :class:`.quantumstate.StateVector`.

    The coherent state is given by the formula:

        .. math::

            |\alpha\rangle = e^{-\frac {|\alpha|^2} {2}} \sum_{n=0}^{N}
                                \frac {\alpha^n} {\sqrt{n!}} |n\rangle

    Calculation is done using the recursive formula:

        .. math::

            a_0 = e^{- \frac {|\alpha|^2} {2}}

        .. math::

            a_n = a_{n-1} * \frac {\alpha} {\sqrt n}
    """
    x = numpy.empty(N)
    x[0] = 1
    for n in range(1, N):
        x[n] = x[n - 1] * alpha / numpy.sqrt(n)
    x = numpy.exp(-numpy.abs(alpha)**2 / 2.) * x
    return quantumstate.StateVector(x)
Ejemplo n.º 6
0
def fock(dim, i):
    r"""
    Generate a Fock space basis vector.

    *Usage*
        >>> sv = fock(dim=8, i=4)
        >>> print sv
        StateVector(8)

    :param int dim:
            Dimension of the Fock space.

    :param int i:
            Genereate the i-th basis vector

    :returns sv:
            A :class:`.quantumstate.StateVector`.
    """
    psi = numpy.zeros(dim)
    psi[i] = 1.
    return quantumstate.StateVector(psi)
Ejemplo n.º 7
0
def gaussian(x0=0, k0=0, sigma=0.5, fin=6, isItInK=False, cppqed=False):
    r"""
    Generate a StateVector with a normal distribution.

    *Usage*
        >>> sv = gaussian(x0=0.3, k0=4, sigma=0.6, fin=7)
        >>> print sv
        StateVector(128)

    :param double x0: (optional)
            Center in the real space.

    :param double k0: (optional)
            Center in the k-space.

    :param double sigma: (optional)
            Width in the real space :math:`(\sigma = \sqrt{Var(x)})`.

    :param int fin: (optional)
            :math:`2^{fin}` determines the amount of sample points.

    :param bool isItInK: (optional)
            if true, sigma is interpreted as width of the wavepacket in k-space

    :param bool cppqed: (optional)
            C++QED compatibility flag, if set to true then x0 (but not sigma) is expected
            in units of Pi. Defaults to False.

    :returns sv:
            A :class:`.quantumstate.StateVector` representing this
            gaussian wave packet in the k-space.

    The generated StateVector is normalized and given in the k-space. It
    is the discretized and Fourier transformed of the following expression:

        .. math::

            \Psi(x) = \frac {1} {\sqrt[4]{2 \pi}} * \frac {1} {\sqrt{\Delta x}}
                            e^{-\frac {x^2} {4*{\Delta x}^2}}*e^{i k_0x}

    The discretization is given by:

        .. math::

            \Psi_j = \sqrt{\frac{L}{N}} \Psi(x_j),\qquad x_j=-\pi,-\pi+dx,...,\pi-dx
    """

    N = 2**fin
    if cppqed: x0 = x0 * numpy.pi
    if isItInK:
        L = N
        offset1 = k0
        offset2 = -x0
    else:
        L = 2 * numpy.pi
        offset1 = x0
        offset2 = k0
    if 6. * sigma > L:
        print "Warning: Sigma is maybe too big."
    dx = L / float(N)
    if sigma < dx:
        print "Warning: Sigma is maybe too small."
    array = numpy.linspace(-L / 2., L / 2., N, endpoint=False)
    Norm = numpy.sqrt(L / N) / (2 * numpy.pi)**(1. / 4) / numpy.sqrt(sigma)
    Psi = quantumstate.StateVector(Norm * numpy.exp(-(array - offset1)**2 /
                                                    (4 * sigma**2)) *
                                   numpy.exp(1j * array * offset2))
    if not isItInK: Psi = Psi.fft()
    return Psi.normalize()
Ejemplo n.º 8
0
 def sv(self, t, points=16):
     X = numpy.linspace(-numpy.pi, numpy.pi, points)
     return quantumstate.StateVector(numpy.sin(X) * numpy.exp(-t), time=t)
Ejemplo n.º 9
0
 def test_adjust(self):
     sv = quantumstate.StateVector(numpy.sin(numpy.linspace(0, 10)))
     sv_new = quantumstate.adjust(sv, 10)
     self.assertEqual(len(sv_new), 10)
Ejemplo n.º 10
0
 def test_outer(self):
     sv1 = quantumstate.StateVector((1, 2), norm=False)
     sv2 = quantumstate.StateVector((3, 4), norm=False)
     sv = quantumstate.StateVector(((3, 4), (6, 8)))
     self.assert_((sv1**sv2 == sv).all())
Ejemplo n.º 11
0
 def test_normalize(self):
     sv = quantumstate.StateVector((5, 1, 6), norm=True)
     N = numpy.sqrt((sv * sv.conjugate()).sum())
     self.assert_(1 - eps < N < 1 + eps)
     self.assert_(1 - eps < sv.norm() < 1 + eps)