{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "635b5261",
   "metadata": {},
   "source": [
    "# Chapitre 4 — Intrication et test de Bell\n",
    "\n",
    "On construit $|\\Phi^+\\rangle$, on calcule son état réduit et son entropie, puis on évalue $S$ (CHSH) : $2\\sqrt2$ pour l'état quantique, au plus $2$ pour toute stratégie locale déterministe.\n",
    "\n",
    "*Carnet PHY321 — fonctionne sans connexion : numpy et matplotlib suffisent.*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "628ed793",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-17T23:20:43.129387Z",
     "iopub.status.busy": "2026-10-17T23:20:43.129208Z",
     "iopub.status.idle": "2026-10-17T23:20:43.609552Z",
     "shell.execute_reply": "2026-10-17T23:20:43.609124Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "np.set_printoptions(precision=4, suppress=True)\n",
    "rng = np.random.default_rng(321)  # graine fixe : résultats reproductibles"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a882f618",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-17T23:20:43.612034Z",
     "iopub.status.busy": "2026-10-17T23:20:43.611807Z",
     "iopub.status.idle": "2026-10-17T23:20:43.615516Z",
     "shell.execute_reply": "2026-10-17T23:20:43.615142Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "rho_A = [[0.4999999999999999, 0.0], [0.0, 0.4999999999999999]]\n",
      "entropie de von Neumann (bits) = 1.0\n"
     ]
    }
   ],
   "source": [
    "phi_plus = np.array([1, 0, 0, 1]) / np.sqrt(2)\n",
    "rho = np.outer(phi_plus, phi_plus.conj()).reshape(2, 2, 2, 2)\n",
    "rho_A = np.einsum('ijkj->ik', rho)                       # trace partielle sur B\n",
    "w = np.linalg.eigvalsh(rho_A); w = w[w > 1e-12]\n",
    "print(\"rho_A =\", rho_A.real.tolist()); print(\"entropie de von Neumann (bits) =\", -(w*np.log2(w)).sum())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "14a2b668",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-17T23:20:43.617378Z",
     "iopub.status.busy": "2026-10-17T23:20:43.617084Z",
     "iopub.status.idle": "2026-10-17T23:20:43.622172Z",
     "shell.execute_reply": "2026-10-17T23:20:43.621807Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "S_quantique = 2.82842712474619   2*sqrt(2) = 2.8284271247461903\n",
      "S maximal, stratégie locale = 2\n"
     ]
    }
   ],
   "source": [
    "sx = np.array([[0,1],[1,0]]); sz = np.array([[1,0],[0,-1]])\n",
    "def obs(a): return np.cos(a)*sz + np.sin(a)*sx\n",
    "def E(a, b): return np.real(phi_plus @ np.kron(obs(a), obs(b)) @ phi_plus)\n",
    "a, a2, b, b2 = 0, np.pi/2, np.pi/4, -np.pi/4\n",
    "S_q = E(a, b) + E(a, b2) + E(a2, b) - E(a2, b2)\n",
    "print(\"S_quantique =\", S_q, \"  2*sqrt(2) =\", 2*np.sqrt(2))\n",
    "# stratégies locales déterministes : A(a),A(a2),B(b),B(b2) ∈ {±1}\n",
    "best = max(abs(A1*B1 + A1*B2 + A2*B1 - A2*B2) for A1 in (-1,1) for A2 in (-1,1) for B1 in (-1,1) for B2 in (-1,1))\n",
    "print(\"S maximal, stratégie locale =\", best)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "adfe85f4",
   "metadata": {},
   "source": [
    "## Échantillonnage de mesures\n",
    "\n",
    "On tire des couples de résultats selon la règle de Born et on retrouve $E(a,b)=\\cos(a-b)$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "9789e8ca",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-17T23:20:43.624181Z",
     "iopub.status.busy": "2026-10-17T23:20:43.623869Z",
     "iopub.status.idle": "2026-10-17T23:20:43.630188Z",
     "shell.execute_reply": "2026-10-17T23:20:43.629831Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.7035 0.7071067811865476\n",
      "-0.712 -0.7071067811865475\n"
     ]
    }
   ],
   "source": [
    "def sample_E(a, b, N=20000):\n",
    "    A, Bm = obs(a), obs(b)\n",
    "    _, va = np.linalg.eigh(A); _, vb = np.linalg.eigh(Bm)\n",
    "    basis = np.array([np.kron(va[:, i], vb[:, j]) for i in range(2) for j in range(2)])  # résultats (-1,-1),(-1,+1),(+1,-1),(+1,+1)\n",
    "    probs = np.abs(basis.conj() @ phi_plus)**2\n",
    "    signs = np.array([-1*-1, -1*1, 1*-1, 1*1])\n",
    "    return rng.choice(signs, size=N, p=probs/probs.sum()).mean()\n",
    "print(sample_E(a, b), np.cos(a-b)); print(sample_E(a2, b2), np.cos(a2-b2))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
