{
 "nbformat": 4,
 "nbformat_minor": 5,
 "metadata": {
  "kernelspec": {
   "name": "python3",
   "language": "python",
   "display_name": "Python 3"
  },
  "language_info": {
   "name": "python"
  },
  "fundamentals": {
   "title": "Can an atom leave its initial state and still pass the final test?",
   "lead": "A driven two-level atom undergoes repeated ideal measurements. Compare two laboratory records, one retaining only runs that passed every test and another retaining every run. The distinction is visible even when every measurement is made with the same apparatus.",
   "unit_slug": "selective-and-unread-zeno-measurements"
  }
 },
 "cells": [
  {
   "cell_type": "markdown",
   "id": "1a3c62817dcc",
   "metadata": {},
   "source": [
    "# Can an atom leave its initial state and still pass the final test?\n",
    "\n",
    "A driven two-level atom undergoes repeated ideal measurements. Compare two laboratory records, one retaining only runs that passed every test and another retaining every run. The distinction is visible even when every measurement is made with the same apparatus."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e6739c02f84f",
   "metadata": {
    "fundamentals": {
     "section": "derive-the-model"
    }
   },
   "source": [
    "## Count histories or evolve a population\n",
    "\n",
    "Write the resonant Hamiltonian in the rotating frame as $H=\\hbar\\Omega\\sigma_x/2$. A pulse of duration $T/N$ rotates the initial ground state into $\\cos(\\alpha/2)|0\\rangle-i\\sin(\\alpha/2)|1\\rangle$ with $\\alpha=\\Omega T/N$. The probability of passing one ground-state test is $c=\\cos^2(\\alpha/2)$. Each successful projection prepares the same ground state for the next interval. The probability that a complete record contains only successes is consequently $P_{\\rm all}=c^N$.\n",
    "\n",
    "For an unread measurement, keep the atoms detected in either state. The projection removes the off-diagonal density-matrix elements, leaving a classical mixture just before the next pulse. If $p_j$ denotes the ground fraction after measurement $j$, the next pulse maps it to $p_{j+1}=c p_j+(1-c)(1-p_j)$. The excess population $z_j=2p_j-1$ then obeys $z_{j+1}=(2c-1)z_j$. Starting in the ground state gives\n",
    "\n",
    "$$P_{\\rm final}=\\frac{1+\\cos^N(\\Omega T/N)}2. $$\n",
    "\n",
    "Leaving the ground state in one interval and returning in a later one contributes to the final fraction. It removes that run from the all-success record. The two probabilities therefore refer to different experimental questions, even though they use exactly the same sequence of pulses and measurements."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6f1b55846c66",
   "metadata": {
    "fundamentals": {
     "section": "worked-prediction"
    }
   },
   "source": [
    "## Follow a half-turn in two steps\n",
    "\n",
    "Choose $\\Omega T=\\pi$ and make two evenly spaced measurements, including the one at the end. Each pulse is a quarter-turn of the Bloch vector and gives either result with equal probability following either previous outcome. The four two-result strings have probability one quarter each. Only 00 passes every test, whereas 00 and 10 both finish in the ground state. The fractions are therefore 0.25 and 0.50. With a single final measurement the undisturbed half-turn ends entirely in the excited state.\n",
    "\n",
    "At fixed total duration and very large $N$, Taylor expansion gives\n",
    "\n",
    "$$\n",
    "\\log P_{\\rm all}\\simeq-(\\Omega T)^2/(4N).\n",
    "$$\n",
    "\n",
    "The survival probability tends to one because the transition probability per interval is quadratic in its short duration. This prediction assumes ideal instantaneous projective measurements and coherent rotations between them. A measurement with a fixed finite duration cannot be packed arbitrarily densely into a fixed time."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9a04b18fdcdc",
   "metadata": {
    "fundamentals": {
     "section": "choose-inputs",
     "notebook_only": true
    }
   },
   "source": [
    "## Set the experimental inputs\n",
    "\n",
    "Edit these values, then run the calculation and both figures again."
   ]
  },
  {
   "cell_type": "code",
   "id": "51ef78ee9649",
   "metadata": {},
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "teal, orange, blue = \"#19596a\", \"#aa462a\", \"#3868a0\"\n",
    "def axes(xlabel, ylabel):\n",
    "    fig, ax = plt.subplots(figsize=(7, 4), constrained_layout=True)\n",
    "    ax.set(xlabel=xlabel, ylabel=ylabel)\n",
    "    ax.grid(alpha=0.2)\n",
    "    return fig, ax"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "id": "65c5e4887c34",
   "metadata": {},
   "source": [
    "pulse_area_in_pi = 1.0\n",
    "measurements = 8"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "id": "9e9d122bdb04",
   "metadata": {},
   "source": [
    "assert measurements >= 1 and int(measurements) == measurements\n",
    "measurements = int(measurements)\n",
    "area = np.pi * pulse_area_in_pi\n",
    "def records(area, count):\n",
    "    c = np.cos(area / (2*count))**2\n",
    "    return c**count, (1 + np.cos(area/count)**count)/2\n",
    "\n",
    "all_pass, final_ground = records(area, measurements)\n",
    "angle = area / (2*measurements)\n",
    "rotation = np.array([[np.cos(angle), -1j*np.sin(angle)], [-1j*np.sin(angle), np.cos(angle)]])\n",
    "rho = np.diag([1., 0.])\n",
    "for j in range(measurements):\n",
    "    rho = np.diag(np.diag(rotation @ rho @ rotation.conj().T))\n",
    "assert np.isclose(rho[0,0], final_ground)\n",
    "print(f\"Passed every test: {all_pass:.6f}. Ground at the final test: {final_ground:.6f}.\")"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "id": "caa4dd12601f",
   "metadata": {
    "fundamentals": {
     "figure": {
      "file": "quantum-mechanics-quantum-laboratory-1.png",
      "alt": "An undisturbed coherent oscillation and two measured probability records are shown against the fraction of the total drive time.",
      "caption": "Markers represent measurements. The continuous reference curve is the ground population of an atom subjected to the same drive with every intermediate test omitted."
     }
    }
   },
   "source": [
    "step = np.arange(measurements+1)\n",
    "time_fraction = np.linspace(0,1,401)\n",
    "fig, ax = axes(\"Time / total drive time\", \"Probability\")\n",
    "ax.plot(time_fraction,np.cos(area*time_fraction/2)**2,color=\"grey\",label=\"Uninterrupted drive\")\n",
    "ax.plot(step/measurements,np.cos(angle)**(2*step),\"o-\",color=teal,label=\"Passed every test\")\n",
    "ax.plot(step/measurements,(1+np.cos(2*angle)**step)/2,\"s--\",color=orange,label=\"Ground at this test\")\n",
    "ax.set_ylim(-.03,1.06); ax.legend()\n",
    "plt.show()"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "a81b73c2500b",
   "metadata": {
    "fundamentals": {
     "notebook_only": true
    }
   },
   "source": [
    "Markers represent measurements. The continuous reference curve is the ground population of an atom subjected to the same drive with every intermediate test omitted."
   ]
  },
  {
   "cell_type": "code",
   "id": "aa951b520351",
   "metadata": {
    "fundamentals": {
     "figure": {
      "file": "quantum-mechanics-quantum-laboratory-2.png",
      "alt": "The probabilities of passing all tests and finishing in the ground state approach one as the number of tests increases.",
      "caption": "Keep the pulse area and total time fixed whilst shortening each free interval. An increasingly dense train of ideal observations preserves the initial state."
     }
    }
   },
   "source": [
    "counts = np.arange(1,max(51,min(500,measurements+1)))\n",
    "series = records(area,counts)\n",
    "fig,ax = axes(\"Number of measurements in the fixed total time\", \"Probability\")\n",
    "ax.plot(counts,series[0],\".-\",color=teal,label=\"Passed every test\")\n",
    "ax.plot(counts,series[1],\"--\",color=orange,label=\"Ground at final test\")\n",
    "ax.set_ylim(-.03,1.06);ax.legend()\n",
    "plt.show()"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "12eb9ef08e2f",
   "metadata": {
    "fundamentals": {
     "notebook_only": true
    }
   },
   "source": [
    "Keep the pulse area and total time fixed whilst shortening each free interval. An increasingly dense train of ideal observations preserves the initial state."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9adfbcf6593b",
   "metadata": {
    "fundamentals": {
     "section": "problem-1"
    }
   },
   "source": [
    "## Exercise · recover a mixed population\n",
    "\n",
    "If the atom initially occupies the ground state with probability 0.7 and has no coherences, find the final ground probability for four measurements and a total pulse area of $\\pi$."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e3ffe54af715",
   "metadata": {
    "fundamentals": {
     "solution": true
    }
   },
   "source": [
    "The initial contrast is 0.4. Each interval multiplies it by $\\cos(\\pi/4)=1/\\sqrt2$. Four steps give contrast 0.1, so the final ground fraction is $(1+0.1)/2=0.55$. The all-pass probability would instead contain a first measurement conditioned on the initially mixed preparation."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "12292ef100e4",
   "metadata": {
    "fundamentals": {
     "section": "problem-2"
    }
   },
   "source": [
    "## Exercise · budget the number of tests\n",
    "\n",
    "Estimate the number of ideal tests needed for 99 percent all-pass survival during a coherent half-turn. Check the estimate with the editable input."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4e8c04c7c75a",
   "metadata": {
    "fundamentals": {
     "solution": true
    }
   },
   "source": [
    "Equating the logarithm of 0.99 to $-\\pi^2/(4N)$ gives $N\\simeq245.5$. Rounding up to 246 and evaluating the exact cosine power gives about 0.99002. This estimate concerns survival over an entire record. Filtering out only the atoms ending in the excited state would estimate a different probability."
   ]
  }
 ]
}
