Skip to content

Commit

Permalink
Renaming b -> c Fixes #14 (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
crazy4pi314 committed Aug 14, 2021
1 parent 209ae3f commit 7ffb011
Showing 1 changed file with 80 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,164 +2,162 @@
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# [Learn Quantum Computing with Python and Q#](https://www.manning.com/books/learn-quantum-computing-with-python-and-q-sharp?a_aid=learn-qc-granade&a_bid=ee23f338)<br>Appendix B Exercise Solutions\n",
"# [Learn Quantum Computing with Python and Q#](https://www.manning.com/books/learn-quantum-computing-with-python-and-q-sharp?a_aid=learn-qc-granade&a_bid=ee23f338)<br>Appendix C Exercise Solutions\n",
"----\n",
"> Copyright (c) Sarah Kaiser and Chris Granade.\n",
"> Code sample from the book \"Learn Quantum Computing with Python and Q#\" by\n",
"> Sarah Kaiser and Chris Granade, published by Manning Publications Co.\n",
"> Book ISBN 9781617296130.\n",
"> Code licensed under the MIT License."
]
],
"metadata": {}
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"### Preamble"
]
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
],
"outputs": [],
"metadata": {}
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"### Exercise B.1 "
]
],
"metadata": {}
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**What would 25 meters West and 110 meters North be in feet?**"
]
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"source": [
"directions_in_meters = np.array([[-25], [110]])\n",
"directions_in_feet = 3.28 * directions_in_meters\n",
"directions_in_feet"
],
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([[-82. ],\n",
" [360.8]])"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
"execution_count": 2
}
],
"source": [
"directions_in_meters = np.array([[-25], [110]])\n",
"directions_in_feet = 3.28 * directions_in_meters\n",
"directions_in_feet"
]
"metadata": {}
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"----\n",
"### Exercise B.2"
]
],
"metadata": {}
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Which of the following functions are linear?**\n",
"\n",
"- $f(x) = 2x$\n",
"- $f(x) = x^2$\n",
"- $f(x) = 2^x$"
]
],
"metadata": {}
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Recall that a function $f$ is linear if and only if for all scalars $a$ and $b$, $f(ax + by) = af(x) + bf(y)$.\n",
"Let's check this condition for each of the three functions above:\n",
"\n",
"- $f(ax + by) = 2(ax + by) = 2ax + 2by = a \\times 2x + b \\times 2y = af(x) + bf(y)$, thus this function is linear.\n",
"- $f(ax + by) = (ax + by)^2 = a^2 x^2 + 2abxy + b^2 y^2$, but $af(x) + bf(y) = ax^2 + by^2$. Since $a \\ne a^2$ for all $a$, and since the second expression is missing the $2abxy$ term, you can conclude that this function is **not** linear.\n",
"- $f(ax + by) = 2^{ax + by} = 2^{ax} \\times 2^{by}$, but $af(x) + bf(y) = a2^x + b2^y$. These two expressions aren't the same, so you can conclude that this function is **not** linear."
]
],
"metadata": {}
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"----\n",
"### Exercise B.3"
]
],
"metadata": {}
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Suppose that you have a linear function $g$ such that $g([[1], [0]]) = [[2.3], [-3.1]]$ and $g([[0], [1]]) = [[-5.2], [0.7]]$.\n",
"Compute $g([[2], [-2]])$.**"
]
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"source": [
"g_horizontal = np.array([[2.3], [-3.1]])\n",
"g_vertical = np.array([[-5.2], [0.7]])\n",
"\n",
"2 * g_horizontal + (-2) * g_vertical"
],
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([[15. ],\n",
" [-7.6]])"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
"execution_count": 3
}
],
"source": [
"g_horizontal = np.array([[2.3], [-3.1]])\n",
"g_vertical = np.array([[-5.2], [0.7]])\n",
"\n",
"2 * g_horizontal + (-2) * g_vertical"
]
"metadata": {}
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"----\n",
"### Exercise B.4"
]
],
"metadata": {}
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Let $X$ be the matrix $[[0, 1], [1, 0]]$, and let $\\vec{y}$ be the vector $[[2], [3]]$.\n",
"Using NumPy, compute $X\\vec{y}$ and $XX$.**"
]
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"X = np.array([\n",
" [0, 1],\n",
Expand All @@ -169,127 +167,129 @@
" [2],\n",
" [3]\n",
"])"
]
],
"outputs": [],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"source": [
"X @ y"
],
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([[3],\n",
" [2]])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
"execution_count": 5
}
],
"source": [
"X @ y"
]
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"source": [
"X @ X"
],
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([[1, 0],\n",
" [0, 1]])"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
"execution_count": 6
}
],
"source": [
"X @ X"
]
"metadata": {}
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"----\n",
"### Exercise B.5"
]
],
"metadata": {}
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Given a vector $[[2], [3]]$, find a vector that points in the same direction but with length 1.**\n",
"\n",
"*HINT*: You can either do this by using an inner product, or the `np.linalg.norm` function."
]
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"v = np.array([\n",
" [2],\n",
" [3]\n",
"])"
]
],
"outputs": [],
"metadata": {}
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can try both ways suggested by the hint above to confirm that you get the same answer from each."
]
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"source": [
"v / np.sqrt(v.transpose() @ v)"
],
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([[0.5547002 ],\n",
" [0.83205029]])"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
"execution_count": 8
}
],
"source": [
"v / np.sqrt(v.transpose() @ v)"
]
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"source": [
"v / np.linalg.norm(v)"
],
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([[0.5547002 ],\n",
" [0.83205029]])"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
"execution_count": 9
}
],
"source": [
"v / np.linalg.norm(v)"
]
"metadata": {}
}
],
"metadata": {
Expand All @@ -313,4 +313,4 @@
},
"nbformat": 4,
"nbformat_minor": 4
}
}

0 comments on commit 7ffb011

Please sign in to comment.