{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d788b475",
   "metadata": {},
   "source": [
    "# Chapter 4 — Entanglement and Bell test\n",
    "\n",
    "We build $|\\Phi^+\\rangle$, compute its reduced state and entropy, then evaluate $S$ (CHSH): $2\\sqrt2$ for the quantum state, at most $2$ for any local deterministic strategy.\n",
    "\n",
    "*PHY321 Notebook — works offline: numpy and matplotlib are enough.*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "da174a60",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-17T23:21:09.761542Z",
     "iopub.status.busy": "2026-10-17T23:21:09.761109Z",
     "iopub.status.idle": "2026-10-17T23:21:10.207343Z",
     "shell.execute_reply": "2026-10-17T23:21:10.206778Z"
    }
   },
   "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)  # fixed seed: reproducible results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "53622ec5",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-17T23:21:10.210065Z",
     "iopub.status.busy": "2026-10-17T23:21:10.209611Z",
     "iopub.status.idle": "2026-10-17T23:21:10.213234Z",
     "shell.execute_reply": "2026-10-17T23:21:10.212966Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "rho_A = [[0.4999999999999999, 0.0], [0.0, 0.4999999999999999]]\n",
      "von Neumann entropy (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)                       # partial trace over B\n",
    "w = np.linalg.eigvalsh(rho_A); w = w[w > 1e-12]\n",
    "print(\"rho_A =\", rho_A.real.tolist()); print(\"von Neumann entropy (bits) =\", -(w*np.log2(w)).sum())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d3a6fe0a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-17T23:21:10.215114Z",
     "iopub.status.busy": "2026-10-17T23:21:10.214933Z",
     "iopub.status.idle": "2026-10-17T23:21:10.219338Z",
     "shell.execute_reply": "2026-10-17T23:21:10.219053Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "S_quantique = 2.82842712474619   2*sqrt(2) = 2.8284271247461903\n",
      "maximum S, local strategy = 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",
    "# local deterministic strategies: 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(\"maximum S, local strategy =\", best)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e224808b",
   "metadata": {},
   "source": [
    "## Sampling measurements\n",
    "\n",
    "We draw outcome pairs from the Born rule and recover $E(a,b)=\\cos(a-b)$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "e1429181",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-17T23:21:10.221402Z",
     "iopub.status.busy": "2026-10-17T23:21:10.221091Z",
     "iopub.status.idle": "2026-10-17T23:21:10.226148Z",
     "shell.execute_reply": "2026-10-17T23:21:10.225864Z"
    }
   },
   "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)])  # outcomes (-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
}
