{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\nConcurrence and (log-)negativity are inexpensive two-qubit entanglement quantifiers suitable for tests and CI.\\n\\nExercises:\\n- Plot C and N vs noise parameter p and compare robustness.\\n'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\"\"\n",
    "Concurrence and (log-)negativity are inexpensive two-qubit entanglement quantifiers suitable for tests and CI.\n",
    "\n",
    "Exercises:\n",
    "- Plot C and N vs noise parameter p and compare robustness.\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "p=1.0  C=1.000  N=0.500\n",
      "p=0.9  C=0.850  N=0.425\n",
      "p=0.8  C=0.700  N=0.350\n",
      "p=0.6  C=0.400  N=0.200\n",
      "p=0.4  C=0.100  N=0.050\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "from qiskit.quantum_info import Statevector, DensityMatrix\n",
    "\n",
    "Y = np.array([[0,-1j],[1j,0]])\n",
    "Y2 = np.kron(Y,Y)\n",
    "\n",
    "phi = Statevector([1,0,0,1])/np.sqrt(2)\n",
    "rho_phi = DensityMatrix(phi).data\n",
    "I4 = np.eye(4)/4\n",
    "\n",
    "def isotropic(p):\n",
    "    return p*rho_phi + (1-p)*I4 #p is the depolarization parameter\n",
    "\n",
    "def concurrence(rho):\n",
    "    # Wootters formula\n",
    "    R = rho @ Y2 @ rho.conj() @ Y2\n",
    "    evals = np.sort(np.real(np.sqrt(np.maximum(0, np.linalg.eigvals(R)))))\n",
    "    return max(0, evals[-1] - np.sum(evals[:-1]))\n",
    "\n",
    "def negativity(rho):\n",
    "    rho_pt = DensityMatrix.partial_transpose(DensityMatrix(rho), [1]).data\n",
    "    eig = np.linalg.eigvalsh(rho_pt)\n",
    "    return np.sum(np.abs(eig[eig<0]))\n",
    "\n",
    "for p in [1.0,0.9,0.8,0.6,0.4]:\n",
    "    rho = isotropic(p)\n",
    "    print(f\"p={p}  C={concurrence(rho):.3f}  N={negativity(rho):.3f}\")"
   ]
  },
  {
   "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
}
