QuantumLearning/notebooks/algorithms/module_03_qft/studio.ipynb

255 lines
10 KiB
Text

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# QFT and Periodic Structure Studio\n"
],
"id": "e7d9f492"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The studio asks you to turn the QFT from a named transform into a defended design artifact. That means explicit structure, explicit verification, and explicit tradeoffs.\n"
],
"id": "c10dab4a"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Design Brief\n",
"\n",
"\n",
" Build a mini-transform notebook that includes one explicit full QFT, one verification workflow, and one approximation discussion. Your job is not only to make it work. Your job is to make it auditable.\n"
],
"id": "7873a473"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from pathlib import Path\n",
"import sys\n",
"\n",
"project_root = Path.cwd().resolve()\n",
"while not (project_root / \"pyproject.toml\").exists():\n",
" if project_root.parent == project_root:\n",
" raise RuntimeError(\"Could not locate the project root from this notebook.\")\n",
" project_root = project_root.parent\n",
"\n",
"src_path = project_root / \"src\"\n",
"if str(src_path) not in sys.path:\n",
" sys.path.insert(0, str(src_path))\n"
],
"id": "3e7046de"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from math import pi\n",
"\n",
"from quantum_learning import (\n",
" counts_to_probabilities,\n",
" draw_circuit,\n",
" editable_circuit_lab,\n",
" plot_counts,\n",
" plot_probabilities,\n",
" quiz_block,\n",
" reflection_box,\n",
" simulate_counts,\n",
" statevector_probabilities,\n",
" step_reference_table,\n",
")\n",
"from qiskit import QuantumCircuit\n",
"from qiskit.quantum_info import Statevector\n"
],
"id": "319d85e5"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Studio Prompt 1: Explicit Transform Notebook\n",
"\n",
"\n",
" Write the transform out explicitly enough that a reviewer could inspect every gate without guessing which part is essential.\n"
],
"id": "a6a0439d"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"editable_code = '\\nfrom math import pi\\nfrom qiskit import QuantumCircuit\\n\\ndef qft3() -> QuantumCircuit:\\n circuit = QuantumCircuit(3, name=\"qft3\")\\n # [1] Start with explicit local structure.\\n circuit.h(2)\\n circuit.cp(pi / 2, 1, 2)\\n circuit.cp(pi / 4, 0, 2)\\n # [2] Continue the controlled-phase ladder.\\n circuit.h(1)\\n circuit.cp(pi / 2, 0, 1)\\n circuit.h(0)\\n # [3] State the output ordering explicitly.\\n circuit.swap(0, 2)\\n return circuit\\n\\ncircuit = qft3()\\n'\n",
"editable_circuit_lab(\n",
" initial_code=editable_code,\n",
" context={\"QuantumCircuit\": QuantumCircuit, \"simulate_counts\": simulate_counts},\n",
" title='Studio 1: Explicit Small QFT',\n",
" instructions='Refine the explicit builder until every gate has a role you can explain clearly in prose.',\n",
" shots=256,\n",
")\n"
],
"id": "9f86f50e"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Studio Prompt 2: Recovery And Ordering\n",
"\n",
"\n",
" Pair the transform with an inverse or other verification strategy, and make sure your ordering convention is stated rather than assumed.\n"
],
"id": "ac1e8a21"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"editable_code = '\\nfrom math import pi\\nfrom qiskit import QuantumCircuit\\n\\ndef qft3() -> QuantumCircuit:\\n circuit = QuantumCircuit(3, name=\"qft3\")\\n circuit.h(2)\\n circuit.cp(pi / 2, 1, 2)\\n circuit.cp(pi / 4, 0, 2)\\n circuit.h(1)\\n circuit.cp(pi / 2, 0, 1)\\n circuit.h(0)\\n circuit.swap(0, 2)\\n return circuit\\n\\ncircuit = QuantumCircuit(3, 3)\\ncircuit.x(0)\\ncircuit.compose(qft3(), inplace=True)\\ncircuit.compose(qft3().inverse(), inplace=True)\\ncircuit.measure([0, 1, 2], [0, 1, 2])\\n'\n",
"editable_circuit_lab(\n",
" initial_code=editable_code,\n",
" context={\"QuantumCircuit\": QuantumCircuit, \"simulate_counts\": simulate_counts},\n",
" title='Studio 2: Recovery Notebook',\n",
" instructions='Choose a prepared state, verify recovery, and write down the ordering convention you are using.',\n",
" shots=256,\n",
")\n"
],
"id": "ecb4fb3b"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Studio Prompt 3: Approximation Memo\n",
"\n",
"\n",
" Compare full and approximate variants as if you had to justify the choice to another engineer working under a depth budget.\n"
],
"id": "ef0f7b3d"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"editable_code = '\\nfrom math import pi\\nfrom qiskit import QuantumCircuit\\n\\ndef qft3(drop_smallest_angle: bool = False) -> QuantumCircuit:\\n circuit = QuantumCircuit(3, name=\"qft3\")\\n circuit.h(2)\\n circuit.cp(pi / 2, 1, 2)\\n if not drop_smallest_angle:\\n circuit.cp(pi / 4, 0, 2)\\n circuit.h(1)\\n circuit.cp(pi / 2, 0, 1)\\n circuit.h(0)\\n circuit.swap(0, 2)\\n return circuit\\n\\ncircuit = qft3(drop_smallest_angle=True)\\n'\n",
"editable_circuit_lab(\n",
" initial_code=editable_code,\n",
" context={\"QuantumCircuit\": QuantumCircuit, \"simulate_counts\": simulate_counts},\n",
" title='Studio 3: Approximation Memo',\n",
" instructions='Create a full and a simplified variant, then defend the simplification with concrete evidence and scope limits.',\n",
" shots=256,\n",
")\n"
],
"id": "7057552b"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"quiz_block([{'prompt': 'What is the right studio objective for a QFT module?', 'options': ['Build and defend a small transform variant with explicit choices about ordering, verification, and approximation', 'Memorize one diagram and redraw it from memory', 'Avoid discussing phases so the notebook stays simpler'], 'correct_index': 0, 'explanation': 'Studio work should convert the transform into a design object.'}, {'prompt': 'Why compare full and approximate variants side by side?', 'options': ['Because design quality depends on explicit cost-benefit reasoning, not only on correctness in the abstract', 'Because the exact transform is never useful', 'Because approximate transforms cannot be verified'], 'correct_index': 0, 'explanation': 'Side-by-side comparison is how tradeoffs become concrete.'}], heading='Studio Design Check')\n"
],
"id": "c0c68fa7"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Studio Debrief\n",
"\n",
"\n",
" A good studio artifact in this module feels restrained and rigorous. It does not rely on fame. It makes the transform inspectable, testable, and discussable. That is the standard worth carrying forward.\n"
],
"id": "130402df"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Studio Standard\n",
"\n",
"A strong studio notebook is compact, explicit, and reviewable. It does not hide behind volume. It makes clear what family of circuits was explored, what invariant mechanism survived across the variants, what evidence justified the final recommendation, and what tradeoffs remained open. If your notebook cannot answer those questions yet, keep refining it. That refinement is the studio.\n"
],
"id": "cb73a53e"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## What A Finished Studio Should Feel Like\n",
"\n",
"The finished notebook should feel like a small engineering artifact rather than a scrapbook. A reviewer should be able to open it, understand the task, inspect the candidate circuits, see the evidence, and understand why one recommendation won. If that standard is met on these small modules, later capstone work becomes much more realistic.\n"
],
"id": "ae01c89c"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"reflection_box('What part of the QFT became most legible to you only after writing it out explicitly?')\n"
],
"id": "7de7fb78"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"reflection_box('Which verification workflow did you choose in the studio, and why was it appropriate?')\n"
],
"id": "a6344b9f"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"reflection_box('How did you describe the tradeoff in your approximate-QFT memo?')\n"
],
"id": "f8863336"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"reflection_box('Name one later band where you expect this QFT vocabulary to matter directly.')\n"
],
"id": "aab268f6"
}
],
"metadata": {
"kernelspec": {
"display_name": "QuantumLearning (.venv)",
"language": "python",
"name": "quantum-learning"
},
"language_info": {
"name": "python",
"version": "3.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}