{ "cells": [ { "cell_type": "markdown", "id": "parameter-sweep-title", "metadata": {}, "source": [ "# Restartable Parameter Sweeps\n", "\n", "`ParameterSweep` expands a parameter grid, passes each point to a callback, saves returned `Result` objects, and writes `manifest.json`, `summary.csv`, and `summary.h5` when an output directory is supplied." ] }, { "cell_type": "code", "execution_count": null, "id": "parameter-sweep-imports", "metadata": {}, "outputs": [], "source": [ "from pathlib import Path\n", "\n", "import numpy as np\n", "import openquantumsim as oqs" ] }, { "cell_type": "markdown", "id": "parameter-sweep-callback-note", "metadata": {}, "source": [ "For a lightweight tutorial, the callback below returns an analytic `Result` instead of launching a backend simulation. The same pattern works for `mesolve` or `mcsolve`." ] }, { "cell_type": "code", "execution_count": null, "id": "parameter-sweep-callback", "metadata": {}, "outputs": [], "source": [ "def run_point(point: oqs.SweepPoint) -> oqs.Result:\n", " gamma = float(point.params[\"gamma\"])\n", " times = np.linspace(0.0, 4.0, 41)\n", " population = np.exp(-gamma * times).astype(np.complex128)\n", " return oqs.Result(\n", " times=times,\n", " expect=[population],\n", " solver_stats={\"retcode\": \"Success\", \"gamma\": gamma},\n", " )" ] }, { "cell_type": "code", "execution_count": null, "id": "parameter-sweep-run", "metadata": {}, "outputs": [], "source": [ "sweep = oqs.ParameterSweep(\n", " base_system={\"model\": \"analytic_decay\"},\n", " params={\"gamma\": [0.1, 0.2, 0.4]},\n", ")\n", "\n", "run = sweep.run(run_point, output_dir=Path(\"runs/tutorial_sweep\"))\n", "run.summary" ] }, { "cell_type": "markdown", "id": "parameter-sweep-rerun-note", "metadata": {}, "source": [ "Rerunning the same cell skips completed points unless `force=True` or `restart=True` is passed." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "name": "python", "pygments_lexer": "ipython3" } }, "nbformat": 4, "nbformat_minor": 5 }