2023年12月1日金曜日

Developing the most basic app to understand Qubit state (3)

Sometimes we need to go back to basics!
In the third article in this series, I'll compare my app's results with that of IBM Quantum Composer to confirm that it's working correctly. In my app I can use quantum gates Z, X, Y, T, H.  The T is a quantum gate that adds the phase of e^iφ to the qubit |1>. By default, φ=π/4. Also, H is a Hadamard gate. Here, as an example, H, T, and H are successively applied in this order to the quantum bit initial state |0>.

Fig.1 shows (a) the execution results of my app and (b) the execution results of IBM Quantum. In (a), the probability (area of the colored disk) and phase (the angle of the straight line coming out from the center of the circle) are shown for |0> and |1>. On the other hand, in (b), the results are displayed only for |1>. Although the way the disks are displayed is slightly different, it can be seen that the probabilities and phases of both are the same. However, the values of probability amplitude (amplitude in (a) and Output state in (b)) seem to be different. This will be explained in Fig.2.
Fig.2 explains that although the expressions of the probability amplitudes of the two are different, they are the same quantum state. In my app, the phase with respect to |0> is zero, and the phase of |1> is the relative phase to it. This does not seem to be the case with IBM Quantum. In fact, when I input IBM's probability amplitude numbers into my app's function state2relphase, the results I got matched my app's representation of probability amplitudes. This result confirms that my app is working perfectly correctly, at least for this example.

2023年11月27日月曜日

Developing the most basic app to understand Qubit state (2)

 Sometimes we need to go back to basics!

This is a major update to the previous app. The aim was to help students become familiar with the most basic elements of quantum computing using only their smartphones. The example below shows the results of (1) starting from the initial state, (2) applying the Pauli X gate, (3) then applying the Hadamard gate, and (4) applying the Hadamard gate again. 

The probability amplitude, probability, and relative phase of each basis |0>, |1> are shown numerically and on a disk. Additionally, the values of θ and φ can be set using text boxes and sliders, making it easy to understand the position of the quantum bit on the Bloch sphere.




2023年11月25日土曜日

Developing the most basic app to understand Qubit state (1)

Sometimes we need to go back to basics!

As I have already written several times, I was able to almost completely understand the basic concepts and ideas of quantum computing by reading the book [1]. Although this book does not explain the Bloch sphere or phases in detail, I would not have been able to write this article without the knowledge I gained from the book.

I developed an app to understand the state of qubits on the Bloch sphere. As shown in the figure below, set the two angular parameters θ and φ using the sliders for a single qubit on the Bloch sphere. This app uses it to calculate the probability amplitude and probability for each of the two basis (|0> and |1>). Probability and phase are also illustrated in the two disks at the top right. Note that since the global phase can be ignored, the phase of |0> is set to 0, and the phase of |1> is expressed as a relative phase to that.

As you can see, the current app specifies θ and φ manually. In the next version, it will also be possible to apply basic quantum gates (X, Y, Z, H, etc.).

References
[1] Chris Bernhardt: Quantum Computing for Everyone, The MIT Press, 2020.
https://www.chrisbernhardt.info/

2023年11月1日水曜日

Try solving 3-SAT using Qiskit's Quantum Algorithms Module

[Summary] Grover's algorithm circuit and its implementation have already been described in this article. One of the challenges was generating an oracle according to the search target. When the number of qubits was 2 or 3, it was possible to create it through trial and error, but as the number of qubits increases, it becomes difficult. Therefore, Qiskit provides the Quantum Algorithms Module[1]. This includes Grover. It consists of PhaseOracle, which generates oracles for phase inversion, and Amplification, which amplifies the probability amplitude. By using this, it becomes easy to create large-scale Grover circuits. Additionally, since PhaseOracle can generate oracles from logical expressions, it can be easily applied to 3-SAT problems, etc.[2] The implementation status will also be described.
There are various 3-SAT solvers that use classical computers. Although the content of this article cannot reach the performance of those systems, I would like to connect it to thinking about the potential possibilities for extremely large-scale problems. This is true for quantum computing in general.

Generate oracle from logical formula and amplify probability amplitude
As a simple 2-qubits example, let |10> be the state to be searched. Give a logical expression '(a&~b)' to PhaseOracle to generate an Oracle for it. Next, give the generated Oracle to AmplificationProblem, which has a probability amplitude amplification function. The generated Oracle circuit diagram is shown below. The two filled-in circles and their connections in the circuit diagram are the same as CZ (Controlled Pauli-Z).
Furthermore, draw the entire Grover circuit diagram as shown below. Here, the open circle means that the gate of the target bit is activated when the control bit is |0>.
The results of running this circuit with a simulator (ibmq_qasm) are shown below. As expected, '10' was observed with probability 1.0. In the display below, '01' is measured. This is because the order of the bits in the execution result on the actual machine is the reverse order compared to when they were given as input.

Solving 3-SAT problems using Grover's algorithm
Next, as an example, solve the 3-SAT shown below. If you give the logical formula of the problem to PhaseOracle as shown below, the corresponding Oracle will be generated. Furthermore, similar to the example above, you can create a circuit of the Grover algorithm that includes this Oracle. Below is the Oracle schematic and the entire algorithm schematic.

Next, as a backend, set up a quantum simulator or a real quantum computer, generate a Grover object, give the above Grover circuit to its method amplify, and run it. The number of repetitions was 1. In other words, iterations=1. The method for calculating the optimal value for Iterations has been clarified, and in this case (3 qubits and 3 solutions), 1 is optimal.
Finally, we show calculation results using a simulator (ibmq_qsam) and an actual quantum computer (brisbane with 127 qubits). Since a certain amount of errors occur on a real machine, the separation of solutions is a little less clear than in the case of a simulator, but it can be said that three correct solutions have been found. (Note) The bit order of the execution result is in the reverse order of the tensor product.
References
[1] James L. Weaver & Frank J. Harkins, “Qiskit Pocket Guide”, O’reilly, 2022.
[2] Grover's algorithm examples, 
https://github.com/Qiskit/qiskit-tutorials/blob/master/tutorials/algorithms/07_grover_examples.ipynb

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量子ビットコンピュータ導入のニュース


2023年10月6日金曜日

Simonの量子アルゴリズムとHadamard行列のKronecker積

【要旨】量子コンピューティングでの重要な行列(ゲート機能)の一つに、アダマール行列があります。複数の量子ビットシステム(テンソル積)の各量子ビットにアダマール変換を施すための行列が、Kronecker Products of Hadamard Matrices(アダマール行列のクロネッカー積) です。記念碑的解法として有名なSimonの量子アルゴリズムにも有効に使われています。それは、参考文献[1]で丁寧に説明されているのですが、時間が経つと忘れてしまうので、私自身の理解をメモとして残して置きます。

Simon's Algorithm
 まず、ここで解くべき問題については既に本ブログ(2022-12-11の記事)に書いたので、以下に極く簡単に記す:

 長さnのbinary string x(0か1から成る文字列)を入力とし、出力も長さnのbinary stringとなる2対1関数fがあるとする。ここで、ある長さnの秘密のbinary string s があり、y=x or y=x⊕s の時に限りf(x)=f(y) である。ただし、sの全ての文字が0であることは無いとする。記号⊕は、bitwise addition of strings modulo 2を意味する。入力xに対する関数値f(x)を問い合わせることはできるが、関数fの定義は与えられていない。解くべきことは、秘密のsを特定することである。

 そのために、関数fを何回評価する必要があるかを問題とする。古典的アルゴリズムでは、最悪の場合、2のn乗のオーダーの関数呼び出し回数となるが、Simonの量子アルゴリズムではnの多項式オーダーとなる。しかし、この言い方はあまり正確ではない。計算量に関する、BQP(Bounded-error Quantum Polynomial-time)などの議論を、Prof. Bernhardtの書籍[1]のChapter 8 (pp.166-170)等でご覧いただく必要がある。

Simon's Algorithmを実装する量子回路
 Simonの問題は、上記の関数fと秘密のsを知っている人が作成した問題を解く、いわばリバースエンジニアリングである。実用的な問題ではないにも拘らず、これに対するSimonの解法は、量子アルゴリズムの威力を示すものとして広く認められている。そのための量子回路を、n=2とn=3の場合について検討するが、その方法は一般のnについても自然に当てはめることができる。この量子回路の出力であるいくつかのbinary string(長さn)から、秘密のsを特定するための連立一次方程式を作ることができる。なぜかと言うと、出力されたbinary string bと秘密のsとのdot product(後述)が0になるからである。以下に、このようなdot productがなぜ0になるかを中心に、図を使って詳しく述べる。

 Fig.1は、n=2とn=3の場合のSimonのアルゴリズムを実装した量子回路である。上述の通り、関数fに対するOracleが設定されている。すなわち、関数fの定義と秘密のsがこのように決められているのだが、回答する人はそれを知らずにsを特定するのである。


 一般のnに対してのSimonの量子回路は、Fig.2のように描ける。

 さて、Fig.3は、Fig.1でのn=2とn=3の場合の量子回路の実行結果である。2段になっているレジスタの1段目を測定した結果をFig.3に示した。n=2の問題では測定結果としてbinary string 00と10がそれぞれ1/2の確率で得られた。これらのbinary stringと秘密のsとのdot productsは0となる。そうすれば、それらから連立一次方程式を作ってsを特定できのだが、この部分に関しては既に述べたので、ここでは省略する。以降では、なぜ、このdot productsが0になるのかを考察する。
測定結果のbinary stringと秘密のsとのdot productsがなぜ0になるのか
 この質問の回答を、以下で検討する。Fig.4は、Fig.1 case(a)でのψaのフェーズ(2回目のHadamard変換後で測定を行う直前)の4量子ビットシステムの状態ベクトルである。いくつかの計算を経ているが、ここに示されているのは、4qubitsのうちの上段の2qubitsに着目して式を整理した結果である。
 テンソル積の左側が|10>(測定結果10に対応)である項の関数fの値に注目する。秘密sに関するペアのbit string(00と01、および10と11)に対する関数fの値は同一であるが、それらの符号(プラス、マイナス)も同一である。もう一つの測定結果|00>についても同様である。
 一方、測定結果に現れない|01>と|11>に関しては、これらのペアの間数値の符号が互いに反転している。そのため、最終的な計算結果として、|00>と|10>の確率振幅が増大され、|01>と|11>に関しては減衰(キャンセル)されて0となった。
 このような増幅と減衰が、「測定結果のbit stringと秘密のsとのdot podurct (・)」とどのように関係しているかを、Fig.5で明らかにしている。
 以上で説明を終わるのだが、最後に、Fig.6をご覧いただきたい。ここに、Hadamard行列のKronecker積がある。たとえば、n=2の場合の行列を見ると、その各要素の符号は、Fig.4で示した項の符号と完全に合致している。すなわち、Fig.4でのテンソル積の左側のqubitのペアを行ラベルとし、関数fの引数に与えるqubitの組みを列ラベルと考えれば、Hadamard行列の該当要素の符号が(マイナス1に対してのdot productの冪乗で)直ちに計算できるのである。むしろ、Fig.4でのテンソル積の右側に出現する項の符号は、このHadamard行列から決められるのである。ここに、Kronecker products of Hadamard matricesの重要性がある。
まとめ
 Prof. Bernhardtは [1]の中で、このSimon's algorithmにダブルスター⭐️⭐️(かなり難しい)を付している。確かにその通りだが、何度か読み返して検討して、上記のように理解できた。確率振幅の増幅と減衰に関しては、Groverのアルゴリズムではその操作を明示的に行なってるが、Simonでは「知らぬ間にそうなっている」感がある。ともかく、以下の文言を言えれば、Simonアルゴリズムは一応理解できたことになるのではないか。Fig.7参照。
「Simonの量子回路での全ての測定結果(行ラベル)について、秘密sでペアとなるbinary string(列ラベル)が示すHadamard行列の要素の符号は互いに一致する」
 Simonのアルゴリズムは、有名なShorの因数分解アルゴリズムに影響を与えたことが[1]に具体的に説明されている。その叙述から、Shor's algorithmの"こころ"をつかむことができると思う。ただし、もちろん、その詳細を知るには、量子フーリエ変換などの進んだ数学が必要になるので、改めてじっくり取り組みたいと思う。

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

2023年9月20日水曜日

生成AIに関するMIT App Inventor Workshopが神奈川工科大学で開催

 MIT App Inventorでは、いち早くChatBotImageBotなどの生成AIを組み込んで、生徒、学生、一般人が利用できる環境を提供している。それに関するワークショップが、日本では初めて、神奈川工科大学で5日間(2023/9/6〜12)に渡り開催された。生成AIのうちでも、自然言語からアプリを生成するAptlyは、まだ研究開発中のものであるが、今回(このようなワークショップとしては)初めて、受講生が試用できるよう設定していただいた。
 MIT AppInventorチームのソフトウェア開発者であるDavid Kimさん(in person)とAngie Zhouさん(online)が講師となり、情報、生命、電気、機械など多様な分野の学生が多数、熱心に受講した。最終日には、受講生が各自のアイディアをApp Inventorで実現したプレゼンも行われた。

 最終日の冒頭、MITの著名なComputer Science教授で、App Inventor創始者であるProf. Hal Abelsonから、受講生を激励するメッセージをライブ出演でいただいた。さらに同チームのDr. Evan Patton、Jeff Frilich、Dr. Natalie Laoの3名の専門家からも同様に貴重なメッセージがあった。これは、MIT App Inventorチーム全体として、本Workshopに強い関心を持って対応していただいた証であり、特筆すべきことであった。

 講師のDavidさんのTweetはこちらにあります。また、MIT App Inventor Facebookにも同様の投稿があります。神奈川工科大学の日本語ニュース英語版ニュースにもこのWorkshopの模様が載っています。

 このワークショップの詳細レポート(8ページ)は、ここには掲載していないが、特にご関心のある方はご連絡いただきたく。

MIT App Inventor Generative AI workshop at 神奈川工科大学

2023年9月8日金曜日

量子ビットの反転エラーの訂正(Quantum Bit-Flip Correction)

 量子テレポーテーションを利用した量子ビットのbit-flipエラー訂正については、下記に詳しく述べた。

 その際は、量子ビット反転エラーの検出(どのビットが反転したのかの)量子回路を説明した。だが、訂正の仕方は説明しただけで、その回路は示していなかった。今回は主にそれを説明する。エラー検出とその訂正の原理については上記URLを参照いただきたく、本記事では省略する。量子回路シミュレータQiskitのComposerを使って検討した。

AliceからBobへ転送する量子ビットの状態
 量子テレポーテーションの場合と同じく、物理的に量子ビットを送信するのではなく、その状態を転送するのである。その途中でエラー(ここではbit-flipに限る)が発生した場合、それを検出し、かつそれを自動的に訂正したい。

 まず、図1にAliceから転送したい量子ビット状態を示す。ここでは一例として、(a)のように、|0>にRYとRZゲートを適用した。エラーの検出は、古典ビットの世界で使われるパリティチェックに基づく。そのために元の量子ビットの複製を2つ作りたいのだが、それは量子複製不可定理により、できない。そこで、(b)に示すように、ancillaビットを2つ用意して、entangledな状態を作る。それをBobへ送り出す。もしも、途中でbit-flipエラーが発生しなければ、Bobは、0.75|000> + 0.25|111>という重ね合わせ状態を、そのまま受け取るはずである。

bit-flipエラーの訂正(ancillaビットの測定による)
 エラーの検出方法は、上記の如くすでに述べたので略す。発生したエラーを訂正する回路を図2の赤枠に示す。ここでは、上に述べた2つのancillaビットの測定を行い、その結果に基づいて、該当する量子ビットをパウリXゲートで反転させ、元通りしている。
 この例では、2番目のancillaビットq[2]がbit-flipを起こしたとした。それが正しく訂正されていることは、図1に示した元の3量子ビットシステムの状態が、図2の実行結果として、そのまま保たれていることから分かる。他のq[0]とq[1]に発生するエラーも同様に訂正される。

bit-flipエラーの訂正(Toffoliゲートの利用)
 上記のように、明示的にancillaビットを測定せずに、エラー訂正することもできる。図3に示すように、CNOTとCCNOTを組み合わせている。CCNOTはToffoliゲートとも呼ばれる。Toffoliは2つの制御ビットが共に|1>の時だけNOTが働く。どんな時に使うのだろうかと思っていたが、今回がその好例となったので記録しておきたい。

古典ビットレジスタと量子ビット状態ベクトルを表す円盤
 上の図2では、古典ビットレジスタに測定結果を格納したが、初めての人にはその使い方が分かりにくい。そこで、図4に要点をまとめた。
 また、右端にある円盤は、測定結果が|1>となる確率(青部面積)とPhase angleを示す。さらに、実線の円はentanglementの有無を意味する。これによれば、送信された3量子ビットはentangledの状態が維持されているが、2つのancillaビットとはもつれ関係がない。それゆえに、このentanglementに影響を与えずに、ancillaビットを測定できるのである。
 図5は、エラーが起こってもそれが訂正され、Aliceの送信時の量子状態ベクトルがそのままBobに伝わったことを説明している。
bit-flipイメージ(Bing)

Qiskit Quantum Labの Noise Modelの利用
 ここまでは、[1]に基づき、Bit-Flipエラーを起こしたいqubitの後ろに、Xゲートを置いてエラーを起こし、テストを行った。だが、Qiskit Quantum Labでプログラミングする場合は、NoiseModelクラス利用して、柔軟に、確率的にエラーを発生させることもできる。もっと複雑な量子回路でのエラー発生の影響を調べるのに有用であろう。その具体的な利用法は、[2]や[3]に掲載されている。また、[2]には、bit-flipの他にphase-flipも説明されていて参考になる。

参考文献
[1] Chris Bernhardt: Quantum Computing for Everyone, The MIT Press, 2020.
https://www.chrisbernhardt.info/
[2] 束野仁政, 量子コンピュータの頭の中、技術評論社、2023年6月
[3] J. L. Weaver & F. J. Harkins, Qiskit Pocket Guide, O'Reilly, 2022-06-15.

-----------
Private Info
Qiskit: Composer files: ErrorCorrec1 & ErrorCorre2, 2023-09-08
Quantum Lab: Chris_Book/Bit_Flipxxxxxx.ipynb

2023年9月3日日曜日

量子テレポーテーションをQiskitでもやってみる

 量子テレポーテーションに関しては、これまでに何度か記事を書きました。例えば次のようなものです:
 これらの記事では、主に量子回路シミュレータQniを使って、説明してきました。内容はほぼ同じなのですが、今回は、Qiskitを使ってみました。新たに分かったこともあるので、記録しておきたいと思った次第です。
地球と月の間の量子テレポーテーション

量子ビットと古典ビットの並び順の相違
 これは(Qiskitに限らず)混乱しやすいので、改めて図1にまとめた。(本図は、私のオリジナル作品)まず、複数の量子ビットシステムのテンソル積での並び順であるが、量子回路図では、これを右側に起こして直立させたものになる。次に、これらの量子ビットの測定結果(古典ビット列)の並び順は、これを右側へ倒して水平にしたものと対応する。すなわち、テンソル積と古典ビット列では、上位と下位が逆転する。これは、慣例となっているので、注意する必要がある。図3にもこれが表れる。

量子テレポーテーションで転送したい量子ビット状態の設定
 任意の量子ビット状態を転送できるのだが、ここでは、一例として、図2に示す量子状態とする。すなわち、量子ビット|0>に対して、Y軸周りに60°回転、さらにZ軸周りに60°回転させた状態を設定した。
 QiskitではComposerとQuantum Labの2つが使える。どちらでも、量子状態ベクトルを表示できる。何か詳細な設定が必要であれば、上図のように、Quantum Labでコーディングすることができる。もちろん、次の図3と同じ量子回路図も、Quantum Labで書くこともできる。

量子テレポーテーションをQiskit Composerでシミュレートする
 さて、ここからが本論なのであるが、原理的なことはすでに上記ブログ記事に十分書いているので、重複する部分は省略する。図3は、図2で設定した量子ビット状態を、AlcieからBobへ転送する様子である。下段には、Aliceの測定結果としての古典2ビット情報の発生確率と、AliceとBobの3量子ビットシステムの確率振幅が表示されている。
 この図で最も重要な点は、Aliceの量子ビット状態①が、Bobの量子ビットに設定されることである。つまり、①と⑥は全く同一の状態である。しかし、①の状態はすでに消滅して⑦のようになっている。これは量子複製不可定理に則っていることなのである。

 すでに既報では、Aliceが②で測定を行う直前の状態は、4通りあり、それぞれ1/4の確率で発生することを示した。図下段の③からそのことが確認できる。これは、Composerが自動的に②の測定を1024回実行(shot)した結果なのである。つまり、測定結果の古典ビット列000、001、010、011の出現確率がそれぞれほぼ等しく、25%程度となっており、それらの合計は100%であることが実証された。なお、3ビットの並びの最上位は0となっているが、これは、Bobの量子ビットがこの時点では測定されていないことによる。また、②の測定の直前に、Aliceが自分の2量子ビットに逆Bell回路を適用していることにも注意する。

 最後に、右端の3量子ビットシステムの状態をみてみよう。この例では、②の測定結果が10であり、|q[2]>|1>|0>という状態になっている。測定結果によらず、q[2]の状態は必ず⑥(すなわち①)となる。そうなるように、Bobによる④の操作(制御付き量子ゲート)が設定されているのである。q[2]が|1>となる確率が0.25(|0>となる確率は0.75)であることは図2に示したので、⑤の確率振幅の分布は納得できるであろう。

Qiskit Composerのその他の機能
 図3の②での一回の測定結果として、特定の2ビットパタンが必要な場合は、図4上部のVisualizations seedを変更して試すことができる。また、図中の量子ビット状態を示す円盤には、量子ビットのpurity(量子もつれの有無)を示す情報も含まれるので、量子回路の検証に利用することができるだろう。

2023年8月29日火曜日

Reconfirmation of quantum teleportation using Qni

Quantum teleportation has already been covered in detail in this article. This time, I would like to reconfirm the point by executing it with the quantum circuit simulator Qni.

In the previous article, Bob applied his I-, X-, Z-, and Y-gates to his qubits, corresponding to the classical 2-bit information 00, 01, 10, and 11 received from Alice. This allowed Bob to obtain the state of the qubit sent by Alice. Logically this is fine. However, when running on the Qni, I found it easier to apply the X and Z gates in succession instead of the Y gate. The description will be made accordingly.

(1) Use of CX (Controlled X) and CZ (Controlled Z)
Considering the gate that Bob should apply to his qubit according to 00, 01, 10, 11 mentioned above, we arrive at the following simple conclusion [3]:

  step1: If the lower bit of the received 2-bit information is 1, the X gate is applied.
  step2: If the upper bit of the received 2-bit information is 1, the Z gate is applied.

According to this, the CX gate and CZ gate can be applied as follows. Note that either |0> or |1> is input to the CX and CZ control lines (other qubits are not input).
As an example in the above diagram, ① is set with the RX(π/3) gate, but any state is acceptable. There are two points worth noting here. First, the state of qubit ① that Alice wants to send to Bob vanishes in Alice's measurement shown in the center, resulting in ②. Nevertheless, the state of ① appeared in ③ by applying the gate by Bob shown on the far right. It matches the No Cloning Theorem. This is exactly what quantum teleportation is.

Second, regardless of the measurement results (classical 2-bit information) by Alice in the central part of the figure, Bob on the far right only needs to apply CX and CZ continuously. However, when Bob is far away from Alice, it is difficult for us to imagine Bob receiving classical 2-bit information from Alice through normal communication. To clarify the image, the following alternative (explicitly checking the classical bits) can also be used.

(2) Explicitly examine classical 2-bit information and apply necessary gates
In Qiskit, etc., classical bit registers can be referenced, but in Qni, the corresponding processing can be realized by adding an execution condition (if) to the gate as shown in the figure below.

In this example, since the measurement result by Alice in the center of the figure is "10", it can be seen that the X gate applied by Bob does not work (it is dark) and only the Z gate is applied.

In this article, I referred to [1] for the principle and mechanism of quantum teleportation, [2] for the Qni simulation, and [3] for the Qiskit simulation.

References
[1] Chris Bernhardt, “Quantum Computing for Everyone”, MIT Press, 2020
https://www.chrisbernhardt.info/
[2] Qni; https://github.com/qniapp/qni
[3] Satoyuki Tsukano, "Inside the mind of a quantum computer", Gijutsu-hyoron-sha, June 2023. (in Japanese)

2023年8月27日日曜日

ChatGPT Plusで自由記述形式からJSONを生成

 何らかの自由形式文のファイルに対して、適当なタグを設定してJSON形式ファイルを生成したい場合があります。例えば、こちらの過去記事は、参考資料[1]の植物の特性を記述した自由形式文をJSONに自動変換する試みでした。そこでは、形態素解析と係り受け解析を利用したプログラム開発が必要でした。しかし、ChatGPTの出現によって、状況は一変しました。そういったプログラムをJavaなどで作成するまでもなく、日本語(英語でも良いが)で僅か数行の指示文(プロンプト)を与えるだけで、それが可能になってしまいました!プログラマー失業の危機かもしれない!
ChatGPTで生成した量子コンピュータのイメージ
 以下にChatGPT(ただし、GPT-4利用の場合)を使ったそのような例をメモとして残しておきたいと思います。

私の指示文(プロンプト)
 以下の例文に対して、4行ほどの日本語のプロンプト(これはsecretです)を与えた。
 今後は、どのようなプロンプトを与えるのかが重要になり、「企業秘密」になってくるかもしれない。ここでもそのように秘密にしました!
 以下に、5つの例文([1]から引用)に対して、ChatGPTが生成したJSON形式を載せます。

ChatGPT(GPT-4)からの回答
--------------------------------------------------------------------------------------
[a] ニッケイ: 樹皮に芳香があり、葉は長楕円形で、液果が黒く熟す。
--------------------------------------------------------------------------------------
  "ニッケイ": {
    "全般": "樹皮 芳香",
    "": {
      "what": "長楕円形",
      "how": ""
    },
    "": {
      "what": "",
      "how": "",
      "when": ""
    },
    "果実": {
      "what": "液果",
      "how": "黒 熟す"
    }
  }

[myコメント] タグ"全般"の値が適切ですね。

--------------------------------------------------------------------------------------
[b] キクザキイチゲ: 3出複葉が3輪生し、茎先に白いキクのような花の咲く春植物。
--------------------------------------------------------------------------------------
  "キクザキイチゲ": {
    "全般": "春植物",
    "": {
      "what": "3出複葉 3輪生",
      "how": ""
    },
    "": {
      "what": "白い キクのような",
      "how": "茎先 咲く",
      "when": "春"
    },
    "果実": {
      "what": "",
      "how": ""
    }
  }

[myコメント] タグ"花"のサブタグ"when"の値が"春"なのは適切ですね。

--------------------------------------------------------------------------------------
[c] オモト: 葉は厚くて縁が波打ち、淡黄緑の花が円筒状に咲き液果は赤い。
--------------------------------------------------------------------------------------
  "オモト": {
    "全般": "",
    "": {
      "what": "厚い 縁 波打ち",
      "how": ""
    },
    "": {
      "what": "淡黄緑 円筒状",
      "how": "咲く",
      "when": ""
    },
    "果実": {
      "what": "液果",
      "how": "赤い"
    }
  }

[myコメント] "葉"のサブタグ"what"の値"波打ち"は、"how"の値の方がいいかも。でも全体的にgoodですね。

--------------------------------------------------------------------------------------  
[d] バイカオウレン: 掌状複葉の小葉は5枚あり、梅に似た白い花が単生し、袋果ができる。
--------------------------------------------------------------------------------------
  "バイカオウレン": {
    "全般": "",
    "": {
      "what": "掌状複葉 小葉 5枚",
      "how": ""
    },
    "": {
      "what": "白い 梅に似た",
      "how": "単生 咲く",
      "when": ""
    },
    "果実": {
      "what": "袋果",
      "how": "できる"
    }
  }

[myコメント] "花"のサブタグ"how"の値"咲く"は、原文には無いところが面白い。

--------------------------------------------------------------------------------------  
[e] ポーチュラカ: 多肉質で地を這い葉はへら形で、枝先に多彩な花が咲く。
--------------------------------------------------------------------------------------
  "ポーチュラカ": {
    "全般": "多肉質 地這い",
    "": {
      "what": "へら形",
      "how": ""
    },
    "": {
      "what": "多彩",
      "how": "枝先 咲く",
      "when": ""
    },
    "果実": {
      "what": "",
      "how": ""
    }
  }

[myコメント] タグ"全般"の値を設定してくれたところが嬉しい。

感 想
 なかなか、まずまず、ですね。このような結果を得ることは、普通の形態素解析と係り受け解析では難しく、膨大なコーパスの学習とattention機構がなせる技だと感じます。
 当然ながら、プロンプト文をどのように与えるかで、結果はかなり異なります。満足できない場合は、試行錯誤である程度改善できるのではないでしょうか。あくまで、道具としてどのように使っていくのか、ということですね。

参考資料
[1]渡辺坦:植物の名前を探しやすい デジタル植物写真集
http://plantidentifier.ec-net.jp/

2023年8月19日土曜日

ChatGPT Plusで和文からの画像生成を楽しむ

 前報では、ChatGPT Plus(有料版)にNASAの公開画像を検索できるプラグインSpace Photo Explorerを組み込んで試行した。今回は、日本語文から画像を生成する(ためのプロンプトを生成する)プラグインPhotorealisticを試した。もっと手軽に使えるMicrosoft BingImage Creattorに比べて、生成されたプロンプトを確認したり、修正したりできる柔軟性が好まれるのかもしれない。

日本語文から画像を生成する例
 一つの例を示す。日本語で次のように要求した:
"量子もつれに基づく量子テレポーテーションのイメージを描いて"

これだけで、Fig.1に示すような画像を生成できた。
 画像は無数に生成することができるが、このうち、(a)の画像は「量子もつれ」を強調したものになっているようだ。一方、(b)の画像は、別々の研究室にいる二人の間で、「量子テレポーテーション」が起こっていることを想起させる。両画像とも、与えた日本語の意図をよく汲み取っているように思われる。驚くべき結果ではなかろうか!

日本語文から画像を生成するフロー
 以下のようなフローで画像が生成される。
山本の要求:量子もつれに基づく量子テレポーテーションのイメージを描いて
ChatGPT(GPT-4 + Photorealistic)の応答(画像生成用プロンプト):
 Quantum entanglement visualized as two intertwined particles glowing in a deep space setting, medium: hyper-realistic photography, style: reminiscent of the visuals from the movie "Interstellar", lighting: soft ambient glow highlighting the particles with a backdrop of distant stars, colors: deep blues, purples, and shimmering whites, composition: shot with a Canon EOS 5D Mark IV DSLR camera, EF 85mm f/1.8 lens, Resolution 30.4 megapixels, ISO sensitivity: 100, Shutter speed 1/125 second, with a focus on the entangled particles in the center of the frame --ar 16:9 --v 5.1 --style raw --q 2 --s 750
Stable Diffusionによる画像生成:
 上記で生成されたプロンプトを、画像生成ソフトStable Diffusionなどへ入力する。その際、(多数公開されている)学習済みモデルから、適当な物を選択する。この選択次第で、画像の質や傾向がかなり変わる。generateボタンを押すと、暫くすると画像が生成される。Fig.2はその一例である。
 Stable Diffusionの実行は、web上でもできるようだが、私の場合は、ローカルマシン(M1 Mac mini)にその実行環境を構築して使った。その際、M1独自のGPUの性能が遺憾無く発揮され、思っていたよりもかなり高速に画像が生成できた。

日常生活の画像も
 さて、「量子もつれ」みたいな専門的話題ではなく、もっと日常生活でのシーンも画像にしてみよう。こんな感じになった。
山本の要求:夏休みの宿題に励む小学生の姿
ChatGPT(GPT-4 + Photorealistic) & Stable Diffusion:Fig.3の図を生成した。

山本の要求:避難訓練で大きなビルから逃げる人々の様子
ChatGPT(GPT-4 + Photorealistic) & Stable Diffusion:Fig.4の図を生成した。
感 想
 自然言語(英語など)でプロンプトを作成し、画像を生成させるソフトはいくつかあるのだが、上記のStable Diffusionはフリーソフトであり、原則として、生成された画像に対する著作権の問題も発生しないようである。
 問題は、そのプロンプト(仕様記述+画像生成制御パラメータ設定)の作成がとても面倒なことであった。それを、ChatGPTのプラグインPhotorealisticが見事に解決した!上記のように、日本語で要求与えるだけでよくなったのだから。
 だが、日本語の要求文次第で、いくらでも生成される画像の構成や質が変わる。経験を積んで適切に利用していくことが求められるだろう。あまりのめり込んで、時間と電力の浪費とならぬように。

2023年8月16日水曜日

ChatGPT PlusでNASAの公開画像を楽しむ

 とうとう、ChatGPT Plus(有料版)を契約した。毎月$20の支払いだが、それだけの価値は十分にあるだろう。まず、無料版はGPT-3.5だったが、Plusでは格段に強力になったGPT-4が使えることが大きい。実はそれだけではない。各種のプラグインも使えるので、用途が大幅に広がるのである。無料版では、テキストチャットだけだったが、Plusではプラグインを使って、画像の高度な検索、画像の生成、グラフ描画、等々ができるのである。素晴らしい!
(->続編はこちら

 以下に、NASAの公開画像を検索できるSpace Photo Explorerというプラグインを試した結果を示す。NASAの公開画像は、ほとんど著作権フリーなので、ブログなどでも、こうして自由に掲載できる。天文、宇宙ファンにはとてもありがたいことだ。
--------------------------

山本:惑星に関して、何か珍しい天体ショーのような現象はありませんか。
ChatGPT(GPT-4 + SpacePhotoExplorer):
 2012年6月5日、地球の隣の惑星である金星が太陽の前を通過する様子が、国際宇宙ステーションから撮影されました。このような金星の太陽面通過は非常に珍しい現象で、次回は2117年になると予測されています。
山本:確か、月から地球を望む写真があったと思います。それをお願いします。
ChatGPT(GPT-4 + SpacePhotoExplorer):
 Apollo 11の宇宙船から見た地球が月の地平線上に昇る様子。写っている月の地形は、近くのSmyth's Sea地域です。
山本:感動的ですね。我々は地球上でどのように生きるべきかを考えさせられます。
ChatGPT(GPT-4 + SpacePhotoExplorer):
 まさにその通りです。これらの画像は、地球が宇宙の広大な空間の中でどれほど小さく、そして特別であるかを示しています。宇宙から見た地球の画像は、国境や戦争、人種や宗教の違いなど、私たちが日常的に直面する多くの人工的な障壁を超えて、私たち全員が同じ小さな青い惑星に住んでいることを思い出させてくれます。

山本:月面に残した人間の足跡の画像を見たい。
ChatGPT(GPT-4 + SpacePhotoExplorer):
 Apollo 11の宇宙飛行士が月面での活動中に70mmの月面カメラで撮影した、宇宙飛行士のブーツの足跡のクローズアップ。
山本:月に雨が降らないとすれば、このような足跡は半永久的に残るような気がしますが、どうでしょうか。
ChatGPT(GPT-4 + SpacePhotoExplorer):
 おっしゃる通りです。月には大気がほとんど存在しないため、風や雨による浸食がありません。そのため、月面に残された足跡や宇宙船の着陸地点、さらには宇宙飛行士たちが設置した機器などの痕跡は、何百万年もの間変わらずそのままの状態で残ると考えられています。この事実は、月探査の歴史や月の環境の独特さを考えるときに非常に興味深いものとなります。

山本:月の重力は地球の重力よりもかなり小さいことを示す画像はありますか。月面で活動している様子などで。
ChatGPT(GPT-4 + SpacePhotoExplorer):
 宇宙飛行士John W. Youngが月面でアメリカの国旗に敬礼している様子。彼がジャンプしている姿は、月の低重力を感じさせます。
山本:次に、火星の北極の氷の画像を見たい。
ChatGPT(GPT-4 + SpacePhotoExplorer):
 火星の北極の氷冠の季節の変化の一例です。
山本:火星に生命の痕跡の可能性を探究する場所はありますか。その付近の画像を見たい。
ChatGPT(GPT-4 + SpacePhotoExplorer):
 火星のJezeroクレーターは、過去の生命の兆候を探求するための主要な場所として選ばれました。このクレーターには、何十億年も前に川が湖に流れ込んでできたデルタが存在します。デルタは、古代の微生物の生命の兆候を探すのに最適な場所と考えられています。以下は、Jezeroクレーターとその周辺の画像です。
山本:海王星という名前の由来は分かりますか。また、その美しい画像があればお願いします。
ChatGPT(GPT-4 + SpacePhotoExplorer):
 海王星は、ローマ神話の海の神「ネプトゥーヌス」から名前が取られています。ネプトゥーヌスは、ギリシャ神話の海の神ポセイドンに相当する神で、海や水を司る神として崇拝されていました。海王星は青く美しい色をしており、この色が海を連想させることから、この名前がつけられました。
山本:海王星とその衛星が写っている画像を見たい。
ChatGPT(GPT-4 + SpacePhotoExplorer):
 以下はVoyager 2によって撮影された海王星とその大きな衛星トリトンの画像の一部です。
山本:冥王星の夜と昼が分かる画像はありますか。
ChatGPT(GPT-4 + SpacePhotoExplorer):
 この画像は、Plutoの明るい高度の大気のヘイズが、日の出前と日の入り後に表面を柔らかく照らす方法を示しています。
山本:冥王星は巨大な衛星を持つとのことですが、それが分かる画像を見たい。
ChatGPT(GPT-4 + SpacePhotoExplorer):
 カロンは、冥王星の5つの衛星の中で最も大きく、直径約755マイル(約1,215キロメートル)あります。冥王星とカロンは、そのサイズと質量の関係から、しばしば「二重惑星」として言及されることがあります。
山本:土星の輪を近くから撮影した画像が欲しい。
ChatGPT(GPT-4 + SpacePhotoExplorer):
 以下は、Cassini宇宙探査機が土星の輪を近くから撮影した画像の一部です。この画像は、土星の輪の驚くべき詳細な構造を示しています。輪は、遠くから見るとぼやけて見える多くの小さなリングレットで構成されています。しかし、近くで撮影すると、輪の構造はかなりの変動を示します。
山本:土星の輪の影が土星に写っている画像が欲しい。
ChatGPT(GPT-4 + SpacePhotoExplorer):
 以下、三日月の土星とその輪、そして輪の影が写っている全面カラー画像。
山本:将来地球に衝突する可能性のある小惑星はありますか。
ChatGPT(GPT-4 + SpacePhotoExplorer):
 はい、地球に衝突する可能性のある小惑星はいくつか知られています。しかし、その多くのリスクは非常に低く、多くの場合、さらなる観測によって衝突の可能性が排除されることが多いです。(例)アポフィス (Apophis): 2004年に発見されたこの小惑星は、当初、2036年に地球に衝突する可能性があるとされました。しかし、さらなる観測により、そのリスクは大幅に低下しました。

 興味深い対話はまだまだ続くのですが、この辺りで筆を置きます。