{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Gate synthesis\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This example demonstrates how the built-in compiler can be used to perform gate\nsynthesis.\n\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Synthesizing Single-Qubit Gates\nWe begin by generating a random gate in $SU(2)$\\; we will synthesize this gate\nlater.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import trueq as tq\n\n# create a Haar random SU(2) gate and print its matrix representation\nU = tq.Gate.random(2)\nU" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can perform single qubit gate decomposition into a number of possible \"modes\".\nHere we decompose the gate U into a ZXZXZ decomposition, which is short hand\nfor $Z(\\theta)X(90)Z(\\phi)X(90)Z(\\gamma)$\\. See\n:py:class:~trueq.math.decomposition.QubitMode for a complete list of all available\nsingle qubit decompositions.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "synthesized_gate = tq.math.QubitMode.ZXZXZ.decompose(U)\n\n# print the synthesized gate as a list of single-qubit rotations about Z and X\nsynthesized_gate" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Synthesizing Two-Qubit Gates\nIn the event that we want to express a two-qubit gate in terms of a different\ntwo-qubit gate, we can use the compiler to synthesize the desired gate.\nHere we decompose a random $SU(4)$ operation so that it can be implemented\nusing iSWAP gates.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# define the gate to be synthesized\ngate_to_be_synthesized = tq.Gate.random(4)\n\n# re-express the gate using an iswap gate as the two-qubit gate\ntwo_qubit_synthesized_gate = tq.math.decompose_unitary(\n target_gate=gate_to_be_synthesized, given_gate=tq.Gate.iswap\n)\n\n# print the synthesized gate\ntwo_qubit_synthesized_gate" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This circuit can be verified to reproduce the original random unitary using an ideal\nsimulator:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "matrix = tq.Simulator().operator(two_qubit_synthesized_gate).mat()\n\n# This will result in an identity gate up to a global complex phase.\ntq.visualization.plot_mat(matrix @ gate_to_be_synthesized.adj.mat)" ] } ], "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.7.12" } }, "nbformat": 4, "nbformat_minor": 0 }