{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "spin-response-1",
   "metadata": {},
   "source": [
    "# A local spin flip on a periodic ring\n",
    "\n",
    "A four-site spin-½ ferromagnet has three distinct energies in its one-flip response. Find the spectral line weights for a flip at site zero and test the first two moments against independent applications of the hopping Hamiltonian. The vacuum energy is zero, the exchange is positive, and $\\hbar=1$. Change the circumference and exchange in the first code cell. Python 3 and its standard library are sufficient."
   ]
  },
  {
   "cell_type": "code",
   "id": "spin-response-2",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "from cmath import exp\n",
    "from math import cos, pi\n",
    "\n",
    "sites = 4\n",
    "exchange = 1.0\n",
    "assert sites >= 3 and exchange > 0\n",
    "state = [complex(n == 0) for n in range(sites)]\n",
    "\n",
    "def apply_h(vector):\n",
    "    return [exchange * (v - (vector[(j-1) % sites] + vector[(j+1) % sites]) / 2)\n",
    "            for j, v in enumerate(vector)]\n",
    "\n",
    "def inner(left, right):\n",
    "    return sum(a.conjugate() * b for a, b in zip(left, right))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "spin-response-3",
   "metadata": {},
   "source": [
    "## Calculate line positions and weights\n",
    "\n",
    "For each allowed $k$, construct the normalized vector $v_k(j)=e^{ikj}/\\sqrt L$. Check its residual against the position-space Hamiltonian. Degenerate energies contribute separate nonnegative terms in this basis; only their sum is independent of basis inside the degenerate subspace."
   ]
  },
  {
   "cell_type": "code",
   "id": "spin-response-4",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "lines = []\n",
    "for n in range(sites):\n",
    "    k = 2*pi*n/sites\n",
    "    vector = [exp(1j*k*j)/sites**0.5 for j in range(sites)]\n",
    "    energy = exchange*(1-cos(k))\n",
    "    weight = abs(inner(vector, state))**2\n",
    "    error = max(abs(y-energy*x) for y, x in zip(apply_h(vector), vector))\n",
    "    assert error < 1e-11*exchange\n",
    "    lines.append((energy, weight))\n",
    "    print(f\"n={n}, energy/J={energy/exchange:.8f}, weight={weight:.8f}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "spin-response-5",
   "metadata": {},
   "source": [
    "## Check the zeroth, first and second moments\n",
    "\n",
    "In position space they are $\\langle v|v\\rangle$, $\\langle v|Hv\\rangle$, and $\\langle Hv|Hv\\rangle$. An omitted degenerate line will fail even the zeroth moment. Changing the hopping sign alone can leave the very same multiset of energies on an even ring: the vector residual above tests the momentum assignment as well."
   ]
  },
  {
   "cell_type": "code",
   "id": "spin-response-6",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "h_state = apply_h(state)\n",
    "direct = [inner(state, state).real, inner(state, h_state).real, inner(h_state, h_state).real]\n",
    "spectral = [sum(weight * energy**m for energy, weight in lines) for m in range(3)]\n",
    "for m, (actual, expected) in enumerate(zip(spectral, direct)):\n",
    "    assert abs(actual-expected) < 1e-11*max(1, exchange**m)\n",
    "    print(f\"moment {m}: spectral={actual:.10g}, position={expected:.10g}\")\n",
    "assert abs(direct[2] - 1.5*exchange**2) < 1e-12*exchange**2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "spin-response-7",
   "metadata": {},
   "source": [
    "## Change the preparation\n",
    "\n",
    "Predict the lines for a normalized uniform one-flip state. Replace the local vector with it in the overlap calculation. The uniform state's weight is one at zero energy. Its first and second moments vanish, although the Hamiltonian is unchanged. At five sites the local preparation still has first moment $J$ and second moment $3J^2/2$, while the actual frequencies move. For an interacting two-flip initial state, use the companion linked on the site's *Notebooks & executable examples* page."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "file_extension": ".py"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
