{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ " Trusted Notebook\" width=\"500 px\" align=\"left\">" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The latest version of this notebook is available on https://github.com/Qiskit/qiskit-tutorial." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Writing a Transpiler Pass" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Introduction" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A central component of Qiskit Terra is the transpiler, which is designed for modularity and extensibility. The goal is to be able to easily write new circuit transformations (known as transpiler *passes*), and combine them with other existing passes. In this way, the transpiler opens up the door for research into aggressive optimization of quantum circuits.\n", "\n", "In this notebook, we show how to develop a simple transpiler pass. To do so, we first introduce the internal representation of quantum circuits in Qiskit, in the form of a Directed Acyclic Graph or DAG. Then, we illustrate a simple swap mapper pass, which transforms an input circuit to be compatible with a limited-connectivity quantum device." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Introducing the DAG" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In Qiskit, we represent circuits internally using a Directed Acyclic Graph or **DAG**. The advantage of this representation over a pure list of gates (i.e. *netlist*) is that the flow of information between operations are explicit, making it easier for passes to make transformation decisions without changing the semantics of the circuit.\n", "\n", "Let's start by building a simple circuit, and examining its DAG." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
┌───┐ ┌─┐ \n", "q_0: |0>┤ H ├──■──┤M├───────────\n", " └───┘┌─┴─┐└╥┘┌─────────┐\n", "q_1: |0>─────┤ X ├─╫─┤ Rz(0.5) ├\n", " └───┘ ║ └────┬────┘\n", "q_2: |0>───────────╫──────┼─────\n", " ║ ┌──┴──┐ \n", " c_0: 0 ═══════════╩═══╡ ╞══\n", " │ │ \n", " c_1: 0 ═══════════════╡ = 2 ╞══\n", " │ │ \n", " c_2: 0 ═══════════════╡ ╞══\n", " └─────┘" ], "text/plain": [ "
┌───┐┌───┐┌─┐ ┌───┐ \n", "q_0: |0>──■──┤ H ├┤ H ├┤M├──────────────────┤ H ├───\n", " │ └───┘└─┬─┘└╥┘┌─────────────┐┌──┴───┴──┐\n", "q_1: |0>──■─────────■───╫─┤ U2(0.1,0.2) ├┤ Rz(0.5) ├\n", " ┌─┴─┐ ║ └─────────────┘└────┬────┘\n", "q_2: |0>┤ X ├───────────╫─────────────────────┼─────\n", " └───┘ ║ ┌──┴──┐ \n", " c_0: 0 ════════════════╩══════════════════╡ ╞══\n", " │ │ \n", " c_1: 0 ═══════════════════════════════════╡ = 2 ╞══\n", " │ │ \n", " c_2: 0 ═══════════════════════════════════╡ ╞══\n", " └─────┘" ], "text/plain": [ "