QuantumLearning/notebooks/professional/module_04_capstone_design_review/lab.ipynb

375 lines
17 KiB
Text

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Capstone Circuit Design Review Lab\n"
],
"id": "94cd712a"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<!-- COURSE_NAV_TOP -->\n",
"## Mainline Navigation\n",
"\n",
"Step 56 of 59. Follow the mainline in order and do not skip ahead.\n",
"\n",
"Previous notebook: [Capstone Circuit Design Review Lecture](lecture.ipynb)\n",
"\n",
"Next notebook: [Capstone Circuit Design Review Problems](problems.ipynb)\n",
"\n",
"Rule: finish this notebook top-to-bottom before you open the next one.\n"
],
"id": "97c217db"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The lab makes the capstone mechanics concrete: define a fixed brief, compare a candidate family, and practice recommendation writing under stable constraints.\n"
],
"id": "41f45308"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Lab Protocol\n",
"\n",
"\n",
" Do not move the brief while comparing candidates. Keep the topology, basis model, and noisy lens fixed. Let the candidates change, not the rules of the game.\n"
],
"id": "bd274bbf"
},
{
"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": "50c7b992"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from quantum_learning import (\n",
" build_demo_noise_model,\n",
" counts_to_probabilities,\n",
" draw_circuit,\n",
" editable_circuit_lab,\n",
" evidence_checklist,\n",
" feedback_iteration_panel,\n",
" line_coupling_map,\n",
" load_assessment_blueprint,\n",
" plot_counts,\n",
" plot_probabilities,\n",
" quiz_block,\n",
" reflection_box,\n",
" rubric_scorecard,\n",
" simulate_counts,\n",
" statevector_probabilities,\n",
" step_reference_table,\n",
" transpile_summary,\n",
")\n",
"from qiskit import QuantumCircuit\n",
"from qiskit.providers.basic_provider import BasicSimulator\n"
],
"id": "933f1ed0"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Lab 1: Candidate Family\n",
"\n",
"\n",
" Start by inspecting the candidate family under the same local constraint model. The challenge is not to pick instantly. The challenge is to keep all plausible candidates alive long enough to compare them honestly.\n"
],
"id": "a0b43a13"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"step_reference_table([{'marker': '[1]', 'code_focus': 'State the design brief and the constraints before generating any candidate.', 'diagram_effect': 'Every candidate diagram is read as an answer to a shared constrained question.', 'why_it_matters': 'Capstone work starts from explicit objective, not from attachment to a favorite circuit shape.'}, {'marker': '[2]', 'code_focus': 'Generate more than one plausible candidate family.', 'diagram_effect': 'The notebook contains alternatives instead of a single self-congratulatory path.', 'why_it_matters': 'Professional design requires comparison, not only construction.'}, {'marker': '[3]', 'code_focus': 'Benchmark ideal, compiled, and noisy behavior with the same reporting contract.', 'diagram_effect': 'Each candidate can be judged across several evidence layers.', 'why_it_matters': 'Recommendations are credible only when they survive more than one lens.'}, {'marker': '[4]', 'code_focus': 'Write a recommendation that names tradeoffs, risks, and why one candidate wins.', 'diagram_effect': 'The notebook ends as a design review, not a gallery.', 'why_it_matters': 'The capstone is about defended judgment under constraints.'}])\n"
],
"id": "50324750"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"LOCAL_BASIS = [\"rz\", \"sx\", \"x\", \"cx\"]\n",
"capstone_noise = build_demo_noise_model(\n",
" single_qubit_error=0.01,\n",
" two_qubit_error=0.04,\n",
" readout_error=0.02,\n",
")\n",
"\n",
"def simulate_capstone_counts(circuit, shots=256):\n",
" return simulate_counts(\n",
" circuit,\n",
" shots=shots,\n",
" noise_model=capstone_noise,\n",
" basis_gates=LOCAL_BASIS,\n",
" coupling_map=line_coupling_map(circuit.num_qubits),\n",
" optimization_level=1,\n",
" )\n",
"\n",
"editable_code = '\\nfrom qiskit import QuantumCircuit\\n\\ndef ghz_candidate(style: str = \"middle_root\") -> QuantumCircuit:\\n circuit = QuantumCircuit(3, 3)\\n if style == \"naive\":\\n circuit.h(0)\\n circuit.cx(0, 1)\\n circuit.cx(0, 2)\\n elif style == \"chain\":\\n circuit.h(0)\\n circuit.cx(0, 1)\\n circuit.cx(1, 2)\\n elif style == \"middle_root\":\\n circuit.h(1)\\n circuit.cx(1, 0)\\n circuit.cx(1, 2)\\n else:\\n raise ValueError(\"style must be naive, chain, or middle_root\")\\n circuit.measure([0, 1, 2], [0, 1, 2])\\n return circuit\\n\\ncircuit = ghz_candidate(style=\"middle_root\")\\n'\n",
"editable_circuit_lab(\n",
" initial_code=editable_code,\n",
" context={\"QuantumCircuit\": QuantumCircuit, \"simulate_counts\": simulate_capstone_counts},\n",
" title='Lab 1: Candidate Family',\n",
" instructions='Switch between naive, chain, and middle_root candidates and explain what brief they are all trying to satisfy.',\n",
" shots=256,\n",
")\n"
],
"id": "aa923742"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"quiz_block([{'prompt': 'What is the right mindset when comparing candidate families?', 'options': ['Keep the brief fixed and ask which candidate best serves it under the same constraints', 'Change the objective whenever a favorite candidate looks weak', 'Ignore transpilation because the capstone is conceptual'], 'correct_index': 0, 'explanation': 'A fixed brief is what makes candidate comparison honest.'}, {'prompt': 'Why compare naive, chain, and middle-root GHZ candidates locally?', 'options': ['To see which structure survives line-topology and noise pressure most credibly', 'Because only one of them creates entanglement', 'Because local simulation can replace final judgement'], 'correct_index': 0, 'explanation': 'The local study gives a concrete engineering comparison under declared constraints.'}], heading='Lab Checkpoint A')\n"
],
"id": "e8ec1563"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"reflection_box('Which candidate stayed plausible the longest in your comparison, and why?')\n"
],
"id": "9f003f26"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Lab 2: Benchmark Under Constraints\n",
"\n",
"\n",
" Now compare how the candidates behave once compiled and noisy pressure are taken seriously. This is the point where intuition must start sharing authority with evidence.\n"
],
"id": "54fbf9ae"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"LOCAL_BASIS = [\"rz\", \"sx\", \"x\", \"cx\"]\n",
"capstone_noise = build_demo_noise_model(\n",
" single_qubit_error=0.01,\n",
" two_qubit_error=0.04,\n",
" readout_error=0.02,\n",
")\n",
"\n",
"def simulate_capstone_counts(circuit, shots=256):\n",
" return simulate_counts(\n",
" circuit,\n",
" shots=shots,\n",
" noise_model=capstone_noise,\n",
" basis_gates=LOCAL_BASIS,\n",
" coupling_map=line_coupling_map(circuit.num_qubits),\n",
" optimization_level=1,\n",
" )\n",
"\n",
"editable_code = '\\nfrom qiskit import QuantumCircuit\\n\\ndef ghz_candidate(style: str = \"chain\") -> QuantumCircuit:\\n circuit = QuantumCircuit(3, 3)\\n if style == \"naive\":\\n circuit.h(0)\\n circuit.cx(0, 1)\\n circuit.cx(0, 2)\\n elif style == \"chain\":\\n circuit.h(0)\\n circuit.cx(0, 1)\\n circuit.cx(1, 2)\\n elif style == \"middle_root\":\\n circuit.h(1)\\n circuit.cx(1, 0)\\n circuit.cx(1, 2)\\n else:\\n raise ValueError(\"style must be naive, chain, or middle_root\")\\n circuit.measure([0, 1, 2], [0, 1, 2])\\n return circuit\\n\\ncircuit = ghz_candidate(style=\"chain\")\\n'\n",
"editable_circuit_lab(\n",
" initial_code=editable_code,\n",
" context={\"QuantumCircuit\": QuantumCircuit, \"simulate_counts\": simulate_capstone_counts},\n",
" title='Lab 2: Benchmark Under Constraints',\n",
" instructions='Keep the brief fixed while you compare candidates under the line-topology noisy preview. Do not anoint a winner until you can name the evidence.',\n",
" shots=256,\n",
")\n"
],
"id": "54f00929"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Lab 3: Recommendation Stress Test\n",
"\n",
"\n",
" Finally, edit a review-oriented candidate and decide what would count as a responsible recommendation. The point is not to eliminate uncertainty. The point is to write with the right kind of conditional confidence.\n"
],
"id": "e2aa6799"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"LOCAL_BASIS = [\"rz\", \"sx\", \"x\", \"cx\"]\n",
"capstone_noise = build_demo_noise_model(\n",
" single_qubit_error=0.01,\n",
" two_qubit_error=0.04,\n",
" readout_error=0.02,\n",
")\n",
"\n",
"def simulate_capstone_counts(circuit, shots=256):\n",
" return simulate_counts(\n",
" circuit,\n",
" shots=shots,\n",
" noise_model=capstone_noise,\n",
" basis_gates=LOCAL_BASIS,\n",
" coupling_map=line_coupling_map(circuit.num_qubits),\n",
" optimization_level=1,\n",
" )\n",
"\n",
"editable_code = '\\nfrom qiskit import QuantumCircuit\\n\\ndef ghz_candidate(style: str = \"middle_root\", add_extra_layer: bool = False) -> QuantumCircuit:\\n circuit = QuantumCircuit(3, 3)\\n if style == \"chain\":\\n circuit.h(0)\\n circuit.cx(0, 1)\\n circuit.cx(1, 2)\\n else:\\n circuit.h(1)\\n circuit.cx(1, 0)\\n circuit.cx(1, 2)\\n if add_extra_layer:\\n circuit.cz(0, 2)\\n circuit.measure([0, 1, 2], [0, 1, 2])\\n return circuit\\n\\ncircuit = ghz_candidate(style=\"middle_root\", add_extra_layer=False)\\n'\n",
"editable_circuit_lab(\n",
" initial_code=editable_code,\n",
" context={\"QuantumCircuit\": QuantumCircuit, \"simulate_counts\": simulate_capstone_counts},\n",
" title='Lab 3: Recommendation Stress Test',\n",
" instructions='Use the extra-layer toggle and candidate choice to test how quickly a recommendation can become fragile when the brief is ignored.',\n",
" shots=256,\n",
")\n"
],
"id": "39be2b65"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"quiz_block([{'prompt': 'What would make a capstone comparison weak?', 'options': ['Selecting a winner before compiling or benchmarking the alternatives', 'Keeping the objective and constraints fixed', 'Comparing noisy-success support across candidates'], 'correct_index': 0, 'explanation': 'Premature attachment is the enemy of serious review.'}, {'prompt': 'What is a useful residual-risk sentence?', 'options': ['The middle-root design wins under the current line model, but the ranking could shift if the error profile changes materially', 'There are no remaining risks because one candidate won', 'Risk does not belong in a design recommendation'], 'correct_index': 0, 'explanation': 'Capstone recommendations should acknowledge scope and uncertainty.'}], heading='Lab Checkpoint B')\n"
],
"id": "76e4a192"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Lab Debrief\n",
"\n",
"\n",
" After this lab, a design recommendation should feel less like a declaration and more like a compact argument. You now have practice keeping a brief fixed, comparing multiple candidates, and naming both the evidence and the scope limits of the conclusion.\n"
],
"id": "f46ba588"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Why The Lab Is Slower Than A Demo\n",
"\n",
"These labs are built to slow down the moment where many learners usually rush. In professional work, the difference between a good decision and a weak one often depends on whether you changed one variable at a time, whether you kept the objective fixed, and whether you wrote down what result you expected before running the next cell. That is why the labs here are not just demonstrations. They are rehearsals for disciplined engineering comparison.\n"
],
"id": "c5e6fa79"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Prediction Ledger\n",
"\n",
"If the comparison starts to blur, return to a prediction ledger. Write down what should stay invariant, what metric or observation should move, and what conclusion would follow if it does. That simple habit will make your later capstone work far stronger because it converts trial-and-error into interpretable evidence.\n"
],
"id": "a68e347a"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"reflection_box('Write a short recommendation sentence that names both a winner and one residual risk.')\n"
],
"id": "9b15a013"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"reflection_box('Write one prediction habit from this lab that you want to preserve in later professional work.')\n"
],
"id": "cb17c0f5"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"feedback_iteration_panel(title='Capstone Circuit Design Review Lab Feedback Loop', prompt='Turn the lab into a review note: state the current claim, cite the strongest evidence, name the main remaining risk, and write the next comparison you would run.')\n"
],
"id": "614022b7"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"assessment_blueprint = load_assessment_blueprint()\n",
"rubric_scorecard(\n",
" assessment_blueprint.get_rubric('module_self_review'),\n",
" title='Capstone Circuit Design Review Lab Self-Grading',\n",
")\n"
],
"id": "4932c100"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<!-- COURSE_NAV_BOTTOM -->\n",
"## What To Open Next\n",
"\n",
"Next notebook: [Capstone Circuit Design Review Problems](problems.ipynb)\n",
"\n",
"If this notebook still feels unstable, repeat it before you move on. The mainline only works if each handoff is earned.\n"
],
"id": "dd28bb04"
}
],
"metadata": {
"kernelspec": {
"display_name": "QuantumLearning (.venv)",
"language": "python",
"name": "quantum-learning"
},
"language_info": {
"name": "python",
"version": "3.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}