{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "META READING · Difficulty 1/10 · Notebook-level rule, objective, or usage guidance.\n", "
\n", "\n", "# Grover and Amplitude Amplification Studio\n" ], "id": "5f6ac20c" }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "META READING · Difficulty 1/10 · Official walkthrough guardrail.\n", "
\n", "\n", "\n", "## Mainline Navigation\n", "\n", "Step 42 of 59. Follow the official walkthrough in order.\n", "\n", "Previous notebook: [Grover and Amplitude Amplification Problems](problems.ipynb)\n", "\n", "Next notebook: [Qiskit Patterns and Workflow Design Lecture](../../professional/module_01_qiskit_patterns/lecture.ipynb)\n", "\n", "Rule: complete the mandatory cells in this notebook before you open the next one.\n" ], "id": "9f0d745e" }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "META READING · Difficulty 1/10 · Notebook-level rule, objective, or usage guidance.\n", "
\n", "\n", "The studio closes the algorithmic-design band by asking for a real search-design memo. This is where the earlier lecture and lab work should condense into a small piece of professional reasoning.\n" ], "id": "12c18de6" }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "META READING · Difficulty 1/10 · Notebook-level rule, objective, or usage guidance.\n", "
\n", "\n", "## Design Brief\n", "\n", "\n", " Build a two-qubit Grover design study that compares at least two targets and at least two iteration choices, then write a recommendation that defends the oracle style, diffuser reuse, and final iteration count.\n" ], "id": "85dab059" }, { "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" ], "id": "d3172a31" }, { "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": "8ad03118" }, { "cell_type": "markdown", "metadata": { "ql_injected": "badge", "ql_track": "mandatory", "ql_role": "exercise" }, "source": [ "\n", "
\n", "MANDATORY EXERCISE · Difficulty 3/10 · Official walkthrough runnable or written exercise.\n", "
\n" ], "id": "540f0930" }, { "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": "3cc4775a" }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "META READING · Difficulty 1/10 · Notebook-level rule, objective, or usage guidance.\n", "
\n", "\n", "## Studio Prompt 1: Target Family\n", "\n", "\n", " Show that the search routine works as a family over multiple marked states and that your target-marking code remains readable across those changes.\n" ], "id": "23f85df4" }, { "cell_type": "markdown", "metadata": { "ql_injected": "badge", "ql_track": "mandatory", "ql_role": "exercise" }, "source": [ "\n", "
\n", "MANDATORY EXERCISE · Difficulty 3/10 · Official walkthrough runnable or written exercise.\n", "
\n" ], "id": "256f55de" }, { "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='Studio 1: Target Family',\n", " instructions='Build a small family of target-marked searches and defend the readability of your oracle implementation.',\n", " shots=256,\n", ")\n" ], "id": "3364ad7b" }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "META READING · Difficulty 1/10 · Notebook-level rule, objective, or usage guidance.\n", "
\n", "\n", "## Studio Prompt 2: Iteration Memo\n", "\n", "\n", " Compare one and two iterations as if you had to advise another engineer which to keep for this search size.\n" ], "id": "216b1747" }, { "cell_type": "markdown", "metadata": { "ql_injected": "badge", "ql_track": "mandatory", "ql_role": "exercise" }, "source": [ "\n", "
\n", "MANDATORY EXERCISE · Difficulty 3/10 · Official walkthrough runnable or written exercise.\n", "
\n" ], "id": "f726e04e" }, { "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 = \"10\") -> 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\\ndef grover_circuit(target: str = \"10\", iterations: int = 2) -> QuantumCircuit:\\n circuit = QuantumCircuit(2, 2)\\n circuit.h([0, 1])\\n for _ in range(iterations):\\n circuit.compose(grover_oracle(target), inplace=True)\\n circuit.compose(diffuser(), inplace=True)\\n circuit.measure([0, 1], [0, 1])\\n return circuit\\n\\ncircuit = grover_circuit(target=\"10\", iterations=2)\\n'\n", "editable_circuit_lab(\n", " initial_code=editable_code,\n", " context={\"QuantumCircuit\": QuantumCircuit, \"simulate_counts\": simulate_counts},\n", " title='Studio 2: Iteration Memo',\n", " instructions='Collect evidence for at least two iteration counts and write a recommendation tied to the small-search geometry.',\n", " shots=256,\n", ")\n" ], "id": "72a88076" }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "MANDATORY READING · Difficulty 3/10 · Official walkthrough reading cell.\n", "
\n", "\n", "## Studio Prompt 3: Oracle Review\n", "\n", "\n", " Rewrite or refactor the oracle helper until you are satisfied that a reviewer could check it quickly without guessing which state is marked.\n" ], "id": "760ccbad" }, { "cell_type": "markdown", "metadata": { "ql_injected": "badge", "ql_track": "mandatory", "ql_role": "exercise" }, "source": [ "\n", "
\n", "MANDATORY EXERCISE · Difficulty 3/10 · Official walkthrough runnable or written exercise.\n", "
\n" ], "id": "ed0949ad" }, { "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 = \"10\") -> 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\\ndef grover_circuit(target: str = \"10\", iterations: int = 2) -> QuantumCircuit:\\n circuit = QuantumCircuit(2, 2)\\n circuit.h([0, 1])\\n for _ in range(iterations):\\n circuit.compose(grover_oracle(target), inplace=True)\\n circuit.compose(diffuser(), inplace=True)\\n circuit.measure([0, 1], [0, 1])\\n return circuit\\n\\ncircuit = grover_circuit(target=\"10\", iterations=2)\\n'\n", "editable_circuit_lab(\n", " initial_code=editable_code,\n", " context={\"QuantumCircuit\": QuantumCircuit, \"simulate_counts\": simulate_counts},\n", " title='Studio 3: Oracle Review',\n", " instructions='Refactor the target-marking code for auditability and explain why the new version is preferable.',\n", " shots=256,\n", ")\n" ], "id": "35814c47" }, { "cell_type": "markdown", "metadata": { "ql_injected": "badge", "ql_track": "mandatory", "ql_role": "test" }, "source": [ "\n", "
\n", "MANDATORY TEST · Difficulty 3/10 · Official walkthrough multiple-choice test.\n", "
\n" ], "id": "81b9a6e6" }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true }, "tags": [ "hide-input" ] }, "outputs": [], "source": [ "quiz_block([{'prompt': 'What is a strong studio deliverable for Grover?', 'options': ['A notebook comparing target choices and iteration counts with a written design recommendation', 'A single run of the textbook circuit', 'A list of gate names with no explanation'], 'correct_index': 0, 'explanation': 'The studio should convert the routine into a defended design decision.'}, {'prompt': 'Why is a tiny search space pedagogically useful here?', 'options': ['Because the mechanism and the over-rotation risk are both easy to see locally', 'Because Grover works only on two qubits', 'Because the oracle no longer matters'], 'correct_index': 0, 'explanation': 'Small systems make the geometry visible.'}], heading='Studio Design Check')\n" ], "id": "c33ae767" }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "MANDATORY READING · Difficulty 3/10 · Official walkthrough reading cell.\n", "
\n", "\n", "## Studio Debrief\n", "\n", "\n", " A successful studio notebook here reads like a design note rather than like a performance. It should be compact, explicit, and willing to defend why one candidate was chosen over another. That is the right note on which to leave the algorithmic-design band.\n" ], "id": "086de0fb" }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "MANDATORY READING · Difficulty 3/10 · Official walkthrough reading cell.\n", "
\n", "\n", "## Studio Standard\n", "\n", "A strong studio notebook is compact, explicit, and reviewable. It does not hide behind volume. It makes clear what family of circuits was explored, what invariant mechanism survived across the variants, what evidence justified the final recommendation, and what tradeoffs remained open. If your notebook cannot answer those questions yet, keep refining it. That refinement is the studio.\n" ], "id": "c57f0a8b" }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "MANDATORY READING · Difficulty 3/10 · Official walkthrough reading cell.\n", "
\n", "\n", "## What A Finished Studio Should Feel Like\n", "\n", "The finished notebook should feel like a small engineering artifact rather than a scrapbook. A reviewer should be able to open it, understand the task, inspect the candidate circuits, see the evidence, and understand why one recommendation won. If that standard is met on these small modules, later capstone work becomes much more realistic.\n" ], "id": "df448163" }, { "cell_type": "markdown", "metadata": { "ql_injected": "badge", "ql_track": "mandatory", "ql_role": "exercise" }, "source": [ "\n", "
\n", "MANDATORY EXERCISE · Difficulty 3/10 · Official walkthrough runnable or written exercise.\n", "
\n" ], "id": "c5a957cd" }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true }, "tags": [ "hide-input" ] }, "outputs": [], "source": [ "reflection_box('Which target family did you compare, and what did that reveal about your oracle helper?')\n" ], "id": "a3fb6757" }, { "cell_type": "markdown", "metadata": { "ql_injected": "badge", "ql_track": "mandatory", "ql_role": "exercise" }, "source": [ "\n", "
\n", "MANDATORY EXERCISE · Difficulty 3/10 · Official walkthrough runnable or written exercise.\n", "
\n" ], "id": "16f3fa60" }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true }, "tags": [ "hide-input" ] }, "outputs": [], "source": [ "reflection_box('What iteration recommendation did you make, and what evidence mattered most?')\n" ], "id": "79a5c617" }, { "cell_type": "markdown", "metadata": { "ql_injected": "badge", "ql_track": "mandatory", "ql_role": "exercise" }, "source": [ "\n", "
\n", "MANDATORY EXERCISE · Difficulty 3/10 · Official walkthrough runnable or written exercise.\n", "
\n" ], "id": "6e989ff6" }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true }, "tags": [ "hide-input" ] }, "outputs": [], "source": [ "reflection_box('How did you explain the diffuser in your own words in the final notebook?')\n" ], "id": "7c0a28ba" }, { "cell_type": "markdown", "metadata": { "ql_injected": "badge", "ql_track": "mandatory", "ql_role": "exercise" }, "source": [ "\n", "
\n", "MANDATORY EXERCISE · Difficulty 3/10 · Official walkthrough runnable or written exercise.\n", "
\n" ], "id": "381771e5" }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true }, "tags": [ "hide-input" ] }, "outputs": [], "source": [ "reflection_box('Name one habit from this algorithmic-design band that should carry into the professional-design band.')\n" ], "id": "19321ce6" }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "META READING · Difficulty 1/10 · Official walkthrough guardrail.\n", "
\n", "\n", "\n", "## What To Open Next\n", "\n", "Next notebook: [Qiskit Patterns and Workflow Design Lecture](../../professional/module_01_qiskit_patterns/lecture.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": "5eea683b" }, { "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 Studio**. Everything below is optional. Use it only if you want deeper consolidation or extra transfer work.\n" ], "id": "fef497ee" }, { "cell_type": "markdown", "metadata": { "ql_injected": "facultative", "ql_track": "facultative", "ql_role": "reading", "ql_difficulty": 5, "ql_note": "Optional extension reading." }, "source": [ "\n", "
\n", "FACULTATIVE READING · Difficulty 5/10 · Optional extension reading.\n", "
\n", "\n", "## Facultative Extension Reading\n", "\n", "Use this optional studio extension only if the mandatory route in **Grover and Amplitude Amplification Studio** feels stable. The right stretch here is not random complexity; it is one extra candidate, one sharper criterion, and one clearer written defence of the final decision.\n" ], "id": "fdbeb292" }, { "cell_type": "markdown", "metadata": { "ql_injected": "badge", "ql_track": "facultative", "ql_role": "test" }, "source": [ "\n", "
\n", "FACULTATIVE TEST · Difficulty 6/10 · Optional multiple-choice extension.\n", "
\n" ], "id": "00503cf5" }, { "cell_type": "code", "execution_count": null, "metadata": { "ql_injected": "facultative", "ql_track": "facultative", "ql_role": "test", "ql_difficulty": 6, "ql_note": "Optional multiple-choice extension.", "jupyter": { "source_hidden": true }, "tags": [ "hide-input" ] }, "outputs": [], "source": [ "quiz_block([{'prompt': 'What makes a facultative studio stretch worthwhile?', 'options': ['It sharpens a design judgement without breaking the single mandatory route', 'It replaces the mandatory brief', 'It adds complexity with no explicit criterion'], 'correct_index': 0, 'explanation': 'A studio stretch should sharpen judgement, not blur the route.'}, {'prompt': 'If a stretch candidate fails, what should change first?', 'options': ['The written explanation of the failure and the next criterion', 'The notebook order', 'The insistence that the failed candidate was still best'], 'correct_index': 0, 'explanation': 'The useful move is clearer review language and a better criterion.'}], heading='Facultative Extension Test')\n" ], "id": "ff99b292" }, { "cell_type": "markdown", "metadata": { "ql_injected": "badge", "ql_track": "facultative", "ql_role": "exercise" }, "source": [ "\n", "
\n", "FACULTATIVE EXERCISE · Difficulty 7/10 · Optional written exercise.\n", "
\n" ], "id": "3f713f96" }, { "cell_type": "code", "execution_count": null, "metadata": { "ql_injected": "facultative", "ql_track": "facultative", "ql_role": "exercise", "ql_difficulty": 7, "ql_note": "Optional written exercise.", "jupyter": { "source_hidden": true }, "tags": [ "hide-input" ] }, "outputs": [], "source": [ "reflection_box('Write one optional stretch brief for Grover and Amplitude Amplification Studio: objective, criterion, and the single risk that would matter most in your design review.')\n" ], "id": "e1f5bf02" } ], "metadata": { "kernelspec": { "display_name": "QuantumLearning (.venv)", "language": "python", "name": "quantum-learning" }, "language_info": { "name": "python", "version": "3.12" } }, "nbformat": 4, "nbformat_minor": 5 }