QuantumLearning/notebooks/professional/module_02_hardware_aware_redesign/studio.ipynb

367 lines
15 KiB
Text

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Hardware-Aware Redesign Studio Studio\n"
],
"id": "4cbe48d5"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<!-- COURSE_NAV_TOP -->\n",
"## Mainline Navigation\n",
"\n",
"Step 50 of 59. Follow the mainline in order and do not skip ahead.\n",
"\n",
"Previous notebook: [Hardware-Aware Redesign Studio Problems](problems.ipynb)\n",
"\n",
"Next notebook: [Noise-Aware Verification and Mitigation Lecture](../module_03_noise_aware_verification/lecture.ipynb)\n",
"\n",
"Rule: finish this notebook top-to-bottom before you open the next one.\n"
],
"id": "9d7fdf6f"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The studio makes the redesign standard explicit: build, compare, justify.\n"
],
"id": "c52bee5a"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Design Brief\n",
"\n",
"\n",
" Create a small local redesign memo that begins with an abstract target, benchmarks at least two constrained candidates, and ends with a clear recommendation tied to the declared topology and basis assumptions.\n"
],
"id": "fb9368c2"
},
{
"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": [
"## Studio Prompt 1: Stress A Clean Ideal Circuit\n",
"\n",
"\n",
" Start from a clean abstract target and let the local constraint model expose where it becomes awkward.\n"
],
"id": "daed8193"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"LOCAL_BASIS = [\"rz\", \"sx\", \"x\", \"cx\"]\n",
"\n",
"def simulate_line_counts(circuit, shots=256):\n",
" return simulate_counts(\n",
" circuit,\n",
" shots=shots,\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 naive_star_ghz() -> QuantumCircuit:\\n circuit = QuantumCircuit(4, 4)\\n # [1] Express the ideal entangling target directly.\\n circuit.h(0)\\n circuit.cx(0, 1)\\n circuit.cx(0, 2)\\n circuit.cx(0, 3)\\n # [2] Keep the evidence path stable while topology pressure is studied elsewhere.\\n circuit.measure([0, 1, 2, 3], [0, 1, 2, 3])\\n return circuit\\n\\ncircuit = naive_star_ghz()\\n'\n",
"editable_circuit_lab(\n",
" initial_code=editable_code,\n",
" context={\"QuantumCircuit\": QuantumCircuit, \"simulate_counts\": simulate_line_counts},\n",
" title='Studio 1: Ideal Target Under Pressure',\n",
" instructions='Use the naive target as a reference point, not as the automatic winner.',\n",
" shots=256,\n",
")\n"
],
"id": "d4514e0a"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Studio Prompt 2: Propose A Human Alternative\n",
"\n",
"\n",
" Produce an alternative whose structure anticipates the line rather than waiting for the transpiler to rescue it.\n"
],
"id": "b2c3e8e8"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"LOCAL_BASIS = [\"rz\", \"sx\", \"x\", \"cx\"]\n",
"\n",
"def simulate_line_counts(circuit, shots=256):\n",
" return simulate_counts(\n",
" circuit,\n",
" shots=shots,\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 line_friendly_ghz() -> QuantumCircuit:\\n circuit = QuantumCircuit(4, 4)\\n circuit.h(1)\\n circuit.cx(1, 0)\\n circuit.cx(1, 2)\\n circuit.cx(2, 3)\\n circuit.measure([0, 1, 2, 3], [0, 1, 2, 3])\\n return circuit\\n\\ncircuit = line_friendly_ghz()\\n'\n",
"editable_circuit_lab(\n",
" initial_code=editable_code,\n",
" context={\"QuantumCircuit\": QuantumCircuit, \"simulate_counts\": simulate_line_counts},\n",
" title='Studio 2: Human-Aware Alternative',\n",
" instructions='Refine a line-aware candidate and prepare to defend it with metrics and structural reasoning.',\n",
" shots=256,\n",
")\n"
],
"id": "5f9f3c44"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Studio Prompt 3: Write The Recommendation\n",
"\n",
"\n",
" Compare the candidate family and make a choice like an engineer rather than a spectator.\n"
],
"id": "d5ec3a7b"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"LOCAL_BASIS = [\"rz\", \"sx\", \"x\", \"cx\"]\n",
"\n",
"def simulate_line_counts(circuit, shots=256):\n",
" return simulate_counts(\n",
" circuit,\n",
" shots=shots,\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 candidate(style: str = \"middle_root\") -> QuantumCircuit:\\n circuit = QuantumCircuit(4, 4)\\n if style == \"naive\":\\n circuit.h(0)\\n circuit.cx(0, 1)\\n circuit.cx(0, 2)\\n circuit.cx(0, 3)\\n elif style == \"chain\":\\n circuit.h(0)\\n circuit.cx(0, 1)\\n circuit.cx(1, 2)\\n circuit.cx(2, 3)\\n elif style == \"middle_root\":\\n circuit.h(1)\\n circuit.cx(1, 0)\\n circuit.cx(1, 2)\\n circuit.cx(2, 3)\\n else:\\n raise ValueError(\"style must be naive, chain, or middle_root\")\\n circuit.measure([0, 1, 2, 3], [0, 1, 2, 3])\\n return circuit\\n\\ncircuit = candidate(style=\"middle_root\")\\n'\n",
"editable_circuit_lab(\n",
" initial_code=editable_code,\n",
" context={\"QuantumCircuit\": QuantumCircuit, \"simulate_counts\": simulate_line_counts},\n",
" title='Studio 3: Recommendation Candidate Family',\n",
" instructions='Use candidate comparison to support a recommendation that explicitly names the topology pressure and the chosen response.',\n",
" shots=256,\n",
")\n"
],
"id": "8d158488"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"quiz_block([{'prompt': 'What is the right studio target for this module?', 'options': ['A constraint-aware redesign memo comparing multiple candidate circuits under the same local topology assumptions', 'A single ideal circuit with no compile study', 'A notebook that treats the coupling map as background trivia'], 'correct_index': 0, 'explanation': 'The studio should feel like local hardware-aware engineering.'}, {'prompt': 'Why is human critique still necessary after compilation?', 'options': ['Because the compiler shows what happened, but the designer still decides whether the original layout was wise', 'Because compiled circuits are never useful', 'Because metrics disappear after transpilation'], 'correct_index': 0, 'explanation': 'Compilation evidence is necessary, not sufficient, for redesign judgement.'}], heading='Studio Design Check')\n"
],
"id": "8ad8d846"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"evidence_checklist(['The notebook states the objective or decision clearly.', 'The relevant constraints are explicit and stable.', 'Evidence is tied to a concrete circuit, output, or metric.', 'At least one uncertainty or risk is named explicitly.', 'The notebook ends with a next action or defended judgement.'], title='Hardware-Aware Redesign Studio Evidence Checklist')\n"
],
"id": "d0d20918"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Studio Debrief\n",
"\n",
"\n",
" A successful studio notebook here reads like a redesign memo, not like a complaint about compilation. It should show what the abstract circuit was trying to do, how the local constraints punished it, and why the recommended alternative better fits the declared hardware story.\n"
],
"id": "91e21431"
},
{
"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 the objective clear, the candidate or case structure visible, the evidence traceable, and the final judgement conditional in the right way. If those things are not yet present, the notebook is not finished no matter how many cells it contains.\n"
],
"id": "2db1e1f8"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## What A Finished Studio Should Feel Like\n",
"\n",
"The finished notebook should feel like something another engineer could open and use. They should be able to understand what the notebook is trying to decide, how the circuits were compared, what evidence was gathered, and why the recommendation or diagnosis ended where it did. That is the practical definition of \"world-class\" in this project: not theatrical polish, but concentrated clarity under real engineering burdens.\n"
],
"id": "5246c40b"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Final Check Before You Stop\n",
"\n",
"Before you leave a studio notebook, ask one last question: if another engineer disagreed with my conclusion, would the notebook give them enough material to locate the disagreement precisely? If the answer is yes, the studio is doing its job.\n"
],
"id": "6add3b66"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"reflection_box('Which structural feature turned out to matter most in your final redesign recommendation?')\n"
],
"id": "abe2ee0f"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"reflection_box('What evidence from the constrained comparison most influenced your choice?')\n"
],
"id": "524e8c6d"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"reflection_box('How did you keep the reporting contract stable across the candidates?')\n"
],
"id": "36d07a53"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"reflection_box('Name one redesign habit from this module that should carry into the capstone.')\n"
],
"id": "a2bebaf1"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"feedback_iteration_panel(title='Hardware-Aware Redesign Studio Studio Revision Loop', prompt='Write the decision or diagnosis the studio currently supports, the evidence carrying that weight, the main remaining uncertainty, and the next revision that would sharpen the notebook.')\n"
],
"id": "e2ef1bee"
},
{
"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='Hardware-Aware Redesign Studio Studio Self-Grading',\n",
")\n"
],
"id": "4c57b32f"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<!-- COURSE_NAV_BOTTOM -->\n",
"## What To Open Next\n",
"\n",
"Next notebook: [Noise-Aware Verification and Mitigation Lecture](../module_03_noise_aware_verification/lecture.ipynb)\n",
"\n",
"When you finish this notebook, open the next notebook shown above. Stay on the guarded mainline route.\n"
],
"id": "e92895d4"
}
],
"metadata": {
"kernelspec": {
"display_name": "QuantumLearning (.venv)",
"language": "python",
"name": "quantum-learning"
},
"language_info": {
"name": "python",
"version": "3.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}