{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Principles and Circuit Literacy Lab\n", "\n", "The lecture gave you the vocabulary and the main circuit story. The lab turns that story into manipulation. This notebook is not for admiring outputs. It is for making controlled changes and checking whether your explanation survives contact with variation.\n" ], "id": "4fcb19e1" }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Lab Protocol\n", "\n", "For every editable cell in this notebook, use the same cycle:\n", "\n", "1. state a prediction in words\n", "2. change one thing only\n", "3. render the circuit and inspect both the graphical and text view\n", "4. compare the new counts or state story to your prediction\n", "5. revise your explanation if needed\n", "\n", "The \u201cone thing only\u201d rule matters. Random edits create noise in your own learning. Controlled edits create evidence.\n" ], "id": "eadf1595" }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Why A Lab Notebook Exists Separately From The Lecture\n", "\n", "In a weak course, the lab is a place where the learner replays the lecture with a little more clicking. In a strong course, the lab carries a distinct burden. The lecture builds the mental model. The lab threatens the mental model by changing the object in controlled ways. That threat is constructive. It is the place where the learner discovers whether an explanation is robust enough to survive variation.\n", "\n", "This matters especially in quantum computing because the surface syntax can be deceptively simple. A one-line change can mean \u201cremoved the branch structure,\u201d \u201cchanged the measurement question,\u201d or \u201cdestroyed the intended relationship between wires.\u201d If you are not used to tracing those distinctions deliberately, the circuit will simply look different and the counts will simply look different. The lab is trying to teach you to say more than that. It is trying to teach you to say *why* the different result deserves the explanation you are giving it.\n", "\n", "Another reason the lab is separate is emotional. When people are still new, they often treat every mismatch as a disaster. The lab normalizes mismatch. A failed prediction is not a reason to panic. It is a reason to locate the exact sentence in your internal explanation that must now be repaired. That is a much healthier and much more professional relationship to error.\n" ], "id": "2808f0ed" }, { "cell_type": "markdown", "metadata": {}, "source": [ "## What To Notice While You Experiment\n", "\n", "The lab becomes much more powerful if you decide in advance what kinds of changes you are looking for. There are at least three categories to track. The first category is **preparation changes**. These alter the circuit before any relationship is created across wires. Removing an initial Hadamard belongs here. The second category is **structural relationship changes**. These alter how one wire depends on another, which is why controlled operations carry more conceptual weight than they first appear to. The third category is **question changes**. These do not necessarily change the prepared object in the same way as the first two categories, but they can drastically change what the readout means. Final basis rotations often belong here.\n", "\n", "If you learn to classify edits into those three categories, your explanations become sharper almost immediately. Instead of saying \u201cthe circuit changed,\u201d you can say \u201cthe preparation changed,\u201d \u201cthe relationship changed,\u201d or \u201cthe readout question changed.\u201d That is a much stronger sentence, and stronger sentences are one of the quiet engines of stronger reasoning.\n" ], "id": "e07709be" }, { "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", " bell_circuit,\n", " counts_to_probabilities,\n", " draw_circuit,\n", " editable_circuit_lab,\n", " load_curriculum,\n", " plot_counts,\n", " quiz_block,\n", " reflection_box,\n", " simulate_counts,\n", " statevector_probabilities,\n", " step_reference_table,\n", ")\n" ], "id": "f0d03ae1" }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Lab 1: Bell Anchor Variation\n", "\n", "Start with the anchor circuit. Remove or move one ingredient at a time and ask which part of the story breaks. This is the fastest way to detect whether you truly know the burden of each line.\n" ], "id": "633baa54" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "step_reference_table([{'marker': '[1]', 'code_focus': 'Prepare qubit 0 with a Hadamard gate.', 'diagram_effect': 'The upper wire branches into a superposition-generating event.', 'why_it_matters': 'This is the first step where the circuit stops behaving like a deterministic classical path.'}, {'marker': '[2]', 'code_focus': 'Use CNOT from qubit 0 to qubit 1.', 'diagram_effect': 'The second wire becomes correlated with the branching created above.', 'why_it_matters': 'This is the moment where local gate syntax becomes shared circuit structure.'}, {'marker': '[3]', 'code_focus': 'Measure both qubits into matching classical bits.', 'diagram_effect': 'The diagram turns a latent correlation into an empirical question.', 'why_it_matters': 'Measurement is not a camera; it is the question being asked of the prepared state.'}])\n" ], "id": "e62aae02" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "editable_code = '\\ncircuit = QuantumCircuit(2, 2)\\n# [1] Prepare the branch point.\\ncircuit.h(0)\\n# [2] Correlate the second qubit with the first.\\ncircuit.cx(0, 1)\\n# [3] Ask the question in the computational basis.\\ncircuit.measure([0, 1], [0, 1])\\n'\n", "editable_circuit_lab(\n", " initial_code=editable_code,\n", " context={\"QuantumCircuit\": __import__(\"qiskit\").QuantumCircuit, \"simulate_counts\": simulate_counts},\n", " title=\"Lab 1: Bell Anchor Variation\",\n", " instructions=\"Delete, move, or replace exactly one marked step and explain the result before you render.\",\n", ")\n" ], "id": "7109fc45" }, { "cell_type": "markdown", "metadata": {}, "source": [ "The first lab should make one principle concrete: circuit meaning is distributed across steps. If you remove the Hadamard, you do not merely delete a gate. You collapse the branch structure the rest of the circuit depends on. If you remove the CNOT, you do not merely simplify the circuit. You destroy the correlation mechanism. If you change the measurement wiring, you change what question is being answered. Those distinctions are the whole point.\n" ], "id": "7419d3a4" }, { "cell_type": "markdown", "metadata": {}, "source": [ "The reason this matters is that most learners overestimate how well they understand a worked example. Variation is the corrective. A design that only makes sense in the exact original arrangement is not yet understood as a design. It is merely remembered as a pattern. By making one local change at a time, the lab forces the remembered pattern to either survive as an explanation or break visibly.\n" ], "id": "54ce000b" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "quiz_block([{'prompt': 'If you remove the Hadamard from a Bell circuit but leave the CNOT, what broad result should you expect?', 'options': ['Only one deterministic outcome from the all-zero input', 'The same balanced Bell histogram as before', 'A completely uniform distribution over all bitstrings'], 'correct_index': 0, 'explanation': 'Without the branch point, the circuit stays on a single classical-looking path from the ground state.'}, {'prompt': 'Why does the lab ask you to compare the graphic and text rendering of the same circuit?', 'options': ['Because they reveal different structural truths and catch different mistakes', 'Because one of them is usually wrong and the other is correct', 'Because Qiskit requires both before simulation'], 'correct_index': 0, 'explanation': 'The pretty diagram and the text rendering support different kinds of inspection.'}], heading='Lab Checkpoint A')\n" ], "id": "4ca85ec8" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "reflection_box(\n", " \"Which single edit most strongly changed the circuit story, and why did that change matter more than the others?\"\n", ")\n" ], "id": "d8ec4e87" }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Lab 2: Basis-Change Experiment\n", "\n", "The next lab tests whether you really believe the sentence \u201cmeasurement is a question.\u201d Insert, remove, or move the final Hadamard on qubit 0 and watch what that does to the meaning of the readout. Keep the circuit small. The point is not complexity. The point is basis awareness.\n" ], "id": "8fa1714b" }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is the lab where many learners realize that they had been treating measurement as a passive camera. Good. That realization is one of the real achievements of the module. Once basis awareness lands, you are no longer merely running circuits. You are beginning to think experimentally: what question did I ask, what object did I prepare, and what evidence do these counts actually justify?\n" ], "id": "294eb88d" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "editable_code = '\\ncircuit = QuantumCircuit(2, 2)\\n# [1] Create the same Bell-style preparation.\\ncircuit.h(0)\\ncircuit.cx(0, 1)\\n# [2] Rotate qubit 0 before asking the measurement question.\\ncircuit.h(0)\\n# [3] Measure both qubits and compare the change.\\ncircuit.measure([0, 1], [0, 1])\\n'\n", "editable_circuit_lab(\n", " initial_code=editable_code,\n", " context={\"QuantumCircuit\": __import__(\"qiskit\").QuantumCircuit, \"simulate_counts\": simulate_counts},\n", " title=\"Lab 2: Basis Change Before Measurement\",\n", " instructions=\"Alter the final basis rotation and explain how the measurement question changes.\",\n", ")\n" ], "id": "7e65cd29" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "basis_demo = __import__(\"qiskit\").QuantumCircuit(2, 2)\n", "basis_demo.h(0)\n", "basis_demo.cx(0, 1)\n", "basis_demo.h(0)\n", "basis_demo.measure([0, 1], [0, 1])\n", "simulate_counts(basis_demo, shots=512)\n" ], "id": "edea8f09" }, { "cell_type": "markdown", "metadata": {}, "source": [ "A basis-change lab is often where a learner\u2019s hidden classical assumptions become visible. If you expected the same histogram as the unrotated Bell circuit, you just found a useful weakness. Good. The notebook did its job. Fix the language, not just the code.\n" ], "id": "126a8b6e" }, { "cell_type": "markdown", "metadata": {}, "source": [ "Basis awareness is one of the earliest serious engineering habits. Once you really believe that a final rotation changes the question rather than merely decorating the circuit, many later topics become easier: interference explanations, tomography-like reasoning, oracle interpretation, and even some debugging of noisy behavior. That is why the lab spends so much energy on what might look like a tiny final gate.\n" ], "id": "4f8a073d" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "reflection_box(\n", " \"Describe the difference between changing the prepared state and changing the question asked of that state.\"\n", ")\n" ], "id": "2d02f7f9" }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Lab 3: Extend The Pattern\n", "\n", "The final lab asks you to extend the same branch-correlate-measure pattern to three wires. This is not yet advanced algorithm design. It is a transfer check. Can you still recognize the mechanism after the surface gets slightly bigger?\n" ], "id": "e6dceaf6" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "editable_code = '\\ncircuit = QuantumCircuit(3, 3)\\n# [1] Start the branching on the first qubit.\\ncircuit.h(0)\\n# [2] Fan the correlation outward.\\ncircuit.cx(0, 1)\\ncircuit.cx(0, 2)\\n# [3] Measure all wires in the computational basis.\\ncircuit.measure([0, 1, 2], [0, 1, 2])\\n'\n", "editable_circuit_lab(\n", " initial_code=editable_code,\n", " context={\"QuantumCircuit\": __import__(\"qiskit\").QuantumCircuit, \"simulate_counts\": simulate_counts},\n", " title=\"Lab 3: Three-Qubit Extension\",\n", " instructions=\"Extend or simplify the branch-correlate-measure pattern and explain how the new wire changes the diagram and the resulting distribution.\",\n", ")\n" ], "id": "f0952ab1" }, { "cell_type": "markdown", "metadata": {}, "source": [ "The three-qubit extension is valuable because it reveals whether you were memorizing a two-wire picture or learning a design pattern. The actual professional skill is the second one.\n" ], "id": "7c871cda" }, { "cell_type": "markdown", "metadata": {}, "source": [ "Leave the lab with a sharper sentence than the one you entered with. A good candidate is: \u201cI can now distinguish changes to preparation, changes to correlation structure, and changes to the measurement question.\u201d If that sentence feels true when you look at your own edits, the lab has started doing real work.\n" ], "id": "a46132b9" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "quiz_block([{'prompt': 'What should a useful lab reflection contain?', 'options': ['Only whether the code ran successfully', 'A prediction, an observed result, and a revised explanation if the prediction failed', 'Mostly notes about notebook formatting and display quality'], 'correct_index': 1, 'explanation': 'The reflection is for diagnosing the model in your head, not merely logging execution success.'}, {'prompt': 'What is the most important consequence of inserting a Hadamard immediately before measuring qubit 0 in the Bell circuit?', 'options': ['You change the question being asked of that wire', 'You remove the need for the CNOT', 'You make the circuit noiseless'], 'correct_index': 0, 'explanation': 'A basis rotation before measurement changes the question, not just the syntax.'}], heading='Lab Checkpoint B')\n" ], "id": "33f851a8" }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Lab Exit Standard\n", "\n", "Leave this notebook only when you can look at a small circuit edit and say, before running anything, whether the change affects preparation, correlation, or the measurement question. That sentence is a small but meaningful piece of circuit literacy.\n" ], "id": "17d2171c" }, { "cell_type": "markdown", "metadata": {}, "source": [ "A good lab exit feeling is not comfort. It is sharper control. You should feel more able to inspect a circuit line and tell whether it changes the prepared state, the structure across wires, or the readout question. If you feel that increased sharpness, the lab has done its work even if some details still need repetition.\n" ], "id": "5686ae87" } ], "metadata": { "kernelspec": { "display_name": "QuantumLearning (.venv)", "language": "python", "name": "quantum-learning" }, "language_info": { "name": "python", "version": "3.12" } }, "nbformat": 4, "nbformat_minor": 5 }