{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f07ee595",
   "metadata": {},
   "source": [
    "# Berechnung der partiellen Spur\n",
    "\n",
    "Die folgenden Beispiele zeigen, wie man mit Hilfe von Qiskit patrielle Spuren berechnen kann. Hierbei ist zu beachten, dass Qiskit die Qubits von \"vorne\" zeählt."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "0eda37ea",
   "metadata": {},
   "outputs": [],
   "source": [
    "import qiskit.quantum_info as qi\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fb776ffc",
   "metadata": {},
   "source": [
    "## Berechnung der partiellen Spur eines zusammengesetzten Systems"
   ]
  },
  {
   "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": "f27d1e00",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\n",
       "\\rho_{A} = \n",
       "\\begin{bmatrix}\n",
       "\\tfrac{4}{9} & 0.4969  \\\\\n",
       " 0.4969 & \\tfrac{5}{9}  \\\\\n",
       " \\end{bmatrix}\n",
       "$$"
      ],
      "text/plain": [
       "<IPython.core.display.Latex object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Zugehörige Dichtematrix\n",
    "rho1 = qi.DensityMatrix(psi1)\n",
    "display( rho1.draw('latex',prefix='\\\\rho_{A} = ') )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "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": 5,
   "id": "5e0bfafd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\n",
       "\\rho_{B} = \n",
       "\\begin{bmatrix}\n",
       "\\tfrac{1}{2} & \\tfrac{1}{2}  \\\\\n",
       " \\tfrac{1}{2} & \\tfrac{1}{2}  \\\\\n",
       " \\end{bmatrix}\n",
       "$$"
      ],
      "text/plain": [
       "<IPython.core.display.Latex object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Zugehörige Dichtematrix\n",
    "rho2 = qi.DensityMatrix(psi2)\n",
    "display( rho2.draw('latex',prefix='\\\\rho_{B} = ') )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "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": 7,
   "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": "code",
   "execution_count": 8,
   "id": "9e548d2c",
   "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": [
    "# Alternative Berechnung \n",
    "# Dichtematrix als Tensorprodukt der Dichtematrizen der Teilsysteme\n",
    "\n",
    "rho_alt = rho1.tensor(rho2)\n",
    "display( rho_alt.draw('latex',prefix='\\\\rho_{AB} = ') )"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "12cc0f42",
   "metadata": {},
   "source": [
    "### Partielle Spur"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "2142bd87",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\n",
       "\\rho_{A} = \n",
       "\\begin{bmatrix}\n",
       "\\tfrac{4}{9} & 0.4969  \\\\\n",
       " 0.4969 & \\tfrac{5}{9}  \\\\\n",
       " \\end{bmatrix}\n",
       "$$"
      ],
      "text/plain": [
       "<IPython.core.display.Latex object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Dichtematrix für System A\n",
    "rho_A = qi.partial_trace(rho,[0]) # BEACHTE: Interne Reihenfolge B-A\n",
    "display( rho_A.draw('latex',prefix='\\\\rho_{A} = ') )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "74aefed4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\n",
       "\\rho_{B} = \n",
       "\\begin{bmatrix}\n",
       "\\tfrac{1}{2} & \\tfrac{1}{2}  \\\\\n",
       " \\tfrac{1}{2} & \\tfrac{1}{2}  \\\\\n",
       " \\end{bmatrix}\n",
       "$$"
      ],
      "text/plain": [
       "<IPython.core.display.Latex object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Dichtematrix für System B\n",
    "rho_B = qi.partial_trace(rho,[1]) # BEACHTE: Interne Reihenfolge B-A\n",
    "display( rho_B.draw('latex',prefix='\\\\rho_{B} = ') )"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "88ccc196",
   "metadata": {},
   "source": [
    "## GHZ-Zustand\n",
    "\n",
    "Berechnung der partiellen Spur bei einem GHZ-Zustand"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "90eca2f0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\frac{\\sqrt{2}}{2} |000\\rangle+\\frac{\\sqrt{2}}{2} |111\\rangle$$"
      ],
      "text/plain": [
       "<IPython.core.display.Latex object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Erzeugung des Zustandsvektors\n",
    "psi = np.array([1,0,0,0,0,0,0,1])/np.sqrt(2)\n",
    "psi_GHZ = qi.Statevector(psi)\n",
    "display( psi_GHZ.draw('latex') )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "a07e1252",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\n",
       "\\rho_{ABE} = \n",
       "\\begin{bmatrix}\n",
       "\\tfrac{1}{2} & 0 & 0 & 0 & 0 & 0 & 0 & \\tfrac{1}{2}  \\\\\n",
       " 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0  \\\\\n",
       " 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0  \\\\\n",
       " 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0  \\\\\n",
       " 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0  \\\\\n",
       " 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0  \\\\\n",
       " 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0  \\\\\n",
       " \\tfrac{1}{2} & 0 & 0 & 0 & 0 & 0 & 0 & \\tfrac{1}{2}  \\\\\n",
       " \\end{bmatrix}\n",
       "$$"
      ],
      "text/plain": [
       "<IPython.core.display.Latex object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Berechnung der Dichtematrix\n",
    "rho_ABC = qi.DensityMatrix(psi_GHZ)\n",
    "display( rho_ABC.draw('latex',prefix='\\\\rho_{ABE} = ') )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "684a08d1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\n",
       "\\rho_{AB} = \n",
       "\\begin{bmatrix}\n",
       "\\tfrac{1}{2} & 0 & 0 & 0  \\\\\n",
       " 0 & 0 & 0 & 0  \\\\\n",
       " 0 & 0 & 0 & 0  \\\\\n",
       " 0 & 0 & 0 & \\tfrac{1}{2}  \\\\\n",
       " \\end{bmatrix}\n",
       "$$"
      ],
      "text/plain": [
       "<IPython.core.display.Latex object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Dichtematrix für System AB\n",
    "rho_AB = qi.partial_trace(rho_ABC,[0]) # Umgekehre Reihenfolge E-B-A\n",
    "display( rho_AB.draw('latex',prefix='\\\\rho_{AB} = ') )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "f89809ea",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\n",
       "\\rho_{E} = \n",
       "\\begin{bmatrix}\n",
       "\\tfrac{1}{2} & 0  \\\\\n",
       " 0 & \\tfrac{1}{2}  \\\\\n",
       " \\end{bmatrix}\n",
       "$$"
      ],
      "text/plain": [
       "<IPython.core.display.Latex object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Dichtematrix für System E\n",
    "rho_E = qi.partial_trace(rho_ABC,[1,2])  # Umgekehre Reihenfolge E-B-A\n",
    "display( rho_E.draw('latex',prefix='\\\\rho_{E} = ') )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d7381f9a",
   "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
}
