<jupyter_start><jupyter_text>Tensor Products & Partial TracesContents- [Tensor Products](tensor)- [Partial Trace](partial)- [Super Operators & Tensor Manipulations](super)<jupyter_code>import numpy as np
from qutip import *<jupyter_output><empty_output><jupyter_text>Tensor ProductsTo describe the states of multipartite quantum systems - such as two coupled qubits, a qubit coupled to an oscillator, etc. - we need to expand the Hilbert space by taking the tensor product of the state vectors for each of the system components. Similarly, the operators acting on the state vectors in the combined Hilbert space (describing the coupled system) are formed by taking the tensor product of the individual operators.In QuTiP the function `tensor` is used to accomplish this task. This function takes as argument a collection::```pythontensor(op1, op2, op3)```or a ``list``:```pythontensor([op1, op2, op3])```of state vectors *or* operators and returns a composite quantum object for the combined Hilbert space. The function accepts an arbitray number of states or operators as argument. The type returned quantum object is the same as that of the input(s).For example, the state vector describing two qubits in their ground states is formed by taking the tensor product of the two single-qubit ground state vectors:<jupyter_code>tensor(basis(2, 0), basis(2, 0))<jupyter_output><empty_output><jupyter_text>or equivalently using the ``list`` format:<jupyter_code>tensor([basis(2, 0), basis(2, 0)])<jupyter_output><empty_output><jupyter_text>This is straightforward to generalize to more qubits by adding more component state vectors in the argument list to the `tensor` function, as illustrated in the following example:<jupyter_code>tensor((basis(2, 0) + basis(2, 1)).unit(), 
        (basis(2, 0) + basis(2, 1)).unit(), basis(2, 0))<jupyter_output><empty_output><jupyter_text>This state is slightly more complicated, describing two qubits in a superposition between the up and down states, while the third qubit is in its ground state.To construct operators that act on an extended Hilbert space of a combined system, we similarly pass a list of operators for each component system to the `tensor` function. For example, to form the operator that represents the simultaneous action of the $\sigma_x$ operator on two qubits:<jupyter_code>tensor(sigmax(), sigmax())<jupyter_output><empty_output><jupyter_text>To create operators in a combined Hilbert space that only act only on a single component, we take the tensor product of the operator acting on the subspace of interest, with the identity operators corresponding to the components that are to be unchanged. For example, the operator that represents $\sigma_z$ on the first qubit in a two-qubit system, while leaving the second qubit unaffected:<jupyter_code>tensor(sigmaz(), identity(2))<jupyter_output><empty_output><jupyter_text>Example: Constructing composite HamiltoniansThe `tensor` function is extensively used when constructing Hamiltonians for composite systems. Here we'll look at some simple examples.Two coupled qubitsFirst, let's consider a system of two coupled qubits. Assume that both qubit has equal energy splitting, and that the qubits are coupled through a $\sigma_x\otimes\sigma_x$ interaction with strength $g = 0.05$ (in units where the bare qubit energy splitting is unity). The Hamiltonian describing this system is:<jupyter_code>H = tensor(sigmaz(), identity(2)) + tensor(identity(2),
            sigmaz()) + 0.05 * tensor(sigmax(), sigmax())
H<jupyter_output><empty_output><jupyter_text>Three coupled qubitsThe two-qubit example is easily generalized to three coupled qubits:<jupyter_code>H = (tensor(sigmaz(), identity(2), identity(2)) + 
       tensor(identity(2), sigmaz(), identity(2)) + 
       tensor(identity(2), identity(2), sigmaz()) + 
       0.5 * tensor(sigmax(), sigmax(), identity(2)) + 
       0.25 * tensor(identity(2), sigmax(), sigmax()))
H<jupyter_output><empty_output><jupyter_text>Jaynes-Cummings ModelThe simplest possible quantum mechanical description for light-matter interaction is encapsulated in the Jaynes-Cummings model, which describes the coupling between a two-level atom and a single-mode electromagnetic field (a cavity mode). Denoting the energy splitting of the atom and cavity ``omega_a`` and ``omega_c``, respectively, and the atom-cavity interaction strength ``g``, the Jaynes-Cumming Hamiltonian can be constructed as:<jupyter_code>N = 10   #Number of Fock states for cavity mode.
omega_a = 1.0
omega_c = 1.25
g = 0.05
a = tensor(identity(2), destroy(N))
sm = tensor(destroy(2), identity(N))
sz = tensor(sigmaz(), identity(N))
H = 0.5 * omega_a * sz + omega_c * a.dag() * a + g * (a.dag() * sm + a * sm.dag())<jupyter_output><empty_output><jupyter_text>Partial TraceThe partial trace is an operation that reduces the dimension of a Hilbert space by eliminating some degrees of freedom by averaging (tracing). In this sense it is therefore the converse of the tensor product. It is useful when one is interested in only a part of a coupled quantum system.  For open quantum systems, this typically involves tracing over the environment leaving only the system of interest.  In QuTiP the class method `ptrace` is used to take partial traces. `ptrace` acts on the `Qobj` instance for which it is called, and it takes one argument ``sel``, which is a ``list`` of integers that mark the component systems that should be **kept**. All other components are traced out.For example, the density matrix describing a single qubit obtained from a coupled two-qubit system is obtained via:<jupyter_code>psi = tensor(basis(2, 0), basis(2, 1))
psi.ptrace(0)
psi.ptrace(1)<jupyter_output><empty_output><jupyter_text>Note that the partial trace always results in a density matrix (mixed state), regardless of whether the composite system is a pure state (described by a state vector) or a mixed state (described by a density matrix):<jupyter_code>psi = tensor((basis(2, 0) + basis(2, 1)).unit(), basis(2, 0))
psi.ptrace(0)
rho = tensor(ket2dm((basis(2, 0) + basis(2, 1)).unit()), fock_dm(2, 0))
rho.ptrace(0)<jupyter_output><empty_output><jupyter_text>Super Operators & Tensor Manipulations*Superoperators* are operatorsthat act on Liouville space, the vectorspace of linear operators.  Superoperators can be represented using the isomorphism $\mathrm{vec} : \mathcal{L}(\mathcal{H}) \to \mathcal{H} \otimes \mathcal{H}$.To represent superoperators acting on $\mathcal{L}(\mathcal{H}_1 \otimes \mathcal{H}_2)$ thus takes some tensor rearrangement to get the desired ordering$\mathcal{H}_1 \otimes \mathcal{H}_2 \otimes \mathcal{H}_1 \otimes \mathcal{H}_2$.In particular, this means that `tensor` does not act as one might expect on the results of `to_super`:<jupyter_code>A = qeye([2])
B = qeye([3])
to_super(tensor(A, B)).dims
tensor(to_super(A), to_super(B)).dims<jupyter_output><empty_output><jupyter_text>In the former case, the result correctly has four copiesof the compound index with dims ``[2, 3]``. In the lattercase, however, each of the Hilbert space indices is listedindependently and in the wrong order.The `super_tensor` function performs the neededrearrangement, providing the most direct analog to `tensor` onthe underlying Hilbert space. In particular, for any two ``type="oper"``Qobjs ``A`` and ``B``, ``to_super(tensor(A, B)) == super_tensor(to_super(A), to_super(B))`` and``operator_to_vector(tensor(A, B)) == super_tensor(operator_to_vector(A), operator_to_vector(B))``. Returning to the previous example:<jupyter_code>super_tensor(to_super(A), to_super(B)).dims<jupyter_output><empty_output><jupyter_text>The `composite` function automatically switches between`tensor` and `super_tensor` based on the ``type``of its arguments, such that ``composite(A, B)`` returns an appropriate Qobj torepresent the composition of two systems.<jupyter_code>composite(A, B).dims
composite(to_super(A), to_super(B)).dims<jupyter_output><empty_output><jupyter_text>QuTiP also allows more general tensor manipulations that areuseful for converting between superoperator representations.In particular, the `tensor_contract` function allows forcontracting one or more pairs of indices. As detailed inthe [channel contraction tutorial](http://nbviewer.ipython.org/github/qutip/qutip-notebooks/blob/master/examples/example-superop-contract.ipynb), this can be used to findsuperoperators that represent partial trace maps.Using this functionality, we can construct some quite exotic maps,such as a map from $3 \times 3$ operators to $2 \times 2$operators:<jupyter_code>tensor_contract(composite(to_super(A), to_super(B)), (1, 3), (4, 6)).dims
from IPython.core.display import HTML
def css_styling():
    styles = open("../styles/guide.css", "r").read()
    return HTML(styles)
css_styling()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09829931972789116, 'fraction_numerical': 0.011224489795918367, 'mean_word_length': 6.090836012861736, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '15', 'dir': 'jupyter-structured-clean-dedup', 'id': '142509', 'license': 'MIT', 'n_tokens_mistral': 2587, 'n_tokens_neox': 2460, 'n_words': 1007, 'path': 'qutip-notebooks-master/docs/guide/TensorPtrace.ipynb', 'repo_name': 'OliverDudgeon/QSync', 'size': '8820'}