{ "cells": [ { "cell_type": "markdown", "id": "157f8340", "metadata": {}, "source": [ "# Transpiler\n", "\n", "The {func}`~.generate_boxing_pass_manager` function is a flexible and convenient tool to\n", "build a {class}`qiskit.transpiler.PassManager` able to group the circuit instructions into annotated boxes.\n", "Whether you want to automate your workflow, you are interested in trying different boxing strategies for your\n", "circuits, or you simply want a quick and easy alternative to grouping and annotating by hand,\n", "{func}`~.generate_boxing_pass_manager` can help you achieve your goals.\n", "\n", "This guide illustrates how to use {func}`~.generate_boxing_pass_manager` and its arguments.\n", "To highlight the effects of each of the function's arguments, in the sections that follow we will mainly target\n", "the circuit below." ] }, { "cell_type": "code", "execution_count": null, "id": "0fd32ec9", "metadata": {}, "outputs": [], "source": [ "from qiskit.circuit import Parameter, QuantumCircuit\n", "\n", "circuit = QuantumCircuit(4, 7)\n", "circuit.h(1)\n", "circuit.h(2)\n", "circuit.cz(1, 2)\n", "circuit.h(1)\n", "circuit.cx(1, 0)\n", "circuit.cx(2, 3)\n", "circuit.measure(range(1, 4), range(3))\n", "circuit.cx(0, 1)\n", "circuit.cx(1, 2)\n", "circuit.cx(2, 3)\n", "for qubit in range(4):\n", " circuit.rz(Parameter(f\"th_{qubit}\"), qubit)\n", " circuit.rx(Parameter(f\"phi_{qubit}\"), qubit)\n", " circuit.rz(Parameter(f\"lam_{qubit}\"), qubit)\n", "circuit.measure(range(4), range(3, 7))\n", "\n", "circuit.draw(\"mpl\", scale=0.8)" ] }, { "cell_type": "markdown", "id": "68eb58a7", "metadata": {}, "source": [ "## Group operations into boxes\n", "\n", "The argument ``enable_gates`` can be set to ``True`` or ``False`` to specify whether the two-qubit gates should be grouped into\n", "boxes. Similarly, ``enable_measures`` allows specifying whether or not measurements should be grouped. The following\n", "snippet shows an example where both gates and measurements are grouped." ] }, { "cell_type": "code", "execution_count": null, "id": "131713bb", "metadata": {}, "outputs": [], "source": [ "from samplomatic.transpiler import generate_boxing_pass_manager\n", "\n", "boxing_pass_manager = generate_boxing_pass_manager(\n", " enable_gates=True,\n", " enable_measures=True,\n", ")\n", "transpiled_circuit = boxing_pass_manager.run(circuit)\n", "transpiled_circuit.draw(\"mpl\", scale=0.8)" ] }, { "cell_type": "markdown", "id": "44fa764d", "metadata": {}, "source": [ "As can be seen in the figure, the pass manager creates boxes of two types: those that contain a single layer of\n", "two-qubit gates, and those that contain a single layer of measurements. This separation reflects standard practices\n", "in noise learning and mitigation protocols, which usually target layers of homogeneous operations. The two-qubit gates\n", "and measurements are placed in the leftmost box that can accommodate them, and every single-qubit gates is placed in\n", "the same box as the two-qubit gate or measurement they preceed.\n", "\n", "The following snippet shows another example where ``enable_gates`` is set to ``False``. As can be seen, the two-qubit\n", "gates are not grouped into boxes, nor are the single-qubit gates that preceed them." ] }, { "cell_type": "code", "execution_count": null, "id": "8d71e0ae", "metadata": {}, "outputs": [], "source": [ "boxing_pass_manager = generate_boxing_pass_manager(\n", " enable_gates=False,\n", " enable_measures=True,\n", ")\n", "transpiled_circuit = boxing_pass_manager.run(circuit)\n", "transpiled_circuit.draw(\"mpl\", scale=0.8)" ] }, { "cell_type": "markdown", "id": "ef3b1822", "metadata": {}, "source": [ "## Choose how to annotate your boxes\n", "\n", "All the two-qubit gates and measurement boxes in the returned circuit own left-dressed annotations. In particular,\n", "all the boxes that contain two-qubit gates are annotated with a {class}`.~Twirl`, while for measurement boxes, users can\n", "choose between {class}`.~Twirl`, {class}`.~BasisTranform` (with ``mode`` aset to ``\"measure\"``), or both. The following code\n", "generates a circuit where the all the boxes are twirled, and the measurement boxes are additionally annotated with\n", "{class}`.~BasisTranform`." ] }, { "cell_type": "code", "execution_count": null, "id": "4463773b", "metadata": {}, "outputs": [], "source": [ "boxing_pass_manager = generate_boxing_pass_manager(\n", " enable_gates=True,\n", " enable_measures=True,\n", " measure_annotations=\"all\",\n", ")\n", "transpiled_circuit = boxing_pass_manager.run(circuit)" ] }, { "cell_type": "markdown", "id": "93aaa255", "metadata": {}, "source": [ "## Prepare your circuit for noise injection\n", "\n", "The ``inject_noise_targets`` allows specifying what boxes should receive an {class}`.~InjectNoise` annotation. As an example,\n", "the following snippet generates a circuit where the two-qubit gates boxes own an {class}`.~InjectNoise` annotation but the\n", "measurement boxes do not." ] }, { "cell_type": "code", "execution_count": null, "id": "3938bee5", "metadata": {}, "outputs": [], "source": [ "boxing_pass_manager = generate_boxing_pass_manager(\n", " enable_gates=True,\n", " enable_measures=True,\n", " inject_noise_targets=\"gates\",\n", ")\n", "transpiled_circuit = boxing_pass_manager.run(circuit)" ] }, { "cell_type": "markdown", "id": "78eb28df", "metadata": {}, "source": [ "If a circuit contains two or more boxes that are equivalent up to single-qubit gates, all of them are annotated with\n", "an {class}`.~InjectNoise` annotation with the same ``ref``. Thus, the number of unique ``ref``s in the returned\n", "circuit is equal to the number of unique boxes, with uniqueness defined up to single-qubit gates.\n", "\n", "By selecting the appropriate value for ``inject_noise_strategy``, users can decide whether the {class}`.~InjectNoise` annotations\n", "should have:\n", "\n", "* ``modifier_ref=''``, recommended when modifying the noise maps prior to sampling from them is not required,\n", "* ``modifier_ref=ref``, recommended when all the noise maps need to be scaled uniformly by the same factor, or\n", "* a unique value of ``modifier_ref``, recommended when every noise map needs to be scaled by a different factor.\n", "\n", "The following code generates a circuit where the two-qubit gates boxes own an {class}`.~InjectNoise` annotation with unique\n", "values of ``modifier_ref``." ] }, { "cell_type": "code", "execution_count": null, "id": "0074b400", "metadata": {}, "outputs": [], "source": [ "boxing_pass_manager = generate_boxing_pass_manager(\n", " enable_gates=True,\n", " enable_measures=True,\n", " inject_noise_targets=\"gates\",\n", " inject_noise_strategy=\"individual_modification\",\n", ")\n", "transpiled_circuit = boxing_pass_manager.run(circuit)" ] }, { "cell_type": "markdown", "id": "41210a41", "metadata": {}, "source": [ "## Select a twirling strategy\n", "\n", "The boxing pass manager begins by choosing which entangling gates and measurement instructions should appear in the same boxes.\n", "Once this is accomplished, boxes will not, in general, be full-width---they will likely include only a subset of the qubits that are active in the circuit or present in the backend target.\n", "In typical configurations of the boxing pass manager, since all qubits a box instruction acts on are twirled (even those for which no instruction inside the box act on), having partial-width boxes affects the noise-tailoring effects of twirling; the idling qubits are not twirled and can, for example, build up coherent errors while the box is applied.\n", "The ``twirling_strategy`` provides options for extending the qubits owned by each box, thereby modifying the number of idling qubits:\n", "\n", " * ``\"active\"`` (default) does not extend the boxes to any idling qubits.\n", " * ``\"active_accum\"`` extends all boxes to those qubits that have already been acted on by some prior instruction in the circuit. This is useful if you want to leave qubits at the start of the circuit in their ground state for as long as possible by not twirling them until they become active.\n", " * ``\"active_circuit\"`` extends all boxes to those qubits that are acted on by any instruction in the entire circuit. This is useful if you have a structured circuit with repeated entangling layers and you want to minimize the number of unique boxes because each one will need to have its noise learned.\n", " * ``\"all\"`` extends all boxes to all qubits in all quantum registers of the circuit. This is useful as an extension to ``\"active_circuit\"`` where you want all qubits in the QPU to always be twirled.\n", "\n", "To demonstrate, we apply each of the strategies to our base circuit and draw the resulting outcomes." ] }, { "cell_type": "code", "execution_count": null, "id": "8f49700f", "metadata": {}, "outputs": [], "source": [ "circuit = QuantumCircuit(5, 2)\n", "circuit.h(range(4))\n", "circuit.cx(0, 1)\n", "circuit.cx(1, 2)\n", "circuit.cx(2, 3)\n", "circuit.cx(0, 1)\n", "circuit.cx(1, 2)\n", "circuit.cx(2, 3)\n", "circuit.barrier()\n", "circuit.h(range(4))\n", "circuit.measure([1, 2], [0, 1])\n", "\n", "circuit.draw(\"mpl\", scale=0.6)" ] }, { "cell_type": "code", "execution_count": null, "id": "2dd4c7d7", "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "\n", "plt.figure(figsize=(9, 6))\n", "for idx, strategy in enumerate([\"active\", \"active_accum\", \"active_circuit\", \"all\"]):\n", " ax = plt.subplot(2, 2, idx + 1)\n", " pm = generate_boxing_pass_manager(twirling_strategy=strategy, remove_barriers=\"never\")\n", " transpiled_circuit = pm.run(circuit)\n", " fig = transpiled_circuit.draw(\"mpl\", scale=0.8, ax=ax)\n", " ax.set_title(f\"twirling_strategy='{strategy}'\")\n", "\n", "plt.tight_layout()" ] }, { "cell_type": "markdown", "id": "345b2bef", "metadata": {}, "source": [ "## Specify how to treat barriers\n", "\n", "By default, barriers are removed from the circuit after all entangling gates and measurement instructions have been boxed as instructed, including their extensions due to the ``twirling_strategy`` selection, but prior to single-qubit gates being added to the box immediately on their right, if possible.\n", "\n", "For example, if we start with the following circuit that contains a barrier:" ] }, { "cell_type": "code", "execution_count": null, "id": "7bbe889b", "metadata": {}, "outputs": [], "source": [ "circuit_with_barrier = QuantumCircuit(4)\n", "circuit_with_barrier.h(range(4))\n", "circuit_with_barrier.cz(0, 1)\n", "circuit_with_barrier.barrier()\n", "circuit_with_barrier.cz(2, 3)\n", "circuit_with_barrier.measure_all()\n", "\n", "circuit_with_barrier.draw(\"mpl\", scale=0.8)" ] }, { "cell_type": "markdown", "id": "ae3e277a", "metadata": {}, "source": [ "Then the default behaviour will prevent the two entangling gates from ending up in the same box, though it will let the single-qubit gates be absorbed into the next available box despite the barrier." ] }, { "cell_type": "code", "execution_count": null, "id": "feeb0d86", "metadata": {}, "outputs": [], "source": [ "boxing_pass_manager = generate_boxing_pass_manager()\n", "transpiled_circuit = boxing_pass_manager.run(circuit_with_barrier)\n", "transpiled_circuit.draw(\"mpl\", scale=0.8)" ] }, { "cell_type": "markdown", "id": "a6836f68", "metadata": {}, "source": [ "Setting `remove_barriers` to `'never'` preserves the barriers so that single-qubit gates are constrained." ] }, { "cell_type": "code", "execution_count": null, "id": "c4e56681", "metadata": {}, "outputs": [], "source": [ "boxing_pass_manager = generate_boxing_pass_manager(remove_barriers=\"never\")\n", "\n", "transpiled_circuit = boxing_pass_manager.run(circuit_with_barrier)\n", "transpiled_circuit.draw(\"mpl\", scale=0.8)" ] }, { "cell_type": "markdown", "id": "4499fbde", "metadata": {}, "source": [ "And finally, if we elect to remove the barriers immediately, then the entanglers are no longer constrained to end up in separate boxes." ] }, { "cell_type": "code", "execution_count": null, "id": "27d64f23", "metadata": {}, "outputs": [], "source": [ "boxing_pass_manager = generate_boxing_pass_manager(remove_barriers=\"immediately\")\n", "\n", "transpiled_circuit = boxing_pass_manager.run(circuit_with_barrier)\n", "transpiled_circuit.draw(\"mpl\", scale=0.8)" ] }, { "cell_type": "markdown", "id": "tld6t9qsk4", "metadata": {}, "source": [ "### How stratification works\n", "\n", "The boxing pass manager runs two key passes in sequence:\n", "\n", "1. **`GroupGatesIntoBoxes`** — stratifies entangling gates into boxes. Single-qubit gates are ignored entirely at this stage; only two-qubit gates are grouped.\n", "2. **`AbsorbSingleQubitGates`** — absorbs each single-qubit gate into the nearest eligible box to its right.\n", "\n", "During stratification, `GroupGatesIntoBoxes` traverses the circuit DAG in topological order and places each two-qubit gate in the **earliest group** whose qubits are not yet occupied. Three kinds of instructions act as **delimiters** that flush the current group for their qubits, forcing subsequent gates on those qubits into a new group:\n", "\n", "- **Barriers**\n", "- **Existing box instructions**\n", "- **Measurements**\n", "\n", "Single-qubit gates are not delimiters — they have no effect on grouping and are left for `AbsorbSingleQubitGates` to handle. No commutativity analysis is currently performed; the grouping respects the topological order of the circuit as-is." ] }, { "cell_type": "markdown", "id": "1gm5d8zjr3ti", "metadata": {}, "source": [ "### Why ``remove_barriers=\"after_stratification\"`` is the recommended default\n", "\n", "The default `\"after_stratification\"` mode is chosen because it combines two benefits:\n", "\n", "1. Barriers constrain how entangling gates are grouped (they act as delimiters during stratification).\n", "2. Barriers are removed *before* {class}`~.AbsorbSingleQubitGates` runs, so single-qubit gates can still flow across former barrier positions and be absorbed into the nearest box.\n", "\n", "By contrast:\n", "- ``\"never\"`` keeps barriers throughout, which traps single-qubit gates between a barrier and a box, preventing their absorption and leaving untwirled rotations in the circuit.\n", "- ``\"immediately\"`` removes barriers before stratification, so they have no effect on grouping at all.\n", "- ``\"finally\"`` is useful when you want barriers in the output circuit (e.g., for visualization), but be aware that single-qubit gates between a barrier and a box will not be absorbed." ] }, { "cell_type": "markdown", "id": "dcnbr1d0fak", "metadata": {}, "source": [ "## Boxing ISA circuits with barriers\n", "\n", "Circuits with a repeated structure—Trotterized Hamiltonian simulations, variational ansatze, error amplification sequences—are typically built programmatically and then transpiled to ISA form. Manually adding boxes to an already-transpiled circuit is cumbersome, so the natural workflow is:\n", "\n", "1. Build the logical circuit, inserting **barriers** at the layer boundaries you want to become box boundaries.\n", "2. Transpile to ISA. Qiskit's transpiler preserves barriers through routing and optimization.\n", "3. Run {func}`~.generate_boxing_pass_manager` on the ISA circuit.\n", "\n", "Barriers are needed when the end of one layer does not cover all qubits: the greedy algorithm would otherwise place gates from the next layer—on those \"free\" qubits—into the same group. As a concrete example, consider a two-step Heisenberg ZZ simulation on a 5-qubit ring. Each Trotter step has three bond layers: even bonds ``(0,1), (2,3)``, odd bonds ``(1,2), (3,4)``, and the wrap bond ``(4,0)``. After the odd-bond layer, qubits 0 and 4 are both free. Without a barrier, the wrap bond ``(4,0)`` gets greedy-grouped with bonds from the *next* Trotter step that use those free qubits, mixing gates from different steps in the same box." ] }, { "cell_type": "code", "execution_count": null, "id": "kszvwbc7pzc", "metadata": {}, "outputs": [], "source": [ "from qiskit.circuit import Parameter, QuantumCircuit\n", "\n", "n = 5 # 5-qubit ring: bonds (0,1), (1,2), (2,3), (3,4), (4,0)\n", "dt = Parameter(\"dt\")\n", "heisenberg = QuantumCircuit(n)\n", "\n", "for step in range(2):\n", " # Layer 1: even bonds (0,1) and (2,3)\n", " for idx in range(0, n - 1, 2):\n", " heisenberg.rzz(dt, idx, idx + 1)\n", " heisenberg.barrier()\n", " # Layer 2: odd bonds (1,2) and (3,4) -- qubits 0 and 4 are now free\n", " for idx in range(1, n - 1, 2):\n", " heisenberg.rzz(dt, idx, idx + 1)\n", " heisenberg.barrier()\n", " # Layer 3: wrap bond (4,0) -- both qubits were free after layer 2\n", " heisenberg.rzz(dt, n - 1, 0)\n", " if step < 1:\n", " heisenberg.barrier() # separates the two Trotter steps\n", "\n", "heisenberg.measure_all()\n", "\n", "heisenberg.draw(\"mpl\", scale=0.7)" ] }, { "cell_type": "markdown", "id": "peqgvwbnxsl", "metadata": {}, "source": [ "We transpile to ISA form, assuming the underlying device has CX as the native entangler. Note that the barriers survive transpilation and remain visible in the output circuit." ] }, { "cell_type": "code", "execution_count": null, "id": "wbvdpidcuhn", "metadata": {}, "outputs": [], "source": [ "from qiskit.transpiler import generate_preset_pass_manager\n", "\n", "preset_pm = generate_preset_pass_manager(\n", " basis_gates=[\"rz\", \"sx\", \"cx\"],\n", " coupling_map=[[0, 1], [1, 2], [2, 3], [3, 4], [4, 0]],\n", " optimization_level=1,\n", ")\n", "isa_circuit = preset_pm.run(heisenberg)\n", "isa_circuit.draw(\"mpl\", scale=0.5, fold=200)" ] }, { "cell_type": "markdown", "id": "9b5y3l4r3xp", "metadata": {}, "source": [ "Now we apply the boxing pass manager to the ISA circuit. Each of the three bond layers becomes a separate box in each Trotter step." ] }, { "cell_type": "code", "execution_count": null, "id": "9ccaoebqt3f", "metadata": {}, "outputs": [], "source": [ "boxing_pm = generate_boxing_pass_manager()\n", "boxed_isa = boxing_pm.run(isa_circuit)\n", "boxed_isa.draw(\"mpl\", scale=0.5, fold=100)" ] }, { "cell_type": "markdown", "id": "d622fa1c", "metadata": {}, "source": [ "## Incorporate Samplomatic's pass manager into `Qiskit`'s preset pass managers\n", "\n", "The code below shows how to incorporate {func}`~.generate_boxing_pass_manager` into one of Qiskit's preset pass managers. The transpiled circuit is ISA and contains boxes." ] }, { "cell_type": "code", "execution_count": null, "id": "5e8f1b0a", "metadata": {}, "outputs": [], "source": [ "from qiskit.transpiler import generate_preset_pass_manager\n", "\n", "preset_pass_manager = generate_preset_pass_manager(\n", " basis_gates=[\"rz\", \"sx\", \"cx\"],\n", " coupling_map=[[0, 1], [1, 2]],\n", " optimization_level=0,\n", ")\n", "boxing_pass_manager = generate_boxing_pass_manager()\n", "\n", "# Run the boxing pass manager after the scheduling stage\n", "preset_pass_manager.post_scheduling = boxing_pass_manager\n", "\n", "circuit = QuantumCircuit(3)\n", "circuit.h(0)\n", "circuit.cx(0, 1)\n", "circuit.cx(0, 2)\n", "circuit.measure_all()\n", "\n", "transpiled_circuit = preset_pass_manager.run(circuit)\n", "transpiled_circuit.draw(\"mpl\", scale=0.8)" ] }, { "cell_type": "markdown", "id": "4k9ok8ip14a", "metadata": {}, "source": [ "This pattern also combines naturally with the barrier-based ISA boxing workflow. The boxing pass manager runs as a post-scheduling step, after barriers from the original logical circuit have survived transpilation, so they guide stratification exactly as expected." ] }, { "cell_type": "code", "execution_count": null, "id": "5v5zjhiftji", "metadata": {}, "outputs": [], "source": [ "preset_pm = generate_preset_pass_manager(\n", " basis_gates=[\"rz\", \"sx\", \"cx\"],\n", " coupling_map=[[0, 1], [1, 2], [2, 3], [3, 4], [4, 0]],\n", " optimization_level=1,\n", ")\n", "preset_pm.post_scheduling = generate_boxing_pass_manager()\n", "\n", "boxed_heisenberg = preset_pm.run(heisenberg)\n", "boxed_heisenberg.draw(\"mpl\", scale=0.5, fold=100)" ] }, { "cell_type": "markdown", "id": "ejdcnq671pw", "metadata": {}, "source": [ "## Inspecting and modifying annotations\n", "\n", "After auto-boxing, you may want to inspect the annotations on specific boxes or adjust them—for example, to add noise injection to select boxes, remove annotations from some boxes, or change the twirling group. Samplomatic provides a set of utility functions in {mod}`samplomatic.utils` for this purpose. All of them return a **new circuit** without modifying the original.\n", "\n", "{func}`~.extend_annotations` appends one or more annotations to every box in the circuit." ] }, { "cell_type": "code", "execution_count": null, "id": "2ets1otrd3w", "metadata": {}, "outputs": [], "source": [ "from samplomatic import InjectNoise, Twirl\n", "from samplomatic.utils import extend_annotations\n", "\n", "boxed = generate_boxing_pass_manager().run(circuit_with_barrier)\n", "\n", "# Add InjectNoise to every box\n", "with_noise = extend_annotations(boxed, InjectNoise(\"my_noise_ref\"))\n", "with_noise.draw(\"mpl\", scale=0.8)" ] }, { "cell_type": "markdown", "id": "8kqcs5nqlyl", "metadata": {}, "source": [ "{func}`~.filter_annotations` keeps only annotations of specified types, removing the rest. {func}`~.replace_annotations` applies a callable to each annotation; the callable returns a list of annotations to substitute in its place (return ``[]`` to remove, a single-element list to replace, or a multi-element list to expand)." ] }, { "cell_type": "code", "execution_count": null, "id": "kp7vc7yi7ba", "metadata": {}, "outputs": [], "source": [ "from samplomatic.utils import filter_annotations, replace_annotations\n", "\n", "# Keep only Twirl annotations, drop everything else\n", "twirl_only = filter_annotations(with_noise, Twirl)\n", "\n", "# Replace the twirling group on every Twirl annotation\n", "updated = replace_annotations(\n", " twirl_only, lambda a: [Twirl(group=\"local_c1\")] if isinstance(a, Twirl) else [a]\n", ")" ] }, { "cell_type": "markdown", "id": "nlx8ix0fpo", "metadata": {}, "source": [ "The general-purpose {func}`~.map_annotations` function accepts any callable mapping a list of annotations to a new list, giving you full control over per-box transformations. {func}`~.strip_annotations` is a convenience wrapper that removes all annotations from every box.\n", "\n", "For bulk changes like adding {class}`.InjectNoise` to all gate boxes, the ``inject_noise_targets`` parameter of {func}`~.generate_boxing_pass_manager` is still the most concise option. These utilities are most useful for selective per-box edits that the pass manager parameters do not cover." ] }, { "cell_type": "markdown", "id": "315671b2", "metadata": {}, "source": [ "## Build your circuit\n", "\n", "Every pass manager produced by {func}`~.generate_boxing_pass_manager` returns\n", "circuits that can be successfully turned into a template/samplex pair by {func}`~.build`. As an\n", "example, the following code calls the {func}`~.build` function on a circuit produced by a boxing pass\n", "manager." ] }, { "cell_type": "code", "execution_count": null, "id": "ed34cc47", "metadata": {}, "outputs": [], "source": [ "from samplomatic import build\n", "\n", "boxing_pass_manager = generate_boxing_pass_manager(\n", " enable_gates=True,\n", " enable_measures=True,\n", ")\n", "transpiled_circuit = boxing_pass_manager.run(circuit)\n", "\n", "template, samplex = build(transpiled_circuit)" ] }, { "cell_type": "markdown", "id": "c8395c86", "metadata": {}, "source": [ "In order to ensure that any transpiled circuit can be successfully built, the pass managers know how to\n", "include additional boxes when they are needed. As an example, consider the circuit below, which ends with an\n", "unmeasured qubit ``0``." ] }, { "cell_type": "code", "execution_count": null, "id": "76fbe38c", "metadata": {}, "outputs": [], "source": [ "circuit_with_unmeasured_qubit = QuantumCircuit(4, 3)\n", "circuit_with_unmeasured_qubit.cz(0, 1)\n", "circuit_with_unmeasured_qubit.cz(2, 3)\n", "for qubit in range(4):\n", " circuit_with_unmeasured_qubit.rz(Parameter(f\"th_{qubit}\"), qubit)\n", " circuit_with_unmeasured_qubit.rx(Parameter(f\"phi_{qubit}\"), qubit)\n", " circuit_with_unmeasured_qubit.rz(Parameter(f\"lam_{qubit}\"), qubit)\n", "circuit_with_unmeasured_qubit.measure(range(1, 4), range(3))\n", "\n", "circuit_with_unmeasured_qubit.draw(\"mpl\", scale=0.8)" ] }, { "cell_type": "markdown", "id": "ab604dc1", "metadata": {}, "source": [ "Drawing left-dressed boxes around the gates and the measurements would result in a circuit that has uncollected\n", "virtual gates on qubit ``0``, and calling {func}`~.build` on this circuit would result in an error. To\n", "avoid this, the pass managers returned by {func}`~.generate_boxing_pass_manager` are allowed\n", "to add right-dressed boxes to act as collectors. As an example, in the following snippet qubit ``0`` is\n", "terminated by a right-dressed box that picks up the uncollected virtual gate. The single-qubit gates acting on qubit\n", "``0`` are also placed inside the box, in order to minimise the depth of the resulting circuit." ] }, { "cell_type": "code", "execution_count": null, "id": "bda391d8", "metadata": {}, "outputs": [], "source": [ "boxing_pass_manager = generate_boxing_pass_manager(\n", " enable_gates=True,\n", " enable_measures=True,\n", ")\n", "transpiled_circuit = boxing_pass_manager.run(circuit_with_unmeasured_qubit)\n", "transpiled_circuit.draw(\"mpl\", scale=0.8)" ] }, { "cell_type": "markdown", "id": "fc9bf4b8", "metadata": {}, "source": [ "In another example, a right-dressed box is added to collect the virtual gates that would otherwise remain\n", "uncollected due to the unboxed measurements." ] }, { "cell_type": "code", "execution_count": null, "id": "910f1e6b", "metadata": {}, "outputs": [], "source": [ "boxing_pass_manager = generate_boxing_pass_manager(\n", " enable_gates=True,\n", " enable_measures=False,\n", ")\n", "transpiled_circuit = boxing_pass_manager.run(circuit_with_unmeasured_qubit)\n", "transpiled_circuit.draw(\"mpl\", scale=0.8)" ] }, { "cell_type": "markdown", "id": "6402f11f", "metadata": {}, "source": [] } ], "metadata": { "kernelspec": { "display_name": ".venv", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.1" } }, "nbformat": 4, "nbformat_minor": 5 }