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

2024年1月7日日曜日

Yet another mobile quantum circuit simulator

In the previous article, I described the development of a quantum circuit simulator that allocates one qubit to one mobile phone. I also placed emphasis on graphical interfaces, including display on the Bloch sphere. This time, I have developed a new quantum circuit simulator that can run applications using three-qubit on a single mobile phone.

Simulate quantum circuits with a mobile phone that fits in the palm of your hand!
Without internet connection!
It's as fun as using a scientific calculator!

As shown in the figure below, I used this to implement Grover's algorithm. The same results were obtained with the IBM Quantum Computer, which had already been carried out.




Acknowledgment
In developing this quantum circuit simulator, the explanations and examples in the following reference [1] were very helpful to me.

References
[1]中山茂、"量子アプリQni量子プログラミング初歩",Gaia教育シリーズ52, 2022.

2023年10月24日火曜日

Oracle and Amplitude Amplifier in Grover's Quantum Algorithm

[Summary] The Grover search algorithm, which is one of the famous quantum algorithms, has already been discussed in the articles below, so I will not explain its meaning or solution in detail here.
In this article, we will review flipping the sign of probability amplitude and inversion about the mean value. Also includes execution results on the latest IBM quantum computer (127 qubits). The following discussion is based on knowledge gained from reference [1].

●Grover's algorithm quantum circuit
Here we consider the case of 2 qubits (number of qubits n = 2). In addition, one more qubit is used for control. The quantum circuit is shown in Fig.1. For all qubits, we apply a Hadamard gate to create a uniform superposition state as an initial state. Next, configure Oracle F for the target function f. The bit string xy with f(xy)=1 is what is searched. Now, the target is set to xy=10. The function of this Oracle F is to flip the sign of the probability amplitude of the basis |10> corresponding to this target. The probability amplitude of other basis is not changed.

The next gate A inverts the four basis probability amplitudes of the top 2 qubits system about the mean of their values. As a result, only the probability amplitude whose sign was flipped in the preceding Oracle is amplified, and the others are attenuated. By repeating Gate F and Gate A as a set, a specific basis can be measured with a high probability (approximately close to 1.0). That's what you're looking for. This repetition needs to be performed only once when n=2.


●Amplitude Amplification Matrix
Now, if we multiply matrix A shown in the upper right corner of Fig. 1 (diagonal elements are -1 and other elements are 1) by the ket vector whose elements are probability amplitudes', the probability amplitudes will certainly be amplified. We can understand that. This is detailed in Chapter 9 of Reference [1]. But how did we derive this matrix? This is explained using Fig.2. Although we will deal with the case of n=2 here, it will be easier to understand if we consider the general case of n. In short, it takes advantage of the fact that the result of inverting the value p of an element around the average value m of all elements is 2m-p.

●Execution on the IBM Quantum 127 qubits real machine
After making the above preparations, execute the quantum circuit shown in Fig.3. The Oracle F circuit was configured for f(10)=1. Also, the amplitude amplification section (purple Amp gate) that inverts about the average value can be configured with Hadamard gates, X gates, and controlled X gates, but it is also possible to just give the unitary matrix A shown in Fig.2. The latter seems easier to think about.
The execution results are shown in Fig.4. These are the execution results of (a) ibm_lagos with a 7-qubits Falcon processor, (b) ibm_brisbane with a 127-qubits Eagle processor, and (c) ibmq_qsam simulator. First, by the simulator (c), the basis of the target was measured with a probability of 1.0, as in theory.

On the other hand, the error rate of the machine (a)lagos was relatively high, and the target basis was measured with a probability of 0.75, and other basis were also measured slightly. However, with the latest model (b) brisbane, the target basis probability has improved to 0.87. Enough information was obtained to find the desired solution.
●Impressions
Until last year, IBM provided general users with real machines with 5-qubits and 7-qubits for free, but recently they have also made the latest machine with 127-qubits available for free. In fact, in April 2023, there was news [2] that "The University of Tokyo will be the first university outside of North America to install an IBM Eagle 127 quantum bits computer.'' Only half a year later, anyone could use this (presumably) same quantum computer over the web, as mentioned above. Amazing progress in quantum computers!

In principle, 127 qubits have the potential to simultaneously process a huge number of states: 2 to the 127th power (= 10 to the 38th power). Such a machine can now be used from home via the web, so although there are still many challenges to overcome, the dream is growing.

●Words from Prof. Chris Bernhardt
==============================
Dear Fujio,
Another great post! Your site contains really useful information. 
I especially like the fact that you are showing how to do things on the IBM quantum computer.
Chris
==============================
Dear Chris
Thank you very much for reading my blog article.
My introductory quantum computing journey has come to an end.
I was able to achieve this by coming across your wonderful book and fully understanding its contents.
Based on this, I can move on to even higher levels of quantum computing.
Fujio
==============================

References
[1] Chris Bernhardt: Quantum Computing for Everyone, The MIT Press, 2020.
[2] News about the introduction of IBM's 127 qubit computer to the University of Tokyo

2023年10月23日月曜日

Groverの量子アルゴリズムにおけるOracleとAmplitude Amplifier

【要旨】著名な量子アルゴリズムの一つであるGrover search algorithmについては、すでに以下の記事で述べていますので、ここではその意味や解法の詳細説明は略します。
今回の記事では、確率振幅の反転と、平均値を中心とした折り返しに関して復習します。最新のIBM量子コンピュータ(127量子ビット)による実行結果も含みます。以下の考察は、参考文献[1]から得た知識に基づいています。

●Groverのアルゴリズムの量子回路
 ここでは2qubits(量子ビット数n=2)の場合を考える。そのほかに、制御用の1qubitを使う。その量子回路をFig.1に示した。全てのqubitsについて、初期状態として均等な重ね合わせ状態を作るため、Hadamardゲートを適用する。次に、対象とする関数fに対してOracle Fを構成する。f(xy)=1となるビット列xyが探索するものである。今はそのターゲットを、xy=10と設定した。このOracleFの機能は、このターゲットに対応するbasis |10>の確率振幅の符号を反転させること(flipping the sign)である。それ以外のbasisの確率振幅は変更しない。

 次のゲートAは、上位の2qubitsシステムの4つのbasisの確率振幅を、それらの値の平均値を中心に反転(inversion about the mean)させる。これによって、前段のOracleで符号反転された確率振幅だけが増幅され、他は減衰することになる。ゲートFとゲートAをセットで繰り返すことによって、(ほぼ1.0に近い)高い確率で特定のbasisが測定されるようになる。それが求めるものである。この繰り返しは、n=2の場合は1回で済む。
●Amplitude Amplification Matrix
 さて、Fig.1の右上に示した行列A(対角要素が-1で他の要素は1)を、「確率振幅を要素とするケットベクトル」に掛ければ、確かに上記の通り、確率振幅が増幅されることが分かる。そのことは、参考文献[1]の第9章に詳しく書かれている。だが、どのようにこの行列を導出したのであろうか?それをFig.2で説明する。ここではn=2の場合を扱うのだが、一般のnに対して考察した方が分かり易い。要は、要素の値aを要素全体の平均値mの周りに反転させた結果は、2m-aとなることを利用しているのである。
●Execution on the IBM Quantum 127 qubits real machine
 以上の準備をしてFig.3に示した量子回路を実行する。Oracle Fの回路は、f(10)=1に対して構成した。また、平均値で折り返す振幅増幅部(紫色のAmpというゲート)は、Hadamardゲート、Xゲート、制御付きXゲートで構成することもできるが、Fig.2で示したユニタリ行列Aをそのまま与えることもできる。後者の方が、考え易いように思われる。
 その実行結果をFig.4に示す。(a)7-qubits Falconプロセッサのibm_lagos、(b)127-qubits Eagleプロセッサのibm_brisbane、(c)ibmq_qsam simulatorの実行結果である。まず、(c)のシミュレータでは、ターゲットのbasisが理論通り、確率1.0で測定された。
 一方、実機(a)lagosのエラー発生率は比較的高く、ターゲットのbasisは確率0.75で測定され、他のbasisも若干測定された。だが、最新機(b)brisbaneでは、ターゲットのbasisの確率は0.87まで向上した。目的の解を求めるには十分な情報が得られた。
●感想
 昨年までは、IBMは一般ユーザに5-qubitsおよび7-qubitsのリアルマシンを無償提供していたが、最近、実に127-qubitsという最新マシンも、無料で使えるようにした。実は、2023年4月に、「北米以外で初めて、東大にIBM Eagle 127量子ビットコンピュータを導入」のニュース[2]があった。それから僅か半年で、これと(恐らく)同じ量子コンピュータを、上記の通り、誰でもwebから使えるようになったのである。驚くべき、量子コンピュータの進歩である!
 127量子ビットは、原理的には、2の127乗(=10の38乗)個の状態を同時に処理できる可能性を秘めている。アルゴリズムの発案次第である。そんなマシンを、今や、自宅からweb経由で利用できるのであるから、まだまだ課題は山積ではあるが、夢は膨らむ。

●Prof. Chris Bernhardtからのコメント
(英語版記事はこちら)
==============================
Dear Fujio,
Another great post! Your site contains really useful information. 
I especially like the fact that you are showing how to do things on the IBM quantum computer.
Chris
==============================
Dear Chris
Thank you very much for reading my blog article.
My introductory quantum computing journey has come to an end.
I was able to achieve this by coming across your wonderful book and fully understanding its contents.
Based on this, I can move on to even higher levels of quantum computing.
Fujio
==============================

References
 [1] Chris Bernhardt: Quantum Computing for Everyone, The MIT Press, 2020.

[2] 東大にIBMの127量子ビットコンピュータ導入のニュース


2022年12月21日水曜日

Groverの探索(量子アルゴリズム)でコンビニ出店案

【要旨】Groverの探索は、典型的な量子アルゴリズムの一つである。これは、可能性のある全ての探索問合せを量子重ね合わせの原理で一度に行い、量子状態の確率振幅を波の干渉のように増幅/減衰させる。この繰り返しによって、解を浮き上がらせ、確定させる。そのような驚異のアルゴリズムである。その概要は、すでに本ブログ記事[1]でも述べた。ここでは、IBM Quantum Challenge 2019[2]で出題された課題「コンビニ出店案作成」に対するGrover探索による解答例を追試行し、その威力と魅力を確認した。

[量子アニーリングによる結果も、末尾に追加した。]

●Grover探索アルゴリズムの概要
 すでに、別の記事[1]で述べたので、ここでは極く簡単に復習する。簡単な例として、00、01、10、11を入力とする関数fがあり、fはある一つの入力に対してのみ1であり、その他の入力に対しては0を出力する。関数値f(xx)=1となる入力xxを求める問題である。古典的探索では、最悪の場合、関数fの評価を3回行う必要があるが、Groverによれば、それは1回で済む。図1はこのアルゴリズムを実現する量子回路とその説明である。
 まず、Oracleと呼ばれる量子回路を作る。それは、4個の全ての入力を纏めた量子重ね合わせを受け取り、f(xx)=1となる|xx⟩の確率振幅の符号を反転させる。その際、 解xxに応じたOracleを構成するのである。だが、これは解をすでに与えているわけではなく、課題そのものの記述に相当する。Oracleの中で、関数fは1回評価される。
 次に、Grover反復と呼ばれる確率振幅の増幅/減衰により、解を他から分離することができるのである。すなわち、解を与える入力のみが多数回測定されるように作られている。そこが核心部分である。この例では反復1回で明確に解が得られた。一般には、入力サイズNに対して、√N回のオーダーの反復で解が得られることが知られている。
●課題:コンビニ出店案の作成
 少し現実的な「コンビニ出店案の作成」にGrover探索を適用する。これは、IBMのQuantum Challenge 2019での出題[2]を若干変更したものである。図2にその仕様を示した。合計11地区に4社のいずれかのコンビニを一つだけ出店する案を全て列挙せよという問題である。
 これはグラフの彩色問題の一つであり、各辺の両端のノードの色が同色になることを禁止することに相当する。すでにA社、B社、C社、D社が図1のようにそれぞれ1店を出店済みなので、残り7地区にどのように出店させるかである。
 これを解くには、全ての条件を満たす場合に最小になるようなエネルギー関数を設定した量子アニーリング法が適しているかも知れない。また、数理計画による解法もあるだろう。しかし、ここでは、Grover探索を用いて、量子アルゴリズムの威力、魅力を確認したい。

●「コンビニ出店案」をGrover探索で解く
 この解法は、最初に挙げた00、01、10、11を入力とする探索と基本的に同じ仕組みである。だが、解がいくつ存在するかわからないので、それに対応するOracleの作成に工夫が必要である。具体的な実装方法とプログラムは参考資料[2]を利用させていただいた。解の候補として考えるべき、7量子による入力は16,384(4の7乗)とおりである。Oracleが構成した後は、上に述べたGrover反復を行えばよい。図3に、Qiskitを用いた量子シミュレーション結果を示した。
 この量子シミュレーションでは同じことを500回(500 shots)行ない、その測定値の統計を示してある。図3(a)は、Grover反復1回の状態である。ほとんどの入力は1回だけ測定され、いくつかの入力に対しては2回〜7回程度測定された。さらに反復を続けると、解を与える入力の測定回数が順次増加し、解でないものの出現が減少する。
 Grover反復を7回実行した結果が図4(b)である。出現頻度が40〜50と際立つものが9個観測された。それが解だと判定される。実に見事な解法ではないか!
(なお、(a)よりも(b)での縦棒の幅が太くなっているのは、出現回数0回の入力が激増したため、図からそれらが取り除かれたためである。)
●得られた解を確認する
 以上の結果を図4の通り確認した。(解が9個しかないことは証明されていないが)恐らくこれが全ての解であろう。確かに、どの解においても、辺の両端のノードは異なる彩色となっており、全ての条件を満たしている。

●感想
 驚嘆すべき量子アルゴリズムの一つであるGrover探索を、ある程度現実的な問題にも適用できることが分かった。IBM Quantum Challengeでは、現実的な課題を順次増やして量子コンピューティングコミュニティを活性化させようとしている。いずれ、そこで出題される課題にも挑戦してみたい。また、冒頭に述べたように、この課題は、量子アニーリングの利用にも適していると思われるので、そちらも試す価値はあるだろう。

[追加]2022-12-22
 その後、量子アニーリングによる解法もやってみた。参考資料[3]のデモアプリ(グラフ彩色)を参考にした。縦軸にノード番号、横軸に色番号のマス目を作り、そこに0(該当せず)か1(該当する)が入るようにする。初期設定済み(出店済み)に対応するマス目は1とする。各行についてone-hot制約を、各色について隣接ノード同士の値の積が0となるように制約を設定する。それに対して量子アニーリングを実行すれば良い。その結果、上記と同一の9個の解が得られたので、以下に掲載する。
参考資料
[1] 初級量子コンピューティングの旅の目的地Grover:

[2] IBM Quantum Challenge 2019: