{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "We have a \"library\" of python code which contains \"functions\" that\n", "are useful in what we are studying. This notebook is an introduction\n", "to the library, and will hopefully get you started using it." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "A python notebook is divided up into \"cells.\" Some cells (like the one you're are reading right now) just contain information. Other cells (like the one directly below this one) contain python \"code\" that can be \"run\" or \"executed.\" The cell below is the first \"executable\" cell in the notebook, and it must be run before anything else is done. To run or execute a cell you need to (1) click on the cell, and (2) press shift-return. (In other words, hold down the shift key while you press the return key.)\n", "
\n", "There are several things going on in the executable setup cell, and there is a comment before each line to tell you what that line of code does (a comment starts with a \"#\" character. Comments are ignored by the computer. They are just there for people to read.\n", "
\n", "The most important thing here is the \"import\" line. This is what reads the functions from our library so that they are available for use in the rest of the notebook.\n", "
\n", "Our code library is called \"sglib.\" The letters \"sg\" stand for \"Sunday Group\" (I couldn't come up with a better name :-)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# The following line prevents changes to the notebook from being\n", "# saved, unless you specifically ask to save them.\n", "%autosave 0\n", "# The next line allows graphics (plots) in the notebook, using the\n", "# \"matplotlib\" library.\n", "%matplotlib inline\n", "# Import everything our library. \n", "from sglib import *\n", "# Import some things we need from the python \"sympy\" library\n", "from sympy import Symbol" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n",
"The first sg functions we'll talk about are ones that can be used to create column vectors, row vectors, and (square) matrices."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Here's a column vector:\n",
"col(1,2,3)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# And here's a row vector\n",
"row(4,5,6)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The mat()
function will create a square matrix. This is a matrix that has the same number of rows and columns. Mostly we will be using square matrices. If you need to create a matrix that is not square, then you can't do it with the mat()
function. I'll explain how to do that some other time."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"mat(1,0,0,-1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"mat(1,2,3,4,5,6,7,8,9)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"I've also \"predefined\" a number of vectors and matrices that we care about. Here are the three measurement operators $\\sigma_x, \\sigma_y,$ and $\\sigma_z$:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sigma_x, sigma_y, sigma_z"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And here are the states $|+z\\rangle$ and $|-z\\rangle$:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"pZ, mZ"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
\n",
"Just making a column or row appear on the screen won't accomplish much. What we really want to do is to actually create an object that we can do something with. When we use something like the col()
function it gives us back an object of some sort (in this case a column vector). If we don't do anything with the returned object, it just gets echoed to the screen. Instead we can keep the returned object and assign a name to it.\n",
"
\n", "The next cell will create a column vector named \"v1\" that we can use for various calculations." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "v1 = col(1,2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that when we create the vector nothing echoes out on the screen. If we want to see v1, the simplest way to do it is just type in its name and execute the cell:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "v1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Later on, in the section called \"Printing things,\" we'll see fancier ways to display information. But now, let's create a few more objects and do some simple calculations with them." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Make another column vector called \"v2\"\n", "v2 = col(3,4)\n", "# And add it to v1\n", "answer = v1 + v2\n", "# Display the answer\n", "answer" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Make two matrices and add them together\n", "m1 = mat(1,2,3,4)\n", "m2 = mat(3,0,3,2)\n", "result = m1 + m2\n", "# Echo the answer\n", "result" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Now multiply those same two matrices. Since I'm not assigning\n", "# the result a name, it will just echo out.\n", "m1 * m2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "You can also do calculations with \"variables.\" For example, say you\n", "want to multiply a matrix times a vector, not using numbers, but with\n", "the symbols $a, b, c,$ etc. like we do on the board." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# The Symbol()function, which we imported from the \"sympy\"\n", "# library, will create symbolic variables. Note that we tell the function\n", "# the same name as the object we are creating, but we have to put it in\n", "# quotes, like this:\n", "\n", "a = Symbol('a')\n", "\n", "# Echo it\n", "a" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Now create some more variables, and use them to construct a matrix\n", "# and a vector\n", "\n", "b = Symbol('b')\n", "c = Symbol('c')\n", "d = Symbol('d')\n", "x = Symbol('x')\n", "y = Symbol('y')\n", "\n", "m = mat(a,b,c,d)\n", "v = col(x,y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note: You can only \"echo\" one thing per cell. So we need two separate cells to display the matrix and the vector." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "v" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we multiply them, echoing the result:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m*v" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "As we found out above, when you simply put the name of an object\n", "in a cell and execute it, python displays the object. But the way\n", "it's displayed, is not always the way we would like to see\n", "it. For example, in vectors like those above the denominators\n", "would be \"rationalized\"\n", "instead of leaving the $\\sqrt{2}$ in the denominator.\n", "
\n", "\n",
"Here's a function called sg_print()
which will attempt\n",
"to display vectors and matrices in a way that is similar to what we do\n",
"on the board. It works pretty well, but it is not always successful.\n",
"
sg_print()
can also do decimal approximations, which can\n",
"be handy when you're deailing with an object too complicated in its\n",
"exact format:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"M2 = M/(1+sqrt(3))\n",
"M2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sg_print(M, exact=False, ndigs=3)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n", "If you set the \"exact\" parameter to False, then you will get a decimal\n", "approximation. Note that python requires \"True\" and \"False\" to begin with\n", "a capital \"T\" and \"F.\" The \"ndigs\" parameter tells how many \"digits\n", "of accuracy\" you want. In many cases, this will end up meaning the number\n", "of digits after the decimal point. There is a limit to how many digits\n", "you can get, but we are mainly interested in making the number small, so\n", "that things print out in a readable way.\n", "
\n", "\n",
"The reason I used the name sg_print()
is because python\n",
"already has a print()
function:\n",
"
\n",
"The print_eigenvectors()
function finds the eigenvalues and\n",
"eigenvectors of an operator (a matrix) and prints them out in a readable\n",
"format:\n",
"
find_eigenvectors()
function:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"eval, evec = find_eigenvectors(sigma_y)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n", "\"eval\" and \"evec\" are just variables. You can name them anything you\n", "want. The first variable will end up with a list of all the eigenvalues,\n", "and the second one will end up with a list of all the eigenvectors.\n", "You can get a specific one by supplying an index in square \n", "brackets. Note that python, like most computer languages, uses zero for\n", "the first index, rather than one.\n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "eval" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "eval[0]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "eval[1]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "evec" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sg_print(evec[0])\n", "sg_print(evec[1])" ] }, { "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.5" } }, "nbformat": 4, "nbformat_minor": 1 }