{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Repetition Codes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Introduction\n", "\n", "In quantum error correction we take many noisy qubits (which we call *physical qubits*) and use them to store a smaller number of *logical qubits*. The error correction procedure is designed to detect and correct for the effects of noise. This makes the logical qubits much less noisy and more reliable than the physical ones from which they are built.\n", "\n", "The repetition code is a simple example of quantum error correction, in which a logical *bit* is stored rather than a logical *qubit*. A given instance of the repetition code is defined by two numbers, which we call $d$ and $T$.\n", "\n", "The $d$ parameter determines how many physical qubits are used. The actual information about the logical bit is stored in $d$ qubits (which we'll call _code qubits_). The encoding is done in a very simple way: to encode a 0, all these qubits are set in the state $|0\\rangle$, to encode a 1 they are set to $|1\\rangle$. To readout the value, you can just look at any qubit. To read it out in a way that protects against single qubit errors, you can read out all the qubits and take a majority vote.\n", "\n", "It is not only at readout that we can extract useful information that will help us correct errors. We can also extract information while the logical bit sits idle, or even while it is involved in computation.\n", "\n", "Since this is an exercise in quantum error correction, we will do this in a way that would also work for logical *qubits*. Specifically, our method of extracting information about errors must not extract any information regarding the stored logical information. In the quantum case, this is required so that we do not disturb superposition states of the stored qubits.\n", "\n", "We will extract information over $T$ rounds of _syndrome measurements_. For the repetition code, these are based on the fact that all code qubits should be in the same state (all $|0\\rangle$ or all $|1\\rangle$). Any departure from this is therefore a signature of error. Specifically, we imagine our $d$ code qubits sitting along a line. We will then perform a measurement on each pair of neighboring code qubits. This will tell us whether they are the same or different, without extracting any information on what their values are.\n", "\n", "The implementation of these measurements requires $d-1$ additional qubits, which we will call _link qubits_ for the repetition code. By performing two CNOTs between the pair of code qubits and a corresponding link qubit, the required information (and only the required information) is placed on the link qubit and can then be measured." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2019-12-10T22:05:19.291583Z", "start_time": "2019-12-10T22:05:17.429434Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " \n", "code_qubit_0: |0>──■───────\n", " │ \n", "code_qubit_1: |0>──┼────■──\n", " ┌─┴─┐┌─┴─┐\n", "link_qubit_0: |0>┤ X ├┤ X ├\n", " └───┘└───┘\n" ] } ], "source": [ "from qiskit import *\n", "cq = QuantumRegister(2,'code_qubit')\n", "lq = QuantumRegister(1,'link_qubit')\n", "qc = QuantumCircuit(cq,lq)\n", "qc.cx(cq,lq)\n", "qc.cx(cq,lq)\n", "print(qc)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here we provide tools for creating and testing repetition codes. The first thing we need to do is import them." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2019-12-10T22:05:19.306393Z", "start_time": "2019-12-10T22:05:19.294551Z" } }, "outputs": [], "source": [ "from qiskit.ignis.verification.topological_codes import RepetitionCode\n", "from qiskit.ignis.verification.topological_codes import GraphDecoder\n", "from qiskit.ignis.verification.topological_codes import lookuptable_decoding, postselection_decoding" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Creating a repetition code\n", "\n", "The repetition_code class creates a code for given values of $d$ and $T$." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "ExecuteTime": { "end_time": "2019-12-10T22:05:19.321485Z", "start_time": "2019-12-10T22:05:19.313135Z" } }, "outputs": [], "source": [ "d = 3\n", "T = 2\n", "code = RepetitionCode(d,T)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "With this we can inspect various properties of the code, such as the names of the qubit registers used for the code and ancilla qubits." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "ExecuteTime": { "end_time": "2019-12-10T22:05:19.332412Z", "start_time": "2019-12-10T22:05:19.324704Z" } }, "outputs": [ { "data": { "text/plain": [ "{'code_qubit', 'link_qubit'}" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "code.qubit_registers" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "These registers are also attributes of the repetition_code object." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "ExecuteTime": { "end_time": "2019-12-10T22:05:19.341298Z", "start_time": "2019-12-10T22:05:19.335386Z" } }, "outputs": [ { "data": { "text/plain": [ "QuantumRegister(3, 'code_qubit')" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "code.code_qubit" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also access the quantum circuits that implement the code. Two of these are produced: one for each of the two possible logical bit values." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "ExecuteTime": { "end_time": "2019-12-10T22:05:19.382788Z", "start_time": "2019-12-10T22:05:19.343805Z" }, "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "========= logical 0 =========\n", "\n", " ┌───┐ ┌───┐ ┌─┐ ░ ┌───┐ ┌───┐ »\n", " link_qubit_0: |0>┤ X ├─────┤ X ├─────┤M├─|0>───────░─┤ X ├─────┤ X ├─────»\n", " └─┬─┘┌───┐└─┬─┘┌───┐└╥┘ ┌─┐ ░ └─┬─┘┌───┐└─┬─┘┌───┐»\n", " link_qubit_1: |0>──┼──┤ X ├──┼──┤ X ├─╫──┤M├──|0>──░───┼──┤ X ├──┼──┤ X ├»\n", " │ └─┬─┘ │ └─┬─┘ ║ └╥┘ ░ │ └─┬─┘ │ └─┬─┘»\n", " code_qubit_0: |0>──■────┼────┼────┼───╫───╫────────░───■────┼────┼────┼──»\n", " │ │ │ ║ ║ ░ │ │ │ »\n", " code_qubit_1: |0>───────■────■────┼───╫───╫────────░────────■────■────┼──»\n", " │ ║ ║ ░ │ »\n", " code_qubit_2: |0>─────────────────■───╫───╫────────░──────────────────■──»\n", " ║ ║ ░ »\n", "round_0_link_bit_0: 0 ═════════════════════╩═══╬══════════════════════════════»\n", " ║ »\n", "round_0_link_bit_1: 0 ═════════════════════════╩══════════════════════════════»\n", " »\n", "round_1_link_bit_0: 0 ════════════════════════════════════════════════════════»\n", " »\n", "round_1_link_bit_1: 0 ════════════════════════════════════════════════════════»\n", " »\n", " code_bit_0: 0 ════════════════════════════════════════════════════════»\n", " »\n", " code_bit_1: 0 ════════════════════════════════════════════════════════»\n", " »\n", " code_bit_2: 0 ════════════════════════════════════════════════════════»\n", " »\n", "« ┌─┐ ░ \n", "« link_qubit_0: ┤M├─|0>───────░──────────\n", "« └╥┘ ┌─┐ ░ \n", "« link_qubit_1: ─╫──┤M├──|0>──░──────────\n", "« ║ └╥┘ ░ ┌─┐ \n", "« code_qubit_0: ─╫───╫────────░─┤M├──────\n", "« ║ ║ ░ └╥┘┌─┐ \n", "« code_qubit_1: ─╫───╫────────░──╫─┤M├───\n", "« ║ ║ ░ ║ └╥┘┌─┐\n", "« code_qubit_2: ─╫───╫────────░──╫──╫─┤M├\n", "« ║ ║ ░ ║ ║ └╥┘\n", "«round_0_link_bit_0: ═╬═══╬═══════════╬══╬══╬═\n", "« ║ ║ ║ ║ ║ \n", "«round_0_link_bit_1: ═╬═══╬═══════════╬══╬══╬═\n", "« ║ ║ ║ ║ ║ \n", "«round_1_link_bit_0: ═╩═══╬═══════════╬══╬══╬═\n", "« ║ ║ ║ ║ \n", "«round_1_link_bit_1: ═════╩═══════════╬══╬══╬═\n", "« ║ ║ ║ \n", "« code_bit_0: ═════════════════╩══╬══╬═\n", "« ║ ║ \n", "« code_bit_1: ════════════════════╩══╬═\n", "« ║ \n", "« code_bit_2: ═══════════════════════╩═\n", "« \n", "\n", "========= logical 1 =========\n", "\n", " ░ ┌───┐ ┌───┐ ┌─┐ ░ ┌───┐ »\n", " link_qubit_0: |0>──────░─┤ X ├─────┤ X ├─────┤M├─|0>───────░─┤ X ├─────»\n", " ░ └─┬─┘┌───┐└─┬─┘┌───┐└╥┘ ┌─┐ ░ └─┬─┘┌───┐»\n", " link_qubit_1: |0>──────░───┼──┤ X ├──┼──┤ X ├─╫──┤M├──|0>──░───┼──┤ X ├»\n", " ┌───┐ ░ │ └─┬─┘ │ └─┬─┘ ║ └╥┘ ░ │ └─┬─┘»\n", " code_qubit_0: |0>┤ X ├─░───■────┼────┼────┼───╫───╫────────░───■────┼──»\n", " ├───┤ ░ │ │ │ ║ ║ ░ │ »\n", " code_qubit_1: |0>┤ X ├─░────────■────■────┼───╫───╫────────░────────■──»\n", " ├───┤ ░ │ ║ ║ ░ »\n", " code_qubit_2: |0>┤ X ├─░──────────────────■───╫───╫────────░───────────»\n", " └───┘ ░ ║ ║ ░ »\n", "round_0_link_bit_0: 0 ═════════════════════════════╩═══╬════════════════════»\n", " ║ »\n", "round_0_link_bit_1: 0 ═════════════════════════════════╩════════════════════»\n", " »\n", "round_1_link_bit_0: 0 ══════════════════════════════════════════════════════»\n", " »\n", "round_1_link_bit_1: 0 ══════════════════════════════════════════════════════»\n", " »\n", " code_bit_0: 0 ══════════════════════════════════════════════════════»\n", " »\n", " code_bit_1: 0 ══════════════════════════════════════════════════════»\n", " »\n", " code_bit_2: 0 ══════════════════════════════════════════════════════»\n", " »\n", "« ┌───┐ ┌─┐ ░ \n", "« link_qubit_0: ┤ X ├─────┤M├─|0>───────░──────────\n", "« └─┬─┘┌───┐└╥┘ ┌─┐ ░ \n", "« link_qubit_1: ──┼──┤ X ├─╫──┤M├──|0>──░──────────\n", "« │ └─┬─┘ ║ └╥┘ ░ ┌─┐ \n", "« code_qubit_0: ──┼────┼───╫───╫────────░─┤M├──────\n", "« │ │ ║ ║ ░ └╥┘┌─┐ \n", "« code_qubit_1: ──■────┼───╫───╫────────░──╫─┤M├───\n", "« │ ║ ║ ░ ║ └╥┘┌─┐\n", "« code_qubit_2: ───────■───╫───╫────────░──╫──╫─┤M├\n", "« ║ ║ ░ ║ ║ └╥┘\n", "«round_0_link_bit_0: ═══════════╬═══╬═══════════╬══╬══╬═\n", "« ║ ║ ║ ║ ║ \n", "«round_0_link_bit_1: ═══════════╬═══╬═══════════╬══╬══╬═\n", "« ║ ║ ║ ║ ║ \n", "«round_1_link_bit_0: ═══════════╩═══╬═══════════╬══╬══╬═\n", "« ║ ║ ║ ║ \n", "«round_1_link_bit_1: ═══════════════╩═══════════╬══╬══╬═\n", "« ║ ║ ║ \n", "« code_bit_0: ═══════════════════════════╩══╬══╬═\n", "« ║ ║ \n", "« code_bit_1: ══════════════════════════════╩══╬═\n", "« ║ \n", "« code_bit_2: ═════════════════════════════════╩═\n", "« \n" ] } ], "source": [ "for log in ['0','1']:\n", " print('\\n========= logical',log,'=========\\n')\n", " print( code.circuit[log] )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Building a custom repetition code\n", "\n", "You can also implement measurement rounds and logical X gates yourself. For example, let's set up a code with no syndrome measurement rounds." ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "ExecuteTime": { "end_time": "2019-12-10T22:05:19.397726Z", "start_time": "2019-12-10T22:05:19.385304Z" } }, "outputs": [], "source": [ "empty_code = RepetitionCode(3,0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This does nothing but set up two circuits for the two logical encoded states. There are no syndrome measurement rounds, and no final readout." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "ExecuteTime": { "end_time": "2019-12-10T22:05:19.423638Z", "start_time": "2019-12-10T22:05:19.403354Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "========= logical 0 =========\n", "\n", " \n", "link_qubit_0: |0>\n", " \n", "link_qubit_1: |0>\n", " \n", "code_qubit_0: |0>\n", " \n", "code_qubit_1: |0>\n", " \n", "code_qubit_2: |0>\n", " \n", "\n", "========= logical 1 =========\n", "\n", " ░ \n", "link_qubit_0: |0>──────░─\n", " ░ \n", "link_qubit_1: |0>──────░─\n", " ┌───┐ ░ \n", "code_qubit_0: |0>┤ X ├─░─\n", " ├───┤ ░ \n", "code_qubit_1: |0>┤ X ├─░─\n", " ├───┤ ░ \n", "code_qubit_2: |0>┤ X ├─░─\n", " └───┘ ░ \n" ] } ], "source": [ "def print_circuits(code):\n", " for log in ['0','1']:\n", " print('\\n========= logical',log,'=========\\n')\n", " print( code.circuit[log] )\n", " \n", "print_circuits(empty_code)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can add a round using the syndrome_measurement() method." ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "ExecuteTime": { "end_time": "2019-12-10T22:05:19.449017Z", "start_time": "2019-12-10T22:05:19.430733Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "========= logical 0 =========\n", "\n", " ┌───┐ ┌───┐ ┌─┐ ░ \n", " link_qubit_0: |0>┤ X ├─────┤ X ├─────┤M├─|0>───────░─\n", " └─┬─┘┌───┐└─┬─┘┌───┐└╥┘ ┌─┐ ░ \n", " link_qubit_1: |0>──┼──┤ X ├──┼──┤ X ├─╫──┤M├──|0>──░─\n", " │ └─┬─┘ │ └─┬─┘ ║ └╥┘ ░ \n", " code_qubit_0: |0>──■────┼────┼────┼───╫───╫────────░─\n", " │ │ │ ║ ║ ░ \n", " code_qubit_1: |0>───────■────■────┼───╫───╫────────░─\n", " │ ║ ║ ░ \n", " code_qubit_2: |0>─────────────────■───╫───╫────────░─\n", " ║ ║ ░ \n", "round_0_link_bit_0: 0 ═════════════════════╩═══╬══════════\n", " ║ \n", "round_0_link_bit_1: 0 ═════════════════════════╩══════════\n", " \n", "\n", "========= logical 1 =========\n", "\n", " ░ ┌───┐ ┌───┐ ┌─┐ ░ \n", " link_qubit_0: |0>──────░─┤ X ├─────┤ X ├─────┤M├─|0>───────░─\n", " ░ └─┬─┘┌───┐└─┬─┘┌───┐└╥┘ ┌─┐ ░ \n", " link_qubit_1: |0>──────░───┼──┤ X ├──┼──┤ X ├─╫──┤M├──|0>──░─\n", " ┌───┐ ░ │ └─┬─┘ │ └─┬─┘ ║ └╥┘ ░ \n", " code_qubit_0: |0>┤ X ├─░───■────┼────┼────┼───╫───╫────────░─\n", " ├───┤ ░ │ │ │ ║ ║ ░ \n", " code_qubit_1: |0>┤ X ├─░────────■────■────┼───╫───╫────────░─\n", " ├───┤ ░ │ ║ ║ ░ \n", " code_qubit_2: |0>┤ X ├─░──────────────────■───╫───╫────────░─\n", " └───┘ ░ ║ ║ ░ \n", "round_0_link_bit_0: 0 ═════════════════════════════╩═══╬══════════\n", " ║ \n", "round_0_link_bit_1: 0 ═════════════════════════════════╩══════════\n", " \n" ] } ], "source": [ "empty_code.syndrome_measurement()\n", "print_circuits(empty_code)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A logical X operation can be added using the x() method. " ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "ExecuteTime": { "end_time": "2019-12-10T22:05:19.474270Z", "start_time": "2019-12-10T22:05:19.451347Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "========= logical 0 =========\n", "\n", " ┌───┐ ┌───┐ ┌─┐ ░ ░ \n", " link_qubit_0: |0>┤ X ├─────┤ X ├─────┤M├─|0>───────░───────░─\n", " └─┬─┘┌───┐└─┬─┘┌───┐└╥┘ ┌─┐ ░ ░ \n", " link_qubit_1: |0>──┼──┤ X ├──┼──┤ X ├─╫──┤M├──|0>──░───────░─\n", " │ └─┬─┘ │ └─┬─┘ ║ └╥┘ ░ ┌───┐ ░ \n", " code_qubit_0: |0>──■────┼────┼────┼───╫───╫────────░─┤ X ├─░─\n", " │ │ │ ║ ║ ░ ├───┤ ░ \n", " code_qubit_1: |0>───────■────■────┼───╫───╫────────░─┤ X ├─░─\n", " │ ║ ║ ░ ├───┤ ░ \n", " code_qubit_2: |0>─────────────────■───╫───╫────────░─┤ X ├─░─\n", " ║ ║ ░ └───┘ ░ \n", "round_0_link_bit_0: 0 ═════════════════════╩═══╬══════════════════\n", " ║ \n", "round_0_link_bit_1: 0 ═════════════════════════╩══════════════════\n", " \n", "\n", "========= logical 1 =========\n", "\n", " ░ ┌───┐ ┌───┐ ┌─┐ ░ ░ \n", " link_qubit_0: |0>──────░─┤ X ├─────┤ X ├─────┤M├─|0>───────░───────░─\n", " ░ └─┬─┘┌───┐└─┬─┘┌───┐└╥┘ ┌─┐ ░ ░ \n", " link_qubit_1: |0>──────░───┼──┤ X ├──┼──┤ X ├─╫──┤M├──|0>──░───────░─\n", " ┌───┐ ░ │ └─┬─┘ │ └─┬─┘ ║ └╥┘ ░ ┌───┐ ░ \n", " code_qubit_0: |0>┤ X ├─░───■────┼────┼────┼───╫───╫────────░─┤ X ├─░─\n", " ├───┤ ░ │ │ │ ║ ║ ░ ├───┤ ░ \n", " code_qubit_1: |0>┤ X ├─░────────■────■────┼───╫───╫────────░─┤ X ├─░─\n", " ├───┤ ░ │ ║ ║ ░ ├───┤ ░ \n", " code_qubit_2: |0>┤ X ├─░──────────────────■───╫───╫────────░─┤ X ├─░─\n", " └───┘ ░ ║ ║ ░ └───┘ ░ \n", "round_0_link_bit_0: 0 ═════════════════════════════╩═══╬══════════════════\n", " ║ \n", "round_0_link_bit_1: 0 ═════════════════════════════════╩══════════════════\n", " \n" ] } ], "source": [ "empty_code.x()\n", "print_circuits(empty_code)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This also has a logs kwarg, which is a list of the circuits for which X is applied. This is ['0','1'] by default." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Final readout is then done with the readout() method." ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "ExecuteTime": { "end_time": "2019-12-10T22:05:19.538852Z", "start_time": "2019-12-10T22:05:19.489443Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "========= logical 0 =========\n", "\n", " ┌───┐ ┌───┐ ┌─┐ ░ ░ \n", " link_qubit_0: |0>┤ X ├─────┤ X ├─────┤M├─|0>───────░───────░──────────\n", " └─┬─┘┌───┐└─┬─┘┌───┐└╥┘ ┌─┐ ░ ░ \n", " link_qubit_1: |0>──┼──┤ X ├──┼──┤ X ├─╫──┤M├──|0>──░───────░──────────\n", " │ └─┬─┘ │ └─┬─┘ ║ └╥┘ ░ ┌───┐ ░ ┌─┐ \n", " code_qubit_0: |0>──■────┼────┼────┼───╫───╫────────░─┤ X ├─░─┤M├──────\n", " │ │ │ ║ ║ ░ ├───┤ ░ └╥┘┌─┐ \n", " code_qubit_1: |0>───────■────■────┼───╫───╫────────░─┤ X ├─░──╫─┤M├───\n", " │ ║ ║ ░ ├───┤ ░ ║ └╥┘┌─┐\n", " code_qubit_2: |0>─────────────────■───╫───╫────────░─┤ X ├─░──╫──╫─┤M├\n", " ║ ║ ░ └───┘ ░ ║ ║ └╥┘\n", "round_0_link_bit_0: 0 ═════════════════════╩═══╬═══════════════════╬══╬══╬═\n", " ║ ║ ║ ║ \n", "round_0_link_bit_1: 0 ═════════════════════════╩═══════════════════╬══╬══╬═\n", " ║ ║ ║ \n", " code_bit_0: 0 ═════════════════════════════════════════════╩══╬══╬═\n", " ║ ║ \n", " code_bit_1: 0 ════════════════════════════════════════════════╩══╬═\n", " ║ \n", " code_bit_2: 0 ═══════════════════════════════════════════════════╩═\n", " \n", "\n", "========= logical 1 =========\n", "\n", " ░ ┌───┐ ┌───┐ ┌─┐ ░ ░ »\n", " link_qubit_0: |0>──────░─┤ X ├─────┤ X ├─────┤M├─|0>───────░───────░────»\n", " ░ └─┬─┘┌───┐└─┬─┘┌───┐└╥┘ ┌─┐ ░ ░ »\n", " link_qubit_1: |0>──────░───┼──┤ X ├──┼──┤ X ├─╫──┤M├──|0>──░───────░────»\n", " ┌───┐ ░ │ └─┬─┘ │ └─┬─┘ ║ └╥┘ ░ ┌───┐ ░ ┌─┐»\n", " code_qubit_0: |0>┤ X ├─░───■────┼────┼────┼───╫───╫────────░─┤ X ├─░─┤M├»\n", " ├───┤ ░ │ │ │ ║ ║ ░ ├───┤ ░ └╥┘»\n", " code_qubit_1: |0>┤ X ├─░────────■────■────┼───╫───╫────────░─┤ X ├─░──╫─»\n", " ├───┤ ░ │ ║ ║ ░ ├───┤ ░ ║ »\n", " code_qubit_2: |0>┤ X ├─░──────────────────■───╫───╫────────░─┤ X ├─░──╫─»\n", " └───┘ ░ ║ ║ ░ └───┘ ░ ║ »\n", "round_0_link_bit_0: 0 ═════════════════════════════╩═══╬═══════════════════╬═»\n", " ║ ║ »\n", "round_0_link_bit_1: 0 ═════════════════════════════════╩═══════════════════╬═»\n", " ║ »\n", " code_bit_0: 0 ═════════════════════════════════════════════════════╩═»\n", " »\n", " code_bit_1: 0 ═══════════════════════════════════════════════════════»\n", " »\n", " code_bit_2: 0 ═══════════════════════════════════════════════════════»\n", " »\n", "« \n", "« link_qubit_0: ──────\n", "« \n", "« link_qubit_1: ──────\n", "« \n", "« code_qubit_0: ──────\n", "« ┌─┐ \n", "« code_qubit_1: ┤M├───\n", "« └╥┘┌─┐\n", "« code_qubit_2: ─╫─┤M├\n", "« ║ └╥┘\n", "«round_0_link_bit_0: ═╬══╬═\n", "« ║ ║ \n", "«round_0_link_bit_1: ═╬══╬═\n", "« ║ ║ \n", "« code_bit_0: ═╬══╬═\n", "« ║ ║ \n", "« code_bit_1: ═╩══╬═\n", "« ║ \n", "« code_bit_2: ════╩═\n", "« \n" ] } ], "source": [ "empty_code.readout()\n", "print_circuits(empty_code)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Running a repetition code\n", "\n", "The code object produces the circuits required for the code. The user can then run them using any method they prefer, allowing full control over compilation, backends, noise models and so on. \n", "\n", "For example, here we run them without noise on the qasm_simulator." ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "ExecuteTime": { "end_time": "2019-12-10T22:05:19.678721Z", "start_time": "2019-12-10T22:05:19.541156Z" }, "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "========= logical 0 =========\n", "\n", "{'000 00 00': 1024}\n", "\n", "========= logical 1 =========\n", "\n", "{'111 00 00': 1024}\n" ] } ], "source": [ "circuits = code.get_circuit_list()\n", "job = execute( circuits, Aer.get_backend('qasm_simulator') )\n", "raw_results = {}\n", "for log in ['0','1']:\n", " raw_results[log] = job.result().get_counts(log)\n", " print('\\n========= logical',log,'=========\\n')\n", " print(raw_results[log])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here the strings from right to left represent the outputs of the syndrome measurement rounds, followed by the final measurement of the code qubits.\n", "\n", "These results need to be rewritten in a different form in order to be decoded. To do this we can use the process_results method of the code object." ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "ExecuteTime": { "end_time": "2019-12-10T22:05:19.686478Z", "start_time": "2019-12-10T22:05:19.681431Z" } }, "outputs": [ { "data": { "text/plain": [ "{'0': {'0 0 00 00 00': 1024}, '1': {'1 1 00 00 00': 1024}}" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "code.process_results( raw_results )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The result is a dictionary whose keys are bit strings that represent outputs of the circuit, and the values represent the number of shots for which this output occurred.\n", "\n", "The strings are not the direct output from the circuits. They have been processed to take the form that helps us correct errors. Here's a short guided tour.\n", "\n", "* The 0 0 to the far left for the logical 0 result, and the 1 1 to the far left of the logical 1, are the logical readout. Any code qubit could be used for this readout, since they should (without errors) all be equal. So we could have just one result here, for one arbitrarily chosen code qubit. Or we could have $d$, one for each qubit. Instead we have two, from the two qubits at either end of the line. This is because it works best with the decoder (which we'll use later). In the absence of errors, these two values will always be equal.\n", "\n", "* The following 0000 is the $d-1$ results of the syndrome measurements for the first round.A 0 implies that the corresponding pair of qubits where the same, and 1 implies different. There are $d-1$ results because the line of $d$ code qubits has $d-1$ possible neighboring pairs. In the absence of errors, they will all be 0.\n", "\n", "* The 0000 that follows that is the syndrome change between the first and second rounds. It is therefore the bitwise OR of the syndrome measurement results from the second round with those from the first. In the absence of errors, they will all be 0.\n", "\n", "* Subsequent blocks follow the same formula, though the last requires some comment. This is not measured using the standard method (with a link qubit). Instead it is calculated from the final readout measurement of all code qubits. Again it is presented as a syndrome change, and will be all 0 in the absence of errors. This is the $T+1$-th block of syndrome measurements since, as it is not done in the same way as the others, it is not counted among the $T$ syndrome measurement rounds.\n", "\n", "**Example 1:** 0 0 0110 0000 0000 would represent a $d=5$, $T=2$ repetition code with encoded 0. The syndrome shows that (most likely) the middle code qubit was flipped by an error before the first measurement round. This causes it to disagree with both neighboring code qubits for the rest of the circuit. This is shown by the syndrome in the first round, but the blocks for subsequent rounds do not report it as it no longer represents a change. Other sets of errors could also have caused this syndrome, but they would need to be more complex and so presumably less likely.\n", "\n", "**Example 2:** 0 0 0010 0010 0000 would represent a $d=5$, $T=2$ repetition code with encoded 0. Here one of the syndrome measurements reported a difference between two code qubits in the first round, leading to a 1. The next round did not see the same effect, and so resulted in a 0. However, since this disagreed with the previous result for the same syndrome measurement, and since we track syndrome changes, this change results in another 1. Subsequent rounds also do not detect anything, but this no longer represents a change and hence results in a 0 in the same position. Most likely the measurement result leading to the first 1 was an error.\n", "\n", "**Example 3:** 0 1 0000 0001 0000 would represent a $d=5$, $T=2$ repetition code with encoded 1. A code qubit on the end of the line is flipped before the second round of syndrome measurements. This is detected by only a single syndrome measurement, because it is on the end of the line. For the same reason, it also disturbs one of the logical readouts.\n", "\n", "Note that in all these examples, a single error causes exactly two characters in the string to change from the value it would have with no errors. This is, in fact, the reason why the logical output consists of both endpoints. It is a property that will be used by the decoder." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To see the effects of noise, we need to specify a noise model. For example, let's set up a simple noise model with gate and measurement errors." ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "ExecuteTime": { "end_time": "2019-12-10T22:05:19.695272Z", "start_time": "2019-12-10T22:05:19.689085Z" } }, "outputs": [], "source": [ "from qiskit.providers.aer.noise import NoiseModel\n", "from qiskit.providers.aer.noise.errors import pauli_error, depolarizing_error\n", "\n", "def get_noise(p_meas,p_gate):\n", "\n", " error_meas = pauli_error([('X',p_meas), ('I', 1 - p_meas)])\n", " error_gate1 = depolarizing_error(p_gate, 1)\n", " error_gate2 = error_gate1.tensor(error_gate1)\n", "\n", " noise_model = NoiseModel()\n", " noise_model.add_all_qubit_quantum_error(error_meas, \"measure\")\n", " noise_model.add_all_qubit_quantum_error(error_gate1, [\"u1\", \"u2\", \"u3\"])\n", " noise_model.add_all_qubit_quantum_error(error_gate2, [\"cx\"])\n", " \n", " return noise_model" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "ExecuteTime": { "end_time": "2019-12-10T22:05:19.703392Z", "start_time": "2019-12-10T22:05:19.698326Z" } }, "outputs": [], "source": [ "noise_model = get_noise(0.04,0.04)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This can then be run to generate noisy results. Let's make a quick function to do this, and then run it." ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "ExecuteTime": { "end_time": "2019-12-10T22:05:19.711079Z", "start_time": "2019-12-10T22:05:19.705910Z" }, "scrolled": false }, "outputs": [], "source": [ "def get_syndrome(code,noise_model,shots=1014):\n", " \n", " circuits = code.get_circuit_list()\n", "\n", " job = execute( circuits, Aer.get_backend('qasm_simulator'),noise_model=noise_model, shots=shots )\n", " raw_results = {}\n", " for log in ['0','1']:\n", " raw_results[log] = job.result().get_counts(log)\n", "\n", " return code.process_results( raw_results )" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "ExecuteTime": { "end_time": "2019-12-10T22:05:19.875001Z", "start_time": "2019-12-10T22:05:19.713652Z" }, "scrolled": false }, "outputs": [ { "data": { "text/plain": [ "{'0': {'1 1 10 10 11': 1,\n", " '0 0 00 01 01': 36,\n", " '0 0 10 11 10': 1,\n", " '0 1 00 10 11': 2,\n", " '0 1 01 10 10': 1,\n", " '0 1 00 00 10': 3,\n", " '0 0 00 00 11': 32,\n", " '0 0 00 11 11': 6,\n", " '0 1 01 00 00': 1,\n", " '0 0 01 00 10': 2,\n", " '0 1 00 11 10': 1,\n", " '0 0 11 11 00': 2,\n", " '0 0 00 01 10': 20,\n", " '1 0 01 01 10': 7,\n", " '0 0 01 00 01': 2,\n", " '0 1 10 10 01': 2,\n", " '1 0 00 00 01': 4,\n", " '1 1 00 01 10': 1,\n", " '0 0 10 10 00': 60,\n", " '0 1 00 00 01': 37,\n", " '1 0 00 11 10': 2,\n", " '0 0 00 00 00': 554,\n", " '0 0 10 10 11': 3,\n", " '1 0 01 10 10': 2,\n", " '0 1 00 01 00': 11,\n", " '1 0 10 01 01': 1,\n", " '0 0 11 00 00': 3,\n", " '1 0 10 10 10': 4,\n", " '1 0 00 00 10': 47,\n", " '0 1 00 11 01': 1,\n", " '1 1 01 10 00': 1,\n", " '0 0 01 01 00': 48,\n", " '1 0 00 10 00': 19,\n", " '0 1 10 11 00': 2,\n", " '0 0 00 11 00': 12,\n", " '0 1 01 11 11': 1,\n", " '0 1 00 01 11': 1,\n", " '1 0 00 01 11': 2,\n", " '0 0 00 10 10': 41,\n", " '1 1 00 00 11': 4,\n", " '0 0 10 11 01': 4,\n", " '0 0 01 11 10': 2,\n", " '1 0 01 01 01': 2,\n", " '0 0 00 10 01': 5,\n", " '0 0 01 10 00': 12,\n", " '0 0 01 01 11': 2,\n", " '0 0 10 00 10': 3,\n", " '1 0 11 11 10': 2,\n", " '0 1 01 01 01': 2},\n", " '1': {'0 1 10 10 10': 2,\n", " '1 1 00 10 10': 57,\n", " '0 1 10 01 01': 1,\n", " '1 1 11 11 11': 2,\n", " '1 1 11 00 00': 2,\n", " '1 0 00 11 01': 2,\n", " '1 1 00 10 01': 7,\n", " '1 1 11 11 00': 3,\n", " '1 1 01 01 11': 5,\n", " '1 1 10 10 11': 4,\n", " '1 1 01 11 10': 3,\n", " '1 1 00 11 11': 1,\n", " '1 1 01 01 00': 39,\n", " '0 1 00 10 11': 1,\n", " '0 1 01 10 10': 1,\n", " '0 1 00 00 10': 45,\n", " '1 0 00 01 00': 15,\n", " '0 0 00 00 11': 2,\n", " '1 1 10 10 00': 52,\n", " '1 1 10 11 10': 1,\n", " '1 1 00 01 01': 37,\n", " '1 1 01 00 01': 4,\n", " '1 1 01 10 11': 1,\n", " '0 1 00 11 10': 1,\n", " '1 0 01 00 00': 2,\n", " '1 1 00 11 00': 12,\n", " '1 1 10 11 01': 4,\n", " '0 0 00 01 10': 1,\n", " '1 1 11 01 10': 1,\n", " '1 0 00 00 01': 33,\n", " '1 1 00 01 10': 12,\n", " '1 1 11 10 01': 1,\n", " '0 1 00 00 01': 1,\n", " '1 0 00 11 10': 1,\n", " '1 1 01 00 10': 1,\n", " '0 1 00 01 00': 4,\n", " '1 1 00 00 00': 553,\n", " '1 0 10 10 10': 1,\n", " '1 0 00 00 10': 2,\n", " '0 1 00 11 01': 2,\n", " '1 1 01 10 00': 16,\n", " '0 1 01 01 10': 3,\n", " '0 1 00 10 00': 17,\n", " '1 0 00 10 00': 1,\n", " '1 0 10 11 00': 1,\n", " '0 1 00 01 11': 4,\n", " '1 1 01 11 01': 2,\n", " '1 1 00 00 11': 36,\n", " '1 0 01 01 01': 3,\n", " '1 1 10 00 10': 5,\n", " '1 0 00 10 11': 4,\n", " '1 0 10 10 01': 3}}" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "get_syndrome(code,noise_model)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here the non-noisy results are the most likely. The rest of the samples are distributed among other possibilities with non-trivial syndromes." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Decoding a repetition code\n", "\n", "Noisy results can change the logical value at readout, and so affect our ability to read out the logical qubit. This can be mitigated by looking at the syndrome values. These can tell us whether or not the logical values are most likely to have changed, and so allow us to correct for the errors. The process of analyzing the syndrome to correct for the errors is called 'decoding'. We do this by building a decoding object for our code." ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "ExecuteTime": { "end_time": "2019-12-10T22:05:24.592276Z", "start_time": "2019-12-10T22:05:19.877650Z" }, "scrolled": false }, "outputs": [], "source": [ "dec = GraphDecoder( RepetitionCode(4,2) )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This analyzes the code by seeing how different kinds of error change the output. With this information, along with a decoding algorithm, we can determine what the logical value is most likely to have been.\n", "\n", "For example, let's use the 'matching' algorithm to decode, which is based on minimum weight perfect matching. This takes specific output strings as input. We'll give it the simple example string '1 0 001 100 100', for a logical 1 that has suffered two errors." ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "ExecuteTime": { "end_time": "2019-12-10T22:05:24.599755Z", "start_time": "2019-12-10T22:05:24.594201Z" } }, "outputs": [ { "data": { "text/plain": [ "'1 1'" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dec.matching('1 0 001 100 100')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The output is what the logical part should have been. As you can see, the decoder correctly determined that the readout should have been of a logical 1." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When we take many samples, we can determine the probability with which the decoder is incorrect. This should decrease exponentially as the code size is increased, since the configurations of noise that fool the decoder become less likely. The probability of a logical error is calculated using the logical_prob() method. This runs matching by default, but other algorithms can be specified by the algorithm kwarg." ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "ExecuteTime": { "end_time": "2019-12-10T22:06:24.991917Z", "start_time": "2019-12-10T22:05:24.601699Z" }, "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "d = 3 ,log = 0\n", "logical error probability for matching = 0.0323486328125\n", "logical error probability for lookup table = 0.021484375\n", "logical error probability for postselection = 0.0004528985507246377\n", "\n", "d = 3 ,log = 1\n", "logical error probability for matching = 0.02880859375\n", "logical error probability for lookup table = 0.0169677734375\n", "logical error probability for postselection = 0.0\n", "\n", "\n", "d = 4 ,log = 0\n", "logical error probability for matching = 0.01806640625\n", "logical error probability for lookup table = 0.01171875\n", "logical error probability for postselection = 0.0\n", "\n", "d = 4 ,log = 1\n", "logical error probability for matching = 0.0206298828125\n", "logical error probability for lookup table = 0.0074462890625\n", "logical error probability for postselection = 0.0\n", "\n", "\n", "d = 5 ,log = 0\n", "logical error probability for matching = 0.0091552734375\n", "logical error probability for lookup table = 0.002197265625\n", "logical error probability for postselection = 0.0\n", "\n", "d = 5 ,log = 1\n", "logical error probability for matching = 0.0086669921875\n", "logical error probability for lookup table = 0.002197265625\n", "logical error probability for postselection = 0.0\n", "\n", "\n", "d = 6 ,log = 0\n", "logical error probability for matching = 0.005859375\n", "logical error probability for lookup table = 0.0\n", "logical error probability for postselection = 0.0\n", "\n", "d = 6 ,log = 1\n", "logical error probability for matching = 0.0050048828125\n", "logical error probability for lookup table = 0.0001220703125\n", "logical error probability for postselection = 0.0\n", "\n", "\n", "d = 7 ,log = 0\n", "logical error probability for matching = 0.0025634765625\n", "logical error probability for lookup table = 0.0\n", "logical error probability for postselection = 0.0\n", "\n", "d = 7 ,log = 1\n", "logical error probability for matching = 0.002197265625\n", "logical error probability for lookup table = 0.0\n", "logical error probability for postselection = 0.0\n", "\n", "\n" ] } ], "source": [ "for d in range(3,8):\n", " \n", " code = RepetitionCode(d,2)\n", " \n", " results = get_syndrome(code,noise_model=noise_model,shots=8192)\n", " \n", " dec = GraphDecoder(code)\n", "\n", " logical_prob_match = dec.get_logical_prob(results)\n", " logical_prob_lookup = lookuptable_decoding(results,results)\n", " logical_prob_post = postselection_decoding(results)\n", "\n", " for log in ['0','1']:\n", " print('d =',d,',log =',log)\n", " print('logical error probability for matching =',logical_prob_match[log])\n", " print('logical error probability for lookup table =',logical_prob_lookup[log])\n", " print('logical error probability for postselection =',logical_prob_post[log])\n", " print('')\n", " print('')" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "ExecuteTime": { "end_time": "2019-12-10T22:06:25.057392Z", "start_time": "2019-12-10T22:06:24.994598Z" } }, "outputs": [ { "data": { "text/html": [ "

### Version Information

Qiskit SoftwareVersion
Qiskit0.14.0
Terra0.11.0
Aer0.3.4
Ignis0.2.0
Aqua0.6.1
IBM Q Provider0.4.4
System information
Python3.7.5 (default, Oct 25 2019, 10:52:18) \n", "[Clang 4.0.1 (tags/RELEASE_401/final)]
OSDarwin
CPUs4
Memory (Gb)16.0
Tue Dec 10 17:06:25 2019 EST
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "