{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Find frequencies that make only $(\\hat{a}^2\\hat{b}^2+\\hat{a}\\hat{b}\\hat{d}^2+\\hat{d}^4)\\hat{c}^\\dagger +h.c.$ resonant in the 5th order expansion of $\\sin(\\hat{a}+\\hat{b}+\\hat{c}+\\hat{d}+h.c.)$\n",
    "\n",
    "## Here we use linear programming instead of constraint programming and search for any positively-valued frequencies (instead of just integer frequencies)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Import the \"Hamiltonian-through-Nonlinearities Engineering\" module (it can be installed from PyPI using `pip`)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import hamnonlineng as hnle"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Set the letters you want to use for annihilation operators (4 modes in our case)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "letters = 'abcd'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Write down (or somehow generate) a list of the monomials that you want to be resonant."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "resonant = [hnle.Monomial(1,'aabbC'), # First argument is the constant real factor in front of the operator\n",
    "            hnle.Monomial(1,'abddC'), # Second argument is the string representing the operators\n",
    "            hnle.Monomial(1,'Cdddd')]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The convention for typing in or printing out is:\n",
    "- lower 'a' represents $\\hat{a}$\n",
    "- capital 'A' represents $\\hat{a}^\\dagger$\n",
    "- the hermitian conjugate is implicit, i.e. `Monomial(1,'Aab')` is $\\hat{a}^\\dagger\\hat{a}\\hat{b}+\\hat{a}^\\dagger\\hat{a}\\hat{b}^\\dagger$\n",
    "- the library sorts the expresion to make it \"canonical\", and given that the presence of a hermitian conjugate is implicit each monomial might print out as its conjugate, i.e. there is no difference between `Monomial(1,'a')` and `Monomial(1,'A')` "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1.AABBc, 1.ABcDD, 1.Cdddd]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "resonant"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now generate the terms that you want to be off resonant: start with the sum $\\hat{a}+\\hat{b}+\\hat{c}+\\hat{d}+h.c.$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.A\n",
       "1.B\n",
       "1.C\n",
       "1.D"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "op_sum = hnle.operator_sum(letters)\n",
    "op_sum"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Generate the list of 3rd and 5th order terms in the expansion of $\\sin(\\hat{a}+\\hat{b}+\\hat{c}+\\hat{d}+h.c.)$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "sine_exp = hnle.sin_terms(op_sum, 3) + hnle.sin_terms(op_sum, 5)\n",
    "sine_exp_list = sine_exp.m"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Filter out of the list:\n",
    "- terms that match the terms we want to be resonant\n",
    "- terms that are only annihilation or only creation operators (definitely off-resonant)\n",
    "- terms that contain only one single mode"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "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)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "How many terms are left."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "365"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(off_resonant)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Finally, solve the constraints:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Adding resonant constraints...\n",
      "Adding off-resonant constraints...\n",
      "Adding distinct constraints...\n",
      "Dumping LP file...\n",
      "Solving...\n",
      "Status: 1 - Optimal\n"
     ]
    }
   ],
   "source": [
    "res = hnle.solve_linearprog_pulp(resonant, off_resonant, letters, maxfreq=20, detune=0.1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0.3, 0.7, 2.0, 0.5]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "res"
   ]
  }
 ],
 "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
}
