{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Gate Expansions\n", "\n", "This notebook is intended to illustrate various gate expansions.\n", "Qubiter allows one to write on a single line in an English file \n", "many types of U(2) matrices with 0, 1, or more controls attached to it. However, there are well known identities for expanding such gates into\n", "a sequence of (1) single qubit rotations and (2) CNOTs with a single control.\n", "This is useful because most quantum computers (for example, IBM Quantum Experience)\n", "can only perform (1) and (2). Expansions into (1) and (2)\n", "can be performed automatically by Qubiter. Here is how.\n", "\n", "The expansions used by Qubiter \n", "were all discovered long ago. They can all be\n", "found in the following 1995 quantum computing paper:\n", "\n", "https://arxiv.org/abs/quant-ph/9503016\n", "\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First change your working directory to the qubiter directory in your computer, and add its path to the path environment variable." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/home/rrtucci/PycharmProjects/qubiter/qubiter/jupyter_notebooks\n", "/home/rrtucci/PycharmProjects/qubiter\n" ] } ], "source": [ "import os\n", "import sys\n", "print(os.getcwd())\n", "os.chdir('../../')\n", "print(os.getcwd())\n", "sys.path.insert(0,os.getcwd())" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "loaded OneQubitGate, WITHOUT autograd.numpy\n" ] } ], "source": [ "from qubiter.CGateExpander import *\n", "from qubiter.SEO_writer import *" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "file_prefix = 'expansions_examples'\n", "num_qbits = 3\n", "emb = CktEmbedder(num_qbits, num_qbits, range(num_qbits))\n", "wr = SEO_writer(file_prefix, emb)\n", "\n", "trols1 = Controls(num_qbits)\n", "trols1.bit_pos_to_kind = {0: True}\n", "trols1.refresh_lists()\n", "\n", "trols2 = Controls(num_qbits)\n", "trols2.bit_pos_to_kind = {0: True, 1: False}\n", "trols2.refresh_lists()\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write simple CNOT ( $\\sigma_X(1)^{n(0)}$ )" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "wr.write_NOTA('simple cnot ( sigx(1)^n(0) )')\n", "wr.write_controlled_one_qbit_gate(1, trols1, OneQubitGate.sigx)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write controlled sigy ( $\\sigma_Y(1)^{n(0)}$ )" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "wr.write_NOTA('controlled sigy ( sigy(1)^n(0) )')\n", "wr.write_controlled_one_qbit_gate(1, trols1, OneQubitGate.sigy)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let $R = e^{i (\\theta_X \\sigma_X + \\theta_Y \\sigma_Y + \\theta_Z \\sigma_Z )}$.\n", "\n", "Write controlled Y,Z rotation ( $R(1)^{n(0)}$ with $\\theta_X = 0$ )" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "# let exp(i*(radx*sigx + rady*sigy + radz*sigz)\n", "wr.write_NOTA('controlled Y,Z rotation ( rot(1)^n(0) with radx = 0 )')\n", "wr.write_controlled_one_qbit_gate(1, trols1, OneQubitGate.rot,\n", " [0.0, np.pi/3, np.pi/4])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write controlled rotation ( $R(1)^{n(0)}$ )" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "wr.write_NOTA('controlled rotation ( rot(1)^n(0) )')\n", "wr.write_controlled_one_qbit_gate(1, trols1, OneQubitGate.rot,\n", " [np.pi/5, np.pi/3, np.pi/4])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write 2-controlled not ( $\\sigma_X(2)^{\\overline{n}(1)n(0)}$ )" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "wr.write_NOTA('2-controlled not ( sigx(2)^(nbar(1)n(0)) )')\n", "wr.write_controlled_one_qbit_gate(2, trols2, OneQubitGate.sigx)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write swap of 0 and 1 ( $E(0, 1)$ )" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "wr.write_NOTA('swap of 0 and 1')\n", "wr.write_qbit_swap(0, 1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write swap of 1 and 2 controlled by 0 ( $E(1, 2)^{n(0)}$ )" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "wr.write_NOTA('swap of 1 and 2 controlled by 0')\n", "wr.write_controlled_qbit_swap(1, 2, trols1)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "wr.close_files()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Look at\n", "\n", "* ../io_folder/expansions_examples_3_eng.txt\n", "* ../io_folder/expansions_examples_3_ZLpic.txt\n", "\n", "to see the quantum circuit that was generated.\n", "Let's print the Picture file" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
1
NOTA\tsimple cnot ( sigx(1)^n(0) )
2
|   X---@
3
NOTA\tcontrolled sigy ( sigy(1)^n(0) )
4
|   Y---@
5
NOTA\tcontrolled Y,Z rotation ( rot(1)^n(0) with radx = 0 )
6
|   R---@
7
NOTA\tcontrolled rotation ( rot(1)^n(0) )
8
|   R---@
9
NOTA\t2-controlled not ( sigx(2)^(nbar(1)n(0)) )
10
X---O---@
11
NOTA\tswap of 0 and 1
12
|   <--->
13
NOTA\tswap of 1 and 2 controlled by 0
14
<--->---@
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "wr.print_pic_file(jup=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "One can create new English and Picture files\n", "from those we just created simply\n", "by creating an object of the CGateExpander class.\n", "The new files contain an expansion of every line of the old files." ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "ex = CGateExpander(file_prefix, num_qbits)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Look at\n", "\n", "* ../io_folder/expansions_examples_X1_3_eng.txt\n", "* ../io_folder/expansions_examples_X1_3_ZLpic.txt\n", "\n", "to see the new expanded quantum circuit that was generated. (Note that\n", "the new files have the same names as the old ones except that an \"_X1\" has been \n", "added to the names of the old files).\n", "Let's print the new Picture file" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
1
NOTA\tsimple cnot ( sigx(1)^n(0) )
2
|   X---@
3
NOTA\tcontrolled sigy ( sigy(1)^n(0) )
4
|   R   |
5
|   X---@
6
|   R   |
7
|   |   Rz
8
NOTA\tcontrolled Y,Z rotation ( rot(1)^n(0) with radx = 0 )
9
|   X---@
10
|   R   |
11
|   X---@
12
|   R   |
13
NOTA\tcontrolled rotation ( rot(1)^n(0) )
14
|   R   |
15
|   X---@
16
|   R   |
17
|   X---@
18
|   R   |
19
NOTA\t2-controlled not ( sigx(2)^(nbar(1)n(0)) )
20
|   X   |
21
R   |   |
22
X---+---@
23
R   |   |
24
X---+---@
25
R   |   |
26
|   |   Rz
27
|   @---X
28
R   |   |
29
X---+---@
30
R   |   |
31
X---+---@
32
R   |   |
33
|   |   Rz
34
|   @---X
35
R   |   |
36
X---@   |
37
R   |   |
38
X---@   |
39
R   |   |
40
|   Rz  |
41
|   X   |
42
NOTA\tswap of 0 and 1
43
|   X---@
44
|   @---X
45
|   X---@
46
NOTA\tswap of 1 and 2 controlled by 0
47
R   |   |
48
X---+---@
49
R   |   |
50
X---+---@
51
R   |   |
52
|   |   Rz
53
|   @---X
54
R   |   |
55
X---+---@
56
R   |   |
57
X---+---@
58
R   |   |
59
|   |   Rz
60
|   @---X
61
R   |   |
62
X---@   |
63
R   |   |
64
X---@   |
65
R   |   |
66
|   Rz  |
67
|   R   |
68
|   X---@
69
|   R   |
70
|   X---@
71
|   R   |
72
|   |   Rz
73
@---+---X
74
|   R   |
75
|   X---@
76
|   R   |
77
|   X---@
78
|   R   |
79
|   |   Rz
80
@---+---X
81
|   R   |
82
@---X   |
83
|   R   |
84
@---X   |
85
|   R   |
86
Rz  |   |
87
R   |   |
88
X---+---@
89
R   |   |
90
X---+---@
91
R   |   |
92
|   |   Rz
93
|   @---X
94
R   |   |
95
X---+---@
96
R   |   |
97
X---+---@
98
R   |   |
99
|   |   Rz
100
|   @---X
101
R   |   |
102
X---@   |
103
R   |   |
104
X---@   |
105
R   |   |
106
|   Rz  |
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "ex.wr.print_pic_file(jup=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "One can count the number of CNOTs in the newly created file by simply creating an object of class\n", "SEO_reader and printing the log file that this creates." ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Number of lines in file = 106\n", "Number of Elem. Ops = 99\n", "Number of CNOTS (SIGX with single control) = 41\n", "List of distinct variable numbers encountered (length=0)=\n", "[]\n", "List of distinct function names encountered (length=0)=\n", "[]\n", "\n" ] } ], "source": [ "rdr = SEO_reader(file_prefix + '_X1', num_qbits, write_log=True)\n", "rdr.print_log_file()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If a quantum circuit contains only qubit rotations and\n", "CNOTs, then its number of CNOTs is a measure of the time \n", "complexity of the circuit. Some would say it's even\n", "a measure of TIME. That's because single qubit rotations\n", "are simple in the sense that they act on a single qubit,\n", "whereas CNOTs are much less trivial because they\n", "represent two body interactions." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.10.9" }, "toc": { "base_numbering": 1, "nav_menu": { "height": "30px", "width": "252px" }, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": "block", "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 4 }