{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\n### Overview\\nContrast GHZ and W states: robustness to loss and pairwise correlations differ.\\n\\nExercises:\\n- Trace out one qubit and compare the remaining 2-qubit entanglement.\\n'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\"\"\n",
    "### Overview\n",
    "Contrast GHZ and W states: robustness to loss and pairwise correlations differ.\n",
    "\n",
    "Exercises:\n",
    "- Trace out one qubit and compare the remaining 2-qubit entanglement.\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Single qubit reductions:\n",
      "GHZ (1.0000000000000002+0j) (1.0000000000000002+0j) (1.0000000000000002+0j)\n",
      "W (0.942809041582063+0j) (0.942809041582063+0j) (0.942809041582063+0j)\n",
      "\n",
      "Two qubit reductions:\n",
      "GHZ (1.0000000000000002+0j) (1.0000000000000002+0j) (1.0000000000000002+0j)\n",
      "W (0.942809041582063+0j) (0.942809041582063+0j) (0.942809041582063+0j)\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "from qiskit import QuantumCircuit\n",
    "from qiskit.quantum_info import Statevector, partial_trace, DensityMatrix\n",
    "\n",
    "\n",
    "def concurrence(reduced_rho):\n",
    "    return np.sqrt(2*(1-np.trace(reduced_rho @ reduced_rho)))\n",
    "\n",
    "qc_g = QuantumCircuit(3); qc_g.h(0); qc_g.cx(0,1); qc_g.cx(1,2) #GHZ state (3 qubits)= (|000> + |111>)/sqrt(2)\n",
    "rho_g = DensityMatrix(Statevector.from_instruction(qc_g))\n",
    "\n",
    "w = Statevector([0,1,1,0,1,0,0,0])/np.sqrt(3)\n",
    "rho_w = DensityMatrix(w)\n",
    "\n",
    "def red(dm, keep):\n",
    "    drop = [i for i in range(3) if i not in keep]\n",
    "    return partial_trace(dm, drop).data\n",
    "\n",
    "print(\"Single qubit reductions:\")\n",
    "for name, dm in [(\"GHZ\", rho_g), (\"W\", rho_w)]:\n",
    "    print(name, concurrence(red(dm,[0])), concurrence(red(dm,[1])), concurrence(red(dm,[2])))\n",
    "\n",
    "print(\"\\nTwo qubit reductions:\")\n",
    "for name, dm in [(\"GHZ\", rho_g), (\"W\", rho_w)]:\n",
    "    print(name, concurrence(red(dm,[0,1])), concurrence(red(dm,[0,2])), concurrence(red(dm,[1,2])))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "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.12.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
