QuantumLearning/notebooks/foundations/module_03_gates_and_measurement/studio.ipynb

264 lines
11 KiB
Text
Raw Normal View History

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Gates and Measurement Studio\n",
"\n",
"This studio asks for protocol design. You are no longer just interpreting prepared examples. You are choosing how to prepare, how to probe, and how to report. That is exactly the kind of ownership a professional circuit designer needs.\n"
],
"id": "20c3e556"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<!-- COURSE_NAV_TOP -->\n",
"## Mainline Navigation\n",
"\n",
"Step 14 of 59. Follow the mainline in order and do not skip ahead.\n",
"\n",
"Previous notebook: [Gates and Measurement Problems](problems.ipynb)\n",
"\n",
"Next notebook: [Circuit Construction and Analysis Lecture](../../qiskit_engineering/module_01_circuit_construction_and_analysis/lecture.ipynb)\n",
"\n",
"Rule: finish this notebook top-to-bottom before you open the next one.\n"
],
"id": "6e57e055"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Studio Standard\n",
"\n",
"Every answer should separate three layers explicitly: preparation, measurement basis, and reporting. If those layers remain fused in your language, the design is not yet under control.\n"
],
"id": "d64403a9"
},
{
"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 quantum_learning import (\n",
" counts_to_probabilities,\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": "99552337"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Brief 1: One-Qubit Basis Contrast\n",
"\n",
"Build a one-qubit protocol that produces a deterministic result in one basis and a non-deterministic result in another. The point is not novelty. The point is to justify why the contrast is caused by a changed question rather than by hand-waving.\n"
],
"id": "915acbee"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"editable_code = '\\ncircuit = QuantumCircuit(1, 1)\\n# [1] Try changing the preparation gate sequence.\\ncircuit.x(0)\\ncircuit.h(0)\\n# [2] Optionally add Z or another H before measurement.\\n# circuit.z(0)\\n# circuit.h(0)\\n# [3] Measure the result.\\ncircuit.measure(0, 0)\\n'\n",
"editable_circuit_lab(\n",
" initial_code=editable_code,\n",
" context={\"QuantumCircuit\": QuantumCircuit, \"simulate_counts\": simulate_counts},\n",
" title='Studio Brief 1: Basis Contrast',\n",
" instructions='Use a small gate vocabulary to make one basis deterministic and another basis informative. Explain the protocol clearly.',\n",
" shots=256,\n",
")\n"
],
"id": "57a2f04a"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"reflection_box('Why does your protocol count as a basis contrast rather than just a different state-preparation demo?')\n"
],
"id": "1fff3ad7"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Brief 2: Two-Qubit Correlation Probe\n",
"\n",
"Start from the Bell preparation and decide which basis or readout change best tests the story you want to claim. Write your explanation as if another engineer had to review it.\n"
],
"id": "66d73649"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"editable_code = '\\ncircuit = QuantumCircuit(2, 2)\\n# [1] Prepare the correlated state.\\ncircuit.h(0)\\ncircuit.cx(0, 1)\\n# [2] Change the basis of the first measurement question.\\ncircuit.h(0)\\n# [3] Measure both qubits into the matching classical bits.\\ncircuit.measure([0, 1], [0, 1])\\n'\n",
"editable_circuit_lab(\n",
" initial_code=editable_code,\n",
" context={\"QuantumCircuit\": QuantumCircuit, \"simulate_counts\": simulate_counts},\n",
" title='Studio Brief 2: Correlation Probe',\n",
" instructions='Tune the basis choices and defend what kind of correlation evidence the circuit should produce.',\n",
" shots=256,\n",
")\n"
],
"id": "0a7fbbae"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"reflection_box('What evidence in the counts would actually support your claim, and what evidence would falsify it?')\n"
],
"id": "6212dede"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Brief 3: Reporting-Layer Stress Test\n",
"\n",
"Use swapped or altered classical wiring deliberately. The point is to produce a circuit whose quantum preparation is fine but whose reporting layer could mislead a careless reader. Then write the warning note that a reviewer should leave.\n"
],
"id": "fe9d1a60"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"editable_code = '\\ncircuit = QuantumCircuit(2, 2)\\n# [1] Prepare a Bell-style state.\\ncircuit.h(0)\\ncircuit.cx(0, 1)\\n# [2] Try swapping the classical wiring.\\ncircuit.measure([0, 1], [1, 0])\\n'\n",
"editable_circuit_lab(\n",
" initial_code=editable_code,\n",
" context={\"QuantumCircuit\": QuantumCircuit, \"simulate_counts\": simulate_counts},\n",
" title='Studio Brief 3: Reporting-Layer Stress Test',\n",
" instructions='Use classical wiring deliberately and then explain how a review note should prevent misinterpretation.',\n",
" shots=256,\n",
")\n"
],
"id": "d77befb8"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"quiz_block([{'prompt': 'What does the measurement studio demand that the lecture does not?', 'options': ['Deliberate protocol design and explicit justification of why a chosen measurement reveals what you claim', 'Less writing', 'No circuit edits'], 'correct_index': 0, 'explanation': 'The studio is where you own the protocol rather than merely understand it.'}, {'prompt': 'What is the hallmark of a good studio answer?', 'options': ['It separates preparation, basis choice, wiring, and evidence instead of blurring them together', 'It uses the most gates possible', 'It avoids comparison between alternatives'], 'correct_index': 0, 'explanation': 'Review language should make the protocol legible and falsifiable.'}], heading='Measurement Studio Check')\n"
],
"id": "8820ce1d"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"reflection_box('What is the most subtle protocol mistake you can now imagine making in your own notebook work?')\n"
],
"id": "ba0f1023"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"reflection_box('Which part of measurement reasoning now feels reliable, and which part still feels too dependent on the exact example in front of you?')\n"
],
"id": "05a16095"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Studio Rubric\n",
"\n",
"Use a strict rubric on yourself here. A good studio answer makes the preparation explicit, makes the basis choice explicit, makes the classical mapping explicit, and says what evidence would count as success or failure. It also admits uncertainty honestly. If you are unsure whether your chosen probe actually distinguishes two candidate explanations, write that uncertainty down and propose the next check. That is not weakness. That is proper review culture.\n",
"\n",
"This rubric is intentionally demanding because measurement notebooks are where many people learn bad habits: overclaiming from too little evidence, blurring protocol choices together, or treating a nice-looking plot as proof. The studio exists to train the opposite behavior. Clear protocol, explicit evidence, and disciplined interpretation. Nothing less will scale to professional work.\n"
],
"id": "ad73de5a"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Studio Exit\n",
"\n",
"A strong studio result is not the most elaborate circuit. It is the clearest protocol. If you can prepare, question, report, and defend the evidence chain cleanly, the module has done its job.\n"
],
"id": "c2411ea6"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<!-- COURSE_NAV_BOTTOM -->\n",
"## What To Open Next\n",
"\n",
"Next notebook: [Circuit Construction and Analysis Lecture](../../qiskit_engineering/module_01_circuit_construction_and_analysis/lecture.ipynb)\n",
"\n",
"When you finish this notebook, open the next notebook shown above. Stay on the guarded mainline route.\n"
],
"id": "8ca10036"
}
],
"metadata": {
"kernelspec": {
"display_name": "QuantumLearning (.venv)",
"language": "python",
"name": "quantum-learning"
},
"language_info": {
"name": "python",
"version": "3.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}