QuantumLearning/notebooks/qiskit_engineering/module_02_transpilation_and_visualization/problems.ipynb

229 lines
12 KiB
Text

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Transpilation and Visualization Problems\n",
"\n",
"This notebook checks whether compile-time thinking is becoming explicit. The questions focus on constraints, routing pressure, metric interpretation, and redesign judgment.\n"
],
"id": "6ba7b96e"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## How To Use This Notebook\n",
"\n",
"Answer as if you were preparing short review comments for another engineer. The goal is not only to know facts about transpilation but to speak about compile-time consequences precisely.\n"
],
"id": "22cbf72a"
},
{
"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": "a84bcf22"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from quantum_learning import (\n",
" build_demo_noise_model,\n",
" counts_to_probabilities,\n",
" draw_circuit,\n",
" editable_circuit_lab,\n",
" line_coupling_map,\n",
" load_experiment_defaults,\n",
" plot_counts,\n",
" plot_probabilities,\n",
" quiz_block,\n",
" reflection_box,\n",
" simulate_counts,\n",
" statevector_probabilities,\n",
" step_reference_table,\n",
" transpile_summary,\n",
")\n",
"from qiskit import ClassicalRegister, QuantumCircuit, QuantumRegister\n",
"from qiskit.providers.basic_provider import BasicSimulator\n"
],
"id": "ef0ee68d"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Transpilation Problem Set A\n",
"\n",
"Treat these quizzes as compact design-review drills. You are practicing the sentences that make compiled circuits intelligible.\n"
],
"id": "d26f3507"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"quiz_block([{'prompt': 'What does basis-gate restriction usually force?', 'options': ['Decomposition of higher-level operations into the target gate vocabulary', 'Automatic noise mitigation', 'Fewer measurements'], 'correct_index': 0, 'explanation': 'Basis-gate sets are part of the implementation contract.'}, {'prompt': 'What is the main beginner danger in transpilation work?', 'options': ['Treating the compiled circuit as arbitrary instead of as a response to explicit constraints', 'Using a coupling map at all', 'Plotting the compiled diagram'], 'correct_index': 0, 'explanation': 'The compiled rewrite should always be read as a constrained response.'}, {'prompt': 'Why compare depth before and after compilation?', 'options': ['To quantify part of the cost of fitting the circuit to the target constraints', 'Because depth determines the statevector size directly', 'Because depth replaces code review'], 'correct_index': 0, 'explanation': 'Depth is one concrete lens on compile-time inflation.'}], heading='Transpilation Problem Set A')\n"
],
"id": "6b3a71de"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Transpilation Problem Set B\n",
"\n",
"Treat these quizzes as compact design-review drills. You are practicing the sentences that make compiled circuits intelligible.\n"
],
"id": "f9b7ae05"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"quiz_block([{'prompt': 'What does a line coupling map make expensive?', 'options': ['Long-range interactions that skip intermediate qubits', 'Single-qubit gates', 'Classical registers'], 'correct_index': 0, 'explanation': 'Non-local interactions create routing pressure on a line.'}, {'prompt': 'Why is a compiled diagram worth discussing with prose?', 'options': ['Because the picture alone does not tell you which cost came from basis conversion and which came from routing', 'Because prose replaces the diagram', 'Because the compiled circuit never changes the metrics'], 'correct_index': 0, 'explanation': 'The review story should tie visible changes back to their causes.'}, {'prompt': 'What is the point of a topology-aware redesign exercise?', 'options': ['To compare compiler rescue with human foresight', 'To avoid compilation entirely', 'To force all circuits into identical shapes'], 'correct_index': 0, 'explanation': 'This is exactly where design judgment starts to matter.'}], heading='Transpilation Problem Set B')\n"
],
"id": "4ec29fff"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Transpilation Problem Set C\n",
"\n",
"Treat these quizzes as compact design-review drills. You are practicing the sentences that make compiled circuits intelligible.\n"
],
"id": "b67d73a7"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"quiz_block([{'prompt': 'What should a good transpilation memo include?', 'options': ['Constraint set, pre/post metrics, and the main structural reason for the rewrite', 'Only the final counts', 'Only the number of qubits'], 'correct_index': 0, 'explanation': 'The memo should make the compile-time story auditable.'}, {'prompt': 'Why is optimization level a teaching lever here?', 'options': ['Because it shows that compilation choices affect the final structure and cost', 'Because it changes the algorithmic goal itself', 'Because it disables two-qubit gates'], 'correct_index': 0, 'explanation': 'Optimization level is part of the engineering design space.'}, {'prompt': 'What is the danger of ignoring the compiled circuit and looking only at counts?', 'options': ['You may miss severe structural inflation that matters for any realistic implementation', 'Counts already encode the full coupling map', 'There is no danger if the output looks correct'], 'correct_index': 0, 'explanation': 'Functional correctness is not the whole implementation story.'}], heading='Transpilation Problem Set C')\n"
],
"id": "86618300"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Transpilation Problem Set D\n",
"\n",
"Treat these quizzes as compact design-review drills. You are practicing the sentences that make compiled circuits intelligible.\n"
],
"id": "006e283f"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"quiz_block([{'prompt': 'What is the strongest sentence about transpilation maturity?', 'options': ['I can explain what the compiler changed, why it changed it, and what I would redesign upstream', 'I can run transpile without errors', 'I can recognize a SWAP symbol'], 'correct_index': 0, 'explanation': 'Maturity means explanatory control and redesign judgment.'}, {'prompt': 'Which comparison is most useful after compilation?', 'options': ['Abstract intent versus compiled form under stated constraints', 'Markdown versus code cell counts', 'Notebook title versus file path'], 'correct_index': 0, 'explanation': 'The entire point is to compare intent with constrained realization.'}, {'prompt': 'What should happen after you identify avoidable routing pressure?', 'options': ['Propose a topology-aware alternative and compare the metrics honestly', 'Ignore it because the transpiler handled it', 'Delete the coupling map'], 'correct_index': 0, 'explanation': 'The engineering response is redesign, not resignation.'}], heading='Transpilation Problem Set D')\n"
],
"id": "27188d8b"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Mini Case: Correct Output, Expensive Realization\n",
"\n",
"One of the central lessons of this module is that functional correctness does not end the engineering discussion. Two circuits can implement the same logical objective and even produce the same sampled evidence while having very different compile-time cost. If you ignore that, you will think the transpiler is only a performance detail. It is not. It is part of the design story.\n",
"\n",
"The quizzes above are therefore not only about definitions. They are practice in keeping implementation cost visible even when the logical story stayed stable. That distinction is one of the main differences between being able to run circuits and being able to engineer them.\n"
],
"id": "7653d93c"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Mini Case: Same Intent, Better Topology Cooperation\n",
"\n",
"A useful transpilation exercise is to imagine two teammates proposing circuits for the same local objective. One writes the conceptually shortest abstract circuit and leaves the routing burden to the compiler. The other writes a slightly more topology-aware version that is less elegant at first glance but compiles more gently. Which one is better? The only serious answer is: compare them under the same constraints and state the tradeoff honestly.\n",
"\n",
"This mini case is here to reinforce that transpilation reasoning is not about punishing abstraction. It is about deciding when abstract elegance and physical cooperation pull in different directions and writing down that tension clearly.\n"
],
"id": "8dc58fcf"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Written Checks\n",
"\n",
"Use the prompts below to rehearse the core explanatory loop of this module: state the constraints, describe the rewrite, and justify the redesign recommendation.\n"
],
"id": "a0647f83"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"reflection_box('Explain why the same logical circuit can have very different compiled forms under different topologies or optimization levels.')\n"
],
"id": "4b51a468"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"reflection_box('Write a short review note on when accepting the transpiler output is good enough and when upstream redesign is the stronger choice.')\n"
],
"id": "b166c697"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exit Condition\n",
"\n",
"Move on only when you can read a compiled circuit as a constrained implementation, not as unexplained visual clutter.\n"
],
"id": "f3700444"
}
],
"metadata": {
"kernelspec": {
"display_name": "QuantumLearning (.venv)",
"language": "python",
"name": "quantum-learning"
},
"language_info": {
"name": "python",
"version": "3.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}