{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Example from *Graph-theoretic Simplification of Quantum Circuits with the ZX-calculus*\n", "\n", "This notebook shows how the circuit that is used as a demonstration in the paper https://arxiv.org/abs/1902.03178 is generated and simplified." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First we need to important the standard things" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import sys; sys.path.append('..')\n", "import random\n", "import pyzx as zx\n", "%config InlineBackend.figure_format = 'svg'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we generate the circuit. " ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Circuit on 4 qubits with 350 gates.\n", " 4 is the T-count\n", " 346 Cliffords among which \n", " 109 2-qubit gates and 0 Hadamard gates.\n" ] } ], "source": [ "random.seed(1344) # Make sure the same circuit is generated\n", "g = zx.generate.cliffordT(4,350,p_t=0.010) # Generate the circuit as a ZX-diagram\n", "c = zx.Circuit.from_graph(g) # Convert it to a sequence-of-gates representation\n", "print(c.stats())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This circuit contains many more gates then the one used in the paper. This is because ``generate.clifordT`` generates a lot of single qubit gate strings that can trivially be simplified. Before processing it with our ZX-simplification routine, we therefore process it trough the ``optimize.basic_optimization`` routine to show that our simplification routine is actually doing non-trivial simplifications." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Circuit on 4 qubits with 195 gates.\n", " 4 is the T-count\n", " 191 Cliffords among which \n", " 86 2-qubit gates and 43 Hadamard gates.\n" ] } ], "source": [ "c2 = zx.optimize.basic_optimization(c.split_phase_gates())\n", "print(c2.stats())" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "g = c2.to_graph()\n", "zx.d3.draw(g)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we simplify our circuit" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "spider_simp: 58. 37. 17. 11. 4. 5 iterations\n", "id_simp: 1. 1 iterations\n", "spider_simp: 8. 1. 2 iterations\n", "pivot_simp: 15. 6. 2. 2. 4 iterations\n", "lcomp_simp: 33. 12. 11. 6. 6. 4. 4. 4. 3. 1. 1. 1. 1. 13 iterations\n" ] }, { "data": { "text/html": [ "\n", "
\n", " \n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "zx.simplify.clifford_simp(g) # This does the simplification routine from the paper\n", "g.normalise() # This simply puts the circuit in a more compact format, that allows easy visualisation\n", "zx.d3.draw(g)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Finally we extract a circuit from this diagram." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Circuit on 4 qubits with 41 gates.\n", " 4 is the T-count\n", " 37 Cliffords among which \n", " 21 2-qubit gates and 12 Hadamard gates.\n" ] }, { "data": { "text/html": [ "\n", "
\n", " \n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "c = zx.extract.streaming_extract(g.copy())\n", "print(c.to_basic_gates().stats())\n", "zx.d3.draw(c.to_graph())" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.6.7" } }, "nbformat": 4, "nbformat_minor": 2 }