mirror of
https://github.com/saymrwulf/QuantumLearning.git
synced 2026-05-22 22:01:20 +00:00
255 lines
12 KiB
Text
255 lines
12 KiB
Text
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Grover and Amplitude Amplification Studio\n"
|
|
],
|
|
"id": "c16f3557"
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"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": "c04f60b7"
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## 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": "989a8dd7"
|
|
},
|
|
{
|
|
"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": "3e7046de"
|
|
},
|
|
{
|
|
"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": "319d85e5"
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## 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": "b1ba7156"
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"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": "8c846877"
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## 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": "addd7131"
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"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": "16aafc62"
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## 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": "b45a635b"
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"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": "c3589d88"
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"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": "c9906c1c"
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## 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": "59158b2b"
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## 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": "cb73a53e"
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## 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": "ae01c89c"
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"reflection_box('Which target family did you compare, and what did that reveal about your oracle helper?')\n"
|
|
],
|
|
"id": "6773ebde"
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"reflection_box('What iteration recommendation did you make, and what evidence mattered most?')\n"
|
|
],
|
|
"id": "976a60c6"
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"reflection_box('How did you explain the diffuser in your own words in the final notebook?')\n"
|
|
],
|
|
"id": "0662a939"
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"reflection_box('Name one habit from this algorithmic-design band that should carry into the professional-design band.')\n"
|
|
],
|
|
"id": "09ac2a16"
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "QuantumLearning (.venv)",
|
|
"language": "python",
|
|
"name": "quantum-learning"
|
|
},
|
|
"language_info": {
|
|
"name": "python",
|
|
"version": "3.12"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|