{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\nThe Schmidt decomposition factorizes a bipartite pure state into local bases revealing its entanglement spectrum.\\nComputing the Schmidt coefficients is a quick diagnostic of bipartite entanglement.\\n\\n\\nExecises:\\n- Modify the circuit to produce a partially entangled state by inserting a Ry(θ) on qubit 1 controlled by qubit 0.\\n- Plot entropy vs θ to visualize entanglement tuning.\\n'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\"\"\n",
    "The Schmidt decomposition factorizes a bipartite pure state into local bases revealing its entanglement spectrum.\n",
    "Computing the Schmidt coefficients is a quick diagnostic of bipartite entanglement.\n",
    "\n",
    "\n",
    "Execises:\n",
    "- Modify the circuit to produce a partially entangled state by inserting a Ry(θ) on qubit 1 controlled by qubit 0.\n",
    "- Plot entropy vs θ to visualize entanglement tuning.\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Schmidt coeffs: [0.707107 0.707107]\n",
      "Schmidt entropy (von Neumann of reduced state):\n",
      "S(ρ_A) = 1.0\n",
      "Von Neumann entropy S(ρ_A) = 1.0\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Bad pipe message: %s [b'he\\r\\nCache-Control: no-cache\\r\\nUser-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 15_6_1) AppleWeb', b't/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36\\r\\nUpgrade: w', b'socket\\r\\nOrigin: file://\\r\\nSec-WebSocket-Version: 13\\r\\nAccept-Encoding: gzip, deflate, br\\r\\nAccept-Langu']\n",
      "Bad pipe message: %s [b'he\\r\\nCache-Control: no-cache\\r\\nUser-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 15_6_1) AppleWeb', b't/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36\\r\\nUpgrade: w', b'socket\\r\\nOrigin: file://\\r\\nSec-WebSocket-Version: 13\\r\\nAccept-Encoding: gzip, deflate, br\\r\\nAccept-Langu']\n"
     ]
    }
   ],
   "source": [
    "from qiskit import QuantumCircuit\n",
    "from qiskit.quantum_info import Statevector, partial_trace, DensityMatrix\n",
    "import numpy as np\n",
    "\n",
    "# Two-qubit entangled state: (|00> + |11>)/sqrt(2)\n",
    "qc = QuantumCircuit(2)\n",
    "qc.h(0); qc.cx(0,1)  # |Φ+>\n",
    "\n",
    "psi = Statevector.from_instruction(qc) # complex vector of length 4\n",
    "vec = psi.data.reshape(2,2) # Reshape into 2x2 matrix for bipartition A|B (qubit 0 as A, qubit 1 as B)\n",
    "s = np.linalg.svd(vec, compute_uv=False) # Schmidt coefficients\n",
    "print(\"Schmidt coeffs:\", np.round(s,6))\n",
    "print(\"Schmidt entropy (von Neumann of reduced state):\")\n",
    "p = s**2\n",
    "entropy = -np.sum(p * np.log2(p))\n",
    "print(\"S(ρ_A) =\", entropy)\n",
    "\n",
    "# Cross-check via reduced density matrix\n",
    "rhoA = partial_trace(DensityMatrix(psi), [1]).data # Trace out qubit 1\n",
    "evals = np.linalg.eigvalsh(rhoA)\n",
    "S = -np.sum(evals* np.log2(evals))\n",
    "print(\"Von Neumann entropy S(ρ_A) =\", 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
}
