mirror of
https://github.com/saymrwulf/QuantumLearning.git
synced 2026-05-15 21:00:49 +00:00
814 lines
35 KiB
Text
814 lines
35 KiB
Text
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"<!-- QL_BADGE -->\n",
|
|
"<div style=\"padding:0.55rem 0.8rem; border-left:6px solid #2563eb; background:#dbeafe; color:#1e3a8a; border-radius:0.35rem; font-family:Helvetica, Arial, sans-serif; margin:0.15rem 0 0.85rem 0;\">\n",
|
|
"<strong>META READING</strong> · Difficulty 1/10 · Notebook-level rule, objective, or usage guidance.\n",
|
|
"</div>\n",
|
|
"\n",
|
|
"# QFT and Periodic Structure Lab\n"
|
|
],
|
|
"id": "2d61acb9"
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"<!-- QL_BADGE -->\n",
|
|
"<div style=\"padding:0.55rem 0.8rem; border-left:6px solid #2563eb; background:#dbeafe; color:#1e3a8a; border-radius:0.35rem; font-family:Helvetica, Arial, sans-serif; margin:0.15rem 0 0.85rem 0;\">\n",
|
|
"<strong>META READING</strong> · Difficulty 1/10 · Official walkthrough guardrail.\n",
|
|
"</div>\n",
|
|
"\n",
|
|
"<!-- COURSE_NAV_TOP -->\n",
|
|
"## Mainline Navigation\n",
|
|
"\n",
|
|
"Step 36 of 59. Follow the official walkthrough in order.\n",
|
|
"\n",
|
|
"Previous notebook: [QFT and Periodic Structure Lecture](lecture.ipynb)\n",
|
|
"\n",
|
|
"Next notebook: [QFT and Periodic Structure Problems](problems.ipynb)\n",
|
|
"\n",
|
|
"Rule: complete the mandatory cells in this notebook before you open the next one.\n"
|
|
],
|
|
"id": "a56a895f"
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"<!-- QL_BADGE -->\n",
|
|
"<div style=\"padding:0.55rem 0.8rem; border-left:6px solid #2563eb; background:#dbeafe; color:#1e3a8a; border-radius:0.35rem; font-family:Helvetica, Arial, sans-serif; margin:0.15rem 0 0.85rem 0;\">\n",
|
|
"<strong>META READING</strong> · Difficulty 1/10 · Notebook-level rule, objective, or usage guidance.\n",
|
|
"</div>\n",
|
|
"\n",
|
|
"The lab focuses on explicit construction, verification, and controlled approximation. Unlike the previous modules, some of the most useful evidence will be statevector-based rather than count-based. That is deliberate.\n"
|
|
],
|
|
"id": "ca6f425a"
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"<!-- QL_BADGE -->\n",
|
|
"<div style=\"padding:0.55rem 0.8rem; border-left:6px solid #2563eb; background:#dbeafe; color:#1e3a8a; border-radius:0.35rem; font-family:Helvetica, Arial, sans-serif; margin:0.15rem 0 0.85rem 0;\">\n",
|
|
"<strong>META READING</strong> · Difficulty 1/10 · Notebook-level rule, objective, or usage guidance.\n",
|
|
"</div>\n",
|
|
"\n",
|
|
"## Lab Protocol\n",
|
|
"\n",
|
|
"\n",
|
|
" Change one structural feature at a time and pair every change with a verification move. If you adjust an angle, say what contribution you are changing. If you remove a swap, say which ordering convention you are abandoning. If you approximate the transform, say how you intend to observe the cost.\n"
|
|
],
|
|
"id": "56044ad0"
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"ql_injected": "badge",
|
|
"ql_track": "mandatory",
|
|
"ql_role": "setup"
|
|
},
|
|
"source": [
|
|
"<!-- QL_BADGE -->\n",
|
|
"<div style=\"padding:0.55rem 0.8rem; border-left:6px solid #6b7280; background:#e5e7eb; color:#111827; border-radius:0.35rem; font-family:Helvetica, Arial, sans-serif; margin:0.15rem 0 0.85rem 0;\">\n",
|
|
"<strong>MANDATORY SETUP</strong> · Difficulty 1/10 · Environment, import, or helper cell required by the notebook.\n",
|
|
"</div>\n"
|
|
],
|
|
"id": "d3172a31"
|
|
},
|
|
{
|
|
"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": "8ad03118"
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"ql_injected": "badge",
|
|
"ql_track": "mandatory",
|
|
"ql_role": "exercise"
|
|
},
|
|
"source": [
|
|
"<!-- QL_BADGE -->\n",
|
|
"<div style=\"padding:0.55rem 0.8rem; border-left:6px solid #15803d; background:#dcfce7; color:#14532d; border-radius:0.35rem; font-family:Helvetica, Arial, sans-serif; margin:0.15rem 0 0.85rem 0;\">\n",
|
|
"<strong>MANDATORY EXERCISE</strong> · Difficulty 2/10 · Official walkthrough runnable or written exercise.\n",
|
|
"</div>\n"
|
|
],
|
|
"id": "26b3f76d"
|
|
},
|
|
{
|
|
"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": "3cc4775a"
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"<!-- QL_BADGE -->\n",
|
|
"<div style=\"padding:0.55rem 0.8rem; border-left:6px solid #2563eb; background:#dbeafe; color:#1e3a8a; border-radius:0.35rem; font-family:Helvetica, Arial, sans-serif; margin:0.15rem 0 0.85rem 0;\">\n",
|
|
"<strong>META READING</strong> · Difficulty 1/10 · Notebook-level rule, objective, or usage guidance.\n",
|
|
"</div>\n",
|
|
"\n",
|
|
"## Lab 1: Build The Transform Explicitly\n",
|
|
"\n",
|
|
"\n",
|
|
" Start by editing the explicit QFT itself. The goal is not speed. The goal is to learn how each visible region contributes to the full transform and to make the diagram readable enough that you can review it without hiding behind the name QFT.\n"
|
|
],
|
|
"id": "2a0a137a"
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"ql_injected": "badge",
|
|
"ql_track": "mandatory",
|
|
"ql_role": "exercise"
|
|
},
|
|
"source": [
|
|
"<!-- QL_BADGE -->\n",
|
|
"<div style=\"padding:0.55rem 0.8rem; border-left:6px solid #15803d; background:#dcfce7; color:#14532d; border-radius:0.35rem; font-family:Helvetica, Arial, sans-serif; margin:0.15rem 0 0.85rem 0;\">\n",
|
|
"<strong>MANDATORY EXERCISE</strong> · Difficulty 2/10 · Official walkthrough runnable or written exercise.\n",
|
|
"</div>\n"
|
|
],
|
|
"id": "85f3642b"
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"jupyter": {
|
|
"source_hidden": true
|
|
},
|
|
"tags": [
|
|
"hide-input"
|
|
]
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"step_reference_table([{'marker': '[1]', 'code_focus': 'Start with a small, fully explicit QFT instead of hiding it behind a library call.', 'diagram_effect': 'The circuit shows every controlled phase and swap directly.', 'why_it_matters': 'A professional designer needs to know which pieces are essential and which are optional approximations.'}, {'marker': '[2]', 'code_focus': 'Interpret each controlled phase as a graded correlation, not as a mysterious flourish.', 'diagram_effect': 'The middle of the circuit becomes a ladder of phase relationships.', 'why_it_matters': 'Controlled-phase structure is the real content of the QFT.'}, {'marker': '[3]', 'code_focus': 'Use swaps to state the intended output ordering explicitly.', 'diagram_effect': 'The end of the diagram makes the bit-reversal issue visible.', 'why_it_matters': 'Output ordering is part of the interface, not a postscript.'}, {'marker': '[4]', 'code_focus': 'Verify the transform with statevector or inverse-QFT checks instead of trusting the picture alone.', 'diagram_effect': 'The notebook connects circuit graphics to basis-change evidence.', 'why_it_matters': 'Basis-change circuits need stronger verification habits than simple compute-and-measure patterns.'}])\n"
|
|
],
|
|
"id": "eae888dd"
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"ql_injected": "badge",
|
|
"ql_track": "mandatory",
|
|
"ql_role": "exercise"
|
|
},
|
|
"source": [
|
|
"<!-- QL_BADGE -->\n",
|
|
"<div style=\"padding:0.55rem 0.8rem; border-left:6px solid #15803d; background:#dcfce7; color:#14532d; border-radius:0.35rem; font-family:Helvetica, Arial, sans-serif; margin:0.15rem 0 0.85rem 0;\">\n",
|
|
"<strong>MANDATORY EXERCISE</strong> · Difficulty 2/10 · Official walkthrough runnable or written exercise.\n",
|
|
"</div>\n"
|
|
],
|
|
"id": "12a4181c"
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"jupyter": {
|
|
"source_hidden": true
|
|
},
|
|
"tags": [
|
|
"hide-input"
|
|
]
|
|
},
|
|
"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='Lab 1: Explicit QFT Builder',\n",
|
|
" instructions='Adjust one phase angle or swap at a time and explain which part of the interface or representation you changed.',\n",
|
|
" shots=256,\n",
|
|
")\n"
|
|
],
|
|
"id": "4fb8a5b7"
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"ql_injected": "badge",
|
|
"ql_track": "mandatory",
|
|
"ql_role": "exercise"
|
|
},
|
|
"source": [
|
|
"<!-- QL_BADGE -->\n",
|
|
"<div style=\"padding:0.55rem 0.8rem; border-left:6px solid #15803d; background:#dcfce7; color:#14532d; border-radius:0.35rem; font-family:Helvetica, Arial, sans-serif; margin:0.15rem 0 0.85rem 0;\">\n",
|
|
"<strong>MANDATORY EXERCISE</strong> · Difficulty 2/10 · Official walkthrough runnable or written exercise.\n",
|
|
"</div>\n"
|
|
],
|
|
"id": "d9ccd8f7"
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def 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",
|
|
"\n",
|
|
"ax = draw_circuit(qft3())\n",
|
|
"ax\n"
|
|
],
|
|
"id": "fac7b42e"
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"ql_injected": "badge",
|
|
"ql_track": "mandatory",
|
|
"ql_role": "test"
|
|
},
|
|
"source": [
|
|
"<!-- QL_BADGE -->\n",
|
|
"<div style=\"padding:0.55rem 0.8rem; border-left:6px solid #15803d; background:#dcfce7; color:#14532d; border-radius:0.35rem; font-family:Helvetica, Arial, sans-serif; margin:0.15rem 0 0.85rem 0;\">\n",
|
|
"<strong>MANDATORY TEST</strong> · Difficulty 2/10 · Official walkthrough multiple-choice test.\n",
|
|
"</div>\n"
|
|
],
|
|
"id": "63b41ca3"
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"jupyter": {
|
|
"source_hidden": true
|
|
},
|
|
"tags": [
|
|
"hide-input"
|
|
]
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"quiz_block([{'prompt': 'If you drop the final swap layer in a QFT notebook, what changes first?', 'options': ['The output ordering convention becomes different even if the phase logic is otherwise intact', 'All controlled phases disappear', 'The circuit can no longer be simulated'], 'correct_index': 0, 'explanation': 'Removing swaps changes the interface contract of the transform.'}, {'prompt': 'What is the best reason to edit one controlled-phase angle at a time?', 'options': ['So you can connect a visible change in structure to a specific representational consequence', 'So the notebook runs faster', 'So the circuit has fewer qubits'], 'correct_index': 0, 'explanation': 'Controlled ablation is how you learn what each phase layer is doing.'}], heading='Lab Checkpoint A')\n"
|
|
],
|
|
"id": "2038fce9"
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"ql_injected": "badge",
|
|
"ql_track": "mandatory",
|
|
"ql_role": "exercise"
|
|
},
|
|
"source": [
|
|
"<!-- QL_BADGE -->\n",
|
|
"<div style=\"padding:0.55rem 0.8rem; border-left:6px solid #15803d; background:#dcfce7; color:#14532d; border-radius:0.35rem; font-family:Helvetica, Arial, sans-serif; margin:0.15rem 0 0.85rem 0;\">\n",
|
|
"<strong>MANDATORY EXERCISE</strong> · Difficulty 2/10 · Official walkthrough runnable or written exercise.\n",
|
|
"</div>\n"
|
|
],
|
|
"id": "cb0164f8"
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"jupyter": {
|
|
"source_hidden": true
|
|
},
|
|
"tags": [
|
|
"hide-input"
|
|
]
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"reflection_box('Which verification move felt most convincing in this lab, and why?')\n"
|
|
],
|
|
"id": "88572121"
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"<!-- QL_BADGE -->\n",
|
|
"<div style=\"padding:0.55rem 0.8rem; border-left:6px solid #15803d; background:#dcfce7; color:#14532d; border-radius:0.35rem; font-family:Helvetica, Arial, sans-serif; margin:0.15rem 0 0.85rem 0;\">\n",
|
|
"<strong>MANDATORY READING</strong> · Difficulty 2/10 · Official walkthrough reading cell.\n",
|
|
"</div>\n",
|
|
"\n",
|
|
"## Lab 2: Verify With Recovery\n",
|
|
"\n",
|
|
"\n",
|
|
" Use QFT followed by inverse QFT as a recovery pattern. This is a disciplined way to verify that your hand-written transform is not merely plausible-looking. A transform that cannot recover cleanly under its own inverse needs explanation.\n"
|
|
],
|
|
"id": "70c88146"
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"ql_injected": "badge",
|
|
"ql_track": "mandatory",
|
|
"ql_role": "exercise"
|
|
},
|
|
"source": [
|
|
"<!-- QL_BADGE -->\n",
|
|
"<div style=\"padding:0.55rem 0.8rem; border-left:6px solid #15803d; background:#dcfce7; color:#14532d; border-radius:0.35rem; font-family:Helvetica, Arial, sans-serif; margin:0.15rem 0 0.85rem 0;\">\n",
|
|
"<strong>MANDATORY EXERCISE</strong> · Difficulty 2/10 · Official walkthrough runnable or written exercise.\n",
|
|
"</div>\n"
|
|
],
|
|
"id": "b8c10e42"
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"jupyter": {
|
|
"source_hidden": true
|
|
},
|
|
"tags": [
|
|
"hide-input"
|
|
]
|
|
},
|
|
"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='Lab 2: QFT Plus Inverse Recovery',\n",
|
|
" instructions='Change the prepared basis state or slightly edit the transform. Then decide whether the recovery still matches the story you think the circuit is telling.',\n",
|
|
" shots=256,\n",
|
|
")\n"
|
|
],
|
|
"id": "03d87149"
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"ql_injected": "badge",
|
|
"ql_track": "mandatory",
|
|
"ql_role": "exercise"
|
|
},
|
|
"source": [
|
|
"<!-- QL_BADGE -->\n",
|
|
"<div style=\"padding:0.55rem 0.8rem; border-left:6px solid #15803d; background:#dcfce7; color:#14532d; border-radius:0.35rem; font-family:Helvetica, Arial, sans-serif; margin:0.15rem 0 0.85rem 0;\">\n",
|
|
"<strong>MANDATORY EXERCISE</strong> · Difficulty 2/10 · Official walkthrough runnable or written exercise.\n",
|
|
"</div>\n"
|
|
],
|
|
"id": "2814ef64"
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def 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",
|
|
"\n",
|
|
"recovered = QuantumCircuit(3, 3)\n",
|
|
"recovered.x(0)\n",
|
|
"recovered.compose(qft3(), inplace=True)\n",
|
|
"recovered.compose(qft3().inverse(), inplace=True)\n",
|
|
"recovered.measure([0, 1, 2], [0, 1, 2])\n",
|
|
"simulate_counts(recovered, shots=256)\n"
|
|
],
|
|
"id": "bd25b629"
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"<!-- QL_BADGE -->\n",
|
|
"<div style=\"padding:0.55rem 0.8rem; border-left:6px solid #15803d; background:#dcfce7; color:#14532d; border-radius:0.35rem; font-family:Helvetica, Arial, sans-serif; margin:0.15rem 0 0.85rem 0;\">\n",
|
|
"<strong>MANDATORY READING</strong> · Difficulty 2/10 · Official walkthrough reading cell.\n",
|
|
"</div>\n",
|
|
"\n",
|
|
"## Lab 3: Approximation Tradeoff\n",
|
|
"\n",
|
|
"\n",
|
|
" Now remove the smallest-angle interaction and inspect the result like an engineer. The aim is not to decide that approximation is always good or always bad. The aim is to learn how to talk about the tradeoff in concrete, local terms.\n"
|
|
],
|
|
"id": "ac02238c"
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"ql_injected": "badge",
|
|
"ql_track": "mandatory",
|
|
"ql_role": "exercise"
|
|
},
|
|
"source": [
|
|
"<!-- QL_BADGE -->\n",
|
|
"<div style=\"padding:0.55rem 0.8rem; border-left:6px solid #15803d; background:#dcfce7; color:#14532d; border-radius:0.35rem; font-family:Helvetica, Arial, sans-serif; margin:0.15rem 0 0.85rem 0;\">\n",
|
|
"<strong>MANDATORY EXERCISE</strong> · Difficulty 2/10 · Official walkthrough runnable or written exercise.\n",
|
|
"</div>\n"
|
|
],
|
|
"id": "0293d25f"
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"jupyter": {
|
|
"source_hidden": true
|
|
},
|
|
"tags": [
|
|
"hide-input"
|
|
]
|
|
},
|
|
"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='Lab 3: Approximate QFT',\n",
|
|
" instructions='Toggle the smallest-angle term and explain what structural simplification you gained and what representational burden you may have lost.',\n",
|
|
" shots=256,\n",
|
|
")\n"
|
|
],
|
|
"id": "d5dd5270"
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"ql_injected": "badge",
|
|
"ql_track": "mandatory",
|
|
"ql_role": "exercise"
|
|
},
|
|
"source": [
|
|
"<!-- QL_BADGE -->\n",
|
|
"<div style=\"padding:0.55rem 0.8rem; border-left:6px solid #15803d; background:#dcfce7; color:#14532d; border-radius:0.35rem; font-family:Helvetica, Arial, sans-serif; margin:0.15rem 0 0.85rem 0;\">\n",
|
|
"<strong>MANDATORY EXERCISE</strong> · Difficulty 2/10 · Official walkthrough runnable or written exercise.\n",
|
|
"</div>\n"
|
|
],
|
|
"id": "b084f968"
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def qft3(drop_smallest_angle: bool) -> 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",
|
|
"\n",
|
|
"basis_one = QuantumCircuit(3)\n",
|
|
"basis_one.x(0)\n",
|
|
"full_probs = statevector_probabilities(basis_one.compose(qft3(False)))\n",
|
|
"approx_probs = statevector_probabilities(basis_one.compose(qft3(True)))\n",
|
|
"{\"full\": full_probs, \"approx\": approx_probs}\n"
|
|
],
|
|
"id": "bf49e653"
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"ql_injected": "badge",
|
|
"ql_track": "mandatory",
|
|
"ql_role": "test"
|
|
},
|
|
"source": [
|
|
"<!-- QL_BADGE -->\n",
|
|
"<div style=\"padding:0.55rem 0.8rem; border-left:6px solid #15803d; background:#dcfce7; color:#14532d; border-radius:0.35rem; font-family:Helvetica, Arial, sans-serif; margin:0.15rem 0 0.85rem 0;\">\n",
|
|
"<strong>MANDATORY TEST</strong> · Difficulty 2/10 · Official walkthrough multiple-choice test.\n",
|
|
"</div>\n"
|
|
],
|
|
"id": "9b519db1"
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"jupyter": {
|
|
"source_hidden": true
|
|
},
|
|
"tags": [
|
|
"hide-input"
|
|
]
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"quiz_block([{'prompt': 'Why is QFT plus inverse-QFT a useful lab pattern?', 'options': ['It checks whether your custom transform preserves information when composed with its inverse', 'It makes all swaps unnecessary', 'It amplifies noise automatically'], 'correct_index': 0, 'explanation': 'Recovery checks are a disciplined way to verify basis-change code.'}, {'prompt': 'What does an approximate-QFT comparison teach best?', 'options': ['How structural simplification can be defended as a tradeoff instead of guessed at', 'How to avoid ever drawing the circuit', 'How to replace controlled phases with measurements'], 'correct_index': 0, 'explanation': 'Approximation should be justified, not mystified.'}], heading='Lab Checkpoint B')\n"
|
|
],
|
|
"id": "e565cf94"
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"<!-- QL_BADGE -->\n",
|
|
"<div style=\"padding:0.55rem 0.8rem; border-left:6px solid #15803d; background:#dcfce7; color:#14532d; border-radius:0.35rem; font-family:Helvetica, Arial, sans-serif; margin:0.15rem 0 0.85rem 0;\">\n",
|
|
"<strong>MANDATORY READING</strong> · Difficulty 2/10 · Official walkthrough reading cell.\n",
|
|
"</div>\n",
|
|
"\n",
|
|
"## Lab Debrief\n",
|
|
"\n",
|
|
"\n",
|
|
" The important outcome of the lab is not that you can rewrite one small QFT from memory. It is that you now have habits for reading, checking, and selectively simplifying basis-change circuits. Those habits are exactly what later algorithmic and professional modules will need.\n"
|
|
],
|
|
"id": "345ed230"
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"<!-- QL_BADGE -->\n",
|
|
"<div style=\"padding:0.55rem 0.8rem; border-left:6px solid #15803d; background:#dcfce7; color:#14532d; border-radius:0.35rem; font-family:Helvetica, Arial, sans-serif; margin:0.15rem 0 0.85rem 0;\">\n",
|
|
"<strong>MANDATORY READING</strong> · Difficulty 2/10 · Official walkthrough reading cell.\n",
|
|
"</div>\n",
|
|
"\n",
|
|
"## Why The Lab Is Slower Than A Tutorial\n",
|
|
"\n",
|
|
"These exercises are intentionally slower than ordinary click-through tutorials because the purpose is different. A tutorial can reward motion. A professional lab has to reward discrimination. You are being asked to notice which edit changed the semantic burden of the circuit, which edit only changed presentation, and which edit damaged the reporting contract even though the diagram still looked familiar. That is harder work, but it is the right work for someone trying to become a designer rather than a consumer of notebooks.\n"
|
|
],
|
|
"id": "39744ce8"
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"<!-- QL_BADGE -->\n",
|
|
"<div style=\"padding:0.55rem 0.8rem; border-left:6px solid #15803d; background:#dcfce7; color:#14532d; border-radius:0.35rem; font-family:Helvetica, Arial, sans-serif; margin:0.15rem 0 0.85rem 0;\">\n",
|
|
"<strong>MANDATORY READING</strong> · Difficulty 2/10 · Official walkthrough reading cell.\n",
|
|
"</div>\n",
|
|
"\n",
|
|
"## Prediction Ledger\n",
|
|
"\n",
|
|
"If the lab begins to feel messy, return to the prediction ledger idea. Before each edit, write down what should remain invariant, what should move, and which evidence will decide the question. That tiny discipline is what keeps experiments from collapsing into aimless button pushing. It also mirrors how real engineering work scales. Good engineers do not only make changes. They keep track of what they expected the change to prove.\n"
|
|
],
|
|
"id": "6f2bab6b"
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"ql_injected": "badge",
|
|
"ql_track": "mandatory",
|
|
"ql_role": "exercise"
|
|
},
|
|
"source": [
|
|
"<!-- QL_BADGE -->\n",
|
|
"<div style=\"padding:0.55rem 0.8rem; border-left:6px solid #15803d; background:#dcfce7; color:#14532d; border-radius:0.35rem; font-family:Helvetica, Arial, sans-serif; margin:0.15rem 0 0.85rem 0;\">\n",
|
|
"<strong>MANDATORY EXERCISE</strong> · Difficulty 2/10 · Official walkthrough runnable or written exercise.\n",
|
|
"</div>\n"
|
|
],
|
|
"id": "db56ed9f"
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"jupyter": {
|
|
"source_hidden": true
|
|
},
|
|
"tags": [
|
|
"hide-input"
|
|
]
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"reflection_box('Write a short engineering note defending either the full or approximate 3-qubit QFT for a small local study.')\n"
|
|
],
|
|
"id": "3c74dd53"
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"ql_injected": "badge",
|
|
"ql_track": "mandatory",
|
|
"ql_role": "exercise"
|
|
},
|
|
"source": [
|
|
"<!-- QL_BADGE -->\n",
|
|
"<div style=\"padding:0.55rem 0.8rem; border-left:6px solid #15803d; background:#dcfce7; color:#14532d; border-radius:0.35rem; font-family:Helvetica, Arial, sans-serif; margin:0.15rem 0 0.85rem 0;\">\n",
|
|
"<strong>MANDATORY EXERCISE</strong> · Difficulty 2/10 · Official walkthrough runnable or written exercise.\n",
|
|
"</div>\n"
|
|
],
|
|
"id": "1a46c83a"
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"jupyter": {
|
|
"source_hidden": true
|
|
},
|
|
"tags": [
|
|
"hide-input"
|
|
]
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"reflection_box('Write one additional prediction habit you want to carry into later modules.')\n"
|
|
],
|
|
"id": "f3b6ec95"
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"<!-- QL_BADGE -->\n",
|
|
"<div style=\"padding:0.55rem 0.8rem; border-left:6px solid #2563eb; background:#dbeafe; color:#1e3a8a; border-radius:0.35rem; font-family:Helvetica, Arial, sans-serif; margin:0.15rem 0 0.85rem 0;\">\n",
|
|
"<strong>META READING</strong> · Difficulty 1/10 · Official walkthrough guardrail.\n",
|
|
"</div>\n",
|
|
"\n",
|
|
"<!-- COURSE_NAV_BOTTOM -->\n",
|
|
"## What To Open Next\n",
|
|
"\n",
|
|
"Next notebook: [QFT and Periodic Structure Problems](problems.ipynb)\n",
|
|
"\n",
|
|
"Official walkthrough rule: once every mandatory cell above is complete, open the next notebook. Anything below this cell is facultative.\n"
|
|
],
|
|
"id": "8d4c4b80"
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"ql_injected": "facultative_zone",
|
|
"ql_track": "meta",
|
|
"ql_role": "reading",
|
|
"ql_difficulty": 1,
|
|
"ql_note": "Optional-zone boundary. The official walkthrough is already complete above."
|
|
},
|
|
"source": [
|
|
"<!-- QL_BADGE -->\n",
|
|
"<div style=\"padding:0.55rem 0.8rem; border-left:6px solid #2563eb; background:#dbeafe; color:#1e3a8a; border-radius:0.35rem; font-family:Helvetica, Arial, sans-serif; margin:0.15rem 0 0.85rem 0;\">\n",
|
|
"<strong>META READING</strong> · Difficulty 1/10 · Optional-zone boundary. The official walkthrough is already complete above.\n",
|
|
"</div>\n",
|
|
"\n",
|
|
"<!-- QL_OPTIONAL_ZONE -->\n",
|
|
"## Facultative Extension Zone\n",
|
|
"\n",
|
|
"You have already completed the mandatory walkthrough for **QFT and Periodic Structure Lab**. Everything below is optional. Use it only if you want deeper consolidation or extra transfer work.\n"
|
|
],
|
|
"id": "c8060e79"
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"ql_injected": "facultative",
|
|
"ql_track": "facultative",
|
|
"ql_role": "reading",
|
|
"ql_difficulty": 4,
|
|
"ql_note": "Optional extension reading."
|
|
},
|
|
"source": [
|
|
"<!-- QL_BADGE -->\n",
|
|
"<div style=\"padding:0.55rem 0.8rem; border-left:6px solid #ea580c; background:#ffedd5; color:#9a3412; border-radius:0.35rem; font-family:Helvetica, Arial, sans-serif; margin:0.15rem 0 0.85rem 0;\">\n",
|
|
"<strong>FACULTATIVE READING</strong> · Difficulty 4/10 · Optional extension reading.\n",
|
|
"</div>\n",
|
|
"\n",
|
|
"## Facultative Extension Reading\n",
|
|
"\n",
|
|
"In **QFT and Periodic Structure Lab**, the mandatory labs already gave you the official practice loop. This optional cell is for deeper experimentation discipline: decide one variable you would perturb next, one quantity you would track, and one false conclusion you want to avoid.\n"
|
|
],
|
|
"id": "e3be197f"
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"ql_injected": "badge",
|
|
"ql_track": "facultative",
|
|
"ql_role": "test"
|
|
},
|
|
"source": [
|
|
"<!-- QL_BADGE -->\n",
|
|
"<div style=\"padding:0.55rem 0.8rem; border-left:6px solid #ea580c; background:#ffedd5; color:#9a3412; border-radius:0.35rem; font-family:Helvetica, Arial, sans-serif; margin:0.15rem 0 0.85rem 0;\">\n",
|
|
"<strong>FACULTATIVE TEST</strong> · Difficulty 5/10 · Optional multiple-choice extension.\n",
|
|
"</div>\n"
|
|
],
|
|
"id": "83c53a07"
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"ql_injected": "facultative",
|
|
"ql_track": "facultative",
|
|
"ql_role": "test",
|
|
"ql_difficulty": 5,
|
|
"ql_note": "Optional multiple-choice extension.",
|
|
"jupyter": {
|
|
"source_hidden": true
|
|
},
|
|
"tags": [
|
|
"hide-input"
|
|
]
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"quiz_block([{'prompt': 'In an optional lab variant, what should you change first?', 'options': ['One design variable you can explain', 'As many gates as possible to make the result surprising', 'The notebook order itself'], 'correct_index': 0, 'explanation': 'Optional exploration is still strongest when the perturbation is controlled.'}, {'prompt': 'What makes an optional lab note useful?', 'options': ['It records what changed, what stayed fixed, and what evidence moved', 'It only reports that the circuit still ran', 'It avoids writing predictions to save time'], 'correct_index': 0, 'explanation': 'The extension is about deeper evidence discipline.'}], heading='Facultative Extension Test')\n"
|
|
],
|
|
"id": "9a166d01"
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"ql_injected": "badge",
|
|
"ql_track": "facultative",
|
|
"ql_role": "exercise"
|
|
},
|
|
"source": [
|
|
"<!-- QL_BADGE -->\n",
|
|
"<div style=\"padding:0.55rem 0.8rem; border-left:6px solid #ea580c; background:#ffedd5; color:#9a3412; border-radius:0.35rem; font-family:Helvetica, Arial, sans-serif; margin:0.15rem 0 0.85rem 0;\">\n",
|
|
"<strong>FACULTATIVE EXERCISE</strong> · Difficulty 6/10 · Optional written exercise.\n",
|
|
"</div>\n"
|
|
],
|
|
"id": "34c04780"
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"ql_injected": "facultative",
|
|
"ql_track": "facultative",
|
|
"ql_role": "exercise",
|
|
"ql_difficulty": 6,
|
|
"ql_note": "Optional written exercise.",
|
|
"jupyter": {
|
|
"source_hidden": true
|
|
},
|
|
"tags": [
|
|
"hide-input"
|
|
]
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"reflection_box('Describe one optional circuit variation you would run after QFT and Periodic Structure Lab, what you would predict before running it, and what evidence would make you abandon the prediction.')\n"
|
|
],
|
|
"id": "8c8df6f0"
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "QuantumLearning (.venv)",
|
|
"language": "python",
|
|
"name": "quantum-learning"
|
|
},
|
|
"language_info": {
|
|
"name": "python",
|
|
"version": "3.12"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|