{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "9ab292bf", "metadata": { "nbsphinx": "hidden" }, "outputs": [], "source": [ "import sys; sys.path.insert(0, '../..') # So that we import the local copy of pyzx if you have installed from Github" ] }, { "cell_type": "markdown", "id": "2b28a205", "metadata": {}, "source": [ "# Supported Gates\n", "\n", "The pyzx library supports a variety of gates, including all the commonly-used gates in the OpenQASM standard library. It also supports a subset of the OpenQASM file format.\n", "\n", "Here are some examples illustrating the usage:" ] }, { "cell_type": "code", "execution_count": null, "id": "7cd93df8", "metadata": {}, "outputs": [], "source": [ "import pyzx as zx\n", "from pyzx.circuit import Circuit, CNOT\n", "\n", "# Add pyzx gates.\n", "c = Circuit(2)\n", "c.add_gate(\"CNOT\", 0, 1)\n", "c.add_gate(CNOT(1, 0))\n", "\n", "# Adding qasm gates.\n", "s = \"\"\"\n", "OPENQASM 2.0;\n", "include \"qelib1.inc\";\n", "qreg q[1];\n", "z q[0];\n", "\"\"\"\n", "\n", "c1 = zx.qasm(s)\n", "c2 = Circuit.from_qasm(s) # or: Circuit.from_qasm_file(\"filename.qasm\")\n", "\n", "print(c2.to_qasm())" ] }, { "cell_type": "markdown", "id": "4fb99b07", "metadata": {}, "source": [ "The set of supported gates are listed below for reference, using the following function to draw their graphs. (Set `simplify` to `True` to reduce the graphs, and set `show_matrix` to `True` to output their matrices.)" ] }, { "cell_type": "code", "execution_count": null, "id": "084548de", "metadata": {}, "outputs": [], "source": [ "from pyzx.circuit.qasmparser import QASMParser\n", "\n", "def _print_gate_name(gate):\n", " print(gate.name + (\" (adjoint)\" if hasattr(gate, \"adjoint\") and gate.adjoint else \"\"))\n", "\n", "def draw_zx_diagram(num_qubits, gate_or_qasm, simplify=False, show_matrix=False):\n", " if isinstance(gate_or_qasm, str):\n", " qasm = gate_or_qasm\n", " c = QASMParser().parse(f\"qreg q[{num_qubits}];\\n\" + qasm, strict=False)\n", " for gate in c.gates:\n", " _print_gate_name(gate)\n", " print(qasm)\n", " else:\n", " gate = gate_or_qasm\n", " c = Circuit(num_qubits)\n", " c.add_gate(gate)\n", " _print_gate_name(gate)\n", " print(\"(no simple qasm command)\")\n", "\n", " g = c.to_graph()\n", " if simplify:\n", " g.auto_detect_io()\n", " zx.simplify.full_reduce(g)\n", " zx.draw(g)\n", " if show_matrix:\n", " print(g.to_matrix())" ] }, { "cell_type": "markdown", "id": "b9a641a1", "metadata": {}, "source": [ "## One-qubit gates\n", "\n", "### Pauli gates and Hadamard" ] }, { "cell_type": "code", "execution_count": null, "id": "91d88893", "metadata": {}, "outputs": [], "source": [ "draw_zx_diagram(1, 'x q;')\n", "draw_zx_diagram(1, 'y q;')\n", "draw_zx_diagram(1, 'z q;')\n", "draw_zx_diagram(1, 'h q;')" ] }, { "cell_type": "markdown", "id": "4d8dba52", "metadata": {}, "source": [ "### Parametrized one-qubit gates\n", "\n", "These are shown with a phase of π/8 for illustrative purposes." ] }, { "cell_type": "code", "execution_count": null, "id": "2ea662a9", "metadata": {}, "outputs": [], "source": [ "draw_zx_diagram(1, 'rx(0.125*pi) q;')\n", "draw_zx_diagram(1, 'ry(0.125*pi) q;')\n", "draw_zx_diagram(1, 'rz(0.125*pi) q;') # can also use 'p' or 'u1'\n", "\n", "draw_zx_diagram(1, 'u2(0.125*pi,0.125*pi) q[0];')\n", "draw_zx_diagram(1, 'u3(0.125*pi,0.125*pi,0.125*pi) q[0];') # can also use 'u'" ] }, { "cell_type": "markdown", "id": "38f1e292", "metadata": {}, "source": [ "### Fixed Z- and X- rotations defined for convenience" ] }, { "cell_type": "code", "execution_count": null, "id": "175fd12c", "metadata": {}, "outputs": [], "source": [ "draw_zx_diagram(1, 's q;')\n", "draw_zx_diagram(1, 'sdg q;')\n", "draw_zx_diagram(1, 't q;')\n", "draw_zx_diagram(1, 'tdg q;')\n", "draw_zx_diagram(1, 'sx q;')\n", "draw_zx_diagram(1, 'sxdg q;')" ] }, { "cell_type": "markdown", "id": "a206e2f0", "metadata": {}, "source": [ "## Two-qubit gates" ] }, { "cell_type": "markdown", "id": "b4788808", "metadata": {}, "source": [ "### Swap" ] }, { "cell_type": "code", "execution_count": null, "id": "ca9ce1dd", "metadata": {}, "outputs": [], "source": [ "draw_zx_diagram(2, 'swap q[0], q[1];')" ] }, { "cell_type": "markdown", "id": "5ec55ec8", "metadata": {}, "source": [ "### Two-qubit rotations" ] }, { "cell_type": "code", "execution_count": null, "id": "aa10bd37", "metadata": {}, "outputs": [], "source": [ "draw_zx_diagram(2, 'rxx(0.125*pi) q[0], q[1];')\n", "draw_zx_diagram(2, 'rzz(0.125*pi) q[0], q[1];')" ] }, { "cell_type": "markdown", "id": "41366a55", "metadata": {}, "source": [ "### Controlled versions of (non-parametrized) one-qubit gates" ] }, { "cell_type": "code", "execution_count": null, "id": "b3136424", "metadata": {}, "outputs": [], "source": [ "from pyzx.circuit import XCX\n", "draw_zx_diagram(2, 'cx q[0], q[1];')\n", "draw_zx_diagram(2, 'cy q[0], q[1];')\n", "draw_zx_diagram(2, 'cz q[0], q[1];')\n", "draw_zx_diagram(2, 'ch q[0], q[1];')\n", "draw_zx_diagram(2, 'csx q[0], q[1];')\n", "draw_zx_diagram(2, XCX(0, 1))" ] }, { "cell_type": "markdown", "id": "74fa6d83", "metadata": {}, "source": [ "### Controlled versions of parametrized one-qubit gates\n", "\n", "These are shown with a phase of π/8 for illustrative purposes." ] }, { "cell_type": "code", "execution_count": null, "id": "e4eb34ab", "metadata": {}, "outputs": [], "source": [ "draw_zx_diagram(2, 'crx(0.125*pi) q[0], q[1];')\n", "draw_zx_diagram(2, 'cry(0.125*pi) q[0], q[1];')\n", "draw_zx_diagram(2, 'crz(0.125*pi) q[0], q[1];')\n", "\n", "# Note that this differs from 'crz' by a relative phase.\n", "draw_zx_diagram(2, 'cp(0.125*pi) q[0], q[1];') # can also use 'cphase' or 'cu1'\n", "\n", "draw_zx_diagram(2, 'cu3(0.125*pi,0.125*pi,0.125*pi) q[0], q[1];')\n", "draw_zx_diagram(2, 'cu(0.125*pi,0.125*pi,0.125*pi,0.125*pi) q[0], q[1];')" ] }, { "cell_type": "markdown", "id": "b1a96dca", "metadata": {}, "source": [ "## Three-qubit gates" ] }, { "cell_type": "markdown", "id": "514830c5", "metadata": {}, "source": [ "### Controlled versions of two-qubit gates" ] }, { "cell_type": "code", "execution_count": null, "id": "63d0996c", "metadata": {}, "outputs": [], "source": [ "draw_zx_diagram(3, 'cswap q[0], q[1], q[2];') # Fredkin" ] }, { "cell_type": "markdown", "id": "8521a36e", "metadata": {}, "source": [ "### Doubly-controlled one-qubit gates" ] }, { "cell_type": "code", "execution_count": null, "id": "90a9ec61", "metadata": {}, "outputs": [], "source": [ "draw_zx_diagram(3, 'ccx q[0], q[1], q[2];') # Toffoli\n", "draw_zx_diagram(3, 'ccz q[0], q[1], q[2];')" ] }, { "cell_type": "markdown", "id": "a79198ef-d527-4c81-bb9f-d5aa9851c266", "metadata": {}, "source": [ "## Other gates" ] }, { "cell_type": "code", "execution_count": null, "id": "bca3b4c3", "metadata": {}, "outputs": [], "source": [ "from pyzx.circuit import ParityPhase, FSim\n", "\n", "draw_zx_diagram(4, ParityPhase(0.5, 0, 1, 2, 3))\n", "draw_zx_diagram(2, FSim(0, 1, 1/2, 1))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.10.12" } }, "nbformat": 4, "nbformat_minor": 5 }