{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Lab 1 - Vectors and Matrices\n", "This notebook demonstrates the use of vectors and matrices in IPython. Note that the basis is not explicit in any of these operations. You must keep track of the basis yourself (using variable names, or notes etc)." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from numpy import array, dot, outer, sqrt, matrix\n", "from numpy.linalg import eig, eigvals\n", "from matplotlib.pyplot import hist" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 2])" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rv = array([1,2]) # a row vector\n", "rv" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[3],\n", " [4]])" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cv = array([[3],[4]]) # a column vector\n", "cv" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Two kinds of vector products we'll see: inner product (dot product) and outer product\n", "\n", "### 1) Use the function _dot(vector1, vector2)_ to find the dot product of rv and cv. Does the order of the arguments matter?" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([11])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dot(rv,cv)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "ename": "ValueError", "evalue": "shapes (2,1) and (2,) not aligned: 1 (dim 1) != 2 (dim 0)", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mdot\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcv\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mrv\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mValueError\u001b[0m: shapes (2,1) and (2,) not aligned: 1 (dim 1) != 2 (dim 0)" ] } ], "source": [ "dot(cv,rv)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2) Use the function _outer(vector1, vector2)_ to find the outer product of rv and cv. Does the order of the arguments matter?" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[3, 4],\n", " [6, 8]])" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "outer(rv,cv)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[3, 6],\n", " [4, 8]])" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "outer(cv,rv)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## II. Complex vectors" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "# Complex numbers in python have a j term:\n", "a = 1+2j" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "v1 = array([1+2j, 3+2j, 5+1j, 4+0j])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The complex conjugate changes the sign of the imaginary part:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1.-2.j, 3.-2.j, 5.-1.j, 4.-0.j])" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v1.conjugate()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 3) Use _dot()_ and _.conjugate()_ to find the dot product of v1 and it's own conjugate:" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(60+0j)" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dot(v1.conjugate(),v1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## III. Matrices" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[2, 1],\n", " [2, 1]])" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# a two-dimensional array\n", "m1 = array([[2,1],[2,1]])\n", "m1" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[2, 2],\n", " [1, 1]])" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# can find transpose with the T method:\n", "m1.T" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(array([ 3., 0.]), array([[ 0.70710678, -0.4472136 ],\n", " [ 0.70710678, 0.89442719]]))" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# find the eigenvalues and eigenvectors of a matrix:\n", "eig(m1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Can also use the `matrix` type which is like array but restricts to 2D. Also, `matrix` adds `.H` and `.I` methods for hermitian and inverse, respectively. For more information, see Stack Overflow question [#4151128](http://stackoverflow.com/questions/4151128/what-are-the-differences-between-numpy-arrays-and-matrices-which-one-should-i-u)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "m2 = matrix( [[2,1],[2,1]])" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "matrix([[2, 2],\n", " [1, 1]])" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m2.H" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(array([ 3., 0.]), matrix([[ 0.70710678, -0.4472136 ],\n", " [ 0.70710678, 0.89442719]]))" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "eig(m2)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "# use a question mark to get help on a command\n", "eig?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Examples:\n", "## Example 1.4\n", "Find the eigenvalues and eigenvectors of M = ([0,1],[-2,3]])" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "M14 = array([[0,1],[-2,3]])" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(array([ 1., 2.]), array([[-0.70710678, -0.4472136 ],\n", " [-0.70710678, -0.89442719]]))" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "eig(M14)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Interpret this result:\n", "the two eigenvalues are 1 and 2\n", "the eigenvectors are strange decimals, but we can check them against the stated solution:" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.70710678118654746" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1/sqrt(2) # this is the value for both entries in the first eigenvector" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.44721359549995793" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1/sqrt(5) # this is the first value in the second eigenvector" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.89442719099991586" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2/sqrt(5) # this is the second value in the second eigenvector" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1., 2.])" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "eigvals(M14)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Signs are opposite compared to the book, but it turns out that (-) doesn't matter in the interpretation of eigenvectors: only \"direction\" matters (the relative size of the entries)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example: Problem 1.16 using Ipython functions" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [], "source": [ "M16 = array([[0,-1j],[1j,0]])" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [], "source": [ "evals, evecs = eig(M16)" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[-0.00000000-0.70710678j, 0.70710678+0.j ],\n", " [ 0.70710678+0.j , 0.00000000-0.70710678j]])" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "evecs" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([-0.00000000-0.70710678j, 0.70710678+0.j ])" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "evecs[:,0]" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0.70710678+0.j , 0.00000000-0.70710678j])" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "evecs[:,1]" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-1.6653345369377348e-16j" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dot(evecs[:,0].conjugate(),evecs[:,1])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Part 2: Using QuTiP\n", "Keeping track of row and column vectors in Ipython is somewhat artificial and tedious. The QuTiP library is designed to take care of many of these headaches" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [], "source": [ "from qutip import *" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "Quantum object: dims = [[1], [2]], shape = (1, 2), type = bra\\begin{equation*}\\left(\\begin{array}{*{11}c}1.0 & 2.0\\\\\\end{array}\\right)\\end{equation*}" ], "text/plain": [ "Quantum object: dims = [[1], [2]], shape = (1, 2), type = bra\n", "Qobj data =\n", "[[ 1. 2.]]" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Create a row vector:\n", "qv = Qobj([[1,2]])\n", "qv" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "Quantum object: dims = [[2], [1]], shape = (2, 1), type = ket\\begin{equation*}\\left(\\begin{array}{*{11}c}1.0\\\\2.0\\\\\\end{array}\\right)\\end{equation*}" ], "text/plain": [ "Quantum object: dims = [[2], [1]], shape = (2, 1), type = ket\n", "Qobj data =\n", "[[ 1.]\n", " [ 2.]]" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Find the corresponding column vector\n", "qv.dag()" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "Quantum object: dims = [[1], [2]], shape = (1, 2), type = bra\\begin{equation*}\\left(\\begin{array}{*{11}c}(1.0+2.0j) & (4.0-1.0j)\\\\\\end{array}\\right)\\end{equation*}" ], "text/plain": [ "Quantum object: dims = [[1], [2]], shape = (1, 2), type = bra\n", "Qobj data =\n", "[[ 1.+2.j 4.-1.j]]" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "qv2 = Qobj([[1+2j,4-1j]])\n", "qv2" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "Quantum object: dims = [[2], [1]], shape = (2, 1), type = ket\\begin{equation*}\\left(\\begin{array}{*{11}c}(1.0-2.0j)\\\\(4.0+1.0j)\\\\\\end{array}\\right)\\end{equation*}" ], "text/plain": [ "Quantum object: dims = [[2], [1]], shape = (2, 1), type = ket\n", "Qobj data =\n", "[[ 1.-2.j]\n", " [ 4.+1.j]]" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "qv2.dag()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Vector products in QuTiP\n", "Only need to know one operator: \"\\*\"\n", "The product will depend on the order, either inner or outer" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "Quantum object: dims = [[1], [1]], shape = (1, 1), type = bra\\begin{equation*}\\left(\\begin{array}{*{11}c}22.0\\\\\\end{array}\\right)\\end{equation*}" ], "text/plain": [ "Quantum object: dims = [[1], [1]], shape = (1, 1), type = bra\n", "Qobj data =\n", "[[ 22.]]" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "qv2*qv2.dag() # inner product (dot product)" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "Quantum object: dims = [[2], [2]], shape = (2, 2), type = oper, isherm = True\\begin{equation*}\\left(\\begin{array}{*{11}c}5.0 & (2.0-9.0j)\\\\(2.0+9.0j) & 17.0\\\\\\end{array}\\right)\\end{equation*}" ], "text/plain": [ "Quantum object: dims = [[2], [2]], shape = (2, 2), type = oper, isherm = True\n", "Qobj data =\n", "[[ 5.+0.j 2.-9.j]\n", " [ 2.+9.j 17.+0.j]]" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "qv2.dag()*qv2 # outer product" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Matrix in QuTiP" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "Quantum object: dims = [[2], [2]], shape = (2, 2), type = oper, isherm = True\\begin{equation*}\\left(\\begin{array}{*{11}c}1.0 & 2.0\\\\2.0 & 1.0\\\\\\end{array}\\right)\\end{equation*}" ], "text/plain": [ "Quantum object: dims = [[2], [2]], shape = (2, 2), type = oper, isherm = True\n", "Qobj data =\n", "[[ 1. 2.]\n", " [ 2. 1.]]" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "qm = Qobj([[1,2],[2,1]])\n", "qm" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([-1., 3.])" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "qm.eigenenergies() # in quantum (as we will learn) eigenvalues often correspond to energy levels" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [], "source": [ "evals, evecs = qm.eigenstates()" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ Quantum object: dims = [[2], [1]], shape = (2, 1), type = ket\n", "Qobj data =\n", "[[-0.70710678]\n", " [ 0.70710678]],\n", " Quantum object: dims = [[2], [1]], shape = (2, 1), type = ket\n", "Qobj data =\n", "[[ 0.70710678]\n", " [ 0.70710678]]], dtype=object)" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "evecs" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "Quantum object: dims = [[2], [1]], shape = (2, 1), type = ket\\begin{equation*}\\left(\\begin{array}{*{11}c}-0.707\\\\0.707\\\\\\end{array}\\right)\\end{equation*}" ], "text/plain": [ "Quantum object: dims = [[2], [1]], shape = (2, 1), type = ket\n", "Qobj data =\n", "[[-0.70710678]\n", " [ 0.70710678]]" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "evecs[0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Practice:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Problem 1.2 using the _hist()_ function." ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAWsAAAEACAYAAAB1dVfhAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAADUNJREFUeJzt3X+M5PVdx/HXi9tWevwoIQq00gZCilatwImkscVOY22u\ntUg1MSkx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c9HsFXMw073tTLslvb2jGW5Wcx7/22r+IbP3SzpT0t1q3t1w\np6QzOp7pGjUXOfdL+l9JT0v6zBwcq2skfUXNuxt2tbePzcFMt7bf57sl3SbprI5m+r+l76llj/+b\npDPX2ge/FAMABfC/9QKAAog1ABRArAGgAGINAAUQawAogFgDQAHEGgAKINYAUMD/A2dWGzCe15ey\nAAAAAElFTkSuQmCC\n", "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Solution\n", "n, bins, patches = hist([10,13,14,14,6,8,7,9,12,14,13,11,10,7,7],bins=5,range=(5,14))" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1., 4., 3., 2., 5.])" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Solution\n", "n" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [], "source": [ "# Solution\n", "pvals = n/n.sum()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Problem 1.8\n", "**Hint:** using sympy, we can calculate the relevant integral. The conds='none' asks the solver to ignore any strange conditions on the variables in the integral. This is fine for most of our integrals. Usually the variables are real and well-behaved numbers." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "first = c/a\n", "second = 1\n" ] } ], "source": [ "# Solution\n", "from sympy import *\n", "c,a,x = symbols(\"c a x\")\n", "Q.positive((c,a))\n", "first = integrate(c*exp(-a*x),(x,0,oo),conds='none')\n", "print(\"first = \",first)\n", "second = integrate(a*exp(-a*x),(x,0,oo),conds='none')\n", "print(\"second = \",second)" ] } ], "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.4" } }, "nbformat": 4, "nbformat_minor": 1 }