Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Commit

Permalink
[LinearAlgebra] Add workbook for the tutorial (#267)
Browse files Browse the repository at this point in the history
This change also
* fixes the testing harness for task 1 to make the error message produced by unmodified code more clear (handle returning ... gracefully).
* improves the set of tests for task 14 to cover more corner cases.

Co-Authored-By: Mariia Mykhailova <mamykhai@microsoft.com>
  • Loading branch information
vivanwin and tcNickolas committed Jan 19, 2020
1 parent a1ebdb3 commit 06fd21c
Show file tree
Hide file tree
Showing 4 changed files with 1,131 additions and 26 deletions.
24 changes: 12 additions & 12 deletions tutorials/ComplexArithmetic/Workbook_ComplexArithmetic.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"[Return to task 1 of the Complex Arithmetic kata.](./ComplexArithmetic.ipynb#Exercise-1:-Powers-of-$i$.)"
"[Return to task 1 of the Complex Arithmetic tutorial.](./ComplexArithmetic.ipynb#Exercise-1:-Powers-of-$i$.)"
]
},
{
Expand Down Expand Up @@ -156,7 +156,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"[Return to task 2 of the Complex Arithmetic kata.](./ComplexArithmetic.ipynb#Exercise-2:-Complex-addition.)"
"[Return to task 2 of the Complex Arithmetic tutorial.](./ComplexArithmetic.ipynb#Exercise-2:-Complex-addition.)"
]
},
{
Expand Down Expand Up @@ -209,7 +209,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"[Return to task 3 of the Complex Arithmetic kata.](./ComplexArithmetic.ipynb#Exercise-3:-Complex-multiplication.)"
"[Return to task 3 of the Complex Arithmetic tutorial.](./ComplexArithmetic.ipynb#Exercise-3:-Complex-multiplication.)"
]
},
{
Expand Down Expand Up @@ -256,7 +256,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"[Return to task 4 of the Complex Arithmetic kata.](./ComplexArithmetic.ipynb#Exercise-4:-Complex-conjugate.)"
"[Return to task 4 of the Complex Arithmetic tutorial.](./ComplexArithmetic.ipynb#Exercise-4:-Complex-conjugate.)"
]
},
{
Expand Down Expand Up @@ -317,7 +317,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"[Return to task 5 of the Complex Arithmetic kata.](./ComplexArithmetic.ipynb#Exercise-5:-Complex-division.)"
"[Return to task 5 of the Complex Arithmetic tutorial.](./ComplexArithmetic.ipynb#Exercise-5:-Complex-division.)"
]
},
{
Expand Down Expand Up @@ -367,7 +367,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"[Return to task 6 of the Complex Arithmetic kata.](./ComplexArithmetic.ipynb#Exercise-6:-Modulus.)"
"[Return to task 6 of the Complex Arithmetic tutorial.](./ComplexArithmetic.ipynb#Exercise-6:-Modulus.)"
]
},
{
Expand Down Expand Up @@ -419,7 +419,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"[Return to task 7 of the Complex Arithmetic kata.](./ComplexArithmetic.ipynb#Exercise-7:-Complex-exponents.)"
"[Return to task 7 of the Complex Arithmetic tutorial.](./ComplexArithmetic.ipynb#Exercise-7:-Complex-exponents.)"
]
},
{
Expand Down Expand Up @@ -493,7 +493,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"[Return to task 8 of the Complex Arithmetic kata.](./ComplexArithmetic.ipynb#Exercise-8*:-Complex-powers-of-real-numbers.)"
"[Return to task 8 of the Complex Arithmetic tutorial.](./ComplexArithmetic.ipynb#Exercise-8*:-Complex-powers-of-real-numbers.)"
]
},
{
Expand Down Expand Up @@ -563,7 +563,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"[Return to task 9 of the Complex Arithmetic kata.](./ComplexArithmetic.ipynb#Exercise-9:-Cartesian-to-polar-conversion.)"
"[Return to task 9 of the Complex Arithmetic tutorial.](./ComplexArithmetic.ipynb#Exercise-9:-Cartesian-to-polar-conversion.)"
]
},
{
Expand Down Expand Up @@ -609,7 +609,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"[Return to task 10 of the Complex Arithmetic kata.](./ComplexArithmetic.ipynb#Exercise-10:-Polar-to-Cartesian-conversion.)"
"[Return to task 10 of the Complex Arithmetic tutorial.](./ComplexArithmetic.ipynb#Exercise-10:-Polar-to-Cartesian-conversion.)"
]
},
{
Expand Down Expand Up @@ -686,7 +686,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"[Return to task 11 of the Complex Arithmetic kata.](./ComplexArithmetic.ipynb#Exercise-11:-Polar-multiplication.)"
"[Return to task 11 of the Complex Arithmetic tutorial.](./ComplexArithmetic.ipynb#Exercise-11:-Polar-multiplication.)"
]
},
{
Expand Down Expand Up @@ -754,7 +754,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"[Return to task 12 of the Complex Arithmetic kata.](./ComplexArithmetic.ipynb#Exercise-12**:-Arbitrary-complex-exponents.)"
"[Return to task 12 of the Complex Arithmetic tutorial.](./ComplexArithmetic.ipynb#Exercise-12**:-Arbitrary-complex-exponents.)"
]
}
],
Expand Down
121 changes: 112 additions & 9 deletions tutorials/LinearAlgebra/LinearAlgebra.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -178,17 +178,17 @@
"@exercise\n",
"def matrix_add(a : Matrix, b : Matrix) -> Matrix:\n",
" # You can get the size of a matrix like this:\n",
" n = len(a)\n",
" m = len(a[0])\n",
" rows = len(a)\n",
" columns = len(a[0])\n",
" \n",
" # You can use the following function to initialize an n×m matrix filled with 0s to store your answer\n",
" c = create_empty_matrix(n, m)\n",
" # You can use the following function to initialize a rows×columns matrix filled with 0s to store your answer\n",
" c = create_empty_matrix(rows, columns)\n",
" \n",
" # You can use a for loop to execute its body several times;\n",
" # in this loop variable i will take on each value from 0 to n-1, inclusive\n",
" for i in range(n):\n",
" for i in range(rows):\n",
" # Loops can be nested\n",
" for j in range(m):\n",
" for j in range(columns):\n",
" # You can access elements of a matrix like this:\n",
" x = a[i][j]\n",
" y = b[i][j]\n",
Expand All @@ -199,6 +199,13 @@
" return c"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*Can't come up with a solution? See the explained solution in the [Complex Arithmetic Workbook](./Workbook_LinearAlgebra.ipynb#Exercise-1:-Matrix-addition.).*"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -261,6 +268,13 @@
" return ..."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*Can't come up with a solution? See the explained solution in the [Complex Arithmetic Workbook](./Workbook_LinearAlgebra.ipynb#Exercise-2:-Scalar-multiplication.).*"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -370,6 +384,13 @@
" return ..."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*Can't come up with a solution? See the explained solution in the [Complex Arithmetic Workbook](./Workbook_LinearAlgebra.ipynb#Exercise-3:-Matrix-multiplication.).*"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -423,6 +444,13 @@
" return ..."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*Can't come up with a solution? See the explained solution in the [Complex Arithmetic Workbook](./Workbook_LinearAlgebra.ipynb#Exercise-4:-Matrix-Inversion.).*"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -505,6 +533,13 @@
" return ..."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*Can't come up with a solution? See the explained solution in the [Complex Arithmetic Workbook](./Workbook_LinearAlgebra.ipynb#Exercise-5:-Transpose.).*"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -564,6 +599,13 @@
" return ..."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*Can't come up with a solution? See the explained solution in the [Complex Arithmetic Workbook](./Workbook_LinearAlgebra.ipynb#Exercise-6:-Conjugate.).*"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -608,6 +650,13 @@
" return ..."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*Can't come up with a solution? See the explained solution in the [Complex Arithmetic Workbook](./Workbook_LinearAlgebra.ipynb#Exercise-7:-Adjoint.).*"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -656,6 +705,13 @@
" return ..."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*Can't come up with a solution? See the explained solution in the [Complex Arithmetic Workbook](./Workbook_LinearAlgebra.ipynb#Exercise-8:-Unitary-Verification.).*"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -710,8 +766,13 @@
" 3 \\\\\n",
" -8\n",
"\\end{bmatrix}\n",
"= (-6) \\cdot (3) + (-9i) \\cdot (-8) = -18 + 72i$$\n",
"\n",
"= (-6) \\cdot (3) + (-9i) \\cdot (-8) = -18 + 72i$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you are familiar with the **dot product**, you will notice that it is equivalent to inner product for real-numbered vectors.\n",
"\n",
"> We use our definition for these tutorials because it matches the notation used in quantum computing. You might encounter other sources which define the inner product a little differently: $\\langle V , W \\rangle = W^\\dagger V = V^T\\overline{W}$, in contrast to the $V^\\dagger W$ that we use. These definitions are almost equivalent, with some differences in the scalar multiplication by a complex number.\n",
Expand Down Expand Up @@ -759,6 +820,13 @@
" return ..."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*Can't come up with a solution? See the explained solution in the [Complex Arithmetic Workbook](./Workbook_LinearAlgebra.ipynb#Exercise-9:-Inner-product.).*"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -788,6 +856,13 @@
" return ..."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*Can't come up with a solution? See the explained solution in the [Complex Arithmetic Workbook](./Workbook_LinearAlgebra.ipynb#Exercise-10:-Normalized-vectors.).*"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -841,6 +916,13 @@
" return ..."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*Can't come up with a solution? See the explained solution in the [Complex Arithmetic Workbook](./Workbook_LinearAlgebra.ipynb#Exercise-11:-Outer-product.).*"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -939,6 +1021,13 @@
" return ..."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*Can't come up with a solution? See the explained solution in the [Complex Arithmetic Workbook](./Workbook_LinearAlgebra.ipynb#Exercise-12*:-Tensor-Product.).*"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -1000,7 +1089,7 @@
"<br/>\n",
"<details>\n",
" <summary><strong>Need a hint? Click here</strong></summary>\n",
" Multiply the matrix by the vector, then divide elements of the result by elements of the original vector. Don't forget though, some elements of the vector may be $0$.\n",
" Multiply the matrix by the vector, then divide the elements of the result by the elements of the original vector. Don't forget though, some elements of the vector may be $0$.\n",
"</details>"
]
},
Expand All @@ -1015,6 +1104,13 @@
" return ..."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*Can't come up with a solution? See the explained solution in the [Complex Arithmetic Workbook](./Workbook_LinearAlgebra.ipynb#Exercise-13:-Finding-an-eigenvalue.).*"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -1047,6 +1143,13 @@
"def find_eigenvector(a : Matrix, x : float) -> Matrix:\n",
" return ..."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*Can't come up with a solution? See the explained solution in the [Complex Arithmetic Workbook](./Workbook_LinearAlgebra.ipynb#Exercise-14**:-Finding-an-eigenvector.).*"
]
}
],
"metadata": {
Expand Down
Loading

0 comments on commit 06fd21c

Please sign in to comment.