{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\nEntanglement growth in local circuits typically obeys light-cone/area-law constraints, informing depth and layout decisions.\\n\\nExercises:\\n- Sweep θ and plot the entropy curves; observe linear growth then saturation.\\n'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\"\"\n",
    "Entanglement growth in local circuits typically obeys light-cone/area-law constraints, informing depth and layout decisions.\n",
    "\n",
    "Exercises:\n",
    "- Sweep θ and plot the entropy curves; observe linear growth then saturation.\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from qiskit import QuantumCircuit\n",
    "from qiskit.quantum_info import Statevector, DensityMatrix, partial_trace\n",
    "\n",
    "def layer(qc, n, theta):\n",
    "    for q in range(n): qc.ry(np.pi/4,q)\n",
    "    for i in range(0,n-1,2): qc.rxx(theta,i,i+1)\n",
    "    for i in range(1,n-1,2): qc.rxx(theta,i,i+1)\n",
    "    return qc\n",
    "\n",
    "def mid_entropy(qc, n):\n",
    "    psi = Statevector.from_instruction(qc)\n",
    "    rhoA = partial_trace(DensityMatrix(psi), list(range(n//2))).data # Trace out qubit 1\n",
    "    evals = np.linalg.eigvalsh(rhoA)\n",
    "    evals = evals[evals>1e-12]\n",
    "    return sum(-evals*np.log2(evals))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A layer for 8 qubits:\n",
      "     ┌─────────┐┌───────────┐             \n",
      "q_0: ┤ Ry(π/4) ├┤0          ├─────────────\n",
      "     ├─────────┤│  Rxx(π/8) │┌───────────┐\n",
      "q_1: ┤ Ry(π/4) ├┤1          ├┤0          ├\n",
      "     ├─────────┤├───────────┤│  Rxx(π/8) │\n",
      "q_2: ┤ Ry(π/4) ├┤0          ├┤1          ├\n",
      "     ├─────────┤│  Rxx(π/8) │├───────────┤\n",
      "q_3: ┤ Ry(π/4) ├┤1          ├┤0          ├\n",
      "     ├─────────┤├───────────┤│  Rxx(π/8) │\n",
      "q_4: ┤ Ry(π/4) ├┤0          ├┤1          ├\n",
      "     ├─────────┤│  Rxx(π/8) │├───────────┤\n",
      "q_5: ┤ Ry(π/4) ├┤1          ├┤0          ├\n",
      "     ├─────────┤├───────────┤│  Rxx(π/8) │\n",
      "q_6: ┤ Ry(π/4) ├┤0          ├┤1          ├\n",
      "     ├─────────┤│  Rxx(π/8) │└───────────┘\n",
      "q_7: ┤ Ry(π/4) ├┤1          ├─────────────\n",
      "     └─────────┘└───────────┘             \n"
     ]
    }
   ],
   "source": [
    "print(\"A layer for 8 qubits:\")\n",
    "layer_8 = layer(QuantumCircuit(8),8, np.pi/8)\n",
    "print(layer_8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Depth vs mid-cut entropy:\n",
      "0 0.0\n",
      "1 0.024978016784828812\n",
      "2 0.021772544197811727\n",
      "3 0.0751972971934856\n",
      "4 0.22373092024896266\n",
      "5 0.29348380892044534\n",
      "6 0.297321075807504\n",
      "7 0.39862734239903624\n",
      "8 0.5430823309221445\n"
     ]
    }
   ],
   "source": [
    "n=8; L=8; theta=0.2\n",
    "qc = QuantumCircuit(n)\n",
    "curve = []\n",
    "for d in range(L+1):\n",
    "    curve.append((d, mid_entropy(qc,n)))\n",
    "    if d<L: qc = layer(qc,n,theta)\n",
    "\n",
    "print(\"Depth vs mid-cut entropy:\")\n",
    "for L,S in curve:\n",
    "    print(L,S)"
   ]
  }
 ],
 "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
}
