mirror of
https://github.com/saymrwulf/QuantumLearning.git
synced 2026-05-28 22:56:46 +00:00
274 lines
12 KiB
Text
274 lines
12 KiB
Text
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Transpilation and Visualization Studio\n",
|
|
"\n",
|
|
"The studio asks you to act like a hardware-aware reviewer. You will compare hostile and cooperative designs under explicit constraints and write the kind of redesign memo that later bands will depend on.\n"
|
|
],
|
|
"id": "a39456c9"
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Studio Standard\n",
|
|
"\n",
|
|
"Every answer should state the abstract objective, the constraint set, and the evidence for why one implementation path is better than another. If any one of those is missing, the redesign claim is too soft.\n"
|
|
],
|
|
"id": "70535bc9"
|
|
},
|
|
{
|
|
"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": [
|
|
"## Brief 1: Rescue Versus Redesign\n",
|
|
"\n",
|
|
"Start from the topology-hostile abstract circuit and write the memo you would leave after seeing the compiled result.\n"
|
|
],
|
|
"id": "6a334328"
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"defaults = load_experiment_defaults()\n",
|
|
"constrained_counts = lambda circuit, shots=256: simulate_counts(\n",
|
|
" circuit,\n",
|
|
" shots=shots,\n",
|
|
" basis_gates=list(defaults.transpile.basis_gates),\n",
|
|
" coupling_map=line_coupling_map(circuit.num_qubits),\n",
|
|
")\n",
|
|
"\n",
|
|
"editable_code = '\\nfrom qiskit import QuantumCircuit\\nfrom qiskit.providers.basic_provider import BasicSimulator\\nfrom quantum_learning import line_coupling_map, load_experiment_defaults, transpile_summary\\n\\ndefaults = load_experiment_defaults()\\n\\nabstract = QuantumCircuit(4, 4)\\n# [1] Start from a circuit with routing pressure.\\nabstract.h(0)\\nabstract.cx(0, 3)\\nabstract.cx(3, 1)\\nabstract.measure([0, 1, 2, 3], [0, 1, 2, 3])\\n\\n# [2] Compile to IBM-style basis gates on a line.\\nsummary = transpile_summary(\\n abstract,\\n BasicSimulator(),\\n basis_gates=list(defaults.transpile.basis_gates),\\n coupling_map=line_coupling_map(4),\\n optimization_level=0,\\n)\\n\\n# [3] Render the compiled circuit directly.\\ncircuit = summary[\"compiled_circuit\"]\\n'\n",
|
|
"editable_circuit_lab(\n",
|
|
" initial_code=editable_code,\n",
|
|
" context={\"QuantumCircuit\": QuantumCircuit, \"simulate_counts\": constrained_counts},\n",
|
|
" title='Studio Brief 1: Rescue Versus Redesign',\n",
|
|
" instructions='Inspect the compiled form and decide whether you would accept the rescue or redesign the abstract circuit.',\n",
|
|
" shots=256,\n",
|
|
")\n"
|
|
],
|
|
"id": "9ec24407"
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"reflection_box('What exactly did the compiler have to rescue, and what upstream change would reduce that burden?')\n"
|
|
],
|
|
"id": "e7712858"
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Brief 2: Cooperative Rewrite\n",
|
|
"\n",
|
|
"Build the topology-aware version and compare it honestly to the compiler-rescued one.\n"
|
|
],
|
|
"id": "e9d1c5e8"
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"defaults = load_experiment_defaults()\n",
|
|
"constrained_counts = lambda circuit, shots=256: simulate_counts(\n",
|
|
" circuit,\n",
|
|
" shots=shots,\n",
|
|
" basis_gates=list(defaults.transpile.basis_gates),\n",
|
|
" coupling_map=line_coupling_map(circuit.num_qubits),\n",
|
|
")\n",
|
|
"\n",
|
|
"editable_code = '\\nfrom qiskit import QuantumCircuit\\n\\ncircuit = QuantumCircuit(4, 4)\\n# [1] Build a version that respects the line from the start.\\ncircuit.h(0)\\ncircuit.cx(0, 1)\\ncircuit.cx(1, 2)\\ncircuit.cx(2, 3)\\n# [2] Keep the final question fixed while you compare cost.\\ncircuit.measure([0, 1, 2, 3], [0, 1, 2, 3])\\n'\n",
|
|
"editable_circuit_lab(\n",
|
|
" initial_code=editable_code,\n",
|
|
" context={\"QuantumCircuit\": QuantumCircuit, \"simulate_counts\": constrained_counts},\n",
|
|
" title='Studio Brief 2: Cooperative Rewrite',\n",
|
|
" instructions='Tune the local-chain design and explain whether it preserves the objective while reducing compile-time pain.',\n",
|
|
" shots=256,\n",
|
|
")\n"
|
|
],
|
|
"id": "bc394736"
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"reflection_box('What did your redesign gain, and what did it potentially give up?')\n"
|
|
],
|
|
"id": "b0c7fb94"
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Brief 3: Optimization-Level Judgment\n",
|
|
"\n",
|
|
"Use the compiled-render widget again, but this time focus on optimization-level tradeoffs and the review language they require.\n"
|
|
],
|
|
"id": "6b887407"
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"defaults = load_experiment_defaults()\n",
|
|
"constrained_counts = lambda circuit, shots=256: simulate_counts(\n",
|
|
" circuit,\n",
|
|
" shots=shots,\n",
|
|
" basis_gates=list(defaults.transpile.basis_gates),\n",
|
|
" coupling_map=line_coupling_map(circuit.num_qubits),\n",
|
|
")\n",
|
|
"\n",
|
|
"editable_code = '\\nfrom qiskit import QuantumCircuit\\nfrom qiskit.providers.basic_provider import BasicSimulator\\nfrom quantum_learning import line_coupling_map, load_experiment_defaults, transpile_summary\\n\\ndefaults = load_experiment_defaults()\\n\\nabstract = QuantumCircuit(4, 4)\\n# [1] Start from a circuit with routing pressure.\\nabstract.h(0)\\nabstract.cx(0, 3)\\nabstract.cx(3, 1)\\nabstract.measure([0, 1, 2, 3], [0, 1, 2, 3])\\n\\n# [2] Compile to IBM-style basis gates on a line.\\nsummary = transpile_summary(\\n abstract,\\n BasicSimulator(),\\n basis_gates=list(defaults.transpile.basis_gates),\\n coupling_map=line_coupling_map(4),\\n optimization_level=0,\\n)\\n\\n# [3] Render the compiled circuit directly.\\ncircuit = summary[\"compiled_circuit\"]\\n'\n",
|
|
"editable_circuit_lab(\n",
|
|
" initial_code=editable_code,\n",
|
|
" context={\"QuantumCircuit\": QuantumCircuit, \"simulate_counts\": constrained_counts},\n",
|
|
" title='Studio Brief 3: Optimization Judgment',\n",
|
|
" instructions=\"Adjust the optimization level or the abstract structure and defend the compiled result you would ship for this lesson's local target.\",\n",
|
|
" shots=256,\n",
|
|
")\n"
|
|
],
|
|
"id": "93bfd054"
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"quiz_block([{'prompt': 'What is the core job of the transpilation studio?', 'options': ['Compare compiler rescue with human redesign under explicit constraints', 'Avoid compiled diagrams', 'Reduce everything to one metric'], 'correct_index': 0, 'explanation': 'The studio asks for engineering judgment, not only observation.'}, {'prompt': 'When is a redesign claim convincing?', 'options': ['When it states the objective, the constraints, and the evidence for why the new structure is better', 'When the circuit looks simpler at first glance', 'When it uses fewer comments'], 'correct_index': 0, 'explanation': 'Convincing redesign requires a reviewable argument.'}], heading='Transpilation Studio Check')\n"
|
|
],
|
|
"id": "d712a5a4"
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"reflection_box('What is the main transpilation smell you can now recognize in an abstract circuit before compiling it?')\n"
|
|
],
|
|
"id": "342611cc"
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"reflection_box('Which redesign judgment in this module still feels tentative, and what extra comparison would strengthen it?')\n"
|
|
],
|
|
"id": "371908e7"
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Studio Rubric\n",
|
|
"\n",
|
|
"Use a strict rubric here. A strong studio answer states the objective without ambiguity, states the constraint set explicitly, shows the relevant pre- and post-compile evidence, and gives a redesign recommendation that another engineer could actually test. A weak answer says only that one circuit \"looks cleaner\" or \"seems more hardware aware.\" The studio exists to eliminate that kind of soft language.\n",
|
|
"\n",
|
|
"This rubric matters because later hardware-aware modules will assume you can already argue about compile-time tradeoffs in review language. The current studio is where that language gets installed.\n"
|
|
],
|
|
"id": "4e7c93bb"
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Studio Review Standard\n",
|
|
"\n",
|
|
"Before you leave this notebook, ask whether your memo would still make sense to a reader who never saw your intermediate experiments. If the answer is no, then the memo is still too dependent on the notebook context. A good engineering memo should survive outside the cell stream. It should say enough about the objective, constraints, and cost story that another person could reproduce the comparison or challenge your conclusion intelligently.\n",
|
|
"\n",
|
|
"That standard is intentionally high. The whole point of the studio is to move from personal experimentation toward shared engineering language.\n"
|
|
],
|
|
"id": "459003a4"
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Studio Exit\n",
|
|
"\n",
|
|
"A strong outcome is a short redesign memo that another engineer could act on: it states the target, the constraints, the cost, and the reason the chosen rewrite is preferable.\n"
|
|
],
|
|
"id": "ecc95ad1"
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "QuantumLearning (.venv)",
|
|
"language": "python",
|
|
"name": "quantum-learning"
|
|
},
|
|
"language_info": {
|
|
"name": "python",
|
|
"version": "3.12"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|