Skip to content

Latest commit

 

History

History
583 lines (319 loc) · 18.3 KB

guide-states.rst

File metadata and controls

583 lines (319 loc) · 18.3 KB

Manipulating States and Operators

.. ipython::
   :suppress:

   In [1]: from qutip import *

Introduction

In the previous guide section :ref:`basics`, we saw how to create states and operators, using the functions built into QuTiP. In this portion of the guide, we will look at performing basic operations with states and operators. For more detailed demonstrations on how to use and manipulate these objects, see the examples on the tutorials web page.

State Vectors (kets or bras)

Here we begin by creating a Fock :func:`qutip.states.basis` vacuum state vector \left|0\right> with in a Hilbert space with 5 number states, from 0 to 4:

.. ipython::

    In [1]: vac = basis(5, 0)

    In [2]: print(vac)


and then create a lowering operator \left(\hat{a}\right) corresponding to 5 number states using the :func:`qutip.operators.destroy` function:

.. ipython::

    In [1]: a = destroy(5)

    In [2]: print(a)


Now lets apply the destruction operator to our vacuum state vac,

.. ipython::

    In [1]: a * vac


We see that, as expected, the vacuum is transformed to the zero vector. A more interesting example comes from using the adjoint of the lowering operator, the raising operator \hat{a}^\dagger:

.. ipython::

    In [1]: a.dag() * vac


The raising operator has in indeed raised the state vec from the vacuum to the \left| 1\right> state. Instead of using the dagger Qobj.dag() method to raise the state, we could have also used the built in :func:`qutip.operators.create` function to make a raising operator:

.. ipython::

    In [1]: c = create(5)

    In [2]: c * vac


which does the same thing. We can raise the vacuum state more than once by successively apply the raising operator:

.. ipython::

    In [1]: c * c * vac


or just taking the square of the raising operator \left(\hat{a}^\dagger\right)^{2}:

.. ipython::

    In [1]: c ** 2 * vac


Applying the raising operator twice gives the expected \sqrt{n + 1} dependence. We can use the product of c * a to also apply the number operator to the state vector vac:

.. ipython::

    In [1]: c * a * vac


or on the \left| 1\right> state:

.. ipython::

    In [1]: c * a * (c * vac)


or the \left| 2\right> state:

.. ipython::

    In [1]: c * a * (c**2 * vac)


Notice how in this last example, application of the number operator does not give the expected value n=2, but rather 2\sqrt{2}. This is because this last state is not normalized to unity as c\left| n\right> = \sqrt{n+1}\left| n+1\right>. Therefore, we should normalize our vector first:

.. ipython::

    In [1]: c * a * (c**2 * vac).unit()


Since we are giving a demonstration of using states and operators, we have done a lot more work than we should have. For example, we do not need to operate on the vacuum state to generate a higher number Fock state. Instead we can use the :func:`qutip.states.basis` (or :func:`qutip.states.fock`) function to directly obtain the required state:

.. ipython::

    In [1]: ket = basis(5, 2)

    In [2]: print(ket)


Notice how it is automatically normalized. We can also use the built in :func:`qutip.operators.num` operator:

.. ipython::

    In [1]: n = num(5)

    In [2]: print(n)


Therefore, instead of c * a * (c ** 2 * vac).unit() we have:

.. ipython::

    In [1]: n * ket


We can also create superpositions of states:

.. ipython::

    In [1]: ket = (basis(5, 0) + basis(5, 1)).unit()

    In [2]: print(ket)


where we have used the :func:`qutip.Qobj.unit` method to again normalize the state. Operating with the number function again:

.. ipython::

    In [1]: n * ket


We can also create coherent states and squeezed states by applying the :func:`qutip.operators.displace` and :func:`qutip.operators.squeeze` functions to the vacuum state:

.. ipython::

    In [1]: vac = basis(5, 0)

    In [2]: d = displace(5, 1j)

    In [3]: s = squeeze(5, 0.25 + 0.25j)

    In [4]: d * vac


.. ipython::

    In [1]: d * s * vac


Of course, displacing the vacuum gives a coherent state, which can also be generated using the built in :func:`qutip.states.coherent` function.

Density matrices

One of the main purpose of QuTiP is to explore the dynamics of open quantum systems, where the most general state of a system is not longer a state vector, but rather a density matrix. Since operations on density matrices operate identically to those of vectors, we will just briefly highlight creating and using these structures.

The simplest density matrix is created by forming the outer-product \left|\psi\right>\left<\psi\right| of a ket vector:

.. ipython::

    In [1]: ket = basis(5, 2)

    In [2]: ket * ket.dag()

A similar task can also be accomplished via the :func:`qutip.states.fock_dm` or :func:`qutip.states.ket2dm` functions:

.. ipython::

    In [1]: fock_dm(5, 2)


.. ipython::

    In [1]: ket2dm(ket)


If we want to create a density matrix with equal classical probability of being found in the \left|2\right> or \left|4\right> number states we can do the following:

.. ipython::

    In [1]: 0.5 * ket2dm(basis(5, 4)) + 0.5 * ket2dm(basis(5, 2))


or use 0.5 * fock_dm(5, 2) + 0.5 * fock_dm(5, 4). There are also several other built-in functions for creating predefined density matrices, for example :func:`qutip.states.coherent_dm` and :func:`qutip.states.thermal_dm` which create coherent state and thermal state density matrices, respectively.

.. ipython::

    In [1]: coherent_dm(5, 1.25)


.. ipython::

    In [1]: thermal_dm(5, 1.25)


QuTiP also provides a set of distance metrics for determining how close two density matrix distributions are to each other. Included are the trace distance :func:`qutip.metrics.tracedist`, fidelity :func:`qutip.metrics.fidelity`, Hilbert-Schmidt distance :func:`qutip.metrics.hilbert_dist`, Bures distance :func:`qutip.metrics.bures_dist`, and Bures angle :func:`qutip.metrics.bures_angle`.

.. ipython::

    In [1]: x = coherent_dm(5, 1.25)

    In [2]: y = coherent_dm(5, 1.25j)  # <-- note the 'j'

    In [3]: z = thermal_dm(5, 0.125)

    In [4]: fidelity(x, x)

    In [5]: tracedist(y, y)


We also know that for two pure states, the trace distance (T) and the fidelity (F) are related by T = \sqrt{1 - F^{2}}.

.. ipython::

    In [1]: tracedist(y, x)

.. ipython::

    In [1]: np.sqrt(1 - fidelity(y, x) ** 2)


For a pure state and a mixed state, 1 - F^{2} \le T which can also be verified:

.. ipython::

    In [1]: 1 - fidelity(x, z) ** 2

.. ipython::

    In [1]: tracedist(x, z)


Qubit (two-level) systems

Having spent a fair amount of time on basis states that represent harmonic oscillator states, we now move on to qubit, or two-level quantum systems (for example a spin-1/2). To create a state vector corresponding to a qubit system, we use the same :func:`qutip.states.basis`, or :func:`qutip.states.fock`, function with only two levels:

.. ipython::

    In [1]: spin = basis(2, 0)

Now at this point one may ask how this state is different than that of a harmonic oscillator in the vacuum state truncated to two energy levels?

.. ipython::

    In [1]: vac = basis(2, 0)

At this stage, there is no difference. This should not be surprising as we called the exact same function twice. The difference between the two comes from the action of the spin operators :func:`qutip.operators.sigmax`, :func:`qutip.operators.sigmay`, :func:`qutip.operators.sigmaz`, :func:`qutip.operators.sigmap`, and :func:`qutip.operators.sigmam` on these two-level states. For example, if vac corresponds to the vacuum state of a harmonic oscillator, then, as we have already seen, we can use the raising operator to get the \left|1\right> state:

.. ipython::

    In [1]: vac

.. ipython::

    In [1]: c = create(2)

    In [2]: c * vac


For a spin system, the operator analogous to the raising operator is the sigma-plus operator :func:`qutip.operators.sigmap`. Operating on the spin state gives:

.. ipython::

    In [1]: spin

    In [2]: sigmap() * spin

Now we see the difference! The :func:`qutip.operators.sigmap` operator acting on the spin state returns the zero vector. Why is this? To see what happened, let us use the :func:`qutip.operators.sigmaz` operator:

.. ipython::

    In [1]: sigmaz()

    In [2]: sigmaz() * spin

    In [3]: spin2 = basis(2, 1)

    In [4]: spin2

    In [5]: sigmaz() * spin2


The answer is now apparent. Since the QuTiP :func:`qutip.operators.sigmaz` function uses the standard z-basis representation of the sigma-z spin operator, the spin state corresponds to the \left|\mathrm{up}\right> state of a two-level spin system while spin2 gives the \left|\mathrm{down}\right> state. Therefore, in our previous example sigmap() * spin, we raised the qubit state out of the truncated two-level Hilbert space resulting in the zero state.

While at first glance this convention might seem somewhat odd, it is in fact quite handy. For one, the spin operators remain in the conventional form. Second, when the spin system is in the \left|\mathrm{up}\right> state:

.. ipython::

    In [1]: sigmaz() * spin

the non-zero component is the zeroth-element of the underlying matrix (remember that python uses c-indexing, and matrices start with the zeroth element). The \left|\mathrm{down}\right> state therefore has a non-zero entry in the first index position. This corresponds nicely with the quantum information definitions of qubit states, where the excited \left|\mathrm{up}\right> state is label as \left|0\right>, and the \left|\mathrm{down}\right> state by \left|1\right>.

If one wants to create spin operators for higher spin systems, then the :func:`qutip.operators.jmat` function comes in handy.

Expectation values

Some of the most important information about quantum systems comes from calculating the expectation value of operators, both Hermitian and non-Hermitian, as the state or density matrix of the system varies in time. Therefore, in this section we demonstrate the use of the :func:`qutip.expect` function. To begin:

.. ipython::

    In [1]: vac = basis(5, 0)

    In [2]: one = basis(5, 1)

    In [3]: c = create(5)

    In [4]: N = num(5)

    In [5]: expect(N, vac)

    In [6]: expect(N, one)


.. ipython::

    In [1]: coh = coherent_dm(5, 1.0j)

    In [2]: expect(N, coh)

.. ipython::

    In [1]: cat = (basis(5, 4) + 1.0j * basis(5, 3)).unit()

    In [2]: expect(c, cat)

The :func:`qutip.expect` function also accepts lists or arrays of state vectors or density matrices for the second input:

.. ipython::

    In [1]: states = [(c**k * vac).unit() for k in range(5)]  # must normalize

    In [2]: expect(N, states)

.. ipython::

    In [1]: cat_list = [(basis(5, 4) + x * basis(5, 3)).unit()
       ...:             for x in [0, 1.0j, -1.0, -1.0j]]

    In [2]: expect(c, cat_list)

Notice how in this last example, all of the return values are complex numbers. This is because the :func:`qutip.expect` function looks to see whether the operator is Hermitian or not. If the operator is Hermitian, than the output will always be real. In the case of non-Hermitian operators, the return values may be complex. Therefore, the :func:`qutip.expect` function will return an array of complex values for non-Hermitian operators when the input is a list/array of states or density matrices.

Of course, the :func:`qutip.expect` function works for spin states and operators:

.. ipython::

    In [1]: up = basis(2, 0)

    In [2]: down = basis(2, 1)

    In [3]: expect(sigmaz(), up)

    In [4]: expect(sigmaz(), down)


as well as the composite objects discussed in the next section :ref:`tensor`:

.. ipython::

    In [1]: spin1 = basis(2, 0)

    In [2]: spin2 = basis(2, 1)

    In [3]: two_spins = tensor(spin1, spin2)

    In [4]: sz1 = tensor(sigmaz(), qeye(2))

    In [5]: sz2 = tensor(qeye(2), sigmaz())

    In [6]: expect(sz1, two_spins)

    In [7]: expect(sz2, two_spins)

Superoperators and Vectorized Operators

In addition to state vectors and density operators, QuTiP allows for representing maps that act linearly on density operators using the Kraus, Liouville supermatrix and Choi matrix formalisms. This support is based on the correspondance between linear operators acting on a Hilbert space, and vectors in two copies of that Hilbert space, \mathrm{vec} : \mathcal{L}(\mathcal{H}) \to \mathcal{H} \otimes \mathcal{H} [Hav03]_, [Wat13]_.

This isomorphism is implemented in QuTiP by the :obj:`~qutip.superoperator.operator_to_vector` and :obj:`~qutip.superoperator.vector_to_operator` functions:

.. ipython::

    In [1]: psi = basis(2, 0)

    In [2]: rho = ket2dm(psi)

    In [3]: rho
    Quantum object: dims = [[2], [2]], shape = [2, 2], type = oper, isherm = True
    Qobj data =
    [[ 1.  0.]
     [ 0.  0.]]

    In [4]: vec_rho = operator_to_vector(rho)

    In [5]: vec_rho
    Quantum object: dims = [[[2], [2]], [1]], shape = [4, 1], type = operator-ket
    Qobj data =
    [[ 1.]
     [ 0.]
     [ 0.]
     [ 0.]]

    In [6]: rho2 = vector_to_operator(vec_rho)

    In [7]: (rho - rho2).norm()
    0.0

The :attr:`~qutip.Qobj.type` attribute indicates whether a quantum object is a vector corresponding to an operator (operator-ket), or its Hermitian conjugate (operator-bra).

Note that QuTiP uses the column-stacking convention for the isomorphism between \mathcal{L}(\mathcal{H}) and \mathcal{H} \otimes \mathcal{H}:

.. ipython::

    In [1]: import numpy as np

    In [2]: A = Qobj(np.arange(4).reshape((2, 2)))

    In [3]: A
    Quantum object: dims = [[2], [2]], shape = [2, 2], type = oper, isherm = False
    Qobj data =
    [[ 0.  1.]
     [ 2.  3.]]

    In [4]: operator_to_vector(A)
    Quantum object: dims = [[[2], [2]], [1]], shape = [4, 1], type = operator-ket
    Qobj data =
    [[ 0.]
     [ 2.]
     [ 1.]
     [ 3.]]

Since \mathcal{H} \otimes \mathcal{H} is a vector space, linear maps on this space can be represented as matrices, often called supermatrices. Using the :obj:`~qutip.Qobj`, the :obj:`~qutip.superoperator.spre` and :obj:`~qutip.superoperator.spost` functions, supermatrices corresponding to left- and right-multiplication respectively can be quickly constructed.

.. ipython::

    In [1]: X = sigmax()

    In [2]: S = spre(X) * spost(X.dag()) # Represents conjugation by X.

Note that this is done automatically by the :obj:`~qutip.superop_reps.to_super` function when given type='oper' input.

.. ipython::

    In [1]: S2 = to_super(X)

    In [2]: (S - S2).norm()
    0.0

Quantum objects representing superoperators are denoted by type='super':

.. ipython::

    In [1]: S
    Quantum object: dims = [[[2], [2]], [[2], [2]]], shape = [4, 4], type = super, isherm = True
    Qobj data =
    [[ 0.  0.  0.  1.]
     [ 0.  0.  1.  0.]
     [ 0.  1.  0.  0.]
     [ 1.  0.  0.  0.]]

Information about superoperators, such as whether they represent completely positive maps, is exposed through the :attr:`~qutip.Qobj.iscp`, :attr:`~qutip.Qobj.istp` and :attr:`~qutip.Qobj.iscptp` attributes:

.. ipython::

    In [1]: S.iscp, S.istp, S.iscptp
    True True True

In addition, dynamical generators on this extended space, often called Liouvillian superoperators, can be created using the :func:`~qutip.superoperator.liouvillian` function. Each of these takes a Hamilonian along with a list of collapse operators, and returns a type="super" object that can be exponentiated to find the superoperator for that evolution.

.. ipython::

    In [1]: H = 10 * sigmaz()

    In [2]: c1 = destroy(2)

    In [3]: L = liouvillian(H, [c1])

    In [4]: L
    Quantum object: dims = [[[2], [2]], [[2], [2]]], shape = [4, 4], type = super, isherm = False, superrep = None
    Qobj data =
    [[ 0.0 +0.j  0.0 +0.j  0.0 +0.j  1.0 +0.j]
     [ 0.0 +0.j -0.5+20.j  0.0 +0.j  0.0 +0.j]
     [ 0.0 +0.j  0.0 +0.j -0.5-20.j  0.0 +0.j]
     [ 0.0 +0.j  0.0 +0.j  0.0 +0.j -1.0 +0.j]]

    In [5]: S = (12 * L).expm()

Once a superoperator has been obtained, it can be converted between the supermatrix, Kraus and Choi formalisms by using the :func:`~qutip.superop_reps.to_super`, :func:`~qutip.superop_reps.to_kraus` and :func:`~qutip.superop_reps.to_choi` functions. The :attr:`~Qobj.superrep` attribute keeps track of what reprsentation is a :obj:`~qutip.Qobj` is currently using.

.. ipython::

    In [1]: J = to_choi(S)

    In [2]: J
    Quantum object: dims = [[[2], [2]], [[2], [2]]], shape = [4, 4], type = super, isherm = True, superrep = choi
    Qobj data =
    [[  1.00000000e+00+0.j           0.00000000e+00+0.j           0.00000000e+00+0.j
        8.07531120e-04-0.00234352j]
     [  0.00000000e+00+0.j           0.00000000e+00+0.j           0.00000000e+00+0.j
        0.00000000e+00+0.j        ]
     [  0.00000000e+00+0.j           0.00000000e+00+0.j           9.99993856e-01+0.j
        0.00000000e+00+0.j        ]
     [  8.07531120e-04+0.00234352j   0.00000000e+00+0.j           0.00000000e+00+0.j
        6.14421235e-06+0.j        ]]

    In [3]: K = to_kraus(J)

    In [4]: K
    [Quantum object: dims = [[2], [2]], shape = [2, 2], type = oper, isherm = False
    Qobj data =
    [[  1.00000000e+00 +5.37489696e-22j   0.00000000e+00 +0.00000000e+00j]
     [  0.00000000e+00 +0.00000000e+00j   8.07531120e-04 +2.34352424e-03j]], Quantum object: dims = [[2], [2]], shape = [2, 2], type = oper, isherm = False
    Qobj data =
    [[ -1.93076357e-13 +5.63930339e-13j   0.00000000e+00 +0.00000000e+00j]
     [  0.00000000e+00 +0.00000000e+00j   2.40470137e-10 -4.73970807e-13j]], Quantum object: dims = [[2], [2]], shape = [2, 2], type = oper, isherm = True
    Qobj data =
    [[ 0.  0.]
     [ 0.  0.]], Quantum object: dims = [[2], [2]], shape = [2, 2], type = oper, isherm = False
    Qobj data =
    [[ 0.          0.99999693]
     [ 0.          0.        ]]]