QuantumLearning/notebooks/algorithms/module_04_grover/problems.ipynb

272 lines
11 KiB
Text

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Grover and Amplitude Amplification Problems\n"
],
"id": "959344a2"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<!-- COURSE_NAV_TOP -->\n",
"## Mainline Navigation\n",
"\n",
"Step 41 of 59. Follow the mainline in order and do not skip ahead.\n",
"\n",
"Previous notebook: [Grover and Amplitude Amplification Lab](lab.ipynb)\n",
"\n",
"Next notebook: [Grover and Amplitude Amplification Studio](studio.ipynb)\n",
"\n",
"Rule: finish this notebook top-to-bottom before you open the next one.\n"
],
"id": "aed09269"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"These problems test whether you can maintain a clear distinction between task, mechanism, and design tradeoff in a Grover notebook.\n"
],
"id": "61619ee9"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## How To Use This Notebook\n",
"\n",
"\n",
" Reject answers that sound strong only because they are famous or dramatic. The best answers keep the routine decomposed into auditable parts and attach claims to evidence.\n"
],
"id": "711d843c"
},
{
"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 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"
],
"id": "6d5918b3"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Oracle And Diffuser Roles\n",
"\n",
"Treat this block as a miniature review situation. Choose the answer that would best survive an engineering conversation.\n"
],
"id": "63332322"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"quiz_block([{'prompt': 'Which statement best distinguishes the oracle from the diffuser?', 'options': ['The oracle marks a target by phase; the diffuser amplifies relative to the mean', 'The oracle and diffuser are interchangeable', 'The diffuser measures while the oracle transpiles'], 'correct_index': 0, 'explanation': 'The routine only makes sense if the two blocks have distinct semantic roles.'}, {'prompt': 'Why is separating helpers useful in review?', 'options': ['You can inspect whether the target-marking logic or the amplification logic is at fault', 'It guarantees lower depth', 'It removes the need for tests'], 'correct_index': 0, 'explanation': 'Separation sharpens diagnosis and discussion.'}], heading='Oracle And Diffuser Roles')\n"
],
"id": "c7e3be06"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Geometric Reasoning\n",
"\n",
"Treat this block as a miniature review situation. Choose the answer that would best survive an engineering conversation.\n"
],
"id": "3609b9a3"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"quiz_block([{'prompt': 'What is the professional value of describing Grover as a rotation?', 'options': ['It explains why more iterations are not automatically better', 'It means the circuit can only search circular data', 'It removes the need for oracle design'], 'correct_index': 0, 'explanation': 'The geometry explains the iteration tradeoff.'}, {'prompt': 'Which phrase should you reject in this module?', 'options': ['The routine just keeps getting stronger forever', 'The routine amplifies by composing two reflections', 'Iteration choice depends on search-space size'], 'correct_index': 0, 'explanation': 'That phrase hides the actual mechanism.'}], heading='Geometric Reasoning')\n"
],
"id": "82805c3c"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Iteration Choice\n",
"\n",
"Treat this block as a miniature review situation. Choose the answer that would best survive an engineering conversation.\n"
],
"id": "f205b803"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"quiz_block([{'prompt': 'What is the strongest reason to compare one and two iterations in a 2-qubit search?', 'options': ['It reveals the difference between useful amplification and over-rotation', 'It doubles the number of valid targets', 'It makes the oracle simpler'], 'correct_index': 0, 'explanation': 'Iteration choice is part of design, not just runtime length.'}, {'prompt': 'What evidence best supports the claim that one iteration is better here?', 'options': ['The count distribution peaks more strongly on the target under the intended search size', 'The circuit text looks shorter', 'The diffuser uses fewer comments'], 'correct_index': 0, 'explanation': 'Iteration claims should be supported by observed amplification behavior.'}], heading='Iteration Choice')\n"
],
"id": "1b3e4ad1"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Review Language\n",
"\n",
"Treat this block as a miniature review situation. Choose the answer that would best survive an engineering conversation.\n"
],
"id": "a2d66e37"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"quiz_block([{'prompt': 'Which review comment is strongest?', 'options': ['The target-marking logic is hard to audit because the X wrappers are hidden behind ambiguous indexing', 'This circuit looks aggressive', 'Please add more Hadamards because Grover uses many Hadamards'], 'correct_index': 0, 'explanation': 'Professional comments focus on auditability and semantic burden.'}, {'prompt': 'What should accompany a final Grover recommendation?', 'options': ['A defense of the oracle, diffuser, and iteration count tied to search size and observed outcomes', 'Only a single final histogram', 'Only a screenshot of the circuit drawing'], 'correct_index': 0, 'explanation': 'The recommendation should be structurally and empirically grounded.'}], heading='Review Language')\n"
],
"id": "d19a9a39"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Mini Case\n",
"\n",
"\n",
" Grover is especially vulnerable to prestige-based learning. Because the routine is widely known, learners often assume they understand it after seeing one polished diagram. But a polished diagram does not answer the questions an engineer will face: how is the target encoded, how readable is the oracle helper, how many iterations are appropriate for this search space, and what evidence supports that choice? The problems notebook asks those questions directly so the prestige does not mask the mechanism.\n"
],
"id": "dc59ea20"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## What These Questions Are Really Testing\n",
"\n",
"The multiple-choice format is only the surface. Underneath it, the notebook is testing whether your explanation can survive small shifts in phrasing, emphasis, and review context. If your understanding is robust, the wording can change and the same mechanism will still come into focus. If your understanding is fragile, the wording change will tempt you back into vague or prestige-based answers. That is why these problems matter.\n"
],
"id": "7f90af7d"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Common Failure Mode\n",
"\n",
"A common failure mode at this stage is to answer with something broadly true but locally weak. For example, saying that a circuit uses superposition or interference may be accurate, yet still fail to identify what role a specific block is playing in the present design. The goal of the problems notebook is to eliminate that kind of vague correctness and replace it with circuit-specific explanation.\n"
],
"id": "28cac969"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Written Checks\n",
"\n",
"Use the prompts below to rehearse full-sentence engineering explanations.\n"
],
"id": "a25e47b5"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"reflection_box('Explain why a strong Grover notebook must separate oracle logic from diffuser logic.')\n"
],
"id": "a802acd7"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"reflection_box('Describe the evidence you would use to justify an iteration count in a tiny local search problem.')\n"
],
"id": "12e3351b"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exit Condition\n",
"\n",
"\n",
" Move on when you can treat Grover as a composed routine with explicit roles and tradeoffs, not as a famous diagram you happened to reproduce.\n"
],
"id": "8c611ed3"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<!-- COURSE_NAV_BOTTOM -->\n",
"## What To Open Next\n",
"\n",
"Next notebook: [Grover and Amplitude Amplification Studio](studio.ipynb)\n",
"\n",
"When you finish this notebook, open the next notebook shown above. Stay on the guarded mainline route.\n"
],
"id": "4dbcccc8"
}
],
"metadata": {
"kernelspec": {
"display_name": "QuantumLearning (.venv)",
"language": "python",
"name": "quantum-learning"
},
"language_info": {
"name": "python",
"version": "3.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}