{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 25460 characters]\n",
      "text/latex": [
       "$\\displaystyle \\left[ \\left[\\begin{matrix}\\frac{\\sqrt{2}}{2} & \\frac{\\sqrt{2}}{2}\\\\\\frac{\\sqrt{2}}{2} & - \\frac{\\sqrt{2}}{2}\\end{matrix}\\right], \\  \\left[\\begin{matrix}1 & 0\\\\0 & i\\end{matrix}\\right], \\  \\left[\\begin{matrix}1 & 0\\\\0 & 1\\end{matrix}\\right], \\  \\left[\\begin{matrix}\\frac{\\sqrt{2}}{2} & \\frac{\\sqrt{2}}{2}\\\\\\frac{\\sqrt{2} i}{2} & - \\frac{\\sqrt{2} i}{2}\\end{matrix}\\right], \\  \\left[\\begin{matrix}\\frac{\\sqrt{2}}{2} & \\frac{\\sqrt{2} i}{2}\\\\\\frac{\\sqrt{2}}{2} & - \\frac{\\sqrt{2} i}{2}\\end{matrix}\\right], \\  \\left[\\begin{matrix}1 & 0\\\\0 & -1\\end{matrix}\\right], \\  \\left[\\begin{matrix}\\frac{\\sqrt{2}}{2} & \\frac{\\sqrt{2} i}{2}\\\\\\frac{\\sqrt{2} i}{2} & \\frac{\\sqrt{2}}{2}\\end{matrix}\\right], \\  \\left[\\begin{matrix}\\frac{\\sqrt{2}}{2} & - \\frac{\\sqrt{2}}{2}\\\\\\frac{\\sqrt{2}}{2} & \\frac{\\sqrt{2}}{2}\\end{matrix}\\right], \\  \\left[\\begin{matrix}1 & 0\\\\0 & - i\\end{matrix}\\right], \\  \\left[\\begin{matrix}\\frac{\\sqrt{2}}{2} & - \\frac{\\sqrt{2}}{2}\\\\\\frac{\\sqrt{2} i}{2} & \\frac{\\sqrt{2} i}{2}\\end{matrix}\\right], \\  \\left[\\begin{matrix}\\frac{\\sqrt{2}}{2} & - \\frac{\\sqrt{2} i}{2}\\\\\\frac{\\sqrt{2}}{2} & \\frac{\\sqrt{2} i}{2}\\end{matrix}\\right], \\  \\left[\\begin{matrix}\\frac{\\sqrt{2}}{2} & - \\frac{\\sqrt{2} i}{2}\\\\\\frac{\\sqrt{2} i}{2} & - \\frac{\\sqrt{2}}{2}\\end{matrix}\\right], \\  \\left[\\begin{matrix}\\frac{1}{2} + \\frac{i}{2} & \\frac{1}{2} - \\frac{i}{2}\\\\\\frac{1}{2} - \\frac{i}{2} & \\frac{1}{2} + \\frac{i}{2}\\end{matrix}\\right], \\  \\left[\\begin{matrix}\\frac{\\sqrt{2}}{2} & \\frac{\\sqrt{2}}{2}\\\\- \\frac{\\sqrt{2}}{2} & \\frac{\\sqrt{2}}{2}\\end{matrix}\\right], \\  \\left[\\begin{matrix}0 & 1\\\\1 & 0\\end{matrix}\\right], \\  \\left[\\begin{matrix}\\frac{\\sqrt{2}}{2} & \\frac{\\sqrt{2}}{2}\\\\- \\frac{\\sqrt{2} i}{2} & \\frac{\\sqrt{2} i}{2}\\end{matrix}\\right], \\  \\left[\\begin{matrix}0 & 1\\\\i & 0\\end{matrix}\\right], \\  \\left[\\begin{matrix}\\frac{1}{2} - \\frac{i}{2} & \\frac{1}{2} + \\frac{i}{2}\\\\- \\frac{1}{2} + \\frac{i}{2} & \\frac{1}{2} + \\frac{i}{2}\\end{matrix}\\right], \\  \\left[\\begin{matrix}0 & i\\\\1 & 0\\end{matrix}\\right], \\  \\left[\\begin{matrix}\\frac{\\sqrt{2}}{2} & \\frac{\\sqrt{2} i}{2}\\\\- \\frac{\\sqrt{2} i}{2} & - \\frac{\\sqrt{2}}{2}\\end{matrix}\\right], \\  \\left[\\begin{matrix}\\frac{1}{2} - \\frac{i}{2} & - \\frac{1}{2} + \\frac{i}{2}\\\\- \\frac{1}{2} + \\frac{i}{2} & - \\frac{1}{2} + \\frac{i}{2}\\end{matrix}\\right], \\  \\left[\\begin{matrix}0 & -1\\\\1 & 0\\end{matrix}\\right], \\  \\left[\\begin{matrix}\\frac{\\sqrt{2}}{2} & - \\frac{\\sqrt{2}}{2}\\\\- \\frac{\\sqrt{2} i}{2} & - \\frac{\\sqrt{2} i}{2}\\end{matrix}\\right], \\  \\left[\\begin{matrix}\\frac{1}{2} - \\frac{i}{2} & \\frac{i \\left(-1 + i\\right)}{2}\\\\- \\frac{1}{2} + \\frac{i}{2} & \\frac{i \\left(-1 + i\\right)}{2}\\end{matrix}\\right]\\right]$"
      ],
      "text/plain": [
       "⎡⎡√2   √2 ⎤                  ⎡ √2     √2  ⎤  ⎡√2   √2⋅ⅈ ⎤           ⎡ √2   √2⋅\n",
       "⎢⎢──   ── ⎥                  ⎢ ──     ──  ⎥  ⎢──   ──── ⎥           ⎢ ──   ───\n",
       "⎢⎢2    2  ⎥  ⎡1  0⎤  ⎡1  0⎤  ⎢ 2      2   ⎥  ⎢2     2   ⎥  ⎡1  0 ⎤  ⎢ 2     2 \n",
       "⎢⎢        ⎥, ⎢    ⎥, ⎢    ⎥, ⎢            ⎥, ⎢          ⎥, ⎢     ⎥, ⎢         \n",
       "⎢⎢√2  -√2 ⎥  ⎣0  ⅈ⎦  ⎣0  1⎦  ⎢√2⋅ⅈ  -√2⋅ⅈ ⎥  ⎢√2  -√2⋅ⅈ ⎥  ⎣0  -1⎦  ⎢√2⋅ⅈ   √2\n",
       "⎢⎢──  ────⎥                  ⎢────  ──────⎥  ⎢──  ──────⎥           ⎢────   ──\n",
       "⎣⎣2    2  ⎦                  ⎣ 2      2   ⎦  ⎣2     2   ⎦           ⎣ 2     2 \n",
       "\n",
       "ⅈ⎤  ⎡√2  -√2 ⎤           ⎡ √2   -√2 ⎤  ⎡√2  -√2⋅ⅈ ⎤  ⎡ √2   -√2⋅ⅈ ⎤  ⎡1   ⅈ  1\n",
       "─⎥  ⎢──  ────⎥           ⎢ ──   ────⎥  ⎢──  ──────⎥  ⎢ ──   ──────⎥  ⎢─ + ─  ─\n",
       " ⎥  ⎢2    2  ⎥  ⎡1  0 ⎤  ⎢ 2     2  ⎥  ⎢2     2   ⎥  ⎢ 2      2   ⎥  ⎢2   2  2\n",
       " ⎥, ⎢        ⎥, ⎢     ⎥, ⎢          ⎥, ⎢          ⎥, ⎢            ⎥, ⎢        \n",
       " ⎥  ⎢√2   √2 ⎥  ⎣0  -ⅈ⎦  ⎢√2⋅ⅈ  √2⋅ⅈ⎥  ⎢√2   √2⋅ⅈ ⎥  ⎢√2⋅ⅈ   -√2  ⎥  ⎢1   ⅈ  1\n",
       " ⎥  ⎢──   ── ⎥           ⎢────  ────⎥  ⎢──   ──── ⎥  ⎢────   ──── ⎥  ⎢─ - ─  ─\n",
       " ⎦  ⎣2    2  ⎦           ⎣ 2     2  ⎦  ⎣2     2   ⎦  ⎣ 2      2   ⎦  ⎣2   2  2\n",
       "\n",
       "   ⅈ⎤  ⎡ √2   √2⎤          ⎡  √2     √2 ⎤          ⎡ 1   ⅈ   1   ⅈ⎤          ⎡\n",
       " - ─⎥  ⎢ ──   ──⎥          ⎢  ──     ── ⎥          ⎢ ─ - ─   ─ + ─⎥          ⎢\n",
       "   2⎥  ⎢ 2    2 ⎥  ⎡0  1⎤  ⎢  2      2  ⎥  ⎡0  1⎤  ⎢ 2   2   2   2⎥  ⎡0  ⅈ⎤  ⎢\n",
       "    ⎥, ⎢        ⎥, ⎢    ⎥, ⎢            ⎥, ⎢    ⎥, ⎢              ⎥, ⎢    ⎥, ⎢\n",
       "   ⅈ⎥  ⎢-√2   √2⎥  ⎣1  0⎦  ⎢-√2⋅ⅈ   √2⋅ⅈ⎥  ⎣ⅈ  0⎦  ⎢  1   ⅈ  1   ⅈ⎥  ⎣1  0⎦  ⎢\n",
       " + ─⎥  ⎢────  ──⎥          ⎢──────  ────⎥          ⎢- ─ + ─  ─ + ─⎥          ⎢\n",
       "   2⎦  ⎣ 2    2 ⎦          ⎣  2      2  ⎦          ⎣  2   2  2   2⎦          ⎣\n",
       "\n",
       "  √2    √2⋅ⅈ⎤  ⎡ 1   ⅈ     1   ⅈ⎤           ⎡  √2     -√2  ⎤  ⎡ 1   ⅈ   ⅈ⋅(-1 \n",
       "  ──    ────⎥  ⎢ ─ - ─   - ─ + ─⎥           ⎢  ──     ──── ⎥  ⎢ ─ - ─   ──────\n",
       "  2      2  ⎥  ⎢ 2   2     2   2⎥  ⎡0  -1⎤  ⎢  2       2   ⎥  ⎢ 2   2       2 \n",
       "            ⎥, ⎢                ⎥, ⎢     ⎥, ⎢              ⎥, ⎢               \n",
       "-√2⋅ⅈ   -√2 ⎥  ⎢  1   ⅈ    1   ⅈ⎥  ⎣1  0 ⎦  ⎢-√2⋅ⅈ   -√2⋅ⅈ ⎥  ⎢  1   ⅈ  ⅈ⋅(-1 \n",
       "──────  ────⎥  ⎢- ─ + ─  - ─ + ─⎥           ⎢──────  ──────⎥  ⎢- ─ + ─  ──────\n",
       "  2      2  ⎦  ⎣  2   2    2   2⎦           ⎣  2       2   ⎦  ⎣  2   2      2 \n",
       "\n",
       "+ ⅈ)⎤⎤\n",
       "────⎥⎥\n",
       "    ⎥⎥\n",
       "    ⎥⎥\n",
       "+ ⅈ)⎥⎥\n",
       "────⎥⎥\n",
       "    ⎦⎦"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import sympy\n",
    "import numpy as np\n",
    "\n",
    "from collections import defaultdict\n",
    "from itertools import product\n",
    "\n",
    "M = sympy.Matrix\n",
    "simplify = sympy.simplify\n",
    "sqrt = sympy.sqrt\n",
    "i = sympy.I\n",
    "_i = i\n",
    "\n",
    "from sympy.physics.quantum import Dagger\n",
    "from sympy.physics.quantum import tensorproduct\n",
    "\n",
    "sympy.init_printing()\n",
    "H = simplify(M([[1, 1], [1, -1]]) / sqrt(2))\n",
    "S = M([[1, 0], [0, i]])\n",
    "C_L = [H, S]\n",
    "basis_vectors = [M([1, 0]), M([0, 1])]\n",
    "\n",
    "def getitem(m, i, j):\n",
    "    # Note that sympy uses an extremely weird way to store the matrices,\n",
    "    # in particular the internal representation is vastly different from what\n",
    "    # is printed. For instance the H matrix is stored as a Mul object\n",
    "    # (even after using simplify) instead of a matrix. \n",
    "    # Therefore m[i][j] will NOT work. This is a workaround.\n",
    "    return (Dagger(basis_vectors[i]) * m * basis_vectors[j])[0]\n",
    "\n",
    "def is_known(C_L, a):\n",
    "    for c in C_L:\n",
    "        test = c * Dagger(a)\n",
    "        if(simplify(getitem(test, 0, 1)) != 0):\n",
    "            continue\n",
    "        if(simplify(getitem(test, 0, 0) / getitem(test, 1, 1)) == 1):\n",
    "            return True\n",
    "    return False\n",
    "for i in range(5):\n",
    "    for m in (H, S):\n",
    "        for c in C_L:\n",
    "            c = simplify(c*m)\n",
    "            if(is_known(C_L, c)):\n",
    "                continue\n",
    "            C_L.append(c)\n",
    "C_L"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAALQAAABlCAYAAADkr8m4AAAACXBIWXMAAA7EAAAOxAGVKw4bAAAIeklEQVR4Ae1d7Y3cNhTcDVyA08K6geDcQIBsOojdQeIaAuR/kHRgF5A/SQdOOohdgIG7AvIjOaSAXGZuJULeD5JvSZHv0U+AfFpRFGeGI+otpX3ePjw8bCTLdrt9iuNfYn2Fus8ldVseawVnjiajcGnB4wkauYGo7y4I+xtM+2Ium47dT59pbJWLFZw54o3CpRYPnOcWuu3OaQevbp8sCn7GNg9eLnfLD6jwHp/f46TfLPdr27aCM0e3UbhU5PHTGd2+xr5HTy4N/RqNfmTgMxV9lyvQVQF49M0xAAyw3PVo6M+OC/2zK2BZATe05d5z7CcKuKFPJPEdlhVwQ1vuPcd+ooAb+kQS32FZgeUsR5IHvk3ucNArrJyL3uHza/y9xTdPTvmpWazgzBFsFC6teHC+Y36w8syn7XIs5sdoUwAXy3fAxGnnrYcc2nrH8RQp4IYuks8ra1MgO4bGsJ58i4lDfm+CVnDm6DQKl5Y8sg2twaw5JrCC81Pi0rJPPOTIcZYfY0aB7BGajHDr4Cuj30/sOIXH5VtcgfeHTR3/WsGZo9YoXFry4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80E117YmIW1zsNSn7TSfJ7l4Lfl4/VXdHjIs4FyjsxvsbLOvDLxzNPlcdq3gXcIHtTZCpdaOHGeW6zHPn38TD34OJvvR7zEem65w0G/zwW4wpjA5SSPBY7p/lh8xpjzdxQe5GqFSy2cOA+n6M4u8OGb/wFmtDR28OD1wgAAAABJRU5ErkJggg==\n",
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}\\frac{1}{2} & \\frac{1}{2} & \\frac{1}{2} & \\frac{1}{2}\\\\\\frac{1}{2} & - \\frac{1}{2} & \\frac{1}{2} & - \\frac{1}{2}\\\\\\frac{1}{2} & \\frac{1}{2} & - \\frac{1}{2} & - \\frac{1}{2}\\\\\\frac{1}{2} & - \\frac{1}{2} & - \\frac{1}{2} & \\frac{1}{2}\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "⎡1/2  1/2   1/2   1/2 ⎤\n",
       "⎢                     ⎥\n",
       "⎢1/2  -1/2  1/2   -1/2⎥\n",
       "⎢                     ⎥\n",
       "⎢1/2  1/2   -1/2  -1/2⎥\n",
       "⎢                     ⎥\n",
       "⎣1/2  -1/2  -1/2  1/2 ⎦"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "H2 = tensorproduct.matrix_tensor_product(H, H)\n",
    "H2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "zero_state = M([1, 0, 0, 0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAB0AAABlCAYAAACx4uijAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAC/klEQVRoBe1ay03DQBB1EGeUEwWYDkgJhA4QHRBKQFSAQgdJAVxSApRAKAApKYADIArAvOfsrhxn7czYgIOYkSb78ey89dvfZJMky7JEo0mS9KEj6FzTrmi71+v1jqFZhc7gPAhtUSAggalRgd2iwl/GBvuFVrfILwplZpfFMnr7hPITHJ4V6yP5caTuFHV5uyLoBE7XQCINRVXwMy0boqOsykH3yg9/o2ygP8pyJ/QWZ+/Wt8MMTGF0CR1CU5QnSBeYrVxuYlGBuiV1JfZeYdgJvZ2AiunF+OX7ZgVjeTXoz7edOhs+E4NKHW4D5PPdppc9BMU8zq6Zh3D5UC7AwvsqK/sU0+vcjQHAdZqLW6dzFI5clSjR0jsC0LDgmecmNwke7mLRgvItH8XeKwxV9ILa8uHMTixRz4hCLNo3DY4dpYwEBqFSmGkECsAU/jmeA+3MZb9U9LKBBwQYAy2WOYneURbHV6o3dYA8zm4I5gA5rq9QsWjflGuSGwTTIHjLsHZDZU1GC3oCX0Pn7xJgqk3B90MFChBpsO39R1PVmEY9NKg00AakyZv8H3pVS8btSNwIuFYt2N46ozqZSBxTnhB30M+6LmI82wbbzw4nh+F5SIcp9lbV9Y7GHv55K4MmmQXbICIuFmzHeamoVW2DmHkWbFcQGa9W0UsX7qThLP57wfaBI8GncU5WtW2C7UPvmPR+uIJP/bNY2ibYfvEOVWOKcbRg2zMnSjuJHAxUNDZNjVRLxoJtLc2dzF7xmH5DsB0IEYNi3xVdlQfPNZndppcdB8V2s80vYWLRjim/+tvN9o/Rmzv2sS8KdrNdS7V4G3Re2gTboSNa0DbBdjNQC7YDb8KMdhsUuq03M9B6flo+Va1TC7a1bHcye8VjasG2djxpL6aXxhZsgwELtjkVNqTx5uAotb+RbFBaruDM429tMZ0hAgy/v8Emhd5D2cYr/1bSL9nxz8kxfzDLEt4j8Nv1OTQm/K/Rg3+AcXxDnvZrApu1+wjY8ce8qMB2+gXddflBWpRqZgAAAABJRU5ErkJggg==\n",
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}\\frac{1}{2}\\\\\\frac{1}{2}\\\\\\frac{1}{2}\\\\\\frac{1}{2}\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "⎡1/2⎤\n",
       "⎢   ⎥\n",
       "⎢1/2⎥\n",
       "⎢   ⎥\n",
       "⎢1/2⎥\n",
       "⎢   ⎥\n",
       "⎣1/2⎦"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "plus_state = H2 * zero_state\n",
    "plus_state"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "I = M([[1, 0], [0, 1]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "C_L_0 = [tensorproduct.matrix_tensor_product(I, c) for c in C_L]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "C_L_1 = [tensorproduct.matrix_tensor_product(c, I) for c in C_L]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAIgAAABkCAYAAABdPHirAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAHJElEQVR4Ae1d7Y0cNwy9Dfw7MGIgBaw7OCcVxOnAlw6S1JASnA6cDoJcCUkFhq+DSwEBYizSwOXxMDQGa2oo3S5JrUQBws5QGpF8fKP50PBud3V1dY36AVUqtw8PDzdSQ8rGQGC3293Dk73kDWK/e7Zq+BXb1Hld/l7v5PaQCLwVvPoesjckXxPkHRiThBDQGlmEmP927B9mFRJ9RpDjfuI+DqZL0h+orzD4QexkIIReZvq/GP4l6lsvQkfq1qD0sI0C/oC6B+BXUkXbc1QixTtUul+h/s+lvhayRecbHpt0oz5eO1lm9RupW/PJyjaM+xMq1D9cfYENtaDjAfUG9Wd0/l094IwdcIaQsUTGWx6W7ME27RNhzUqkbs0pL9uqCKIZa9xOT1F3go73kL0GUDSbWJVI3ZpPLrZdAkFeA6mPAlo0i1ChdqsSqVvzycW2rglSOTt8pSH5lPZI3Zq9nrZ1TRAAxcHn2WKNHc8qVpeYSN1rP6VtN9t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b19rSCQcrooidavGBXZww6WGIDU4nCVJp0aR0CdSt2BON6Kz4FJDEJ4lJM+ZyVaJU5G6JX97kbnhohIEz9t8aZEuIywzSZyK1N0LEyQ7PHFRCbIYaJmkI2GwlkXqXtvR27YLLrUEcUnSKUQgUnfBpC7ELrhUEQRTWljiVKTuLmhQMMILl5bFOvMknQIWJA7TjXcOdKbSvVZE0hb5vlXMccnEqS34J23DSZGJU5PGvtntqnuQ5lHzgGEQSIIME0obR5IgNrgOM2oSZJhQ2jiSBLHBdZhRkyDDhNLGkSSIDa7DjJoEGSaUNo4kQWxwHWbUlrWYaROn8Oo5NGnrVLbB/muMQWtKT/pPXXRwJk4VkqiATUjSFlZrN5O6tHbYTQuMRIrmpC8ck4lTAEEty6JVSNKWapzSAQQ6y3/qqr0HcUnSKfg8q+4CHL7iWoK4JOkUXJ9VdwEOX7FKEEyz/GHylmX8dftWn+a2WXU3A2V4gEoQ6ObgHwQ7+PP7GhIJh6uiWXWrwHh1qCFIjS1nSdKpUST0mVW3AMX5RTXvQXiWkLTzGT5i4lSk3/TOiWblv1BbZucbPL3cSYF6qkwlCD0uwVgaXzKUZWaJUzPqJrAJd/zQR8mhpfYS45KkU0BiVt0FOHzFtQRxSdIpuD6r7gIcvuIqgmC6mzJxKtJvXxqUtan3IKtDzZN0VrqON2fVfYxD0z7u32j2pfvEJyd9ZeJUE+RzdAaxMnFqjlCf7mXVPcjpanKES0UgCXKpkXOyOwniBPSlqkmCXGrknOxeP+beL6+116pv8S6APtjJMigCiPk9XNuX3COC8H+ckvqYrLFIilIWhgB/kC0a8D9jejx3xeetigAAAABJRU5ErkJggg==\n",
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}1 & 0 & 0 & 0\\\\0 & 1 & 0 & 0\\\\0 & 0 & 1 & 0\\\\0 & 0 & 0 & -1\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "⎡1  0  0  0 ⎤\n",
       "⎢           ⎥\n",
       "⎢0  1  0  0 ⎥\n",
       "⎢           ⎥\n",
       "⎢0  0  1  0 ⎥\n",
       "⎢           ⎥\n",
       "⎣0  0  0  -1⎦"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "CZ = M([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, -1]])\n",
    "CZ"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAC0AAABlCAYAAADZEWqbAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAD30lEQVR4Ae1cXW7UMBjcVDyj0gcOsEgcgB6B7Q2AB95pj4A4ASo3aA/Qlx4BuAHtAZC2B+ABVhyAZSZ8jtxgb7Lx2CjIllz/xPt5Mpn97HxNttlutwtlaprmEPZeIZ/B9rHStrN1gEmeIW8j+doNHFPSFsadIhM486QEO+sInpbhB57VD6ivvTard732ziaYvcWAW0z4YufA4YPngSEn6Gvt+qAvMOleIAOGJV3Acdk3BCLY1YI+6B+cQ7uCLnWVKtOlmPa9R/Kc+IYvYeQMeYW8RPsC5RregO5UlqSgzWW+laGLGKqajhAj75bJA/od3HlBPu2ylnoWMtAqQGNOaJaaljFNhiARbkffGVt0f0xvcBU2f6qav1LQgHQOgPTTbTI/fYPGE+uSFGp5nALoykPGfTEXGd4cyJIaNFn+IkMXMSSVB6TR37zzJO7QzzsaWVIz3QEzSfBOQ35zmwU0AC8Blno+VnsOsiKVBw06wADLG1G2+SXcoC27/5QybYC5HX1PsAaYuv6OLEtqpumTucCw7BJY7nx315lQUYN+Diwrw8MIk3RRcecpBQ2QqmCNwxcspZoOzpChs4LOQGrQZGU6SEuGzlkyLXV5tiJyIaGvrsEaX2WzlMcsQVPT3IFdIf/yL8G+deg5d7Dmq+FckOkj5NfISaxj39EMZcyRkp7iw8SZBjQFQcpn1S6vBmtiVyNJxwGjNVgTIKXtkmoa3qMGa2JMqzXdzmMbpxqs8VmXMm0MFwnWPLSzcKV/UvvWcwZrHjsw9B4/reFKd2xKmTNY880BUru8GqxxzPZL6RexbzxXu4LOxWzfbmW6z0iuttTl1WDNjstUNb2DHOkhmaYLBGu6E5eBZqCms5q5MktNy5gmuZBIDdbEVKaWRw3WxJiWaroGa2I0o1+t6XYq2zjVYI1PvJRpYzh7sIYnwGeM+E+eJb5Ii5QMGz/MFu112beJfi5AfOvoxu8fqttnMGyrffAKBh/BeDThSpCglQ0g+ElJ6vKGEOCkJMEcqaaHQKuOV9AqJofsVKaHGFIdnyXTRV2erZjJT94UBQ0/zSd7k1+TmqU8Zgl6lDygRe4TPiPvs194act26zRgQ/bkzSjQmHyDmZOe7YcNWTDn/5VHe30Ff0xmya9JjZKHAK8zIXlNqrQ8JMGc0qC5Gia/JlVUHvAgkidvSjPttO1e2pn0mtQ/AW0bp8nBnKLyIM0OMKSS9JqUi3t0cQrYdvVrGE+Khfifh90l8kdkzukygzuHvXH8cRSH4V7JcVxauZ/gb8yEEt8l/BQ6MKUPLDOY89f+BXPcW+IxjsGcYMLYy9/xqBy2/jxphQAAAABJRU5ErkJggg==\n",
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}\\frac{1}{2}\\\\\\frac{1}{2}\\\\\\frac{1}{2}\\\\- \\frac{1}{2}\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "⎡1/2 ⎤\n",
       "⎢    ⎥\n",
       "⎢1/2 ⎥\n",
       "⎢    ⎥\n",
       "⎢1/2 ⎥\n",
       "⎢    ⎥\n",
       "⎣-1/2⎦"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "CZ * plus_state"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAHgAAABkCAYAAABNcPQyAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAGmklEQVR4Ae1c/83bN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i+NmBD8Wj/ZIX6uqyDDDjAHYCU46IDnJO9Dvo6wB2AlOPi+DXpI/59Tc91R1bBVOj77WQAmLFGcLjmEQGWL76HdKp8XCVkyGWLMyCEwOAJ/gPMO+U3kXQlGwAAAABJRU5ErkJggg==\n",
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}1 & 0 & 0 & 0\\\\0 & 1 & 0 & 0\\\\0 & 0 & 1 & 0\\\\0 & 0 & 0 & 1\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "⎡1  0  0  0⎤\n",
       "⎢          ⎥\n",
       "⎢0  1  0  0⎥\n",
       "⎢          ⎥\n",
       "⎢0  0  1  0⎥\n",
       "⎢          ⎥\n",
       "⎣0  0  0  1⎦"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "I_4 = M([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]])\n",
    "I_4"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Compute all possible starting matrices: Two qbits either entangled or unentangled with any of the 24 $C_L$ operators."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "starting_matrices = [a * b * c for a, b, c in product(C_L_0, C_L_1, (I_4, CZ))]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Compute the resulting matrix after a $CZ$ has been applied."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "resulting_matrices = [CZ * c for c in starting_matrices]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAGoAAAAVCAYAAACuVXuDAAAACXBIWXMAAA7EAAAOxAGVKw4bAAADtklEQVRoBe2ZjXEaMRCFjScFMEkHuAMSOsAdJKQDuwOnhqQDu4Q4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br/HCeretAmbOzBhbnObm5MInSADmc977HoHjKZM+OfzV8rIUlHFYT/7ZvL05uqUvIoTZ37XMq5ffBv2hyjqUWFrkQ+618wCAc2Szyx1x0/XX972WdCCgPfCnh1cC/hP8HWgYo7Ny1PQYAAAAASUVORK5CYII=\n",
      "text/latex": [
       "$\\displaystyle \\left( 1152, \\  4, \\  4\\right)$"
      ],
      "text/plain": [
       "(1152, 4, 4)"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "starting_matrices_np = np.array([np.array(m).astype(np.cdouble) for m in starting_matrices])\n",
    "\n",
    "starting_matrices_np.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAGoAAAAVCAYAAACuVXuDAAAACXBIWXMAAA7EAAAOxAGVKw4bAAADtklEQVRoBe2ZjXEaMRCFjScFMEkHuAMSOsAdJKQDuwOnhqQDu4Q4HWCXQDqADmLTAXmfkDQSJ7jjLGlgxjuzSFrt7Vvt6u+OwWazuQhpMBiMJFuFsvd63QikcnAZuiCFO7XHoSx3XRgN+5INcS431hnbG9lc+CH4RKnjq6SftJr++N6gov6xeCkeBuJGtYPes3Q24oXlpYwsxC+hMfXh7L1ldOfiRpLDZ3LWwQI7p82UrRSOcvCErvrIyZbs1kfwF9RDlgz5oxiHCSb75DDUCZ5v1bO6JAbGFuVPcWRTbVbXY4hj9XhmGspL1YWDb5EPJbAO4aiPmJvYmMSoQSJuDjmifrbFZKLC59r01N86eHTEUfLAEL3CIV6JujAYa/FEteGo/0Z8zxjd1jdT40HCU6GpHHnV0t/dZtkSip5nbEXCWFtWUYa64NiczIjDpX4Iyqnd8kjISo4SsBTtJjCl01f2vdKk7YpDbmYf9PNNbA6vviM79jlNDpa0C/aV6izvv86O6viUInOZCHVTSn1l8ostr8YF4hgccnNNor7UcE4YjkjQbwXbrBYFh4sDt8lryfZOGPWTJHR/iLOT9WMtH4ruLj1wOCunnFEELroaZ49CYFCBICEmSYhtYEhQ20zmgvEg/V88V4BuZbvGOX0sDrkZkqiPYh+4AgHoYpJZzHsTK6ZBkpPEJwXyttGZQSD75naVwdRBEz1xTGxIVDWSo7y08m6wj1jdEdnBsfJKJYnJwatAjS2vD45ZSJxRONgIUBStfA3Ow9Q2izMkw18oaCtJvJlfhUmSzKy6jIHF3kR22VpDMmeilXMDfevZ2BeH3LyQKAJnBh96WajOGZMa8FR40UVCASJQk4Q+yYvOEukyU3tt33oO3AhbbSYJL9dst9ENtC/WsTj4YIlJvLrUD7N4YoWlC/PtLgTRwLmqQj4gkjFxzNcJ1d33PkpkHMY+KZIx43g5PrSlSuVowi7sqRBWA8cDbiufVcypMpuXGrz5nLRbqo/goMgMc9/nkN2Fump31SMJXA5gnqFkRXh8td13RfB2OfVNkissHNkJbXatywb+hPiM3Y9V9SxYbTjOX4s3MsGxjbHrPMdSY2BLfHOiuoy9FpZwmNRmEbH1QXzBLnKrMtbr/HCeretAmbOzBhbnObm5MInSADmc977HoHjKZM+OfzV8rIUlHFYT/7ZvL05uqUvIoTZ37XMq5ffBv2hyjqUWFrkQ+618wCAc2Szyx1x0/XX972WdCCgPfCnh1cC/hP8HWgYo7Ny1PQYAAAAASUVORK5CYII=\n",
      "text/latex": [
       "$\\displaystyle \\left( 1152, \\  4, \\  4\\right)$"
      ],
      "text/plain": [
       "(1152, 4, 4)"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "resulting_matrices_np = np.array([np.array(m).astype(np.cdouble) for m in resulting_matrices])\n",
    "\n",
    "resulting_matrices_np.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Invert the resulting matrices to check if they match a starting matrix."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [],
   "source": [
    "resulting_matrices_np_invers = np.array([m.transpose().conjugate() for m in resulting_matrices_np])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [],
   "source": [
    "resulting_matrices_np_invers = np.array([m.transpose().conjugate() for m in starting_matrices_np])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAB0AAAAOCAYAAADT0Rc6AAAACXBIWXMAAA7EAAAOxAGVKw4bAAABsklEQVQ4EaWUj1FCMQyHeZ4DcLgBbAA6gYyAK+gGeoygG+AKuAFs4MEGsIHABvh9pcHy7w4wd3lN0uTXNMlrtV6va0FVVbWRh3AH+yrssbLfRH7L+j3rQh3fabalBb86Qh/+ge9g9SF+Y9ZEyYA0gCewWdRNpmRsHmjg1o7+Duvf3bMPSl0Z8jK9JJebGF/hU4cadCyZJfZl4CA/y6HHaiw8Ub9BOJe6OC5z6coYS1bPpdfegp8UTtElhwo+J9PVCTBvIo3gLkmM9hK0FbaQWu726GR5S79SBmK2gdnpta2wTbKYHpb6aey/DgXIaU/AZSIJ+G/I3HdAm+FzSXmJOyBv9AnYR7lDWXtZt7+2xeRm2J2L629KqCU79ms4vTt2dJPwxk56/aryEngAHKUL4NBjxe5/7sG9Wz4XUS5dC7CXCMQmoOQL5b98MOHY5vh9sd+4qKcE2ZsHAOIpRE1k+Rb5sFWRRN7eLv5W47NvmoEcnDHy5n/bYDVY2hwYw+TDMMTnMSeRvNBtyQjbvOJTw5CeOIw+4mYzh33EvwMMH8femx6jKX6d2MBXvz5suYO2D/4vzMjHQvKY+PsAAAAASUVORK5CYII=\n",
      "text/latex": [
       "$\\displaystyle 128$"
      ],
      "text/plain": [
       "128"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "indices = defaultdict(list)\n",
    "check_indices_zero = [(1,2), (1,3), (1,4), (2,3), (2,4), (3,4)]\n",
    "check_indices_zero += [(k,i) for i,k in check_indices_zero]\n",
    "check_indices_zero = [(i - 1, k - 1) for i, k in check_indices_zero]\n",
    "\n",
    "for rm_np, rm_np_i in zip(resulting_matrices_np, resulting_matrices_np_invers):\n",
    "    rm_tuple = tuple((tuple(r) for r in rm_np))\n",
    "    for i, sm_np in enumerate(starting_matrices_np):\n",
    "        test = sm_np.dot(rm_np_i)\n",
    "\n",
    "        #for check in check_indices_zero:\n",
    "        #    if(not np.allclose(test[check], 0)):\n",
    "        #        continue\n",
    "        \n",
    "        diag = np.diagonal(test)\n",
    "        if(not np.allclose(np.absolute(diag), np.ones(4))):\n",
    "            continue\n",
    "            \n",
    "        phases = np.angle(diag)\n",
    "        phases = phases - phases[0]\n",
    "        #if(not np.allclose(phases, np.zeros(4))):\n",
    "            #print(phases)\n",
    "            #print(test)\n",
    "        #    continue\n",
    "\n",
    "        if(not (phases[0] == phases[1]\n",
    "                  and phases[2] == phases[3])):\n",
    "            continue\n",
    "        \n",
    "        indices[rm_tuple].append(i)\n",
    "        \n",
    "len(indices.keys())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAGAAAAAVCAYAAAC5d+tKAAAACXBIWXMAAA7EAAAOxAGVKw4bAAADDElEQVRYCe2ZjVHjMBCFzxkKyNx1YDqAowPoAEIHXAdQAyVwLXAdhBZCB6EDfjrIvU+RzFqW73IeyzfKZGc2lp5+vHqydtdOtdlsvlipqqoW9mKxQ3kcBlLczuzU6nCr+onFDuVRGag9x82kzQao4VLoNz39v5pWFdg16YPXla5L6V83iT6MsXONVda8Rdokbp/gQPbD9Va8C5qrtqJsVVgtfYywe2H4rXOLx2W1r+OxcZ8hdc1ZvE1aw0o6Z/2OcFV4Um9iQoQ9ho62Tdg7ajFbVhuuLNcGFG+TuLmRPsBZcEELVX4KjOVcwLuODCfECkdpjiuwIGVhuKcPr0BjS/E2ea4X8DrTDwvqy3og+kUDIDQl8cbQ57pnM1Pjh2D7YhOcL470cyV1wSFmQ0TSlhIXhNX+bBu1mbieLIE33GcfbPJrgfMLXNB3Kf56J/EuBtdzZwcIB/sQQX2nyXYftVySTWbhcO5cOIVLEdfKgPrq6kt/F0BsH2H3UZ1I38qgbPuY5ZJsCuuWzaSia07AV2mfj1fTp+hJw708aZIfn6gLvC6qW2yqckk2RZzgKWo2YCfRQiGZUxKTzzEip/0frqcYmxIkuwefIAxxqWymGSPyOS7Hlnxhzn8J53qmOvm5FQI1b6zgZFKtmGE7DimXZlPiAYXzN9a+lN6qQzIGqA0iW/6dvoyRure51Fi18bLWiQF/GpOaJ4VpjuJt0ho4vctKP3xa4AtoJ+X0TzkbFKepHJ8TjTnWNSkayy4RLy5CB2HsOhvzLPw04P9y3RebtA7iqcs+eRFbi5DOCRBOJgORKe18O2IOCRPbca0TpjZuivaenpQtAYvmju0qySY4qB3pnhCe6M4m5MB0P2LKoA3IYQ9zTmmT7kXcdA/9jDtLcEOt7Mah+X7OtOidUt98JnRmntImEhI4336MExl8iCNjCZkNbVnEx4HXLJMPnHRKmzzHxNztx0+OHyohQC5DPddV9+h89s51r13nndImOJY27rfCyCB+d/ijJfVpOnQ7XAcyIH5JPckMm5fW3+gnifLF8ptzAAAAAElFTkSuQmCC\n",
      "text/latex": [
       "$\\displaystyle \\left( 24, \\  24, \\  2\\right)$"
      ],
      "text/plain": [
       "(24, 24, 2)"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from collections import deque\n",
    "lookup_edge_c0_c1_to_number = []\n",
    "r = []\n",
    "c = []\n",
    "\n",
    "i = 0\n",
    "\n",
    "for _ in C_L_0:\n",
    "    r = []\n",
    "    for _ in C_L_1:\n",
    "        c = []\n",
    "        for _ in (I_4, CZ):\n",
    "            c.append(i)\n",
    "            i += 1          \n",
    "        r.append(c)\n",
    "    lookup_edge_c0_c1_to_number.append(r)\n",
    "    \n",
    "lookup_edge_c0_c1_to_number = np.array(lookup_edge_c0_c1_to_number, dtype=np.int)\n",
    "lookup_edge_c0_c1_to_number.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Produce (almost valid) `C` code to look up the lookup table number from given clifford operator indices and entanglement (0 means no entanglement)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "    , { {0, 1}, {2, 3}, {4, 5}, {6, 7}, {8, 9}, {10, 11}, {12, 13}, {14, 15}, {16, 17}, {18, 19}, {20, 21}, {22, 23}, {24, 25}, {26, 27}, {28, 29}, {30, 31}, {32, 33}, {34, 35}, {36, 37}, {38, 39}, {40, 41}, {42, 43}, {44, 45}, {46, 47} }\n",
      "    , { {48, 49}, {50, 51}, {52, 53}, {54, 55}, {56, 57}, {58, 59}, {60, 61}, {62, 63}, {64, 65}, {66, 67}, {68, 69}, {70, 71}, {72, 73}, {74, 75}, {76, 77}, {78, 79}, {80, 81}, {82, 83}, {84, 85}, {86, 87}, {88, 89}, {90, 91}, {92, 93}, {94, 95} }\n",
      "    , { {96, 97}, {98, 99}, {100, 101}, {102, 103}, {104, 105}, {106, 107}, {108, 109}, {110, 111}, {112, 113}, {114, 115}, {116, 117}, {118, 119}, {120, 121}, {122, 123}, {124, 125}, {126, 127}, {128, 129}, {130, 131}, {132, 133}, {134, 135}, {136, 137}, {138, 139}, {140, 141}, {142, 143} }\n",
      "    , { {144, 145}, {146, 147}, {148, 149}, {150, 151}, {152, 153}, {154, 155}, {156, 157}, {158, 159}, {160, 161}, {162, 163}, {164, 165}, {166, 167}, {168, 169}, {170, 171}, {172, 173}, {174, 175}, {176, 177}, {178, 179}, {180, 181}, {182, 183}, {184, 185}, {186, 187}, {188, 189}, {190, 191} }\n",
      "    , { {192, 193}, {194, 195}, {196, 197}, {198, 199}, {200, 201}, {202, 203}, {204, 205}, {206, 207}, {208, 209}, {210, 211}, {212, 213}, {214, 215}, {216, 217}, {218, 219}, {220, 221}, {222, 223}, {224, 225}, {226, 227}, {228, 229}, {230, 231}, {232, 233}, {234, 235}, {236, 237}, {238, 239} }\n",
      "    , { {240, 241}, {242, 243}, {244, 245}, {246, 247}, {248, 249}, {250, 251}, {252, 253}, {254, 255}, {256, 257}, {258, 259}, {260, 261}, {262, 263}, {264, 265}, {266, 267}, {268, 269}, {270, 271}, {272, 273}, {274, 275}, {276, 277}, {278, 279}, {280, 281}, {282, 283}, {284, 285}, {286, 287} }\n",
      "    , { {288, 289}, {290, 291}, {292, 293}, {294, 295}, {296, 297}, {298, 299}, {300, 301}, {302, 303}, {304, 305}, {306, 307}, {308, 309}, {310, 311}, {312, 313}, {314, 315}, {316, 317}, {318, 319}, {320, 321}, {322, 323}, {324, 325}, {326, 327}, {328, 329}, {330, 331}, {332, 333}, {334, 335} }\n",
      "    , { {336, 337}, {338, 339}, {340, 341}, {342, 343}, {344, 345}, {346, 347}, {348, 349}, {350, 351}, {352, 353}, {354, 355}, {356, 357}, {358, 359}, {360, 361}, {362, 363}, {364, 365}, {366, 367}, {368, 369}, {370, 371}, {372, 373}, {374, 375}, {376, 377}, {378, 379}, {380, 381}, {382, 383} }\n",
      "    , { {384, 385}, {386, 387}, {388, 389}, {390, 391}, {392, 393}, {394, 395}, {396, 397}, {398, 399}, {400, 401}, {402, 403}, {404, 405}, {406, 407}, {408, 409}, {410, 411}, {412, 413}, {414, 415}, {416, 417}, {418, 419}, {420, 421}, {422, 423}, {424, 425}, {426, 427}, {428, 429}, {430, 431} }\n",
      "    , { {432, 433}, {434, 435}, {436, 437}, {438, 439}, {440, 441}, {442, 443}, {444, 445}, {446, 447}, {448, 449}, {450, 451}, {452, 453}, {454, 455}, {456, 457}, {458, 459}, {460, 461}, {462, 463}, {464, 465}, {466, 467}, {468, 469}, {470, 471}, {472, 473}, {474, 475}, {476, 477}, {478, 479} }\n",
      "    , { {480, 481}, {482, 483}, {484, 485}, {486, 487}, {488, 489}, {490, 491}, {492, 493}, {494, 495}, {496, 497}, {498, 499}, {500, 501}, {502, 503}, {504, 505}, {506, 507}, {508, 509}, {510, 511}, {512, 513}, {514, 515}, {516, 517}, {518, 519}, {520, 521}, {522, 523}, {524, 525}, {526, 527} }\n",
      "    , { {528, 529}, {530, 531}, {532, 533}, {534, 535}, {536, 537}, {538, 539}, {540, 541}, {542, 543}, {544, 545}, {546, 547}, {548, 549}, {550, 551}, {552, 553}, {554, 555}, {556, 557}, {558, 559}, {560, 561}, {562, 563}, {564, 565}, {566, 567}, {568, 569}, {570, 571}, {572, 573}, {574, 575} }\n",
      "    , { {576, 577}, {578, 579}, {580, 581}, {582, 583}, {584, 585}, {586, 587}, {588, 589}, {590, 591}, {592, 593}, {594, 595}, {596, 597}, {598, 599}, {600, 601}, {602, 603}, {604, 605}, {606, 607}, {608, 609}, {610, 611}, {612, 613}, {614, 615}, {616, 617}, {618, 619}, {620, 621}, {622, 623} }\n",
      "    , { {624, 625}, {626, 627}, {628, 629}, {630, 631}, {632, 633}, {634, 635}, {636, 637}, {638, 639}, {640, 641}, {642, 643}, {644, 645}, {646, 647}, {648, 649}, {650, 651}, {652, 653}, {654, 655}, {656, 657}, {658, 659}, {660, 661}, {662, 663}, {664, 665}, {666, 667}, {668, 669}, {670, 671} }\n",
      "    , { {672, 673}, {674, 675}, {676, 677}, {678, 679}, {680, 681}, {682, 683}, {684, 685}, {686, 687}, {688, 689}, {690, 691}, {692, 693}, {694, 695}, {696, 697}, {698, 699}, {700, 701}, {702, 703}, {704, 705}, {706, 707}, {708, 709}, {710, 711}, {712, 713}, {714, 715}, {716, 717}, {718, 719} }\n",
      "    , { {720, 721}, {722, 723}, {724, 725}, {726, 727}, {728, 729}, {730, 731}, {732, 733}, {734, 735}, {736, 737}, {738, 739}, {740, 741}, {742, 743}, {744, 745}, {746, 747}, {748, 749}, {750, 751}, {752, 753}, {754, 755}, {756, 757}, {758, 759}, {760, 761}, {762, 763}, {764, 765}, {766, 767} }\n",
      "    , { {768, 769}, {770, 771}, {772, 773}, {774, 775}, {776, 777}, {778, 779}, {780, 781}, {782, 783}, {784, 785}, {786, 787}, {788, 789}, {790, 791}, {792, 793}, {794, 795}, {796, 797}, {798, 799}, {800, 801}, {802, 803}, {804, 805}, {806, 807}, {808, 809}, {810, 811}, {812, 813}, {814, 815} }\n",
      "    , { {816, 817}, {818, 819}, {820, 821}, {822, 823}, {824, 825}, {826, 827}, {828, 829}, {830, 831}, {832, 833}, {834, 835}, {836, 837}, {838, 839}, {840, 841}, {842, 843}, {844, 845}, {846, 847}, {848, 849}, {850, 851}, {852, 853}, {854, 855}, {856, 857}, {858, 859}, {860, 861}, {862, 863} }\n",
      "    , { {864, 865}, {866, 867}, {868, 869}, {870, 871}, {872, 873}, {874, 875}, {876, 877}, {878, 879}, {880, 881}, {882, 883}, {884, 885}, {886, 887}, {888, 889}, {890, 891}, {892, 893}, {894, 895}, {896, 897}, {898, 899}, {900, 901}, {902, 903}, {904, 905}, {906, 907}, {908, 909}, {910, 911} }\n",
      "    , { {912, 913}, {914, 915}, {916, 917}, {918, 919}, {920, 921}, {922, 923}, {924, 925}, {926, 927}, {928, 929}, {930, 931}, {932, 933}, {934, 935}, {936, 937}, {938, 939}, {940, 941}, {942, 943}, {944, 945}, {946, 947}, {948, 949}, {950, 951}, {952, 953}, {954, 955}, {956, 957}, {958, 959} }\n",
      "    , { {960, 961}, {962, 963}, {964, 965}, {966, 967}, {968, 969}, {970, 971}, {972, 973}, {974, 975}, {976, 977}, {978, 979}, {980, 981}, {982, 983}, {984, 985}, {986, 987}, {988, 989}, {990, 991}, {992, 993}, {994, 995}, {996, 997}, {998, 999}, {1000, 1001}, {1002, 1003}, {1004, 1005}, {1006, 1007} }\n",
      "    , { {1008, 1009}, {1010, 1011}, {1012, 1013}, {1014, 1015}, {1016, 1017}, {1018, 1019}, {1020, 1021}, {1022, 1023}, {1024, 1025}, {1026, 1027}, {1028, 1029}, {1030, 1031}, {1032, 1033}, {1034, 1035}, {1036, 1037}, {1038, 1039}, {1040, 1041}, {1042, 1043}, {1044, 1045}, {1046, 1047}, {1048, 1049}, {1050, 1051}, {1052, 1053}, {1054, 1055} }\n",
      "    , { {1056, 1057}, {1058, 1059}, {1060, 1061}, {1062, 1063}, {1064, 1065}, {1066, 1067}, {1068, 1069}, {1070, 1071}, {1072, 1073}, {1074, 1075}, {1076, 1077}, {1078, 1079}, {1080, 1081}, {1082, 1083}, {1084, 1085}, {1086, 1087}, {1088, 1089}, {1090, 1091}, {1092, 1093}, {1094, 1095}, {1096, 1097}, {1098, 1099}, {1100, 1101}, {1102, 1103} }\n",
      "    , { {1104, 1105}, {1106, 1107}, {1108, 1109}, {1110, 1111}, {1112, 1113}, {1114, 1115}, {1116, 1117}, {1118, 1119}, {1120, 1121}, {1122, 1123}, {1124, 1125}, {1126, 1127}, {1128, 1129}, {1130, 1131}, {1132, 1133}, {1134, 1135}, {1136, 1137}, {1138, 1139}, {1140, 1141}, {1142, 1143}, {1144, 1145}, {1146, 1147}, {1148, 1149}, {1150, 1151} }\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "print(\"{\")\n",
    "for r in lookup_edge_c0_c1_to_number:\n",
    "    print(\"    , {\", \", \".join((\"{%d, %d}\" % (c[0], c[1]) for c in r)), \"}\")\n",
    "print(\"}\")    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To satisfy some constraints try one wants to keep the indentity on one qbit if possible."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 96,  97,  98,  99, 100, 101, 102, 103, 104, 105, 106, 107, 108,\n",
       "       109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121,\n",
       "       122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134,\n",
       "       135, 136, 137, 138, 139, 140, 141, 142, 143])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "I0_indices = lookup_edge_c0_c1_to_number[2, :, :].reshape(2*24)\n",
    "I0_indices"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([   4,    5,   52,   53,  100,  101,  148,  149,  196,  197,  244,\n",
       "        245,  292,  293,  340,  341,  388,  389,  436,  437,  484,  485,\n",
       "        532,  533,  580,  581,  628,  629,  676,  677,  724,  725,  772,\n",
       "        773,  820,  821,  868,  869,  916,  917,  964,  965, 1012, 1013,\n",
       "       1060, 1061, 1108, 1109])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "I1_indices = lookup_edge_c0_c1_to_number[:, 2, :].reshape(2*24)\n",
    "I1_indices"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Throw away all the matrices that produce the same result that we don't need."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "def contract_indices(equivs):\n",
    "    indices_I1 = [e for e in equivs if e in I1_indices]\n",
    "    indices_I0 = [e for e in equivs if e in I0_indices]\n",
    "    \n",
    "    if(not indices_I0 and not indices_I1):\n",
    "        return [equivs[0], -1, -1]\n",
    "    if(indices_I0 and not indices_I1):\n",
    "        return [-1, indices_I0[0], -1]\n",
    "    if(indices_I1 and not indices_I0):\n",
    "        return [-1, -1, indices_I1[0]]\n",
    "    return [-1, indices_I0[0], indices_I1[0]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "indices_contracted = {k: contract_indices(v) for k,v in indices.items()}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_resulting_matrix_index(tpl):\n",
    "    arr = np.array(tpl)\n",
    "    for i, test in enumerate(resulting_matrices_np):\n",
    "        if(np.allclose(test, arr)):\n",
    "            return i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "indices_contracted_by_resulting_index = {get_resulting_matrix_index(k): v for k,v in indices_contracted.items()}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "None in indices_contracted_by_resulting_index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [],
   "source": [
    "remap_lookup_table_multiple_results = [v for k,v in sorted(indices_contracted_by_resulting_index.items())]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAAgAAAAVCAYAAAB7R6/OAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAARUlEQVQoFWNgYGAoA+K7UGyEhc/QCRR0+f//PwMyBoqBxZmADLxgVAEkeEbDgchwYIEmp5mMjIwgZigQuwBxOhALAfFuAP68FOR8/BSoAAAAAElFTkSuQmCC\n",
      "text/latex": [
       "$\\displaystyle \\left[ \\right]$"
      ],
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "remap_lookup_table_multiple_results[0:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAAoAAAAOCAYAAAAWo42rAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAxElEQVQoFYWRjQ2CMBCFbeMABDeADfyZQEZgBndwBGZhBFhBNpANUDco7zV95DRNbHJt7/W7n7YuhLDTcM51ab9grWEdzueoEUzwA0Jr/AL+E1bF8wTdKArSCo0VBgsOEHoBWqG1MPZWeEwcDewVd9/TJ7mNxwXYy79RMmOZKEXbIFXZStvD3P7AjIrKAaq2eNxQJXO9Spt16xHpqkxKZRwF9oDOGfAEbYpVzePyu36/8A3tSGZvsjD6jndVCxf4V0ATmRXq32m5fsF9TwAAAABJRU5ErkJggg==\n",
      "text/latex": [
       "$\\displaystyle 0$"
      ],
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(remap_lookup_table_multiple_results)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "starting_indices_to_operators = {i: (c0, c1, e) for i, (c0, c1, e) in enumerate(product(range(24), range(24), range(2)))}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "def contract_indices_to_single(result_index, indices):\n",
    "    c0, c1, e = starting_indices_to_operators[result_index]\n",
    "    # Preserve identity on one qbit.\n",
    "    # Note that there is no case where both vops are the identity,\n",
    "    # As they are handled by a different case.\n",
    "    if(c0 == 2):\n",
    "        return indices[1]\n",
    "    if(c1 == 2):\n",
    "        return indices[2]\n",
    "    \n",
    "    # Try to have an identity if it is possible.\n",
    "    if(indices[1] != -1):\n",
    "        return indices[1]\n",
    "    if(indices[2] != -1):\n",
    "        return indices[2]\n",
    "    # No identity is possible.\n",
    "    return indices[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [],
   "source": [
    "remap_lookup_table_indices = [contract_indices_to_single(i, indices) for i,indices in enumerate(remap_lookup_table_multiple_results)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [],
   "source": [
    "remap_lookup_table_operators = [starting_indices_to_operators[i] for i in remap_lookup_table_indices]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAAgAAAAVCAYAAAB7R6/OAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAARUlEQVQoFWNgYGAoA+K7UGyEhc/QCRR0+f//PwMyBoqBxZmADLxgVAEkeEbDgchwYIEmp5mMjIwgZigQuwBxOhALAfFuAP68FOR8/BSoAAAAAElFTkSuQmCC\n",
      "text/latex": [
       "$\\displaystyle \\left[ \\right]$"
      ],
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "remap_lookup_table_operators[0:10]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Produce some (almost valid) `C` code from the lookup table:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "print(\"{\")\n",
    "print(\"\\n    , \".join((\"{%d, %d, %d}\" % i for i in remap_lookup_table_operators)))\n",
    "print(\"}\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
