{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "name": "NumPyExercises_01.ipynb", "provenance": [], "collapsed_sections": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" } }, "cells": [ { "cell_type": "code", "metadata": { "id": "UK6FKrQvnyvc", "colab_type": "code", "colab": {} }, "source": [ "import numpy as np" ], "execution_count": 2, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "x372HtpBOI8b", "colab_type": "text" }, "source": [ "In these exercises, where possible, try to utilize the available numpy functionality rather than native python code." ] }, { "cell_type": "markdown", "metadata": { "id": "Hj89Mhdxo4HG", "colab_type": "text" }, "source": [ "# Exercise 1. \n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "t7OQpnRPTQH9", "colab_type": "text" }, "source": [ "Consider the $3\\times 5$ array a3by5 created below." ] }, { "cell_type": "code", "metadata": { "id": "7-C2xtfQn7wm", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 69 }, "outputId": "9f744a76-8826-46a5-e13c-88d59250ccaf" }, "source": [ "a3by5 = np.random.random((3,5))\n", "a3by5" ], "execution_count": 3, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array([[0.57510832, 0.58927687, 0.00135697, 0.0172401 , 0.77029201],\n", " [0.43512053, 0.67630272, 0.82444291, 0.82316868, 0.5305925 ],\n", " [0.20805945, 0.22954856, 0.63356502, 0.89652967, 0.33690487]])" ] }, "metadata": { "tags": [] }, "execution_count": 3 } ] }, { "cell_type": "markdown", "metadata": { "id": "fN7NkTs9pEvP", "colab_type": "text" }, "source": [ "Flatten it and sort the list.\n" ] }, { "cell_type": "markdown", "metadata": { "id": "Pi6Us9m9peCT", "colab_type": "text" }, "source": [ "# Exercise 2.\n" ] }, { "cell_type": "markdown", "metadata": { "id": "EHl1-nllqczu", "colab_type": "text" }, "source": [ "Consider the arrays x, y and z below." ] }, { "cell_type": "code", "metadata": { "id": "M-9h0_qrrETE", "colab_type": "code", "colab": {} }, "source": [ "x = np.array([-4, -3, -2, -1, 1, 2, 3, 4])\n", "y = np.array([-3, -2, -1, 0, 1, 2, 3])\n", "z = np.array([0, 0, 0, 0])" ], "execution_count": 4, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "a_axeFpUrD4y", "colab_type": "text" }, "source": [ "I. Write some code to test whether \n", "\n", "1. at least one of the elements in the array is non-zero.\n", "2. all of the elements in the array are non-zero.\n", "3. at least one of the elements in the array is equal to zero. \n", "\n", "and apply it to each of the three arrays.\n", "\n", "II. Add 5 to every positive entry in each array." ] }, { "cell_type": "markdown", "metadata": { "id": "4BGBV-ynTifC", "colab_type": "text" }, "source": [ "# Exercise 3" ] }, { "cell_type": "markdown", "metadata": { "id": "d0y1-ieNTlDl", "colab_type": "text" }, "source": [ "Consider the Pauli matrices defined below.\n", "\n", "1. Verify that they are traceless\n", "2. Verify that they have determinants -1.\n", "3. Verify that they are Hermitian.\n", "4. Verify that they are unitary.\n", "5. Verify the SU(2) algebra, i.e., the commutation relations among the Pauli matrices. If you don't remember the formulas, derive them.\n" ] }, { "cell_type": "code", "metadata": { "id": "KKieoaw2U3dd", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 121 }, "outputId": "86b606d4-0425-480f-f63f-d54602b53f1f" }, "source": [ "sigX = np.matrix([[0+0j,1+0j],[1+0j,0+0j]])\n", "sigY = np.matrix([[0+0j,0-1j],[0+1j,0+0j]])\n", "sigZ = np.matrix([[1+0j,0+0j],[0+0j,-1+0j]])\n", "print(sigX) \n", "print(sigY)\n", "print(sigZ)" ], "execution_count": 5, "outputs": [ { "output_type": "stream", "text": [ "[[0.+0.j 1.+0.j]\n", " [1.+0.j 0.+0.j]]\n", "[[0.+0.j 0.-1.j]\n", " [0.+1.j 0.+0.j]]\n", "[[ 1.+0.j 0.+0.j]\n", " [ 0.+0.j -1.+0.j]]\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "4WPdcqmBRLH7", "colab_type": "text" }, "source": [ "# Solutions" ] }, { "cell_type": "markdown", "metadata": { "id": "5TmpbnWLRQPm", "colab_type": "text" }, "source": [ "### Exercise 1" ] }, { "cell_type": "code", "metadata": { "id": "mzRzNgZSpxSB", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 121 }, "outputId": "1c1973c9-d683-471f-dbc8-c2592e11ca6f" }, "source": [ "a3by5 = np.random.random((3,5))\n", "print(a3by5)\n", "\n", "print(np.sort(a3by5, axis=None))" ], "execution_count": 6, "outputs": [ { "output_type": "stream", "text": [ "[[0.18112778 0.51840541 0.69346231 0.91198826 0.27033298]\n", " [0.5708392 0.23373761 0.33349345 0.25319413 0.59236421]\n", " [0.32415792 0.83976683 0.58430907 0.36911337 0.76621798]]\n", "[0.18112778 0.23373761 0.25319413 0.27033298 0.32415792 0.33349345\n", " 0.36911337 0.51840541 0.5708392 0.58430907 0.59236421 0.69346231\n", " 0.76621798 0.83976683 0.91198826]\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "FhQcKc62RztY", "colab_type": "text" }, "source": [ "### Exercise 2" ] }, { "cell_type": "code", "metadata": { "id": "md0gAhLCrgc3", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 311 }, "outputId": "8138eed1-c0d9-44cb-c554-db3fa26238f4" }, "source": [ "x = np.array([-4, -3, -2, -1, 1, 2, 3, 4])\n", "y = np.array([-3, -2, -1, 0, 1, 2, 3])\n", "z = np.array([0, 0, 0, 0])\n", "\n", "# Part I\n", "\n", "print('at least one non-zero element in x = ', np.any(x))\n", "print('at least one non-zero element in y = ', np.any(y))\n", "print('at least one non-zero element in z = ', np.any(z))\n", "print('zero is absent in x = ', np.all(x))\n", "print('zero is absent in y = ', np.all(y))\n", "print('zero is absent in z = ', np.all(z))\n", "print('at least one zero element in x = ', np.any(x==0))\n", "print('at least one zero element in y = ', np.any(y==0))\n", "print('at least one zero element in z = ', np.any(z==0))\n", "\n", "# Part II\n", "\n", "print('Method I')\n", "print(x+5*np.heaviside(x,0))\n", "print(y+5*np.heaviside(y,0))\n", "print(z+5*np.heaviside(z,0))\n", "\n", "print('Method II')\n", "print(np.where(x>0, 5, 0) + x)\n", "print(np.where(y>0, y+5, y))\n", "print(np.where(z>0, 5, 0) + z)" ], "execution_count": 7, "outputs": [ { "output_type": "stream", "text": [ "at least one non-zero element in x = True\n", "at least one non-zero element in y = True\n", "at least one non-zero element in z = False\n", "zero is absent in x = True\n", "zero is absent in y = False\n", "zero is absent in z = False\n", "at least one zero element in x = False\n", "at least one zero element in y = True\n", "at least one zero element in z = True\n", "Method I\n", "[-4. -3. -2. -1. 6. 7. 8. 9.]\n", "[-3. -2. -1. 0. 6. 7. 8.]\n", "[0. 0. 0. 0.]\n", "Method II\n", "[-4 -3 -2 -1 6 7 8 9]\n", "[-3 -2 -1 0 6 7 8]\n", "[0 0 0 0]\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "FFeLxxDHYdaZ", "colab_type": "text" }, "source": [ "### Exercise 3" ] }, { "cell_type": "code", "metadata": { "id": "Dq_J-UtHYfre", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 259 }, "outputId": "3a82cd4f-2dcd-4e6d-bf22-0609bf15512d" }, "source": [ "print('Traces: ', np.trace(sigX), np.trace(sigY), np.trace(sigZ))\n", "print('Determinants: ', np.linalg.det(sigX), np.linalg.det(sigY), np.linalg.det(sigZ))\n", "print('sigmaX is Hermitian = ', np.all(sigX-sigX.H==0))\n", "print('sigmaY is Hermitian = ', np.all(sigY-sigY.H==0))\n", "print('sigmaZ is Hermitian = ', np.all(sigZ-sigZ.H==0))\n", "print('Checking if X.Xdagger - 1 = 0')\n", "print('sigmaX is unitary = ', np.all(np.dot(sigX,sigX.H)-np.identity(2)==0))\n", "print('Checking if Y.Ydagger = 1')\n", "print('sigmaY is unitary = ', np.all(np.dot(sigY,sigY.H)==np.identity(2)))\n", "print('Checking if Z-dagger = Z-inverse ')\n", "print('sigmaZ is unitary = ', np.all(sigZ.H==np.linalg.inv(sigZ)))\n", "print('[X,Y]=2iZ = ', np.all(np.dot(sigX,sigY)-np.dot(sigY,sigX)-2j*sigZ==0))\n", "print('[Y,Z]=2iX = ', np.all(np.dot(sigY,sigZ)-np.dot(sigZ,sigY)-2j*sigX==0))\n", "print('[Z,X]=2iY = ', np.all(np.dot(sigZ,sigX)-np.dot(sigX,sigZ)-2j*sigY==0))" ], "execution_count": 8, "outputs": [ { "output_type": "stream", "text": [ "Traces: 0j 0j 0j\n", "Determinants: (-1+0j) (-1+0j) (-1+0j)\n", "sigmaX is Hermitian = True\n", "sigmaY is Hermitian = True\n", "sigmaZ is Hermitian = True\n", "Checking if X.Xdagger - 1 = 0\n", "sigmaX is unitary = True\n", "Checking if Y.Ydagger = 1\n", "sigmaY is unitary = True\n", "Checking if Z-dagger = Z-inverse \n", "sigmaZ is unitary = True\n", "[X,Y]=2iZ = True\n", "[Y,Z]=2iX = True\n", "[Z,X]=2iY = True\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "lOlKqqeoiB7n", "colab_type": "code", "colab": {} }, "source": [ "" ], "execution_count": null, "outputs": [] } ] }