Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Analytical solutions for the dynamics of a two-level system (Rabi cycling)
.ipynb_checkpoints
{
"cells": [
{
"cell_type": "markdown",
"id": "e8b420fc",
"metadata": {},
"source": [
"# Rabi Cycling in the Two-Level-System — From Scratch "
]
},
{
"cell_type": "markdown",
"id": "6d870f6f",
"metadata": {},
"source": [
"This notebook uses sympy to symbolically solve the Schrödinger equation for a two-level system, resulting in the exact analytic expressions for the complex amplitudes and the population dynamics of Rabi cycling.\n",
"\n",
"Having this written out in a computer algebra system allows to easily adapt the calculation to variations in the form of the two-level-system Hamiltonian, or different boundary conditions."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "099e6f02",
"metadata": {
"ExecuteTime": {
"end_time": "2021-12-15T20:45:09.576682Z",
"start_time": "2021-12-15T20:45:09.266346Z"
}
},
"outputs": [],
"source": [
"from sympy import Function, symbols, sqrt, Eq, Abs, exp, cos, sin, Matrix, dsolve, solve"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "392d26c5",
"metadata": {
"ExecuteTime": {
"end_time": "2021-12-15T20:45:09.579927Z",
"start_time": "2021-12-15T20:45:09.577819Z"
}
},
"outputs": [],
"source": [
"from sympy import I as 𝕚"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "22151c90",
"metadata": {
"ExecuteTime": {
"end_time": "2021-12-15T20:45:09.583750Z",
"start_time": "2021-12-15T20:45:09.580729Z"
}
},
"outputs": [],
"source": [
"g = Function('g')\n",
"e = Function('e')\n",
"t = symbols('t', positive=True)\n",
"Δ = symbols('Delta', real=True)\n",
"Ω0 = symbols('Ω_0', real=True)\n",
"Ω = symbols('Omega', real=True)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "b7ac0d90",
"metadata": {
"ExecuteTime": {
"end_time": "2021-12-15T20:45:09.716024Z",
"start_time": "2021-12-15T20:45:09.585275Z"
}
},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\left[\\begin{matrix}0 & - \\frac{Ω_{0}}{2}\\\\- \\frac{Ω_{0}}{2} & \\Delta\\end{matrix}\\right]$"
],
"text/plain": [
"Matrix([\n",
"[ 0, -Ω_0/2],\n",
"[-Ω_0/2, Delta]])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Ĥ = Matrix([\n",
" [ 0 , -Ω0/2],\n",
" [-Ω0/2, Δ ]\n",
"])\n",
""
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "4d51a2cf",
"metadata": {
"ExecuteTime": {
"end_time": "2021-12-15T20:45:09.721307Z",
"start_time": "2021-12-15T20:45:09.717074Z"
}
},
"outputs": [],
"source": [
"TDSE_system = [\n",
" g(t).diff(t) + 𝕚 * (Ĥ[0,0] * g(t) + Ĥ[0,1] * e(t)),\n",
" e(t).diff(t) + 𝕚 * (Ĥ[1,0]* g(t) + Ĥ[1,1] * e(t)),\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "74fa6ba2",
"metadata": {
"ExecuteTime": {
"end_time": "2021-12-15T20:45:10.060209Z",
"start_time": "2021-12-15T20:45:09.722256Z"
}
},
"outputs": [],
"source": [
"sols_gen = dsolve(TDSE_system, [g(t), e(t)])"
]
},
{
"cell_type": "markdown",
"id": "a501c1d6",
"metadata": {},
"source": [
"Note that `dsolve` allows to specify boundary conditions, but the resulting expressions are hard to simply. We do better by solving for the integration constants \"manually\" later on."
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "2718f04e",
"metadata": {
"ExecuteTime": {
"end_time": "2021-12-15T20:45:10.063009Z",
"start_time": "2021-12-15T20:45:10.061017Z"
}
},
"outputs": [],
"source": [
"effective_rabi_freq = {\n",
" Ω: sqrt(Δ**2 + Ω0**2)\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "75ee2c2a",
"metadata": {
"ExecuteTime": {
"end_time": "2021-12-15T20:45:10.068994Z",
"start_time": "2021-12-15T20:45:10.064272Z"
}
},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\Omega = \\sqrt{\\Delta^{2} + Ω_{0}^{2}}$"
],
"text/plain": [
"Eq(Omega, sqrt(Delta**2 + Ω_0**2))"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Eq(Ω, effective_rabi_freq[Ω])"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "79b723c4",
"metadata": {
"ExecuteTime": {
"end_time": "2021-12-15T20:45:10.071767Z",
"start_time": "2021-12-15T20:45:10.069861Z"
}
},
"outputs": [],
"source": [
"find_Ω = {\n",
" effective_rabi_freq[Ω]: Ω\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "b05f056e",
"metadata": {
"ExecuteTime": {
"end_time": "2021-12-15T20:45:10.098172Z",
"start_time": "2021-12-15T20:45:10.074005Z"
}
},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle g{\\left(t \\right)} = - \\frac{C_{1} Ω_{0} e^{- \\frac{t \\left(i \\Delta - i \\Omega\\right)}{2}}}{\\Delta - \\Omega} - \\frac{C_{2} Ω_{0} e^{- \\frac{t \\left(i \\Delta + i \\Omega\\right)}{2}}}{\\Delta + \\Omega}$"
],
"text/plain": [
"Eq(g(t), -C1*Ω_0*exp(-t*(I*Delta - I*Omega)/2)/(Delta - Omega) - C2*Ω_0*exp(-t*(I*Delta + I*Omega)/2)/(Delta + Omega))"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sols_gen[0].subs(find_Ω)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "e8e0977b",
"metadata": {
"ExecuteTime": {
"end_time": "2021-12-15T20:45:10.129875Z",
"start_time": "2021-12-15T20:45:10.099175Z"
}
},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle e{\\left(t \\right)} = C_{1} e^{- \\frac{i \\Delta t}{2}} e^{\\frac{i \\Omega t}{2}} + C_{2} e^{- \\frac{i \\Delta t}{2}} e^{- \\frac{i \\Omega t}{2}}$"
],
"text/plain": [
"Eq(e(t), C1*exp(-I*Delta*t/2)*exp(I*Omega*t/2) + C2*exp(-I*Delta*t/2)*exp(-I*Omega*t/2))"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sols_gen[1].subs(find_Ω).expand()"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "394f4efd",
"metadata": {
"ExecuteTime": {
"end_time": "2021-12-15T20:45:10.133217Z",
"start_time": "2021-12-15T20:45:10.130725Z"
}
},
"outputs": [],
"source": [
"boundary_conditions = {\n",
" t: 0,\n",
" g(t): 1,\n",
" e(t) : 0,\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "23be389b",
"metadata": {
"ExecuteTime": {
"end_time": "2021-12-15T20:45:10.136309Z",
"start_time": "2021-12-15T20:45:10.134102Z"
}
},
"outputs": [],
"source": [
"find_Ω0sq = {\n",
" Ω**2 - Δ**2: Ω0**2\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "be349bd8",
"metadata": {
"ExecuteTime": {
"end_time": "2021-12-15T20:45:10.138733Z",
"start_time": "2021-12-15T20:45:10.137171Z"
}
},
"outputs": [],
"source": [
"C1, C2 = symbols(\"C1, C2\")"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "7d321d1e",
"metadata": {
"ExecuteTime": {
"end_time": "2021-12-15T20:45:10.176835Z",
"start_time": "2021-12-15T20:45:10.139910Z"
}
},
"outputs": [],
"source": [
"_integration_constants = solve(\n",
" [sol.subs(find_Ω).subs(boundary_conditions) for sol in sols_gen],\n",
" [C1, C2]\n",
")\n",
"integration_constants = {\n",
" k: v.subs(find_Ω0sq)\n",
" for (k, v) in _integration_constants.items()\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "894eeee2",
"metadata": {
"ExecuteTime": {
"end_time": "2021-12-15T20:45:10.182662Z",
"start_time": "2021-12-15T20:45:10.177878Z"
}
},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle C_{1} = \\frac{Ω_{0}}{2 \\Omega}$"
],
"text/plain": [
"Eq(C1, Ω_0/(2*Omega))"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Eq(C1, integration_constants[C1])"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "851352b3",
"metadata": {
"ExecuteTime": {
"end_time": "2021-12-15T20:45:10.188218Z",
"start_time": "2021-12-15T20:45:10.183516Z"
}
},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle C_{2} = - \\frac{Ω_{0}}{2 \\Omega}$"
],
"text/plain": [
"Eq(C2, -Ω_0/(2*Omega))"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Eq(C2, integration_constants[C2])"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "339ee928",
"metadata": {
"ExecuteTime": {
"end_time": "2021-12-15T20:45:10.191145Z",
"start_time": "2021-12-15T20:45:10.189072Z"
}
},
"outputs": [],
"source": [
"global_phase = exp(-𝕚 * Δ * t / 2)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "21dc5c9c",
"metadata": {
"ExecuteTime": {
"end_time": "2021-12-15T20:45:10.194138Z",
"start_time": "2021-12-15T20:45:10.191962Z"
}
},
"outputs": [],
"source": [
"def remove_global_phase(eq, global_phase=global_phase):\n",
" return Eq(eq.lhs, eq.rhs * global_phase.conjugate())\n",
"\n",
"def restore_global_phase(eq, global_phase=global_phase):\n",
" return Eq(eq.lhs, eq.rhs * global_phase)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "43fb88d4",
"metadata": {
"ExecuteTime": {
"end_time": "2021-12-15T20:45:10.196779Z",
"start_time": "2021-12-15T20:45:10.195038Z"
}
},
"outputs": [],
"source": [
"def collect(eq, term):\n",
" return Eq(eq.lhs, eq.rhs.collect(term))"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "78cc5307",
"metadata": {
"ExecuteTime": {
"end_time": "2021-12-15T20:45:10.199875Z",
"start_time": "2021-12-15T20:45:10.197590Z"
}
},
"outputs": [],
"source": [
"def simplify_sol_g(sol_g):\n",
" rhs = (\n",
" sol_g\n",
" .rhs\n",
" .rewrite(sin)\n",
" .expand()\n",
" .collect(𝕚)\n",
" .subs(effective_rabi_freq)\n",
" .simplify()\n",
" .subs(find_Ω)\n",
" )\n",
" return Eq(sol_g.lhs, rhs)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "3a034ee0",
"metadata": {
"ExecuteTime": {
"end_time": "2021-12-15T20:45:10.602923Z",
"start_time": "2021-12-15T20:45:10.200800Z"
}
},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle g{\\left(t \\right)} = \\left(\\frac{i \\Delta \\sin{\\left(\\frac{\\Omega t}{2} \\right)}}{\\Omega} + \\cos{\\left(\\frac{\\Omega t}{2} \\right)}\\right) e^{- \\frac{i \\Delta t}{2}}$"
],
"text/plain": [
"Eq(g(t), (I*Delta*sin(Omega*t/2)/Omega + cos(Omega*t/2))*exp(-I*Delta*t/2))"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sol_g = restore_global_phase(\n",
" simplify_sol_g(\n",
" collect(\n",
" remove_global_phase(\n",
" sols_gen[0]\n",
" .subs(find_Ω)\n",
" .subs(integration_constants)\n",
" ).expand(),\n",
" exp(𝕚 * Ω * t / 2 )\n",
" )\n",
" )\n",
")\n",
"sol_g"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "143d31e9",
"metadata": {
"ExecuteTime": {
"end_time": "2021-12-15T20:45:10.660312Z",
"start_time": "2021-12-15T20:45:10.603849Z"
}
},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle e{\\left(t \\right)} = \\frac{i Ω_{0} e^{- \\frac{i \\Delta t}{2}} \\sin{\\left(\\frac{\\Omega t}{2} \\right)}}{\\Omega}$"
],
"text/plain": [
"Eq(e(t), I*Ω_0*exp(-I*Delta*t/2)*sin(Omega*t/2)/Omega)"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sol_e = restore_global_phase(\n",
" remove_global_phase(\n",
" sols_gen[1].subs(find_Ω).subs(integration_constants)\n",
" ).expand().rewrite(sin).expand()\n",
")\n",
"sol_e"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "5480adc7",
"metadata": {
"ExecuteTime": {
"end_time": "2021-12-15T20:45:10.670174Z",
"start_time": "2021-12-15T20:45:10.661098Z"
}
},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\left|{e{\\left(t \\right)}}\\right|^{2} = \\frac{Ω_{0}^{2} \\sin^{2}{\\left(\\frac{\\Omega t}{2} \\right)}}{\\Omega^{2}}$"
],
"text/plain": [
"Eq(Abs(e(t))**2, Ω_0**2*sin(Omega*t/2)**2/Omega**2)"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pop_e = (sol_e.rhs * sol_e.rhs.conjugate())\n",
"Eq(Abs(e(t))**2, pop_e)"
]
}
],
"metadata": {
"hide_input": false,
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.9.7"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
},
"varInspector": {
"cols": {
"lenName": 16,
"lenType": 16,
"lenVar": 40
},
"kernels_config": {
"python": {
"delete_cmd_postfix": "",
"delete_cmd_prefix": "del ",
"library": "var_list.py",
"varRefreshCmd": "print(var_dic_list())"
},
"r": {
"delete_cmd_postfix": ") ",
"delete_cmd_prefix": "rm(",
"library": "var_list.r",
"varRefreshCmd": "cat(var_dic_list()) "
}
},
"types_to_exclude": [
"module",
"function",
"builtin_function_or_method",
"instance",
"_Feature"
],
"window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment