{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "3580d661-a471-4131-a3e3-a88161d38209",
   "metadata": {},
   "source": [
    "# A new paradigm in arithmetization: Depth-aware arithmetization"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d97c8705-c1d1-4b3b-848e-0a68dc7a703b",
   "metadata": {},
   "source": [
    "#### An equality circuit is easy to define but somewhat hard to optimize!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "b615a377-1777-4aec-acb6-79f202dec6ac",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 9916 characters]"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from galois import GF\n",
    "\n",
    "from circuit_compiler.compiler.nodes.leafs import Input\n",
    "from circuit_compiler.compiler.comparison.equality import Equals\n",
    "from circuit_compiler.compiler.circuit import Circuit\n",
    "\n",
    "gf = GF(467)\n",
    "\n",
    "a = Input(\"a\", gf)\n",
    "b = Input(\"b\", gf)\n",
    "\n",
    "output = Equals(a, b, gf)\n",
    "\n",
    "circuit = Circuit(outputs=[output], gf=gf)\n",
    "circuit.display_graph()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e466a3dd-b3ef-4449-979b-c443bf860f79",
   "metadata": {},
   "source": [
    "#### Naive methods only find only one arithmetization for this high-level circuit."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "3828eb5d-6da2-421f-b5e0-9cc0e399d434",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 79328 characters]"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "naive_arithmetic_circuit = circuit.arithmetize(\"naive\")\n",
    "naive_arithmetic_circuit.display_graph()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7a721fc6-ce6c-441e-8ce2-67fee11af6cc",
   "metadata": {},
   "source": [
    "#### Our depth-aware arithmetization method finds a second circuit that is potentially faster!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "18d08a7b-a97d-4a74-856a-79dcf7197f07",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Depth:  9 ,  Cost:  8.0\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 78668 characters]"
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Depth:  11 ,  Cost:  7.5\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 83156 characters]"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "depth_aware_circuits = circuit.arithmetize_depth_aware(0.5)\n",
    "\n",
    "for depth, cost, depth_aware_circuit in depth_aware_circuits:\n",
    "    print(\"Depth: \", depth, \", \", \"Cost: \", cost)\n",
    "    depth_aware_circuit.display_graph()"
   ]
  }
 ],
 "metadata": {
  "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.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
