{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2a8f179c",
   "metadata": {},
   "source": [
    "# Berechnung von Dichtematrizen\n",
    "\n",
    "Das Codebeispiel berechnet verschiedene Dichtematrizen"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9cb5862b",
   "metadata": {},
   "source": [
    "## Dichtematrix eines Zustands aus zwei Qubits\n",
    "\n",
    "$$\n",
    "|\\Psi\\rangle = \\left( \\frac{2}{3}|0\\rangle +  \\frac{\\sqrt{5}}{3}|1\\rangle \\right) \\otimes \\frac{1}{\\sqrt{2}} \\left( |0\\rangle +  |1\\rangle\\right)\n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "0eda37ea",
   "metadata": {},
   "outputs": [],
   "source": [
    "import qiskit.quantum_info as qi\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "13c72f68",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\frac{2}{3} |0\\rangle+\\frac{\\sqrt{5}}{3} |1\\rangle$$"
      ],
      "text/plain": [
       "<IPython.core.display.Latex object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Erzeugung des ersten Zustandsvektors\n",
    "psi1 = np.array([2/3,np.sqrt(5)/3])\n",
    "psi_A = qi.Statevector(psi1)\n",
    "display( psi_A.draw('latex') )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "8cfca769",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\frac{\\sqrt{2}}{2} |0\\rangle+\\frac{\\sqrt{2}}{2} |1\\rangle$$"
      ],
      "text/plain": [
       "<IPython.core.display.Latex object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Erzeugung des zweiten Zustandsvektors\n",
    "psi2 = np.array([1, 1])/np.sqrt(2)\n",
    "psi_B = qi.Statevector(psi2)\n",
    "display( psi_B.draw('latex') )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "5aafe1db",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\frac{\\sqrt{2}}{3} |00\\rangle+\\frac{\\sqrt{2}}{3} |01\\rangle+\\frac{\\sqrt{10}}{6} |10\\rangle+\\frac{\\sqrt{10}}{6} |11\\rangle$$"
      ],
      "text/plain": [
       "<IPython.core.display.Latex object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Bildung des Tensorprodukts\n",
    "psi_AB = psi_A.tensor(psi_B)\n",
    "display( psi_AB.draw('latex') )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "86e5644b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\n",
       "\\rho_{AB} = \n",
       "\\begin{bmatrix}\n",
       "\\tfrac{2}{9} & \\tfrac{2}{9} & 0.24845 & 0.24845  \\\\\n",
       " \\tfrac{2}{9} & \\tfrac{2}{9} & 0.24845 & 0.24845  \\\\\n",
       " 0.24845 & 0.24845 & 0.27778 & 0.27778  \\\\\n",
       " 0.24845 & 0.24845 & 0.27778 & 0.27778  \\\\\n",
       " \\end{bmatrix}\n",
       "$$"
      ],
      "text/plain": [
       "<IPython.core.display.Latex object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Berechnung der Dichtematrix\n",
    "rho = qi.DensityMatrix(psi_AB)\n",
    "display( rho.draw('latex',prefix='\\\\rho_{AB} = ') )"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "12cc0f42",
   "metadata": {},
   "source": [
    "## Dichtematrix für einen Bell-Zustand\n",
    "\n",
    "$$\n",
    "|\\Psi\\rangle = \n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "2142bd87",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\n",
       "\\rho_{Bell} = \n",
       "\\begin{bmatrix}\n",
       "\\tfrac{1}{2} & 0 & 0 & \\tfrac{1}{2}  \\\\\n",
       " 0 & 0 & 0 & 0  \\\\\n",
       " 0 & 0 & 0 & 0  \\\\\n",
       " \\tfrac{1}{2} & 0 & 0 & \\tfrac{1}{2}  \\\\\n",
       " \\end{bmatrix}\n",
       "$$"
      ],
      "text/plain": [
       "<IPython.core.display.Latex object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Dichtematrix kann direkt aus einem Array erzeugt werden\n",
    "psi_bell = np.array([1,0,0,1])/np.sqrt(2)\n",
    "rho = qi.DensityMatrix(psi_bell)\n",
    "display( rho.draw('latex',prefix='\\\\rho_{Bell} = ') )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "74aefed4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\frac{\\sqrt{2}}{2} |00\\rangle+\\frac{\\sqrt{2}}{2} |11\\rangle$$"
      ],
      "text/plain": [
       "<IPython.core.display.Latex object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Anzeige des zugehörigen Zustandsvektors\n",
    "display( qi.Statevector(psi_bell).draw('latex') )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eea2f40a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
