QuantumLearning/notebooks/algorithms/module_02_bernstein_vazirani/problems.ipynb

272 lines
11 KiB
Text

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Bernstein-Vazirani and Structured Oracles Problems\n"
],
"id": "0ab134db"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<!-- COURSE_NAV_TOP -->\n",
"## Mainline Navigation\n",
"\n",
"Step 33 of 59. Follow the mainline in order and do not skip ahead.\n",
"\n",
"Previous notebook: [Bernstein-Vazirani and Structured Oracles Lab](lab.ipynb)\n",
"\n",
"Next notebook: [Bernstein-Vazirani and Structured Oracles Studio](studio.ipynb)\n",
"\n",
"Rule: finish this notebook top-to-bottom before you open the next one.\n"
],
"id": "97687d04"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"These problems test whether you can maintain family-level reasoning in the presence of tempting shortcuts. Treat them like short design-review scenarios rather than isolated exam items.\n"
],
"id": "8097b551"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## How To Use This Notebook\n",
"\n",
"\n",
" The correct answers are the ones that preserve structure, interface clarity, and evidence all at once. If an option sounds attractive because it ignores one of those burdens, it is probably setting up a later failure.\n"
],
"id": "657a812e"
},
{
"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 Structure\n",
"\n",
"Treat this block as a miniature review situation. Choose the answer that would best survive an engineering conversation.\n"
],
"id": "83699e9f"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"quiz_block([{'prompt': 'Which change directly changes the hidden string implemented by a BV oracle?', 'options': ['Changing which query wires control the ancilla', 'Renaming the classical register', 'Adding a plot title'], 'correct_index': 0, 'explanation': 'The secret is the connectivity pattern into the ancilla.'}, {'prompt': 'What is the strongest description of the BV oracle?', 'options': ['A structured parity-like query on the input bits', 'A random entangling layer', 'A measurement-only gadget'], 'correct_index': 0, 'explanation': 'BV is organized around a structured linear rule.'}], heading='Oracle Structure')\n"
],
"id": "97e47cc3"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Bit Order And Reporting\n",
"\n",
"Treat this block as a miniature review situation. Choose the answer that would best survive an engineering conversation.\n"
],
"id": "ce6858f0"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"quiz_block([{'prompt': 'Which statement is most defensible about bit order?', 'options': ['It can be ignored as long as counts look stable', 'It is part of the API between the circuit and the reader', 'It matters only in cloud execution'], 'correct_index': 1, 'explanation': 'If the learner cannot defend the mapping, the result is not well engineered.'}, {'prompt': 'What kind of bug is a reversed-secret report?', 'options': ['An interface bug that can mask a correct oracle', 'A hardware-only bug', 'A plotting bug with no circuit significance'], 'correct_index': 0, 'explanation': 'Interface bugs are still design bugs.'}], heading='Bit Order And Reporting')\n"
],
"id": "a540a650"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Reusable Motif\n",
"\n",
"Treat this block as a miniature review situation. Choose the answer that would best survive an engineering conversation.\n"
],
"id": "f4af2502"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"quiz_block([{'prompt': 'What is the professional value of recognizing BV as a motif?', 'options': ['You can reuse its structure when a task asks for hidden linear information in a circuit', 'You can stop learning other algorithms', 'You can avoid using classical post-processing forever'], 'correct_index': 0, 'explanation': 'The goal is transfer, not mere memorization.'}, {'prompt': 'What survives when the secret string changes?', 'options': ['The global circuit pattern and decoding logic', 'Only the exact same oracle wiring', 'The need to measure the ancilla'], 'correct_index': 0, 'explanation': 'The family structure is what matters.'}], heading='Reusable Motif')\n"
],
"id": "2cee6175"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Review Judgement\n",
"\n",
"Treat this block as a miniature review situation. Choose the answer that would best survive an engineering conversation.\n"
],
"id": "07b2929b"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"quiz_block([{'prompt': 'Which review note is strongest?', 'options': ['The code runs, so the oracle is fine', 'The secret-bit mapping is hidden behind index tricks, so the builder is hard to verify', 'Please use more barriers so it looks quantum'], 'correct_index': 1, 'explanation': 'Review should target what makes the circuit auditable or opaque.'}, {'prompt': 'What evidence should accompany a claim that a BV notebook is correct?', 'options': ['Several secrets, explicit oracle definitions, and counts matched against the intended reporting order', 'A single screenshot of one circuit', 'Only a statevector plot with no explanation'], 'correct_index': 0, 'explanation': 'Correctness here includes structural, interface, and empirical evidence.'}], heading='Review Judgement')\n"
],
"id": "6fca0817"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Mini Case\n",
"\n",
"\n",
" A recurring anti-pattern in quantum notebooks is the 'works for my secret' builder. It runs for one hand-picked example, but it is too opaque or too brittle to support variation. That anti-pattern is especially dangerous in a module like Bernstein-Vazirani because the whole point is that a clean family builder should exist. The problems notebook is therefore asking for something stronger than one successful run. It is asking whether your explanation survives secret changes, interface changes, and review pressure.\n"
],
"id": "27c0493d"
},
{
"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 family-level testing is more persuasive than a single successful BV example.')\n"
],
"id": "8e3f4449"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"reflection_box('Describe a minimal checklist you would use before trusting a hidden-string notebook written by someone else.')\n"
],
"id": "8e64163c"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exit Condition\n",
"\n",
"\n",
" Move on when the phrase hidden string makes you think first of oracle structure and reporting contract, not of a memorized output.\n"
],
"id": "19b4ab5d"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<!-- COURSE_NAV_BOTTOM -->\n",
"## What To Open Next\n",
"\n",
"Next notebook: [Bernstein-Vazirani and Structured Oracles Studio](studio.ipynb)\n",
"\n",
"When you finish this notebook, open the next notebook shown above. Stay on the guarded mainline route.\n"
],
"id": "07ccdbad"
}
],
"metadata": {
"kernelspec": {
"display_name": "QuantumLearning (.venv)",
"language": "python",
"name": "quantum-learning"
},
"language_info": {
"name": "python",
"version": "3.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}