{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Find frequencies that make only $(\\hat{a}^2\\hat{b}^2\\hat{e}^{\\dagger2}+\\hat{a}\\hat{b}\\hat{d}^2\\hat{e}^{\\dagger2})\\hat{c}^\\dagger +h.c.$ resonant in the 7th order expansion of $\\sin(\\hat{a}+\\hat{b}+\\hat{c}+\\hat{d}+\\hat{e}+h.c.)$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Proceed as in the 5th order example."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import hamnonlineng as hnle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Number of off-resonant constraints: 6308\n"
     ]
    }
   ],
   "source": [
    "letters = 'abcde'\n",
    "resonant = [hnle.Monomial(1, 'aabbEEC'), hnle.Monomial(1,'abddEEC')]\n",
    "op_sum = hnle.operator_sum(letters)\n",
    "sine_exp = (\n",
    "            hnle.sin_terms(op_sum, 3)\n",
    "           +hnle.sin_terms(op_sum, 5)\n",
    "           +hnle.sin_terms(op_sum, 7)\n",
    "           )\n",
    "off_resonant = hnle.drop_single_mode(\n",
    "                 hnle.drop_definitely_offresonant(\n",
    "                   hnle.drop_matching(sine_exp.m, resonant)))\n",
    "off_resonant = list(off_resonant)\n",
    "print('Number of off-resonant constraints: %d'%len(off_resonant))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Try to solve (takes around a minute):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 solutions.\n"
     ]
    }
   ],
   "source": [
    "res = hnle.head_and_count(\n",
    "        hnle.solve_constraints_gecode(resonant, off_resonant, letters, maxfreq=50))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Remove constraints on terms of the form $\\hat{a}^2\\hat{b}^2\\dots$ or $\\hat{a}\\hat{b}\\dots$ or those that do not contain any $\\hat{a}$s or $\\hat{b}$s."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Number of constraints starting with ab or aabb or containing no a or b: 3766\n"
     ]
    }
   ],
   "source": [
    "# Drop all of the form aabb...\n",
    "starts_with_aabb = [_ for _ in off_resonant if _.s[4:5].lower() not in 'ab' and _.s.startswith('AABB')]\n",
    "# Drop all of the form ab...\n",
    "starts_with_ab   = [_ for _ in off_resonant if _.s[2:3].lower() not in 'ab' and _.s.startswith('AB')]\n",
    "# Drop all that do not contain any a or b\n",
    "no_a_or_b = [_ for _ in off_resonant if 'a' not in _.s.lower() or 'b' not in _.s.lower()]\n",
    "to_be_removed = starts_with_aabb + starts_with_ab + no_a_or_b\n",
    "print('Number of constraints starting with ab or aabb or containing no a or b: %d'%len(to_be_removed))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Number of new off-resonant constraints: 2542\n"
     ]
    }
   ],
   "source": [
    "off_resonant = hnle.drop_matching(off_resonant, to_be_removed)\n",
    "off_resonant = list(off_resonant)\n",
    "print('Number of new off-resonant constraints: %d'%len(off_resonant))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.AAaB\n",
       "1.AAab\n",
       "2.AB\n",
       "2.Ab"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "hnle.Monomial(1,'aaA')*hnle.Monomial(1,'b')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Try to solve again:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[23, 25, 30, 24, 33]\n",
      "[25, 23, 30, 24, 33]\n",
      "2 solutions.\n"
     ]
    }
   ],
   "source": [
    "res = hnle.head_and_count(\n",
    "        hnle.solve_constraints_gecode(resonant, off_resonant, letters, maxfreq=33))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "See which of the removed constraints fail:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For the first solution:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[23, 25, 30, 24, 33]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "res[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[-1/12.AAAccDe, -1/36.AAACeee, -1/36.BBBDeee, -1/48.CddddEE]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "hnle.filter_resonant(res[0], to_be_removed, letters)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For the second solution:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[25, 23, 30, 24, 33]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "res[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[-1/36.AAADeee, -1/12.BBBccDe, -1/36.BBBCeee, -1/48.CddddEE]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "hnle.filter_resonant(res[1], to_be_removed, letters)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
