2026-04-15 08:21:41 +00:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
2026-04-15 15:34:45 +00:00
"<!-- 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",
2026-04-15 08:21:41 +00:00
"# Deutsch Family and Oracle Thinking Lab\n"
2026-04-15 09:14:19 +00:00
],
2026-04-15 15:34:45 +00:00
"id": "a41085d2"
2026-04-15 08:21:41 +00:00
},
2026-04-15 13:48:28 +00:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
2026-04-15 15:34:45 +00:00
"<!-- 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",
2026-04-15 13:48:28 +00:00
"<!-- COURSE_NAV_TOP -->\n",
"## Mainline Navigation\n",
"\n",
2026-04-15 15:34:45 +00:00
"Step 28 of 59. Follow the official walkthrough in order.\n",
2026-04-15 13:48:28 +00:00
"\n",
"Previous notebook: [Deutsch Family and Oracle Thinking Lecture](lecture.ipynb)\n",
"\n",
"Next notebook: [Deutsch Family and Oracle Thinking Problems](problems.ipynb)\n",
"\n",
2026-04-15 15:34:45 +00:00
"Rule: complete the mandatory cells in this notebook before you open the next one.\n"
2026-04-15 13:48:28 +00:00
],
2026-04-15 15:34:45 +00:00
"id": "0a51a00a"
2026-04-15 13:48:28 +00:00
},
2026-04-15 08:21:41 +00:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
2026-04-15 15:34:45 +00:00
"<!-- 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",
2026-04-15 08:21:41 +00:00
"The lab turns the oracle story into controlled edits. Each exercise asks you to preserve the question you are asking while changing one part of the mechanism. That discipline matters because algorithm notebooks become noisy very quickly when multiple causal features are changed at once.\n"
2026-04-15 09:14:19 +00:00
],
2026-04-15 15:34:45 +00:00
"id": "386a9d4e"
2026-04-15 08:21:41 +00:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2026-04-15 15:34:45 +00:00
"<!-- 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",
2026-04-15 08:21:41 +00:00
"## Lab Protocol\n",
"\n",
"\n",
" Before each edit, say what should remain invariant and what should change. Then render the circuit, inspect the diagram, inspect the preview counts, and decide whether the result matches the mechanism you think you are testing. If you cannot state the intended invariant, the edit is too loose.\n"
2026-04-15 09:14:19 +00:00
],
2026-04-15 15:34:45 +00:00
"id": "5a72678d"
},
{
"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"
2026-04-15 08:21:41 +00:00
},
{
"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"
2026-04-15 09:14:19 +00:00
],
2026-04-15 15:34:45 +00:00
"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"
2026-04-15 08:21:41 +00:00
},
{
"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"
2026-04-15 09:14:19 +00:00
],
2026-04-15 15:34:45 +00:00
"id": "3cc4775a"
2026-04-15 08:21:41 +00:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2026-04-15 15:34:45 +00:00
"<!-- 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",
2026-04-15 08:21:41 +00:00
"## Lab 1: Deutsch Oracle Variants\n",
"\n",
"\n",
" Start with the smallest circuit and change only the oracle kind. You should be able to explain why the constant and balanced families differ before you run the code. Do not let the count histogram be the first time you decide what the circuit means.\n"
2026-04-15 09:14:19 +00:00
],
2026-04-15 15:34:45 +00:00
"id": "a476a2d6"
},
{
"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"
2026-04-15 08:21:41 +00:00
},
{
"cell_type": "code",
"execution_count": null,
2026-04-15 16:16:18 +00:00
"metadata": {
"jupyter": {
"source_hidden": true
},
"tags": [
"hide-input"
]
},
2026-04-15 08:21:41 +00:00
"outputs": [],
"source": [
"step_reference_table([{'marker': '[1]', 'code_focus': 'Prepare the query wire in superposition and the ancilla in the |-> state.', 'diagram_effect': 'The left side of the diagram separates the question register from the phase-sensitive ancilla.', 'why_it_matters': 'Without this preparation, the oracle behaves like an ordinary reversible gadget instead of a phase-encoding query.'}, {'marker': '[2]', 'code_focus': 'Apply an oracle that represents a promise class rather than a single numerical answer.', 'diagram_effect': 'The middle region becomes the semantic core of the circuit.', 'why_it_matters': 'Algorithmic design starts when you think in contracts and promise structures rather than in isolated gates.'}, {'marker': '[3]', 'code_focus': 'Use a final Hadamard on the query wire to convert hidden phase information into a measurable bit.', 'diagram_effect': 'The right side of the circuit shows a deliberate interference stage instead of direct readout after the oracle.', 'why_it_matters': 'Interference is the mechanism that cashes out the query advantage.'}, {'marker': '[4]', 'code_focus': 'Measure only the wire that carries the decision information.', 'diagram_effect': 'The reporting layer stays narrow and intentional.', 'why_it_matters': 'Professional design protects the evidence path and avoids measuring wires that are not part of the claim.'}])\n"
2026-04-15 09:14:19 +00:00
],
2026-04-15 15:34:45 +00:00
"id": "ee63d5fc"
},
{
"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"
2026-04-15 08:21:41 +00:00
},
{
"cell_type": "code",
"execution_count": null,
2026-04-15 16:16:18 +00:00
"metadata": {
"jupyter": {
"source_hidden": true
},
"tags": [
"hide-input"
]
},
2026-04-15 08:21:41 +00:00
"outputs": [],
"source": [
"editable_code = '\\nfrom qiskit import QuantumCircuit\\n\\ndef deutsch_oracle(kind: str) -> QuantumCircuit:\\n oracle = QuantumCircuit(2, name=f\"oracle_{kind}\")\\n if kind == \"balanced\":\\n oracle.cx(0, 1)\\n elif kind == \"constant_one\":\\n oracle.x(1)\\n elif kind != \"constant_zero\":\\n raise ValueError(\"kind must be constant_zero, constant_one, or balanced\")\\n return oracle\\n\\ncircuit = QuantumCircuit(2, 1)\\n# [1] Query wire in superposition, ancilla in |->\\ncircuit.h(0)\\ncircuit.x(1)\\ncircuit.h(1)\\n# [2] Oracle encodes the promise class\\ncircuit.compose(deutsch_oracle(\"balanced\"), inplace=True)\\n# [3] Final Hadamard turns phase into a measurable distinction\\ncircuit.h(0)\\n# [4] Only the decision wire needs to be reported\\ncircuit.measure(0, 0)\\n'\n",
"editable_circuit_lab(\n",
" initial_code=editable_code,\n",
" context={\"QuantumCircuit\": QuantumCircuit, \"simulate_counts\": simulate_counts},\n",
" title='Lab 1: Deutsch Oracle Variants',\n",
" instructions='Switch between constant_zero, constant_one, and balanced. Keep the explanation of the decision wire and the promise class explicit.',\n",
" shots=256,\n",
")\n"
2026-04-15 09:14:19 +00:00
],
2026-04-15 15:34:45 +00:00
"id": "884e0f6a"
},
{
"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"
2026-04-15 08:21:41 +00:00
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def deutsch_counts(kind: str) -> dict[str, int]:\n",
" circuit = QuantumCircuit(2, 1)\n",
" circuit.h(0)\n",
" circuit.x(1)\n",
" circuit.h(1)\n",
" if kind == \"balanced\":\n",
" circuit.cx(0, 1)\n",
" elif kind == \"constant_one\":\n",
" circuit.x(1)\n",
" circuit.h(0)\n",
" circuit.measure(0, 0)\n",
" return simulate_counts(circuit, shots=256)\n",
"\n",
"results = {kind: deutsch_counts(kind) for kind in [\"constant_zero\", \"constant_one\", \"balanced\"]}\n",
"results\n"
2026-04-15 09:14:19 +00:00
],
2026-04-15 15:34:45 +00:00
"id": "14c860d1"
},
{
"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"
2026-04-15 08:21:41 +00:00
},
{
"cell_type": "code",
"execution_count": null,
2026-04-15 16:16:18 +00:00
"metadata": {
"jupyter": {
"source_hidden": true
},
"tags": [
"hide-input"
]
},
2026-04-15 08:21:41 +00:00
"outputs": [],
"source": [
"quiz_block([{'prompt': 'If you change the Deutsch oracle from balanced to constant_zero, what should happen to the measured query bit?', 'options': ['It should reliably flip to 1', 'It should move toward 0 because the phase distinction disappears', 'It should become uniformly random'], 'correct_index': 1, 'explanation': 'A constant oracle does not create the same phase relation, so the final interference returns the constant verdict.'}, {'prompt': 'What is the right way to inspect a circuit edit in this lab?', 'options': ['Change several lines at once so the effect is obvious', 'Change one causal feature at a time and compare the diagram and counts together', 'Ignore the diagram and focus only on the text representation'], 'correct_index': 1, 'explanation': 'Controlled editing is the only way to attach causes to outcomes.'}], heading='Lab Checkpoint A')\n"
2026-04-15 09:14:19 +00:00
],
2026-04-15 15:34:45 +00:00
"id": "6f31fa12"
},
{
"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"
2026-04-15 08:21:41 +00:00
},
{
"cell_type": "code",
"execution_count": null,
2026-04-15 16:16:18 +00:00
"metadata": {
"jupyter": {
"source_hidden": true
},
"tags": [
"hide-input"
]
},
2026-04-15 08:21:41 +00:00
"outputs": [],
"source": [
"reflection_box('Which lab edit most improved your understanding of why the ancilla must be prepared as it is?')\n"
2026-04-15 09:14:19 +00:00
],
2026-04-15 15:34:45 +00:00
"id": "5eee032a"
2026-04-15 08:21:41 +00:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2026-04-15 15:34:45 +00:00
"<!-- 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",
2026-04-15 08:21:41 +00:00
"## Lab 2: Deutsch-Jozsa Scaling\n",
"\n",
"\n",
" Move to a wider query register without abandoning the same mechanism. The point is to feel what scales and what does not. If your explanation changes from 'promise plus interference' to 'I copied a bigger circuit,' stop and reset.\n"
2026-04-15 09:14:19 +00:00
],
2026-04-15 15:34:45 +00:00
"id": "6d9f0f4a"
},
{
"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"
2026-04-15 08:21:41 +00:00
},
{
"cell_type": "code",
"execution_count": null,
2026-04-15 16:16:18 +00:00
"metadata": {
"jupyter": {
"source_hidden": true
},
"tags": [
"hide-input"
]
},
2026-04-15 08:21:41 +00:00
"outputs": [],
"source": [
"editable_code = '\\nfrom qiskit import QuantumCircuit\\n\\ndef deutsch_jozsa_oracle(kind: str = \"balanced\") -> QuantumCircuit:\\n oracle = QuantumCircuit(3, name=f\"dj_{kind}\")\\n if kind == \"balanced\":\\n oracle.cx(0, 2)\\n oracle.cx(1, 2)\\n elif kind == \"constant_one\":\\n oracle.x(2)\\n elif kind != \"constant_zero\":\\n raise ValueError(\"kind must be constant_zero, constant_one, or balanced\")\\n return oracle\\n\\ncircuit = QuantumCircuit(3, 2)\\n# Put both query wires into superposition.\\ncircuit.h([0, 1])\\n# Prepare the ancilla in |->\\ncircuit.x(2)\\ncircuit.h(2)\\n# Query the oracle contract.\\ncircuit.compose(deutsch_jozsa_oracle(\"balanced\"), inplace=True)\\n# Interference reveals whether the promise is constant or balanced.\\ncircuit.h([0, 1])\\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='Lab 2: Deutsch-Jozsa Promise Cases',\n",
" instructions='Toggle the oracle kind and explain how the all-zero result versus non-zero pattern tracks the promise class.',\n",
" shots=256,\n",
")\n"
2026-04-15 09:14:19 +00:00
],
2026-04-15 15:34:45 +00:00
"id": "74d7ed25"
},
{
"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"
2026-04-15 08:21:41 +00:00
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def dj_counts(kind: str) -> dict[str, int]:\n",
" circuit = QuantumCircuit(3, 2)\n",
" circuit.h([0, 1])\n",
" circuit.x(2)\n",
" circuit.h(2)\n",
" if kind == \"balanced\":\n",
" circuit.cx(0, 2)\n",
" circuit.cx(1, 2)\n",
" elif kind == \"constant_one\":\n",
" circuit.x(2)\n",
" circuit.h([0, 1])\n",
" circuit.measure([0, 1], [0, 1])\n",
" return simulate_counts(circuit, shots=256)\n",
"\n",
"{kind: dj_counts(kind) for kind in [\"constant_zero\", \"constant_one\", \"balanced\"]}\n"
2026-04-15 09:14:19 +00:00
],
2026-04-15 15:34:45 +00:00
"id": "646bb05b"
2026-04-15 08:21:41 +00:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2026-04-15 15:34:45 +00:00
"<!-- 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",
2026-04-15 08:21:41 +00:00
"## Lab 3: Kickback Ablation\n",
"\n",
"\n",
" The fastest way to discover whether you truly understand the mechanism is to break it on purpose. Remove or alter one preparation step and see whether you can predict exactly which explanatory sentence stops being true. This is a design habit, not just a debugging trick.\n"
2026-04-15 09:14:19 +00:00
],
2026-04-15 15:34:45 +00:00
"id": "e0fc6abc"
},
{
"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"
2026-04-15 08:21:41 +00:00
},
{
"cell_type": "code",
"execution_count": null,
2026-04-15 16:16:18 +00:00
"metadata": {
"jupyter": {
"source_hidden": true
},
"tags": [
"hide-input"
]
},
2026-04-15 08:21:41 +00:00
"outputs": [],
"source": [
"editable_code = '\\nfrom qiskit import QuantumCircuit\\n\\ndef balanced_oracle() -> QuantumCircuit:\\n oracle = QuantumCircuit(2, name=\"balanced\")\\n oracle.cx(0, 1)\\n return oracle\\n\\ncircuit = QuantumCircuit(2, 1)\\n# Toggle these preparation choices to see phase kickback disappear.\\ncircuit.h(0)\\ncircuit.x(1)\\ncircuit.h(1)\\ncircuit.compose(balanced_oracle(), inplace=True)\\ncircuit.h(0)\\ncircuit.measure(0, 0)\\n'\n",
"editable_circuit_lab(\n",
" initial_code=editable_code,\n",
" context={\"QuantumCircuit\": QuantumCircuit, \"simulate_counts\": simulate_counts},\n",
" title='Lab 3: Phase-Kickback Ablation',\n",
" instructions='Delete or modify one preparation step at a time. Say which causal link you just removed and what verdict you expect to lose.',\n",
" shots=256,\n",
")\n"
2026-04-15 09:14:19 +00:00
],
2026-04-15 15:34:45 +00:00
"id": "ade34e1d"
},
{
"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"
2026-04-15 08:21:41 +00:00
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"intact = QuantumCircuit(2, 1)\n",
"intact.h(0)\n",
"intact.x(1)\n",
"intact.h(1)\n",
"intact.cx(0, 1)\n",
"intact.h(0)\n",
"intact.measure(0, 0)\n",
"\n",
"broken = QuantumCircuit(2, 1)\n",
"broken.h(0)\n",
"broken.x(1)\n",
"broken.cx(0, 1)\n",
"broken.h(0)\n",
"broken.measure(0, 0)\n",
"\n",
"{\"intact\": simulate_counts(intact, shots=256), \"broken\": simulate_counts(broken, shots=256)}\n"
2026-04-15 09:14:19 +00:00
],
2026-04-15 15:34:45 +00:00
"id": "65fedab1"
},
{
"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"
2026-04-15 08:21:41 +00:00
},
{
"cell_type": "code",
"execution_count": null,
2026-04-15 16:16:18 +00:00
"metadata": {
"jupyter": {
"source_hidden": true
},
"tags": [
"hide-input"
]
},
2026-04-15 08:21:41 +00:00
"outputs": [],
"source": [
"quiz_block([{'prompt': 'What usually happens if you remove the ancilla Hadamard and keep the rest of the Deutsch circuit unchanged?', 'options': ['Phase kickback logic is broken, so the final decision bit loses its meaning', 'The query wire is automatically corrected by transpilation', 'Nothing changes because the ancilla is not measured'], 'correct_index': 0, 'explanation': 'The ancilla preparation is part of the mechanism, not optional decoration.'}, {'prompt': 'Why is Deutsch-Jozsa a useful engineering module and not only a historical algorithm?', 'options': ['It teaches how to scale promise-structured oracles and interference reasoning', 'It is the fastest route to hardware execution', 'It eliminates the need for later algorithm study'], 'correct_index': 0, 'explanation': 'The reusable lesson is how an oracle contract and interference stage compose.'}], heading='Lab Checkpoint B')\n"
2026-04-15 09:14:19 +00:00
],
2026-04-15 15:34:45 +00:00
"id": "62470135"
2026-04-15 08:21:41 +00:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2026-04-15 15:34:45 +00:00
"<!-- 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",
2026-04-15 08:21:41 +00:00
"## Lab Debrief\n",
"\n",
"\n",
" The lab should leave you with a stricter sense of what counts as an explanation. A strong explanation in this module ties the promise class to the oracle body, the ancilla preparation to phase kickback, the final Hadamard to interference, and the measured wire to the actual decision being made. If any of those links felt fuzzy, that is useful information. The whole point of the bundle structure is to surface that fuzziness before later modules assume the pattern is already solid.\n"
2026-04-15 09:14:19 +00:00
],
2026-04-15 15:34:45 +00:00
"id": "a573e44e"
2026-04-15 08:21:41 +00:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2026-04-15 15:34:45 +00:00
"<!-- 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",
2026-04-15 08:21:41 +00:00
"## 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"
2026-04-15 09:14:19 +00:00
],
2026-04-15 15:34:45 +00:00
"id": "39744ce8"
2026-04-15 08:21:41 +00:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2026-04-15 15:34:45 +00:00
"<!-- 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",
2026-04-15 08:21:41 +00:00
"## 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"
2026-04-15 09:14:19 +00:00
],
2026-04-15 15:34:45 +00:00
"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"
2026-04-15 08:21:41 +00:00
},
{
"cell_type": "code",
"execution_count": null,
2026-04-15 16:16:18 +00:00
"metadata": {
"jupyter": {
"source_hidden": true
},
"tags": [
"hide-input"
]
},
2026-04-15 08:21:41 +00:00
"outputs": [],
"source": [
"reflection_box('Write a short review note about one Deutsch-Jozsa oracle candidate that looks plausible but does not clearly expose its promise class.')\n"
2026-04-15 09:14:19 +00:00
],
2026-04-15 15:34:45 +00:00
"id": "fde9e2ea"
},
{
"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"
2026-04-15 08:21:41 +00:00
},
{
"cell_type": "code",
"execution_count": null,
2026-04-15 16:16:18 +00:00
"metadata": {
"jupyter": {
"source_hidden": true
},
"tags": [
"hide-input"
]
},
2026-04-15 08:21:41 +00:00
"outputs": [],
"source": [
"reflection_box('Write one additional prediction habit you want to carry into later modules.')\n"
2026-04-15 09:14:19 +00:00
],
2026-04-15 15:34:45 +00:00
"id": "f3b6ec95"
2026-04-15 13:48:28 +00:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2026-04-15 15:34:45 +00:00
"<!-- 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",
2026-04-15 13:48:28 +00:00
"<!-- COURSE_NAV_BOTTOM -->\n",
"## What To Open Next\n",
"\n",
"Next notebook: [Deutsch Family and Oracle Thinking Problems](problems.ipynb)\n",
"\n",
2026-04-15 15:34:45 +00:00
"Official walkthrough rule: once every mandatory cell above is complete, open the next notebook. Anything below this cell is facultative.\n"
],
"id": "1d5b1f1a"
},
{
"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 **Deutsch Family and Oracle Thinking Lab**. Everything below is optional. Use it only if you want deeper consolidation or extra transfer work.\n"
],
"id": "ae456221"
},
{
"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 **Deutsch Family and Oracle Thinking 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": "3570e3b7"
},
{
"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,
2026-04-15 16:16:18 +00:00
"ql_note": "Optional multiple-choice extension.",
"jupyter": {
"source_hidden": true
},
"tags": [
"hide-input"
]
2026-04-15 15:34:45 +00:00
},
"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,
2026-04-15 16:16:18 +00:00
"ql_note": "Optional written exercise.",
"jupyter": {
"source_hidden": true
},
"tags": [
"hide-input"
]
2026-04-15 15:34:45 +00:00
},
"outputs": [],
"source": [
"reflection_box('Describe one optional circuit variation you would run after Deutsch Family and Oracle Thinking Lab, what you would predict before running it, and what evidence would make you abandon the prediction.')\n"
2026-04-15 13:48:28 +00:00
],
2026-04-15 15:34:45 +00:00
"id": "038bca2c"
2026-04-15 08:21:41 +00:00
}
],
"metadata": {
"kernelspec": {
"display_name": "QuantumLearning (.venv)",
"language": "python",
"name": "quantum-learning"
},
"language_info": {
"name": "python",
"version": "3.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}