{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Berechnung der von Neumann Entropie"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import qiskit.quantum_info as qi\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Bell-Zustand: \n"
     ]
    },
    {
     "data": {
      "text/latex": [
       "$$\\frac{\\sqrt{2}}{2} |00\\rangle+\\frac{\\sqrt{2}}{2} |11\\rangle$$"
      ],
      "text/plain": [
       "<IPython.core.display.Latex object>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Definiere einen Bell-Zustand\n",
    "psi = np.array([1,0,0,1])/np.sqrt(2)\n",
    "psi_AB = qi.Statevector(psi)\n",
    "\n",
    "print(\"Bell-Zustand: \")\n",
    "psi_AB.draw(output='latex') "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Entropie : 0.000000\n"
     ]
    }
   ],
   "source": [
    "S_psi = qi.entropy(psi)\n",
    "\n",
    "print(\"Entropie : {:>3f}\".format(S_psi))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\n",
       "\\rho_{AB} = \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>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Erzeugung der Dichtematrix\n",
    "rho = qi.DensityMatrix(psi)\n",
    "rho.draw('latex',prefix='\\\\rho_{AB} = ') "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\n",
       "\\rho_{A} = \n",
       "\\begin{bmatrix}\n",
       "\\tfrac{1}{2} & 0  \\\\\n",
       " 0 & \\tfrac{1}{2}  \\\\\n",
       " \\end{bmatrix}\n",
       "$$"
      ],
      "text/plain": [
       "<IPython.core.display.Latex object>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Berechnung der partiellen Spur\n",
    "rho_A = qi.partial_trace(rho,[1])\n",
    "rho_A.draw('latex',prefix='\\\\rho_{A} = ') "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Entropie : 1.000000\n"
     ]
    }
   ],
   "source": [
    "S_rho_A = qi.entropy(rho_A)\n",
    "print(\"Entropie : {:>3f}\".format(S_rho_A))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "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": 2
}
