{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "3eea6d3e", "metadata": { "execution": { "iopub.execute_input": "2024-03-26T18:57:17.353790Z", "iopub.status.busy": "2024-03-26T18:57:17.353414Z", "iopub.status.idle": "2024-03-26T18:57:17.356286Z", "shell.execute_reply": "2024-03-26T18:57:17.355845Z" }, "nbsphinx": "hidden" }, "outputs": [], "source": [ "# Copyright 2024 Keysight Technologies Inc." ] }, { "cell_type": "raw", "id": "56d3c914", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "Example: Interfacing with Other Software\n", "========================================\n", "\n", "You can use the unique functionality of |True-Q| for projects developed in other\n", "quantum computing software libraries by converting objects created by those projects\n", "to |True-Q|. You can also use functionality from other quantum computing software\n", "libraries by converting |True-Q| objects into their format. |True-Q| can natively\n", "convert to and from:\n", "\n", "#. QASM\n", " A quantum assembly language used by many quantum hardware makers.\n", "#. Qiskit\n", " IBM's quantum computing software package.\n", "#. Cirq\n", " Google's quantum computing software package.\n", "#. PyQuil\n", " Rigetti's quantum computing software package.\n", "\n", "Before converting a circuit to/from a software package, the first thing to consider is\n", "what gates you want the outputted circuit to use---since each software package is\n", "independently written, the primitive gates they each use may differ. By default,\n", "|True-Q| will recompile using only the gates supported by the software package.\n", "However, more advanced users can specify a subset of supported gates to compile into\n", "when converting between software platforms by using a custom configuration, as shown\n", "below.\n", "\n", ".. note:: Before starting, make sure that you have installed the software that you\n", " want to convert from, by trying to import it into your Python notebook. If they are\n", " successfully installed, continue on to the next section. |True-Q| does not\n", " automatically install the third-party libraries from above, so if you want to use\n", " them you need to install them separately.\n", "\n", "Examples of Back and Forth Conversions\n", "--------------------------------------\n", "To get a feel for converting between |True-Q| and other software packages, we will\n", "show an example converting from |True-Q| to Cirq and back. Try constructing a circuit\n", "in |True-Q| then running :py:func:`~trueq.Circuit.to_cirq`\\, like this:" ] }, { "cell_type": "code", "execution_count": 2, "id": "cff71dc9", "metadata": { "execution": { "iopub.execute_input": "2024-03-26T18:57:17.358248Z", "iopub.status.busy": "2024-03-26T18:57:17.357904Z", "iopub.status.idle": "2024-03-26T18:57:20.277007Z", "shell.execute_reply": "2024-03-26T18:57:20.276544Z" } }, "outputs": [ { "data": { "text/html": [ "
(0, 0): ───@───\n",
       "           │\n",
       "(1, 0): ───@───
" ], "text/plain": [ "(0, 0): ───@───\n", " │\n", "(1, 0): ───@───" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import trueq as tq\n", "\n", "# True-Q does not attempt to load the third party module---in this example, Cirq---until\n", "# it is needed, for example by to_cirq(), or until load() is called. the method\n", "# to_trueq() is attached to the third party library's circuit object at this time.\n", "tq.interface.cirq.load()\n", "\n", "circuit = tq.Circuit({(0, 1): tq.Gate.cz})\n", "circuit.draw(interactive=False)\n", "\n", "cirq_circuit = circuit.to_cirq()\n", "cirq_circuit" ] }, { "cell_type": "raw", "id": "ff71540f", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "Notice that the ``cirq_circuit`` object is a Cirq circuit, so the output for that\n", "object is drawn using Cirq's visualization tools. If we want to convert back to\n", "|True-Q|, we can use the following code and use :py:meth:`~trueq.Circuit.draw` to draw\n", "a circuit diagram." ] }, { "cell_type": "code", "execution_count": 3, "id": "86266366", "metadata": { "execution": { "iopub.execute_input": "2024-03-26T18:57:20.279392Z", "iopub.status.busy": "2024-03-26T18:57:20.278893Z", "iopub.status.idle": "2024-03-26T18:57:20.282843Z", "shell.execute_reply": "2024-03-26T18:57:20.282422Z" } }, "outputs": [ { "data": { "text/html": [ "
0 1 1 CZ CZ
" ], "text/plain": [ "DisplayWrapper( 0 1 1 ID ID 2 ID X 3 8 S 4 CX CX 5 ID ID 6 ID X 7 S S " ], "text/plain": [ "DisplayWrapper((0, 0): ───X^0───X^0───S^-1───@───X^0───X^0───S───\n", " │\n", "(1, 0): ───X^0───X─────S──────X───X^0───X─────S───" ], "text/plain": [ "(0, 0): ───X^0───X^0───S^-1───@───X^0───X^0───S───\n", " │\n", "(1, 0): ───X^0───X─────S──────X───X^0───X─────S───" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "circuit.to_cirq()" ] }, { "cell_type": "raw", "id": "f763b737", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ ".. note ::\n", " Setting the config to ``None`` resets to the default config.\n", "\n", "Reset the config to the default:" ] }, { "cell_type": "code", "execution_count": 9, "id": "caaafdf5", "metadata": { "execution": { "iopub.execute_input": "2024-03-26T18:57:20.318905Z", "iopub.status.busy": "2024-03-26T18:57:20.318614Z", "iopub.status.idle": "2024-03-26T18:57:20.320948Z", "shell.execute_reply": "2024-03-26T18:57:20.320543Z" } }, "outputs": [], "source": [ "tq.interface.cirq.set_config(None)" ] }, { "cell_type": "raw", "id": "8358c78e", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "Convert the circuit to Cirq using the default config" ] }, { "cell_type": "code", "execution_count": 10, "id": "5dbe6c77", "metadata": { "execution": { "iopub.execute_input": "2024-03-26T18:57:20.322783Z", "iopub.status.busy": "2024-03-26T18:57:20.322492Z", "iopub.status.idle": "2024-03-26T18:57:20.329037Z", "shell.execute_reply": "2024-03-26T18:57:20.328638Z" } }, "outputs": [ { "data": { "text/html": [ "
(0, 0): ───H^0───H^0─────Rz(-0.5π)───@───H^0───H^0─────Rz(0.5π)───\n",
       "                                     │\n",
       "(1, 0): ───H^0───Rx(π)───Rz(0.5π)────X───H^0───Rx(π)───Rz(0.5π)───
" ], "text/plain": [ "(0, 0): ───H^0───H^0─────Rz(-0.5π)───@───H^0───H^0─────Rz(0.5π)───\n", " │\n", "(1, 0): ───H^0───Rx(π)───Rz(0.5π)────X───H^0───Rx(π)───Rz(0.5π)───" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "circuit.to_cirq()" ] }, { "cell_type": "raw", "id": "0d619e01", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "As you can see in the printed circuits, changing the config alters the gates used in\n", "converting from |True-Q| to other software. For example, removing the ``X`` gate from\n", "the config causes the interface to pick the next viable representation of this\n", "operation, so that the ``X`` gates are given as rotations about ``X`` by :math:`\\pi`\\." ] } ], "metadata": { "jupytext": { "cell_metadata_filter": "raw_mimetype,nbsphinx,-all", "main_language": "python", "notebook_metadata_filter": "-all", "text_representation": { "extension": ".py", "format_name": "percent" } }, "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.8.10" } }, "nbformat": 4, "nbformat_minor": 5 }