{ "cells": [ { "cell_type": "markdown", "id": "72751fb3", "metadata": {}, "source": [ "# Toffoli ladders", "\n", "**Download Notebook** - {nb-download}`toffoli_ladders.ipynb`\n" ] }, { "cell_type": "code", "execution_count": 1, "id": "f8812ac5", "metadata": {}, "outputs": [], "source": [ "from pytket import Circuit\n", "from pytket.circuit.display import render_circuit_jupyter as draw" ] }, { "cell_type": "code", "execution_count": 2, "id": "e7191c1f", "metadata": {}, "outputs": [], "source": [ "def toffoli_ladder_circuit(\n", " n_qubits: int,\n", " n_anc: int,\n", " labeled_indices: list[\n", " tuple[tuple[int, int], tuple[int, int], tuple[int, int]]\n", " ],\n", ") -> Circuit:\n", " \"\"\"Create pytket circuit for a Toffoli ladder.\n", "\n", " Args:\n", " n_qubits: Number of main/data qubits in register \"q\".\n", " n_anc: Number of ancilla qubits in register \"a\".\n", " labeled_indices: Triples of qubit references of the form\n", " ((reg1, i1), (reg2, i2), (reg3, i3)),\n", " where reg is 0 or 1.\n", "\n", " Returns:\n", " A pytket Circuit on n_qubits + n_anc qubits, with the first n_qubits\n", " corresponding to q[0], ..., q[n_qubits-1], and the next n_anc\n", " corresponding to a[0], ..., a[n_anc-1].\n", " \"\"\"\n", " circuit = Circuit(n_qubits + n_anc)\n", "\n", " def resolve(ref: tuple[str, int]) -> int:\n", " reg, idx = ref\n", " if reg == 0:\n", " if 0 <= idx < n_qubits:\n", " return idx\n", " raise IndexError(f\"q index out of range: q[{idx}] for n_qubits={n_qubits}\")\n", " if reg == 1:\n", " if 0 <= idx < n_anc:\n", " return n_qubits + idx\n", " raise IndexError(f\"a index out of range: a[{idx}] for n_anc={n_anc}\")\n", " raise ValueError(f\"Unknown register {reg!r}; expected 'q' or 'a'\")\n", "\n", " for c1, c2, t in labeled_indices:\n", " circuit.CCX(resolve(c1), resolve(c2), resolve(t))\n", "\n", " return circuit" ] }, { "cell_type": "markdown", "id": "29585152", "metadata": {}, "source": [ "## Linear Toffoli ladder" ] }, { "cell_type": "code", "execution_count": 3, "id": "1c28f06a", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "