{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/Users/enielse/research/pyGSTi/packages/pygsti/tools/matrixtools.py:23: UserWarning: Could not import Cython extension - falling back to slower pure-python routines\n", " _warnings.warn(\"Could not import Cython extension - falling back to slower pure-python routines\")\n" ] } ], "source": [ "from __future__ import division, print_function, absolute_import, unicode_literals\n", "import numpy as np\n", "\n", "import pygsti\n", "from pygsti.tools import matrixmod2 # module moved\n", "import pygsti.tools.symplectic as symplectic # and replace 'circuit' with 'symplectic' most places below" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "# Tests for the symplectic code\n", "This is just tests, and doesn't give any info on what of this is. This should run most of the functions in symplectic.py, but currently not absolutely everything." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [], "source": [ "# Tests that check the symplectic random sampler, check_symplectic, the convention converter,\n", "# and the symplectic form constructing function are working.\n", "\n", "# Randomly have odd or even dimension (so things might behave differently for odd or even)\n", "n = 4 + np.random.randint(0,2)\n", "\n", "omega_S = symplectic.symplectic_form(n,convention='standard')\n", "#print(omega_S)\n", "omega_DS = symplectic.symplectic_form(n,convention='directsum')\n", "#print(omega_DS)\n", "omega_DStoS = symplectic.change_symplectic_form_convention(omega_DS)\n", "assert(np.array_equal(omega_S,omega_DStoS))\n", "omega_StoDS = symplectic.change_symplectic_form_convention(omega_S,outconvention='directsum')\n", "assert(np.array_equal(omega_DS,omega_StoDS))\n", "\n", "# Pick a random symplectic matrix in the standard convention and check it is symplectic\n", "s_S = symplectic.random_symplectic_matrix(n)\n", "assert(symplectic.check_symplectic(s_S))\n", "\n", "# Pick a random symplectic matrix in the directsum convention and check it is symplectic\n", "s_DS = symplectic.random_symplectic_matrix(n,convention='directsum')\n", "assert(symplectic.check_symplectic(s_DS,convention='directsum'))\n", "\n", "# Convert the directsum convention to the standard convention and check the output is symplectic in new convention\n", "s_DStoS = symplectic.change_symplectic_form_convention(s_DS)\n", "assert(symplectic.check_symplectic(s_DStoS,convention='standard'))\n", "\n", "# Convert back to the directsum convention, and check the original matrix is recovered.\n", "s_DStoStoDS = symplectic.change_symplectic_form_convention(s_DStoS,outconvention='directsum')\n", "assert(np.array_equal(s_DS,s_DStoStoDS))\n", "\n", "# Check that the inversion function is working.\n", "sin = symplectic.inverse_symplectic(s_S)\n", "assert(np.array_equal(matrixmod2.dotmod2(sin,s_S),np.identity(2*n,int)))\n", "assert(np.array_equal(matrixmod2.dotmod2(s_S,sin),np.identity(2*n,int)))" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [], "source": [ "# Check the Clifford sampler runs.\n", "s, p = symplectic.random_clifford(n)\n", "\n", "# Check that a randomly sampled Clifford is a valid Clifford\n", "assert(symplectic.check_valid_clifford(s,p))\n", "\n", "# Check the inverse Clifford function runs, and gives a valid Clifford\n", "sin, pin = symplectic.inverse_clifford(s,p)\n", "assert(symplectic.check_valid_clifford(sin,pin))\n", "\n", "# Check the symplectic matrix part of the inverse Clifford works\n", "assert(np.array_equal(matrixmod2.dotmod2(sin,s),np.identity(2*n,int)))\n", "assert(np.array_equal(matrixmod2.dotmod2(s,sin),np.identity(2*n,int)))\n", "\n", "# Check that the composite Clifford function runs, and works correctly in the special case whereby\n", "# one Clifford is the inverse of the other.\n", "scomp, pcomp = symplectic.compose_cliffords(s,p,sin,pin)\n", "assert(np.array_equal(scomp,np.identity(2*n,int)))\n", "assert(np.array_equal(pcomp,np.zeros(2*n,int)))\n", "\n", "# Check the p returned is unchanged when the seed is valid.\n", "pvalid = symplectic.construct_valid_phase_vector(s,p)\n", "assert(np.array_equal(p,pvalid))\n", "\n", "# Check that p returned is a valid Clifford when the input pseed is not\n", "pseed = (p - 1) % 2\n", "pvalid = symplectic.construct_valid_phase_vector(s,pseed)\n", "assert(symplectic.check_valid_clifford(s,pvalid))" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [], "source": [ "# OLD NOTE: symplectic_representation -> standard_symplectic_representations\n", "# and return value is a *single* dictionary holding (smatrix,pvector) tuples\n", "\n", "# Get the full hard-coded symplectic rep dictionaries\n", "srep_dict = symplectic.standard_symplectic_representations()\n", "assert('CNOT' in list(srep_dict.keys()))\n", "\n", "# Get the a subset of the full hard-coded symplectic rep dictionaries\n", "srep_dict = symplectic.standard_symplectic_representations(['CNOT'])\n", "assert('CNOT' in list(srep_dict.keys()))" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [], "source": [ "# Define a Clifford circuit to test `composite_clifford_from_clifford_circuit`\n", "glist = []\n", "glist.append(pygsti.obj.Label('CPHASE',(0,1)))\n", "glist.append(pygsti.obj.Label('SWAP',(0,3)))\n", "glist.append(pygsti.obj.Label('H',(2)))\n", "glist.append(pygsti.obj.Label('P',(2)))\n", "glist.append(pygsti.obj.Label('HP',(0)))\n", "glist.append(pygsti.obj.Label('PH',(2)))\n", "glist.append(pygsti.obj.Label('HPH',(1)))\n", "glist.append(pygsti.obj.Label('CNOT',(2,0)))\n", "\n", "#OLD: c = circuit.Circuit(gate_list=glist,n=n)\n", "c = pygsti.obj.Circuit(gatestring=glist,num_lines=n)\n", "\n", "# This checks the function runs when we don't provide a symplectic library.\n", "s, p = symplectic.composite_clifford_from_clifford_circuit(c)\n", "\n", "# This checks the function runs when we do provide a symplectic library.\n", "# OLD: sdict, pdict = circuit.symplectic_representation()\n", "# OLD: s, p = symplectic.composite_clifford_from_clifford_circuit(c,s_dict = sdict, p_dict = pdict)\n", "srep_dict = symplectic.standard_symplectic_representations()\n", "s, p = symplectic.composite_clifford_from_clifford_circuit(c,srep_dict)\n", "\n", "# Define a Clifford circuit that composes to the identity\n", "glist = []\n", "glist.append(pygsti.obj.Label('CPHASE',(0,1)))\n", "glist.append(pygsti.obj.Label('CPHASE',(1,0)))\n", "glist.append(pygsti.obj.Label('H',(2)))\n", "glist.append(pygsti.obj.Label('H',(2)))\n", "glist.append(pygsti.obj.Label('X',(3)))\n", "glist.append(pygsti.obj.Label('X',(3)))\n", "\n", "c = pygsti.obj.Circuit(gatestring=glist,num_lines=n)\n", "\n", "# Check the composite clifford functions says this circuit is the identity. \n", "s, p = symplectic.composite_clifford_from_clifford_circuit(c)\n", "assert(np.array_equal(scomp,np.identity(2*n,int)))\n", "assert(np.array_equal(pcomp,np.zeros(2*n,int)))" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [], "source": [ "H = (1/np.sqrt(2))*np.array([[1.,1.],[1.,-1.]],complex)\n", "s, p = symplectic.unitary_to_symplectic_1Q(H)\n", "assert(np.array_equal(s,srep_dict['H'][0])) # OLD asserts used s_dict['H']\n", "assert(np.array_equal(p,srep_dict['H'][1])) # or p_dict['H']\n", "s, p = symplectic.unitary_to_symplectic(H)\n", "assert(np.array_equal(s,srep_dict['H'][0]))\n", "assert(np.array_equal(p,srep_dict['H'][1]))\n", "\n", "\n", "CNOT = np.array([[1.,0.,0.,0.],[0.,1.,0.,0.],[0.,0.,0.,1.],[0.,0.,1.,0.]],complex)\n", "s, p = symplectic.unitary_to_symplectic_2Q(CNOT)\n", "assert(np.array_equal(s,srep_dict['CNOT'][0]))\n", "assert(np.array_equal(p,srep_dict['CNOT'][1]))\n", "s, p = symplectic.unitary_to_symplectic(CNOT)\n", "assert(np.array_equal(s,srep_dict['CNOT'][0]))\n", "assert(np.array_equal(p,srep_dict['CNOT'][1]))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "deletable": true, "editable": 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.10" } }, "nbformat": 4, "nbformat_minor": 2 }