{
"cells": [
{
"cell_type": "code",
"execution_count": 40,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%%html\n",
""
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# CS 121 Lecture 6 : Code and data"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [],
"source": [
"# utility code \n",
"%run \"Utilities.ipynb\"\n",
"from IPython.display import clear_output\n",
"clear_output()"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [],
"source": [
"def AND(a,b): return a*b\n",
"\n",
"def OR(a,b): return 1 if a+b else 0\n",
"\n",
"def NOT(a): return 1-a"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [],
"source": [
"def IF(cond,a,b):\n",
" notcond = NAND(cond,cond)\n",
" temp = NAND(b,notcond)\n",
" temp1 = NAND(a,cond)\n",
" return NAND(temp,temp1)\n",
"mystery = circuit2prog(circuit(IF))"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"scrolled": true,
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"gate_0 = NAND(X[0],X[0])\n",
"gate_1 = NAND(X[2],gate_0)\n",
"gate_2 = NAND(X[1],X[0])\n",
"Y[0] = NAND(gate_1,gate_2)\n",
"\n"
]
}
],
"source": [
"# What function does the following NAND-CIRC program compute?\n",
"print(mystery)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": []
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/svg+xml": [
"\r\n",
"\r\n",
"\r\n",
"\r\n"
],
"text/plain": [
""
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"circuit(mystery)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Sugar all the way "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [],
"source": [
"def NOT(a): return NAND(a,a)\n",
"def AND(a,b): return NOT(NAND(a,b))\n",
"def OR(a,b): return NAND(NOT(a),NOT(b))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [],
"source": [
"GLOBAL_NANDONLY = True"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [],
"source": [
"def MAJ(a,b,c):\n",
" u = NAND(a,b)\n",
" v = NAND(a,c)\n",
" w = NAND(b,c)\n",
" z = NAND(u,v)\n",
" return NAND(w,NOT(z))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [],
"source": [
"def XOR(a,b):\n",
" u = NAND(a,b)\n",
" v = NAND(a,u)\n",
" w = NAND(b,u)\n",
" return NAND(v,w)\n",
"\n",
"def XOR3(a,b,c):\n",
" return XOR(XOR(a,b),c)"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"data": {
"text/plain": []
},
"execution_count": 58,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/svg+xml": [
"\r\n",
"\r\n",
"\r\n",
"\r\n"
],
"text/plain": [
""
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"def ADD1(a,b):\n",
" return AND(a,b),XOR(a,b)\n",
"\n",
"circuit(ADD1)"
]
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"data": {
"text/plain": [
"[1, 1, 0]"
]
},
"execution_count": 59,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def addnumbers(X,Y):\n",
" n = len(X)\n",
" Z = [0]*(n+1)\n",
" carry = zero(X[0])\n",
" for i in range(n-1,-1,-1):\n",
" Z[i+1] = XOR3(X[i],Y[i],carry)\n",
" carry = MAJ(carry,X[i],Y[i])\n",
" Z[0] = carry\n",
" return Z\n",
"\n",
"addnumbers([1,1],[1,1])"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"data": {
"text/plain": []
},
"execution_count": 60,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/svg+xml": [
"\r\n",
"\r\n",
"\r\n",
"\r\n"
],
"text/plain": [
""
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"add2circ = circuit(addnumbers,2,2)\n",
"add2circ"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"table(add2circ)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"add2circ.size()"
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\r\n",
"\r\n",
"\r\n",
"\r\n"
],
"text/plain": [
""
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"X = list(range(10,50,5))\n",
"Y = [circuit(addnumbers,n,n).size() for n in X]\n",
"import matplotlib.pyplot as plt\n",
"plt.plot(X,Y)\n",
"plt.ylabel('circuit size')\n",
"plt.xlabel('input length')\n",
"plt.show()"
]
}
],
"metadata": {
"celltoolbar": "Slideshow",
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.9.12"
}
},
"nbformat": 4,
"nbformat_minor": 4
}