{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Circuit generation\n", "\n", "**Download this notebook - {nb-download}`circuit_generation_example.ipynb`**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook will provide a brief introduction to some of the more advanced methods of circuit generation available in `pytket`, including:\n", "* how to address wires and registers;\n", "* reading in circuits from QASM and Quipper ASCII files;\n", "* various types of 'boxes';\n", "* composition of circuits (both 'horizontally' and 'vertically');\n", "* use of symbolic gate parameters;\n", "* representation of classically controlled gates." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Wires, unit IDs and registers" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's get started by constructing a circuit with 3 qubits and 2 classical bits:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from pytket.circuit import Circuit\n", "from pytket.circuit.display import render_circuit_jupyter as draw" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[q[0]]\n", "[c[0], c[1]]\n" ] } ], "source": [ "circ = Circuit(1, 2)\n", "print(circ.qubits)\n", "print(circ.bits)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The qubits have automatically been assigned to a register with name `q` and indices 0, 1 and 2, while the bits have been assigned to a register with name `c` and indices 0 and 1.\n", "\n", "We can give these units arbitrary names and indices of arbitrary dimension:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "from pytket.circuit import Qubit" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[alpha[0], beta[2, 1], q[0]]\n" ] } ], "source": [ "new_q1 = Qubit(\"alpha\", 0)\n", "new_q2 = Qubit(\"beta\", 2, 1)\n", "circ.add_qubit(new_q1)\n", "circ.add_qubit(new_q2)\n", "print(circ.qubits)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also add a new register of qubits in one go:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[alpha[0], beta[2, 1], delta[0], delta[1], q[0]]\n" ] } ], "source": [ "delta_reg = circ.add_q_register(\"delta\", 2)\n", "print(circ.qubits)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Similar commands are available for classical bits.\n", "\n", "We can add gates to the circuit as follows:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[CX delta[0], delta[1]; ]" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "circ.CX(delta_reg[0], delta_reg[1])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This command appends a CX gate with control `q[0]` and target `q[1]`. Note that the integer arguments are automatically converted to the default unit IDs. For simple circuits it is often easiest to stick to the default register and refer to the qubits by integers. To add gates to our own named units, we simply pass the `Qubit` (or classical `Bit`) as an argument. (We can't mix the two conventions in one command, however.)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[H alpha[0]; CX q[0], beta[2, 1]; CX delta[0], delta[1]; Rz(0.5) beta[2, 1]; ]" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "circ.H(new_q1)\n", "circ.CX(Qubit(\"q\", 0), new_q2)\n", "circ.Rz(0.5, new_q2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's have a look at our circuit using the interactive circuit renderer:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "\n", "