{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "#Import the pyGSTi module -- you probably want this at the beginning of every notebook\n", "import pygsti\n", "import pygsti.construction as pc" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Creating Gatesets" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [], "source": [ "#There are more or less three ways to create GateSet objects in GST.\n", "# 1) By creating an empty GateSet and populating with calls to build_gate and build_vector\n", "# 2) By a single call to build_gateset, which automates 1)\n", "# 3) By loading from a text-format gateset file\n", "\n", "# We'll do each of these in turn" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [], "source": [ "#1) Create an empty gateset and build gates, rho-vectors, and E-vectors manually\n", "stateSpace = [2] #density matrix is a 2x2 matrix\n", "spaceLabels = [('Q0',)] #interpret the 2x2 density matrix as a single qubit named 'Q0'\n", "gateset1 = pygsti.objects.GateSet()\n", "gateset1.set_rhovec( pc.build_vector(stateSpace,spaceLabels,\"0\") )\n", "gateset1.set_evec( pc.build_vector(stateSpace,spaceLabels,\"1\") )\n", "gateset1.set_gate('Gi', pc.build_gate(stateSpace,spaceLabels,\"I(Q0)\"))\n", "gateset1.set_gate('Gx', pc.build_gate(stateSpace,spaceLabels,\"X(pi/2,Q0)\"))\n", "gateset1.set_gate('Gy', pc.build_gate(stateSpace,spaceLabels,\"Y(pi/2,Q0)\"))\n", "gateset1.set_identity_vec( pc.build_identity_vec(stateSpace) )\n", "gateset1.add_spam_label(0,0,'plus')\n", "gateset1.add_spam_label(0,-1,'minus')\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [], "source": [ "#2) Create a gateset in a single call to build_gateset\n", "gateset2 = pc.build_gateset( [2], [('Q0',)],['Gi','Gx','Gy'], \n", " [ \"I(Q0)\",\"X(pi/2,Q0)\", \"Y(pi/2,Q0)\"],\n", " rhoExpressions=[\"0\"], EExpressions=[\"1\"], \n", " spamLabelDict={'plus': (0,0), 'minus': (0,-1) }) #basis=\"gm\" )" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [], "source": [ "#3) Write a text-format gateset file and read it in.\n", "gateset3_txt = \\\n", "\"\"\"\n", "# Example text file describing a gateset\n", "\n", "# State prepared, specified as a state in the Pauli basis (I,X,Y,Z)\n", "rho\n", "PauliVec\n", "1/sqrt(2) 0 0 1/sqrt(2)\n", "\n", "# State measured as yes outcome, also specified as a state in the Pauli basis\n", "E\n", "PauliVec\n", "1/sqrt(2) 0 0 -1/sqrt(2)\n", "\n", "Gi\n", "PauliMx\n", "1 0 0 0\n", "0 1 0 0\n", "0 0 1 0\n", "0 0 0 1\n", "\n", "Gx\n", "PauliMx\n", "1 0 0 0\n", "0 1 0 0\n", "0 0 0 1\n", "0 0 -1 0\n", "\n", "Gy\n", "PauliMx\n", "1 0 0 0\n", "0 0 0 -1\n", "0 0 1 0\n", "0 1 0 0\n", "\n", "IDENTITYVEC sqrt(2) 0 0 0\n", "SPAMLABEL plus = rho E\n", "SPAMLABEL minus = rho remainder\n", "\"\"\"\n", "open(\"tutorial_files/Example_Gateset.txt\",\"w\").write(gateset3_txt)\n", "\n", "gateset3 = pygsti.io.load_gateset(\"tutorial_files/Example_Gateset.txt\")" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gateset 1:\n", "rhoVec[0] = 0.7071 0 0 0.7071\n", "\n", "\n", "EVec[0] = 0.7071 0 0 -0.7071\n", "\n", "\n", "Gi = \n", " 1.0000 0 0 0\n", " 0 1.0000 0 0\n", " 0 0 1.0000 0\n", " 0 0 0 1.0000\n", "\n", "\n", "Gx = \n", " 1.0000 0 0 0\n", " 0 1.0000 0 0\n", " 0 0 0 -1.0000\n", " 0 0 1.0000 0\n", "\n", "\n", "Gy = \n", " 1.0000 0 0 0\n", " 0 0 0 1.0000\n", " 0 0 1.0000 0\n", " 0 -1.0000 0 0\n", "\n", "\n", "\n", "Gateset 2:\n", "rhoVec[0] = 0.7071 0 0 0.7071\n", "\n", "\n", "EVec[0] = 0.7071 0 0 -0.7071\n", "\n", "\n", "Gi = \n", " 1.0000 0 0 0\n", " 0 1.0000 0 0\n", " 0 0 1.0000 0\n", " 0 0 0 1.0000\n", "\n", "\n", "Gx = \n", " 1.0000 0 0 0\n", " 0 1.0000 0 0\n", " 0 0 0 -1.0000\n", " 0 0 1.0000 0\n", "\n", "\n", "Gy = \n", " 1.0000 0 0 0\n", " 0 0 0 1.0000\n", " 0 0 1.0000 0\n", " 0 -1.0000 0 0\n", "\n", "\n", "\n", "Gateset 3:\n", "rhoVec[0] = 0.7071 0 0 0.7071\n", "\n", "\n", "EVec[0] = 0.7071 0 0 -0.7071\n", "\n", "\n", "Gi = \n", " 1.0000 0 0 0\n", " 0 1.0000 0 0\n", " 0 0 1.0000 0\n", " 0 0 0 1.0000\n", "\n", "\n", "Gx = \n", " 1.0000 0 0 0\n", " 0 1.0000 0 0\n", " 0 0 0 1.0000\n", " 0 0 -1.0000 0\n", "\n", "\n", "Gy = \n", " 1.0000 0 0 0\n", " 0 0 0 -1.0000\n", " 0 0 1.0000 0\n", " 0 1.0000 0 0\n", "\n", "\n", "\n" ] } ], "source": [ "#All three of the above gatesets are identical. See for yourself by printing each one:\n", "print \"Gateset 1:\\n\",gateset1\n", "print \"Gateset 2:\\n\",gateset2\n", "print \"Gateset 3:\\n\",gateset3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Basic Operations with Gatesets" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [], "source": [ "#Once you have a gateset, you can use the functions in GST.GateSetTools to modify them:\n", "depol_gateset3 = gateset3.depolarize(gate_noise=0.1) # 10% depolarization on gates" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [], "source": [ "#Writing a gateset as a text file\n", "pygsti.io.write_gateset(depol_gateset3, \"tutorial_files/Example_depolarizedGateset.txt\", title=\"My Gateset\")" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false, "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "rhoVec[0] = 0.7071 0 0 0.7071\n", "\n", "\n", "EVec[0] = 0.7071 0 0 -0.7071\n", "\n", "\n", "Gi = \n", " 1.0000 0 0 0\n", " 0 0.9000 0 0\n", " 0 0 0.9000 0\n", " 0 0 0 0.9000\n", "\n", "\n", "Gx = \n", " 1.0000 0 0 0\n", " 0 0.9000 0 0\n", " 0 0 0 0.9000\n", " 0 0 -0.9000 0\n", "\n", "\n", "Gy = \n", " 1.0000 0 0 0\n", " 0 0 0 -0.9000\n", " 0 0 0.9000 0\n", " 0 0.9000 0 0\n", "\n", "\n", "\n", "\n", "\n", "Choi Matrices:\n", "Choi(Gi) in pauli basis = \n", " --eigenvals = [0.024999999999999977, 0.024999999999999998, 0.024999999999999998, 0.92500000000000016] \n", "\n", "Choi(Gx) in pauli basis = \n", " --eigenvals = [0.025000000000000005, 0.025000000000000005, 0.025000000000000071, 0.92500000000000049] \n", "\n", "Choi(Gy) in pauli basis = \n", " --eigenvals = [0.025000000000000005, 0.025000000000000005, 0.025000000000000071, 0.92500000000000049] \n", "\n", "Sum of negative Choi eigenvalues = 0.0\n", "rhoVec Penalty (>0 if invalid rhoVecs) = 0.0\n", "EVec Penalty (>0 if invalid EVecs) = 0\n" ] } ], "source": [ "#Printing more detailed information about a gateset\n", "depol_gateset3.print_info()" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 0.095]])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "def SPAM( G ):\n", " return np.dot( G.rhoVecs[0].T, G.EVecs[0])\n", "dp3 = depol_gateset3.copy()\n", "#dp3.set_evec( dp3.get_rho_vec(0), 0)\n", "dp4 = dp3.depolarize(spam_noise=0.1)\n", "SPAM(dp4)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "rhoVec[0] = 0.7071 0 0 0.7071\n", "\n", "\n", "EVec[0] = 0.7071 0 0 -0.7071\n", "\n", "\n", "Gi = \n", " 1.0000 0 0 0\n", " 0 0.9000 0 0\n", " 0 0 0.9000 0\n", " 0 0 0 0.9000\n", "\n", "\n", "Gx = \n", " 1.0000 0 0 0\n", " 0 0.9000 0 0\n", " 0 0 0 0.9000\n", " 0 0 -0.9000 0\n", "\n", "\n", "Gy = \n", " 1.0000 0 0 0\n", " 0 0 0 -0.9000\n", " 0 0 0.9000 0\n", " 0 0.9000 0 0\n", "\n", "\n", "\n" ] } ], "source": [ "print depol_gateset3\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.3" } }, "nbformat": 4, "nbformat_minor": 0 }