{"cells":[{"cell_type":"markdown","metadata":{},"source":["# Symbolic compilation\n","\n","**Download this notebook - {nb-download}`symbolics_example.ipynb`**"]},{"cell_type":"markdown","metadata":{},"source":["Motivation: in compilation, particularly of hybrid classical-quantum variational algorithms in which the structure of a circuit remains constant but the parameters of some gates change, it can be useful to compile using symbolic parameters and optimise the circuit without knowledge of what these parameters will be instantiated to afterwards.
\n","
\n","In this tutorial, we will show how to compile a circuit containing mathematical symbols, and then instantiate the symbols afterwards. To do this, you need to have `pytket` installed. Run:
\n","
\n","`pip install pytket`
\n","
\n","To begin, we will import the `Circuit` and `Transform` classes from `pytket`, and the `fresh_symbol` method from `pytket.circuit`."]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["from pytket.circuit import Circuit, fresh_symbol\n","from pytket.transform import Transform"]},{"cell_type":"markdown","metadata":{},"source":["Now, we can construct a circuit containing symbols. You can ask for symbols by calling the `fresh_symbol` method with a string as an argument. This string represents the preferred symbol name; if this has already been used elsewhere, an appropriate suffix of the form `_x`, with `x` a natural number, will be added to generate a new symbol, as shown below:"]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["a = fresh_symbol(\"a\")\n","a1 = fresh_symbol(\"a\")\n","print(a)\n","print(a1)"]},{"cell_type":"markdown","metadata":{},"source":["We are going to make a circuit using just three 'phase gadgets': `Rz` gates surrounded by ladders of `CX` gates."]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["b = fresh_symbol(\"b\")\n","circ = Circuit(4)\n","circ.CX(0, 1)\n","circ.CX(1, 2)\n","circ.CX(2, 3)\n","circ.Rz(a, 3)\n","circ.CX(2, 3)\n","circ.CX(1, 2)\n","circ.CX(0, 1)\n","circ.CX(3, 2)\n","circ.CX(2, 1)\n","circ.CX(1, 0)\n","circ.Rz(b, 0)\n","circ.CX(1, 0)\n","circ.CX(2, 1)\n","circ.CX(3, 2)\n","circ.CX(0, 1)\n","circ.CX(1, 2)\n","circ.CX(2, 3)\n","circ.Rz(0.5, 3)\n","circ.CX(2, 3)\n","circ.CX(1, 2)\n","circ.CX(0, 1)"]},{"cell_type":"markdown","metadata":{},"source":["Now we can use the `render_circuit_jupyter` method to display the circuit."]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["from pytket.circuit.display import render_circuit_jupyter"]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["render_circuit_jupyter(circ)"]},{"cell_type":"markdown","metadata":{},"source":["Now let's use a transform to shrink the circuit. For more detail on transforms, see the `transform_example` notebook."]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["Transform.OptimisePhaseGadgets().apply(circ)\n","render_circuit_jupyter(circ)"]},{"cell_type":"markdown","metadata":{},"source":["Note that the type of gate has changed to `U1`, but the phase gadgets have been successfully combined. The `U1` gate is an IBM-specific gate that is equivalent to an `Rz`.
\n","
\n","We can now instantiate the symbols with some desired values. We make a dictionary, with each key a symbol name, and each value a double. Note that this value is in units of 'half-turns', in which a value of $1$ corresponds to a rotation of $\\pi$.
\n","
\n","Before instantiating our parameters we make a copy of the circuit, so that we can repeat the exercise without the need for recompilation."]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["symbol_circ = circ.copy()"]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["symbol_dict = {a: 0.5, b: 0.75}\n","circ.symbol_substitution(symbol_dict)"]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["render_circuit_jupyter(circ)"]},{"cell_type":"markdown","metadata":{},"source":["Because this symbol substitution was called on the copy, we still have our original symbolic circuit."]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["render_circuit_jupyter(symbol_circ)"]},{"cell_type":"markdown","metadata":{},"source":["Note: the expression tree for this symbolic expression is very small, consisting of only a couple of different operations, but tket is capable of handling large and complex expressions containing many different types of operation, such as trigonometric functions.
\n","
\n","It is usually possible to instantiate a symbolic circuit with specific values that allow further optimisation: for example, if we had chosen $a=1.5$ and $b=0$, this circuit would be reduce to the identity. If there are likely to be many parameters set to trivial values (such as $0$ or $1$), it can be beneficial to perform further optimisation after instantiation."]}],"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.6.4"}},"nbformat":4,"nbformat_minor":2}