\n",
"\n",
"\n",
"## Mainline Navigation\n",
"\n",
"Step 40 of 59. Follow the official walkthrough in order.\n",
"\n",
"Previous notebook: [Grover and Amplitude Amplification Lecture](lecture.ipynb)\n",
"\n",
"Next notebook: [Grover and Amplitude Amplification Problems](problems.ipynb)\n",
"\n",
"Rule: complete the mandatory cells in this notebook before you open the next one.\n"
],
"id": "13d110d1"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"
\n",
"\n",
"The lab emphasizes target variation, iteration comparison, and oracle readability. The small search space is a feature, not a limitation, because it lets you see the mechanism before later reality bands complicate it.\n"
],
"id": "53811385"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"
\n",
"\n",
"## Lab Protocol\n",
"\n",
"\n",
" When you edit the target, inspect the oracle wrappers. When you edit the iteration count, predict whether you are improving or overshooting the amplification. When you edit the helper shape, ask whether the semantic role of each block stayed visible.\n"
],
"id": "e283b5ca"
},
{
"cell_type": "markdown",
"metadata": {
"ql_injected": "badge",
"ql_track": "mandatory",
"ql_role": "setup"
},
"source": [
"\n",
"
\n",
"MANDATORY SETUP · Difficulty 1/10 · Environment, import, or helper cell required by the notebook.\n",
"
\n",
"\n",
"## Lab 1: Target Changes\n",
"\n",
"\n",
" Start by moving the marked state around the two-qubit search space. The question is not only whether the peak moves. The question is whether the target-marking logic remains readable enough that you can audit it.\n"
],
"id": "6127b66f"
},
{
"cell_type": "markdown",
"metadata": {
"ql_injected": "badge",
"ql_track": "mandatory",
"ql_role": "exercise"
},
"source": [
"\n",
"
\n",
"MANDATORY EXERCISE · Difficulty 2/10 · Official walkthrough runnable or written exercise.\n",
"
\n"
],
"id": "85f3642b"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"jupyter": {
"source_hidden": true
},
"tags": [
"hide-input"
]
},
"outputs": [],
"source": [
"step_reference_table([{'marker': '[1]', 'code_focus': 'Prepare the search register in a uniform superposition.', 'diagram_effect': 'The circuit opens with a clean equal-weight state over all candidates.', 'why_it_matters': 'Amplitude amplification starts from a fair baseline, not from a pre-biased guess.'}, {'marker': '[2]', 'code_focus': 'Implement the oracle as a phase-marking operation on the chosen target.', 'diagram_effect': 'The middle block identifies the marked state without directly measuring it.', 'why_it_matters': 'Grover oracles mark by phase, not by classical tagging.'}, {'marker': '[3]', 'code_focus': 'Apply the diffuser as a reflection about the mean amplitude.', 'diagram_effect': 'The circuit mirrors the oracle block with a deliberate inversion pattern.', 'why_it_matters': 'The diffuser is the engineering heart of amplitude amplification.'}, {'marker': '[4]', 'code_focus': 'Choose the iteration count as a design decision, then measure.', 'diagram_effect': 'The right edge of the circuit closes the amplification loop with a concrete reporting contract.', 'why_it_matters': 'Over-iteration is a real design failure, not a harmless extra pass.'}])\n"
],
"id": "af162c91"
},
{
"cell_type": "markdown",
"metadata": {
"ql_injected": "badge",
"ql_track": "mandatory",
"ql_role": "exercise"
},
"source": [
"\n",
"
\n",
"MANDATORY EXERCISE · Difficulty 2/10 · Official walkthrough runnable or written exercise.\n",
"
\n"
],
"id": "12a4181c"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"jupyter": {
"source_hidden": true
},
"tags": [
"hide-input"
]
},
"outputs": [],
"source": [
"editable_code = '\\nfrom qiskit import QuantumCircuit\\n\\ndef grover_oracle(target: str = \"11\") -> QuantumCircuit:\\n oracle = QuantumCircuit(2, name=f\"oracle_{target}\")\\n if target[0] == \"0\":\\n oracle.x(1)\\n if target[1] == \"0\":\\n oracle.x(0)\\n oracle.cz(0, 1)\\n if target[1] == \"0\":\\n oracle.x(0)\\n if target[0] == \"0\":\\n oracle.x(1)\\n return oracle\\n\\ndef diffuser() -> QuantumCircuit:\\n circuit = QuantumCircuit(2, name=\"diffuser\")\\n circuit.h([0, 1])\\n circuit.x([0, 1])\\n circuit.cz(0, 1)\\n circuit.x([0, 1])\\n circuit.h([0, 1])\\n return circuit\\n\\ncircuit = QuantumCircuit(2, 2)\\n# [1] Uniform search state\\ncircuit.h([0, 1])\\n# [2] Mark the target by phase\\ncircuit.compose(grover_oracle(\"11\"), inplace=True)\\n# [3] Reflect about the mean\\ncircuit.compose(diffuser(), inplace=True)\\n# [4] Report the amplified candidate\\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 1: Target Changes',\n",
" instructions='Change the target string and verify that the X-wrapper logic marks the state you intended before you trust the counts.',\n",
" shots=256,\n",
")\n"
],
"id": "06bd385a"
},
{
"cell_type": "markdown",
"metadata": {
"ql_injected": "badge",
"ql_track": "mandatory",
"ql_role": "exercise"
},
"source": [
"\n",
"
\n",
"MANDATORY EXERCISE · Difficulty 2/10 · Official walkthrough runnable or written exercise.\n",
"
\n",
"MANDATORY TEST · Difficulty 2/10 · Official walkthrough multiple-choice test.\n",
"
\n"
],
"id": "63b41ca3"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"jupyter": {
"source_hidden": true
},
"tags": [
"hide-input"
]
},
"outputs": [],
"source": [
"quiz_block([{'prompt': 'If you change the Grover target from 11 to 01, what should stay invariant?', 'options': ['The overall oracle-plus-diffuser pattern', 'The exact same X wrappers around the oracle', 'The measured winning bitstring'], 'correct_index': 0, 'explanation': 'The motif is stable even though the marking details change.'}, {'prompt': 'What is the right way to inspect a target-change edit?', 'options': ['Check whether the oracle wrappers match the new target and whether the counts peak moved accordingly', 'Only inspect circuit depth', 'Only inspect the diffuser block'], 'correct_index': 0, 'explanation': 'The target lives in the oracle mapping and the resulting readout distribution.'}], heading='Lab Checkpoint A')\n"
],
"id": "267adcfc"
},
{
"cell_type": "markdown",
"metadata": {
"ql_injected": "badge",
"ql_track": "mandatory",
"ql_role": "exercise"
},
"source": [
"\n",
"
\n",
"MANDATORY EXERCISE · Difficulty 2/10 · Official walkthrough runnable or written exercise.\n",
"
\n"
],
"id": "cb0164f8"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"jupyter": {
"source_hidden": true
},
"tags": [
"hide-input"
]
},
"outputs": [],
"source": [
"reflection_box('What did the one-versus-two iteration comparison teach you about the geometric story of Grover?')\n"
],
"id": "ef7d9ebc"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"
\n",
"\n",
"## Lab 2: Iteration Comparison\n",
"\n",
"\n",
" Next compare one and two iterations explicitly. This is where the rotation language should stop feeling decorative and start feeling predictive.\n"
],
"id": "b0d3c697"
},
{
"cell_type": "markdown",
"metadata": {
"ql_injected": "badge",
"ql_track": "mandatory",
"ql_role": "exercise"
},
"source": [
"\n",
"
\n",
"MANDATORY EXERCISE · Difficulty 2/10 · Official walkthrough runnable or written exercise.\n",
"
\n",
"MANDATORY TEST · Difficulty 2/10 · Official walkthrough multiple-choice test.\n",
"
\n"
],
"id": "9b519db1"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"jupyter": {
"source_hidden": true
},
"tags": [
"hide-input"
]
},
"outputs": [],
"source": [
"quiz_block([{'prompt': 'What does two iterations on a 2-qubit Grover problem often illustrate?', 'options': ['Over-rotation or unnecessary extra work relative to the one-iteration optimum', 'Guaranteed improvement over one iteration', 'Automatic error mitigation'], 'correct_index': 0, 'explanation': 'A small search space makes the iteration-choice tradeoff easy to see.'}, {'prompt': 'Why compare separate oracle implementations for different targets?', 'options': ['To see whether the target-selection code stays readable and reviewable as the marking logic changes', 'Because only one target is physically valid', 'Because the diffuser depends on the target string'], 'correct_index': 0, 'explanation': 'Readable target handling matters for maintainable search code.'}], heading='Lab Checkpoint B')\n"
],
"id": "277d1d75"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"
\n",
"\n",
"## Lab Debrief\n",
"\n",
"\n",
" The key lab outcome is that Grover should no longer feel like a single memorized diagram. It should feel like a modular routine with independent moving parts: target selection, diffusion, and iteration choice. Once those parts become independently discussable, the routine becomes something you can engineer rather than merely admire.\n"
],
"id": "a746be21"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"
\n",
"\n",
"## 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"
],
"id": "39744ce8"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"
\n",
"\n",
"## 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"
],
"id": "6f2bab6b"
},
{
"cell_type": "markdown",
"metadata": {
"ql_injected": "badge",
"ql_track": "mandatory",
"ql_role": "exercise"
},
"source": [
"\n",
"
\n",
"MANDATORY EXERCISE · Difficulty 2/10 · Official walkthrough runnable or written exercise.\n",
"
\n"
],
"id": "db56ed9f"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"jupyter": {
"source_hidden": true
},
"tags": [
"hide-input"
]
},
"outputs": [],
"source": [
"reflection_box('Write a short review note about one oracle helper that would be too opaque for serious use.')\n"
],
"id": "d7c8836c"
},
{
"cell_type": "markdown",
"metadata": {
"ql_injected": "badge",
"ql_track": "mandatory",
"ql_role": "exercise"
},
"source": [
"\n",
"
\n",
"MANDATORY EXERCISE · Difficulty 2/10 · Official walkthrough runnable or written exercise.\n",
"
\n",
"\n",
"\n",
"## What To Open Next\n",
"\n",
"Next notebook: [Grover and Amplitude Amplification Problems](problems.ipynb)\n",
"\n",
"Official walkthrough rule: once every mandatory cell above is complete, open the next notebook. Anything below this cell is facultative.\n"
],
"id": "f039ba86"
},
{
"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": [
"\n",
"
\n",
"META READING · Difficulty 1/10 · Optional-zone boundary. The official walkthrough is already complete above.\n",
"
\n",
"\n",
"\n",
"## Facultative Extension Zone\n",
"\n",
"You have already completed the mandatory walkthrough for **Grover and Amplitude Amplification Lab**. Everything below is optional. Use it only if you want deeper consolidation or extra transfer work.\n"
],
"id": "61f51636"
},
{
"cell_type": "markdown",
"metadata": {
"ql_injected": "facultative",
"ql_track": "facultative",
"ql_role": "reading",
"ql_difficulty": 4,
"ql_note": "Optional extension reading."
},
"source": [
"\n",
"
\n",
"\n",
"## Facultative Extension Reading\n",
"\n",
"In **Grover and Amplitude Amplification 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": "186a4978"
},
{
"cell_type": "markdown",
"metadata": {
"ql_injected": "badge",
"ql_track": "facultative",
"ql_role": "test"
},
"source": [
"\n",
"
\n"
],
"id": "83c53a07"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ql_injected": "facultative",
"ql_track": "facultative",
"ql_role": "test",
"ql_difficulty": 5,
"ql_note": "Optional multiple-choice extension.",
"jupyter": {
"source_hidden": true
},
"tags": [
"hide-input"
]
},
"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": [
"\n",
"