ラベル quantum Fourier transform の投稿を表示しています。 すべての投稿を表示
ラベル quantum Fourier transform の投稿を表示しています。 すべての投稿を表示

2024年10月16日水曜日

Testing my mobile quantum circuit simulator

[Abstract] This article tests the applicability of the mobile quantum circuit simulator Qsim_multi that I have developed so far with MIT App Inventor. As examples, I have taken the inverse Quantum Fourier transform and the order finding problems that appear in a recently published book. These form the basis of Shor's prime factorization algorithm. In conclusion, I have confirmed that my simulator Qsim_multi can handle these problems successfully.

🔴 A new book on quantum computing
Recently, I bought a new book (FIg.1) by Nivio Dos Santos. The title is "How to code for Quantum Computers", but it is not a so-called how-to book, but a compact summary of basic concepts. As soon as you open the book, you get the feeling that something fun is written in it. In the second half, the quantum Fourier transform, quantum phase estimation, and Shor's algorithm calculations are explained in detail. Therefore, I would say that this is a book for beginners to intermediate level.
Although the main text does not include any program code, the Python code (Jupyter Notebook) for the Google Cirq environment is publicly available on the Web, which is useful.

I ran the following two examples from the book on my mobile quantum simulator. However, I noticed something important here. That is the order of the quantum bits. My simulator is the same as IBM Qiskit, but in Cirq, the order is reversed. Therefore, it was necessary to reverse it. However, even if I did that, for example, the results of the quantum Fourier transform would have different phases even if the probabilities of each basis were the same. If you keep that in mind, there is no problem.

🔴 Example1: Using Inverse-QFT
First, here is an example using the inverse quantum Fourier transform. Fig.2 shows the circuit diagram and quantum state consisting of 16 bases explained in this book. The inverse quantum Fourier transform invQFT is shown in its expanded form. The small filled circles in each circle indicate the probability, and the slope of the red line indicates the phase.
The result of converting this to my simulator and running it is shown in Fig. 3. This simulator has a built-in inverse quantum Fourier transform (IQFT), so I used it as is. Comparing the quantum state with Fig. 2, the probability is the same, and the phase is a mirror image. This result shows that my simulator is operating normally.

🔴 Example2: Finding the order of [gk mod N = 1]
Next, we will look at the order-finding problem, which is important in Shor's algorithm. Here, we find the order of the specific example "7k mod 15 = 1", that is, the smallest integer k that satisfies this equation.

The quantum circuit and the order discovery results are shown below. Fig. 4 is from the Cirq environment, and Fig. 5 is from my simulator. The measurement results after applying "7k mod 15 " and invQFT were consistent for both in a 3-qubit system. That is, out of the eight possibilities, only 010, 100, 000, and 110 were measured with equal probability. By approximating the result to a rational number, we obtained 1/4, and found that the order was 4. The details of why we can say this and its relationship to Shor's algorithm are shown below:

 2024年7月9日火曜日
 Shor's Algorithm:量子コンピューティングの学びの最高峰
In Fig. 4, the measurement results are shown as the frequency of occurrence in 1000 trials, and in Fog.5 they are shown as probability calculation results.

In addition, in my simulator in Fig. 5, the measured values ​​for the four cases (3-qubit) are divided into four. This is a little difficult to see, so it would be better to summarize it as shown in Fig. 4. I would like to address this in the next version update.

🔴Conclusions
For simple cases of the inverse quantum Fourier transform and the order finding problem, my mobile quantum circuit simulator gave identical results to those run in the Google Cirq environment, confirming the applicability of the simulator to a certain extent.

-------------------------------------------------------------
Thank you Nivio for your great response!
-------------------------------------------------------------

自作モバイル量子回路シミュレータのテスト

【要旨】本稿は、これまでに自作したモバイル量子回路シミュレータQsim_multiの適用性をテストするものである。例題として、最近発刊されたある書籍に載っている、逆量子フーリエ変換位数発見問題を取り上げた。これらは、Shorの素因数分解アルゴリズムの根幹を成している。結論として、自作シミュレータQsim_multiがこれらを正常に処理できることを確認できた。

🔴 A new book on quantum computing
 最近、Nivio Dos Santos著の新刊(FIg.1)を購入した。タイトルは、"How to code for Quantum Computers"となっているが、いわゆるハウツー本ではなく、基本概念がコンパクトにまとめられている。本を開いたとたんに、何か楽しいことが書いてある、という雰囲気がある。後半には、量子フーリエ変換、量子位相推定、Shorのアルゴリズムの計算が詳細に説明されている。だから、初級から中級レベルの本だと言える。
 本文には、プログラムコードはほとんど出てこないが、Google Cirq環境用のPythonコード(Jupyter Notebook)がWeb上に公開されているので重宝する。

 この本に掲載されている以下の二つの例題を、私の自作モバイル量子シミュレータで稼働させるのである。だが、ここで重要なことに気づいた。それは、量子ビットの並べ順である。私のシミュレータは、IBM Qiskitなどと同じだが、Cirqでは、それが逆順になっているのである。したがって、それを逆転させる必要があった。しかし、そうしても、例えば、量子フーリエ変換の結果などは、各基底の確率は同一になっても、位相は異なるだろう。それを踏まえて扱えば問題ない。

🔴 Example1: Using Inverse-QFT
 まず、逆量子フーリエ変換を使う例である。Fig.2はこの書籍で説明されている回路図と16個の基底からなる量子状態である。逆量子フーリエ変換invQFTは展開形で示されている。それぞれの円内の塗り潰した小さな円は確率を示し、赤い直線の傾きは位相を示す。
 これを私のシミュレータ用に変換して実行した結果をFig.3に示す。このシミュレータには、逆量子フーリエ変換IQFTを内蔵しているので、それをそのまま使った。量子状態をFig.2と比較すると、確率は同じであり、位相は鏡像反転している。この結果から、私のシミュレータは、正常に稼働していることが分かる。

🔴 Example2: Finding the order of [gk mod N = 1]
 次に、Shorのアルゴリズムで重要な位数発見問題である。ここでは、具体例「7k mod 15 = 1」の位数、すなわち、この式を満たす最小の整数kを求める。
 量子回路と位数発見結果を以下に示す。Fig.4はCirq環境であり、Fig.5は私のシミュレータによる。「7k mod 15 」とinvQFTの適用後の測定結果は、3-qubitシステムにおいて、両者で一致した。すなわち、8個の可能性のうち、010、100、000、110の四つだけが、それぞれ等しい確率で測定される。その結果を有理数近似して1/4が得られ、位数が4であることがわかった。なぜそう言えるのかの詳細と、Shorのアルゴリズムとの関係は以下に示されている:
 2024年7月9日火曜日
 Shor's Algorithm:量子コンピューティングの学びの最高峰
 Fig.4では、測定結果は、1000回試行おける出現頻度で示してあり、Fog.5では確率計算結果として示している。
 なお、Fig.5の私のシミュレータでは、測定した4ケース(3-qubit)の値が、それぞれ四つに分かれている。これは少し見にくいので、Fig4.のようにまとめた方が良い。次回のバージョンアップの際に対処したい。

🔴結論
 逆量子フーリエ変換と位数発見問題の簡単な場合について、私のモバイル量子回路シミュレータは、Google Cirq環境で実行されたのと同一の結果を与えた。一定の適用性を確認できたと考える。

2024年2月21日水曜日

Illustrating Shor's algorithm with my quantum circuit simulator

Shor's algorithm is mathematically quite difficult, so many books on quantum computing often only briefly mention it. On the other hand, when an explanation is given, it is difficult to understand because it is a list of many mathematical formulas. In this context, the book by Prof. Barry Burd [1] is surprisingly easy to understand and explains the basics. Chapter 9 of this book takes 40 pages to thoroughly explain the essence of Shor's algorithm. Using a concrete example, he explains that if you can find the period (frequency) in a coprime powers sequence, you can factorize the public key number. He then demonstrated quantum Fourier transform (QFT) to find that frequency, expressed it in Qiskit code, and ran it on IBM Quantum Lab. This is fantastic! With this, I was able to grasp the heart of Shor's algorithm!

I have so far developed my own quantum circuit simulator for 3-qubit as a mobile phone app with MIT App Inventor. The outline is shown in Fig.1.
This time, I was able to use my simulator to identify frequencies using quantum Fourier transform, following the instructions in this book. The results matched those from IBM Quantum Lab. In other words, using just a mobile phone, they were able to perform a quantum Fourier transform and obtain the results, just like a scientific calculator. The situation is shown in Fig.2 and Fig.3.
(Notes)
As of 02/23/2024, the execution environment of Qiskit (IBM Quantum Lab) has changed, so it is necessary to modify the original Python code (for example, Chapter09.ipynb) to run it.
Here is how to fix it:
---------------------------------------------
(A)change libraries:
from qiskit import QuantumCircuit, Aer, execute
from qiskit.tools.visualization import plot_histogram
  ↓
from qiskit.primitives import Sampler
from qiskit.visualization import plot_histogram
---------------------------------------------
(B)use 'Sampler' instead of 'Aer' as follows:
sampler = Sampler()
result = sampler.run(circ, shots=1000).result()
print("result: ", result)
quasi_dists = result.quasi_dists
print("quasi_dists: ", quasi_dists) 
display(plot_histogram(quasi_dists))
---------------------------------------------
More details here:
Migration examples
---------------------------------------------

Reference
[1] Barry Burd, Quantum Computing Algorithms -Discover how a little math goes a long way, Packt Publishing, 2023.

2024年1月23日火曜日

Development of scientific calculator-like quantum circuit simulator V2

(This is an English translation of a previous Japanese article)

I have been developing a quantum circuit simulator that is as easy to use as a scientific calculator. As a single smartphone app, you can try out various quantum algorithms and apps within the 3-qubit range. This time, version 2 has been completed. I also added a little bit of play.

If you shake your smartphone lightly, one of five icons will randomly appear. It's a little fun. Of the five images, one was created by me, while the other four were created by ChatGPT.
Of course, this is not the only thing. Added a playback function that applies quantum gates. That is, by recording the steps of an algorithm (which quantum gates were applied to which registers (qubits)) in sequence, and pressing one or two buttons, the algorithm can be played back. I found this to be very useful when showing a demo to someone or when running under different conditions myself. Although details are omitted, three algorithm playback examples recorded in this manner are shown below.

Superdense Coding
In Fig.1, Alice has the top two qubits (q0 and q1), and Bob has q2. Both qubits are in a state of quantum entanglement. Just like that, Bob went away. Alice wants to send any of the classical 3-bit information (000, 001, 010, ..., 111) to Bob. In other words, she wants to send "101" out of eight, for example, in (a). However, there are only two qubits that can be sent. This is not enough!
Nevertheless, with this wonderful Superdense Coding, Bob can always obtain any information from Alice simply by operating a predetermined quantum gate! Fig.1(b) shows this.
Grover's Algorithm
For example, assume that eight pieces of information (000, 001, ..., 111) are arranged randomly. The problem is finding specific information from among them. Fig.2 is an example of searching for "101". First, mark the information in (a). This is achieved by inverting the phase of the corresponding basis vector |101>. This is not a scam! It simply tells you what to look for. Without such information, the search itself is meaningless.
In (b), when the button Amp3step is pressed, the probability amplitude amplification is activated, and only the probability amplitude of the basis vector whose phase was inverted in (a) is amplified, and the desired information "101" is obtained. In this example, we only needed to perform the phase inversion and amplification pair once!
Quantum Fourier Transform
This is an application example of quantum Fourier transform QFT. One of the characteristics of QFT is that it can be applied to states of quantum superposition. In Fig.3(a), we created a superposition of two certain quantum states. Next, in (b), when the button QFT is pressed, the phase waves that are the results of each QFT in both states interfere and appear as separate phase waves. The wave swell can be seen from the phase angle and amplitude (area of the disk filled in red) in the lower part of (b).
Although omitted here, you can restore the original quantum superposition state by pressing the button next to it, IQFT. IQFT is the inverse quantum Fourier transform.

2024年1月21日日曜日

関数電卓型の量子回路シミュレータV2

 関数電卓のような使い勝手の量子回路シミュレータを目指し、独自に開発を進めてきた。1台のスマホのアプリとして、3-qubitの範囲で、種々の量子アルゴリズムやアプリをこれで試すことができる。今回、バージョン2を完成させた。遊びもちょっと入れてみた。
 スマホを軽く振れば、4つのアイコンのいずれかがランダムに現れる。ちょっと楽しい。4つのイメージのうち、1つは自作したが、他の3つはChatGPT氏によるものである。

 もちろん、これだけではない。目玉は、量子ゲート適用の再生機能を追加したことだ。すなわち、あるアルゴリズムの手順(どのレジスタ(量子ビット)にどの量子ゲート適用したか)を順に記録して、ボタン1つか2つを押すと、そのアルゴリズムが再生できる。これは、人にデモを見せたり、自分でも条件を変えて実行する場合に非常に便利であることが分かった。詳細は略すが、そのようにして記録した3つのアルゴリズム再生例を以下に示した。

Superdense Coding(超高密度符号化)
 Fig.1において、Aliceは上段の2つのqubit(q0とq1)を持ち、Bobは下段のq2を保有する。両者のqubitは量子もつれの状態にしてある。その後、Bobは遠方へ行ってしまった。もつれは保持されたままである。Aliceは古典3ビット情報(000, 001, 010, ... , 111)のどれかをBobに送りたい。つまり、8個のうちから、例えば、(a)では"101"を送たい。だが、送れるqubitは2つしかない。これでは足りない!
 それにも拘らず、この素晴らしいSuperdense Codingでは、Bobは常に予め決められた量子ゲートを操作するだけで、Aliceからのどの情報でも得られる!(b)はそれを示している。
Grover's Algorithm(グローバーの探索アルゴリズム)
 これは、例えば、8個の情報(000, 001, ... , 111)がランダムに並んでいるとする。その中から特定の情報を探し当てる問題である。Fig.2は、"101"を探す例である。まず、(a)で、その情報に印をつける。それは、対応するbasis vector |101>の位相を反転させることで実現させている。これはイカサマではない!単に、何を探すべきかの情報を与えているだけである。そもそも、そのような情報がなければ、探すこと自体が意味をなさない。
 (b)において、ボタンAmp3stepを押すと、確率振幅増幅が作動して、(a)で位相反転したbasis vectorの確率振幅だけが増幅されて、求める情報"101"が得られた。この例では、位相反転と増幅のペアをただ1回実行するだけで解決した!
Quantum Fourier Transform(量子フーリエ変換)
 これは、量子フーリエ変換QFTの適用例である。QFTの特徴の一つは、量子重ね合わせの状態に適用できることである。Fig.3(a)で、ある2つの量子状態の重ね合わせを作った。次に、(b)で、ボタンQFTを押すと、両方の状態のそれぞれのQFTの結果である位相の波が干渉して、別の位相波として現れる。(b)の下段の位相角度と振幅の大きさ(赤で塗り潰した円盤の面積)から、波のうねりが分かる。
 なお、ここでは省略したが、隣のボタンIQFTを押すと、元の量子重ね合わせ状態を復元できる。IQFTは、逆量子フーリエ変換である。

2024年1月13日土曜日

Running quantum Fourier transform on my mobile simulator

I have developed a 3-qubit quantum circuit simulator that runs on a mobile phone. As shown in the top row of Fig. 1, a large number of basic quantum gates can be used. This time, I was able to demonstrate quantum Fourier transform (QFT) and inverse quantum Fourier transform (IQFT) using these quantum gates. QFT and IQFT each consist of six quantum gates, which are registered to buttons.

In Fig1.(a), when the button QFT is pressed for the quantum bit state |101>, a wave with the phase shown at the bottom is generated. Next, in Fig2.(b), the original qubit state could be restored by pressing the button IQFT. This means that we were able to determine the periodicity of the waves, as shown in Fig.2.

As mentioned above, QFT and IQFT were constructed by combining individual quantum gates, but alternatively, a unitary matrix can be used as shown in Fig. 3. I won't go into details, but these unitary matrices are actually built into my simulator. Note that when using this method, it was necessary to perform swap processing according to the simulator's policy. Swap means reversing the direction in which three qubits are arranged.