2024年3月16日土曜日

量子コンピュータで動的回路(Dynamic circuits)を使う

要旨:量子コンピューティングでは、量子ビットを測定してしまうとそこでお終いになる。つまり、その測定結果に応じて量子回路を変更することはできない、と思っていたが、最近それができるようになっていた。QiskitでIBM Quantumマシンを使う場合のDynamic Circuits機能がそれだ。

動的回路(Dynamic Circuits)機能
 量子回路を実行していて、あるところで測定を実行したとする。その結果(古典ビット0か1)に応じて、その後に必要な量子ゲートを動的に加えて実行を継続することができる機能[1][2][3]である。具体的には、図1(a)のようなif文による制御、(b)のようなswitch(case)文による制御、それ以外に、forループやwhileループも使える。ただし、使用するシミュレータ、および実機マシンによっては、これらのいくつかはサポートされていないようだ。


動的回路で量子テレポーテーションを実行
 参考資料[3]に示されているように、このような動的回路で思いつくのが、量子テレポーテーションである。量子テレポーテーションの仕組みについては、[4]の記事を参照願いたい。図2に示す、Aliceによる2つの量子q0とq1の測定後、Bobにその古典2ビット情報を通常通信で送るのだが、そこでその量子回路はおしまいになっていた。その後別途、Bobは受信内容を確認し、それに応じたデコード用の量子回路を設定していた。

 この状況に対して、動的回路を図3のように組むことができる。これによって、途中の動的回路を含む、全体で一つの量子回路で動作を完結させることができる。これは、アルゴリズムを確かめる上でとても便利であり、強力である。
 念の為であるが、qc0はどんな状態であっても良い。ここでは一例として、|0>にRX(π/4)を適用した状態とした。もちろん、その状態はBobは知らない訳だが、動的回路の部分はいつもこのままで良く、Bobは送られてきたどんな状態でも再現できるのである。そこが、量子テレポーテーションの驚異的なところである。


量子テレポーテーションの実機による確認 
 さて、図3の量子回路がうまく働くかを、IBM Quantum実機(ibm_kyoto)で確認した結果が図4である。結論を言うと、この図から、この実機では80%の確率で量子テレポーテーションが成功した。全部で1000shotの実行のうち、図4の赤点線で囲ったカウント(合計200)は、何らかのノイズによるエラーであろう。本来はこの部分は全て0となるはずである。
 ここで80%成功とは何か?それは、図4の左側4本のカウントは、いずれも最上位ビット(qc2に対応)が0であることを指す。レジスタqc2の最後でBobが測定する直前に、RY(-π/4)を設定しているので、これによって、元々Aliceがqc0に設定した状態をキャンセルする、すなわち|0>に戻るからである。

 なお、実機ではなくシミュレータで実行した結果は図5のようになった。現状の実機では、シミュレータとの差がこのように生ずる場合がある。

 なお、IBM Quantum Composer(ただし、シミュレータによる実行)でも以下ように同様にできる。だが、古典ビットレジスタCの検査が、図3の場合と異なり、ビット毎ではなく、3ビットの10進数として扱うので、注意が必要である。if文による動的ゲートの設定が4つになっている。

(補足事項1)Quantum Labに置くfile容量に注意
 突然、QuantumのLabサーバ起動しなくなった。ブラウザのキャッシュやクッキーを削除したりしてみたが直らない。Qiskiには、ipynbファイル内で、ブロッホ球やその上のtransition表示や、Latex形式回路図表示などができる。しかし、これらはかなりのファイル容量を占める。一つの回路で35MBにもなっていた。それが数十個もあった。そこが原因だったらしい。これらの表示をコメントアウトしてsaveして、再起動したところ回復したようだ。

(補足事項2)実機使用可能無料枠
 実機での実行を繰り返してきたので、使用状況を見てみた。今月は、無料枠ではあと3分強しか使えない。有効に活用しよう。

参考文献

[1] Classical feedforward and control flow
https://docs.quantum.ibm.com/build/classical-feedforward-and-control-flow

[2] Repeat until success
https://learning.quantum.ibm.com/tutorial/repeat-until-success

[3] @kifumi、動的回路で量子テレポーテーションを実行する
Qiita記事、最終更新日 2023年03月20日

[4] 超高密度符号化と量子テレポーテーション(その3)
https://sparse-dense.blogspot.com/2022/12/blog-post_2.html

2024年3月15日金曜日

動画:量子コンピューティングに親しむためのスマホアプリ

"量子コンピューティングに親しむためのスマホアプリ"動画をここに公開しました。
神奈川工科大学「ITを活用した教育研究シンポジウム2023」で発表したものです。

"量子コンピューティングに取り組む"動画はここに公開しました。
神奈川工科大学「ITを活用した教育研究シンポジウム2022」で発表したものです。

2024年3月13日水曜日

Qiskit API for IBM Quantumが変更になった!

 数週間までまで正常に動いていた、Qiskitコードが、2024-03-10現在、突然エラーが出て動かなくなった。これはどうしたことか。Qiskitドキュメントを調べると、Qiskit 1.0.1になって、かなりの変更があったことが分かった。よくみると、migration guideもあるではないか。それに従って、以下のような点を修正する必要があった。

シミュレータを使う場合:以下のようにして解決
 qiskitからAer, executeなどをimportできなくなった。
 qiskit.tools.visualizationからplot_histogramなどがimportできなくなった。等々。
→別のライブラリから、QiskitRuntimeService等のimportが必要。

IBM Quantum実機を使う場合:以下のようにして解決
 Qiskitの素晴らしい点として、シミュレータibmq_qasm_simulatorで動かしていた量子回路はそのままIBM Quantum実機でも動くことであった。シミュレータの名前を単に、実機の名前、例えばibm_brisbane等に変更するだけであった。

 しかし、こちらも、突然動かなくなったのである。実機で動かす前に、まず、無償で利用できる実機を確認した。登録ユーザの地域で異なるのだろうが、私の場合、ibm_brisbane、ibm_osaka、ibm_kyotoの3つであったが、いずれも、127量子ビットを装備した新鋭機 Eagle r3である。素晴らしい!だが、早速実行させてみると、以下のエラーが発生。しかも、"CX (CNOT) is not supported"という信じ難いエラーメッセージが!

 最初は、戸惑ったのだが、すぐにピンと来ました。このCXやHなどという量子ゲートは数学モデルにすぎない。実機がそのまま実装している訳ではないということだろうと。案の定そのその通りでした。シミュレータで動かしていた時の量子回路をtranspileというツールで変換した後に実機で実行すると成功しました!
 これまでは、実機で使う場合にはシステムが自動transpileをやっていたが、それを止めたと言うことであろう。

 このような状況は、確実な量子コンピュータの進歩の一面と言えるだろう。今後の色々な量子コンピュータ実機はそれぞれ独自の命令セットを持つはずだ。だから、論理レベルと物理レベルを明確に分離しているのだ。従来のプログラミング言語のコンパイラみたいなものが必須になってくる。

Transpileが1分間でわかるビデオ解説
 以下のビデオはとても良いのではないか!最適化レベルもいくつかある!
How can I Transpile a Quantum Circuit? 1 Minute Qiskit
https://www.youtube.com/watch?v=8mrPNSctRIg


2024年3月12日火曜日

量子コンピューティングを学ぶためのアプリをMIT App Inventorで開発(総集編)

----------------------------------------
この記事は、MIT App Inventorで量子コンピューティングの総集編であり、こちらの英文記事の和訳です。
----------------------------------------
 私は、日本の神奈川工科大学の名誉教授(コンピュータサイエンス)山本富士男です。実用的な量子コンピュータはまだ完成していないが、それが実用に供されれば社会に与える影響は非常に大きいと予測されるので、企業や大学での量子コンピューティングに関する研究開発は盛んになっている。私は、量子コンピューティングの基礎を学ぶための多数のアプリを、App Inventorを用いて開発を行なってきた。その目的は、関数電卓を使うような手軽な感覚で、少数の量子ビットを使う計算を楽しむことにある。だが、それだけではない。小規模であっても、量子回路シミュレータなどのアプリを自作するならば、理解は一層深まるに違いない。そのように考えて行ってきた取り組みを紹介したい。
 最初に、最も基本となるシングル量子ビットとはどんなものなのかを、自分の操作で確かめられるアプリを作成した。つまり、古典ビットに対する演算に該当する量子ビット操作のためのシミュレータアプリである。次に、2量子ビット、または3量子ビットを使う量子アルゴリズムのための、量子回路シミュレータアプリを開発した。実際、数例の量子アルゴリズムをそのシミュレータで再現してみた。最後に、別の観点から、大量の量子ビットのストリームを入力とする、個別のアルゴリズムについて、それぞれの動作原理の把握を容易にするアプリも開発した。

何と言ってもまずはQubit(量子ビット)
 まず、基本となる量子ビットがある。これは、0と1とを値とする従来のビットとは大きく異なる。量子ビット(Qubit)は、下図に示すようなブロッホ球と呼ばれる球面上にあると考えられる。北極点は従来の0、南極点は1に該当する。したがって、量子ビットは、一般に、北極点と南極点をある確率で重ねあわせた状態にあると言える。量子ビットは「測定」という操作を行うことによって初めて、その値が0か1かのどちらかに、確率的に確定する。測定するまでは、0と1の値が重なった状態にある。その不確定な状態のまま、種々の操作、すなわち演算を行うことができる。演算の結果として、qubitの状態、すなわちブロッホ球面上の位置が変化する。その際の変化に随伴する情報を利用して、必要な計算を進めることができる。複数の量子ビットの場合は、それらの情報の相互干渉さえも有効に利用できる。では、最初に、単一量子ビットの場合を見てみよう。
 量子ビットの状態をどのような方法で変化させるのだろうか?それに答えるのが、下図に示すアプリである。ブロッホ球面の量子ビットの緯度と経度を、(a)のようにスライダーで自由に変えることができる。しかし、通常は、むしろ、量子ゲートと呼ばれる演算で変える。例えば、アダマールゲート(H)を、北極点の状態に適用すると、それは赤道上に移動する。下図の(b)では、Hゲート適用後にさらにSゲート(赤道上を90°回転)を適用した例である。他にも多数の量子ゲートを利用できる。
2量子ビットを一つのシステムとして扱う
 一つの量子ビットだけでできることは少ないが、2量子ビットを使うと、急に様子が変わる。一般に、複数量子ビットを扱う場合には、個々の量子ビットを個別に扱うのではなく、一つのシステムとみなすことが必要となる。例えば、2量子ビットの場合、下図のように一方にHゲートを適用し、次に、両者に跨るCNOT(Controlled NOT)を適用すると、有名な量子もつれ状態(Bell状態)にすることができる。
 この場合の量子もつれとは、AliceとBobの量子ビット間に、強い相関関係が生ずることを意味する。すなわち、たとえ、AliceとBobが遠隔にあっても、Aliceが自分の量子ビットを測定して0を得た場合は、Bobの量子ビット自動的に即座に0になる。同様に、測定結果が1であれば、両者とも1となる。以下の図のアプリは、その状況を示している。
 しかしながら、現状の量子回路シミュレータや量子コンピュータでは、AliceとBobが遠隔にいるというイメージを持つことができない。そこで、私は両者の量子ビットをそれぞれ別のモバイイルフォン割り当てることにした。これらにコントロール用の1台を加えて、3台のモバイルフォンの構成で、遠隔にある量子間のもつれを模倣し、実感できるアプリを作った。下図にそれを示す。この図のトップにある、Cloud DB(リアルタイムDB)は、量子もつれの効果を表現する上で重要な役割を果たしている。

3量子ビットを使う色々な量子アルゴリズム
 次のステップとして、種々の基本的な量子アルゴリズムを試すには、3量子ビットが必要となる。だが、上記のように、各量子ビットを1台の台のスマホに割り当てる方法は、煩雑になるので採用しないことにした。それに替えて、ここでは、1台のモバイルフォンで3-qubitを扱えるように、新たに設計し直した。そのアウトラインを下図に示す。
 この3-qubitシミュレータアプリを使って、例えば、以下のような量子アルゴリズムを実装することができた。
(1) Phase kickback(制御ビットへの位相変化の戻し)
(2) Grover’s algorithm(構造化されていないデータの探索)
(3) Superdense coding(少数の量子ビットで多くの古典ビット情報を送信)
(4) Quantum Fourier Transform(to find out the frequency of coprime powers sequence)

 詳細説明は略すが、このうちの(2)のGrover’s algorithmの実行例を下図に示す。(a)では、8個の要素のうちの、探索対象をマーキングしている。(b)では、確率振幅の増幅という手法で解を求めている。
 もう一例示そう。次の図は、量子フーリエ変換QFTの利用例である。詳細説明は略すが、ここでは、ある数列(coprime powers sequence)から、その構成要素のパタンの周期、あるいは周波数を求めている。この結果は、IBM Quantumマシンでの実行結果と一致することも示している。このような周波数の検出は、Shor’s素因数分解アルゴリズムでも重要である。

大量の量子ビットのストリームを入力とするアルゴリズム 
 ここまでは、汎用的な量子回路シミュレータ利用であったが、次に、個別の量子アルゴリズムに特化した、説明用アプリを示す。例えば、大量の量子ビットのストリームを入力とするアルゴリズムがいくつかある。ここでは、光子の偏光の量子計算モデルと、量子もつれを利用する量子鍵配送を取り上げる。

(1) 量子計算モデルに基づく光子の偏光
 Qubitは、電子のspinや光子の偏光で表現される。ここでは、後者を扱う。今、偏光板3枚の軸角度を様々に変えて重ねる。その状態で物体を見た場合の明るさを問題とする。その際に必要なのは、入射される大量の光子が、それぞれの偏光板を通過する確率の計算である。作成したアプリでは、それらの通過確率を、偏光板の色の透明度に反映させて、実際の見え方を模倣した。その様子を下図に示す。

(2) 量子もつれに基づく量子鍵配送
 次のアプリは、量子鍵配送プロトコルEkertのイラストレーションである。このプロトコルでは、2者間の情報が第3者に盗聴されたか否かを確実に判定できる。その方法では、もつれた状態の量子ビット対から成る十分長いストリームを用意する。対の一方をAliceが、他方をBobが受け取る。各対毎に、両者はそれぞれ独立に、3種類の基底から一つをランダムに選び測定を行う。全ストリームの受信を完了後、両者はそれぞれ、古典ビット列(測定結果)と選択基底の列を得ることになる。
 明らかに、確率2/3で両者の基底は合致しない。もつれた状態において、両者が異なる基底で測定した結果は、確率1/4で合致することが、Bellの定理で示されている。ただし、それは、第3者による盗聴が無い場合である。
 一方、例えば、第三者Eveが、AliceとBobの測定よりも前に量子を盗聴したとする。盗聴には必ず、適当な基底での測定が必要となる。その結果、AliceとBobの量子対のもつれ状態が消失することに起因して、測定結果の一致確率は3/8に上昇する。これにより、盗聴を検出できる。これをシミュレートするアプリの様子を下図に示す。理論値通りの実行結果が得られていることが分かる。
まとめ
 量子コンピューティングの基礎に親しむためのモバイルフォンアプリとして、量子回路シミュレータや量子アルゴリズムのイラストレーションを開発してきた。これらの開発は、すべて、MIT App Inventorのみを用いて、効率的に行うことができた。App Inventorは、このように、新しい分野を学ぶためのアプリの開発に極めて有用であることを再確認できた。
 なお、この記事で省略した内容の詳細をご覧になりたい場合は、以下のブログ記事を参照いただきたい。ここには、約90件の関連記事の一覧が掲載されている。

In these developments, the books [1] , [2] and [3] written by three professors, Chirs Bernhardt, Barry Burd and Shigeru Nakayama, respectively, were very helpful.

参考文献

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

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

[3] Shigeru Nakayama(中山 茂): Quantum app Qni, Gaia Education series 52, 2022. (In Japanese)

 

2024年3月10日日曜日

Developing apps to learn quantum computing with App Inventor

Although a practical quantum computer has not yet been completed, it is predicted that if it is put into practical use it will have a huge impact on society, so research and development on quantum computing at companies and universities is increasing. I have used App Inventor to develop numerous apps for learning the basics of quantum computing. The purpose is to enjoy computations using a small number of qubits, as easily as using a scientific calculator. But that's not all. If you create your own app, such as quantum circuit simulator, even on a small scale, your understanding will definitely deepen. I would like to introduce the efforts I have made with this in mind.

First, I created an app that allows you to see what the most basic single qubit is. In other words, it is a simulator app for quantum bit operations that correspond to operations on classical bits. Next, I developed a quantum circuit simulator for quantum algorithms that use two or three qubits. In fact, I tried solving several quantum computing problems using the simulator. Finally, from a different perspective, I have also developed apps that make it easier to understand the operating principles of individual algorithms that take streams of large numbers of qubits as input. 
1. First of all, Qubit (Quantum Bit)

First, there is the fundamental quantum bit. This is significantly different from classical bits whose values are 0 and 1. A quantum bit (Qubit) is thought to lie on a spherical surface called a Bloch sphere, as shown in the figure below. The North Pole corresponds to the classical 0, and the South Pole corresponds to the classical 1. Therefore, it can be said that a quantum bit is generally in a state (superposition) in which the north and south poles are superposed with a certain probability. The value of a quantum bit is stochastically determined to be either 0 or 1 only through an operation called measurement. Until measurement, the values of 0 and 1 overlap. Various operations can be performed in this uncertain state. As a result of the operation, the state of the qubit, or its position on the Bloch sphere, changes. The necessary computations can be performed using the information accompanying the changes. In the case of multiple qubits, even the mutual interference of their information can be used effectively. First, let's look at the case of a single qubit.
How do we change the state of a qubit? The answer to this question is the app shown below. You can freely change the latitude and longitude of the qubit on the Bloch sphere using the sliders as shown in (a). However, this is usually changed using an operation called a quantum gate. For example, if you apply the Hadamard gate (H) to the state of the North Pole, it will move to the equator. In (b) of the figure below, an S gate (90° rotation on the equator) is applied after the H gate is applied. Many other quantum gates are available.
2. Two-qubit as a single system

There is little that can be done with just one qubit, but when two qubits are used, things suddenly change. In general, in the case of multiple qubits, it is necessary to consider them as a whole, rather than treating each qubit individually. For example, in the case of two qubits, by applying an H gate to one of them as shown in the figure below, and then applying a CNOT (Controlled NOT) spanning both, it is possible to create the famous quantum entangled state (Bell state).
Quantum entanglement in this case means that there is a strong correlation between Alice's and Bob's qubits. In other words, even if Alice and Bob are far apart, if Alice measures her own qubit and gets 0, Bob's qubit automatically becomes 0 immediately. Similarly, if the measurement result is 1, both will be 1. The app in the figure below shows this situation.
However, with current quantum circuit simulators and quantum computers, it is not possible to imagine Alice and Bob being far apart. Therefore, I decided to allocate both qubits to different mobile phones. With a configuration of three mobile phones (plus one more for control), I created an app that mimics entanglement between remote quanta. This is shown in the figure below. Cloud DB (real-time DB) at the top of this figure plays an important role in expressing the effects of quantum entanglement.

3. Various quantum algorithms with 3-qubit system 

As a next step, three qubits are needed to try out various basic quantum algorithms. The method described above, in which each quantum bit is assigned to a separate smartphone, will be complicated, so I will not use it here. Instead, it has been redesigned so that a single mobile phone can handle 3-qubits. The outline is shown below.
For example, using this 3-qubit simulator app, I was able to implement the following quantum algorithm.
(1) Phase kickback (returning phase change to control bits)
(2) Grover’s algorithm (exploration of unstructured data)
(3) Superdense coding (sending many classical bits of information with a small number of qubits)
(4) Quantum Fourier Transform (to find out the frequency of coprime powers sequence)

Although detailed explanation is omitted, an example of executing Grover's algorithm (2) is shown in the figure below. In (a), the search target among the eight elements is marked. In (b), the solution is found using a method called probability amplitude amplification.
The following figure shows an example of the use of quantum Fourier transform QFT in (4). Although detailed explanation is omitted, here, the period (or frequency) of the pattern of the constituent elements is detected from a certain number sequence. The results also show that they are consistent with those run on an IBM Quantum machine. Detection of such frequencies is also important in Shor's prime factorization algorithm.

4. Algorithms that take a long stream of qubits as input

Up to this point, we have used a general-purpose quantum circuit simulator, but next I will show apps to explain the operation of individual quantum algorithms. For example, there are several algorithms that take as input a long stream of qubits. Here, I will discuss quantum computational models of photon polarization and quantum key distribution using quantum entanglement.

(1) Photon polarization based on quantum computational model

Qubit is expressed by the spin of an electron or the polarization of a photon. Here, we will deal with the latter. Now, stack the three polarizing plates with various axis angles. The problem is how bright the object is when viewed in that state. What is needed in this case is calculation of the probability that a large number of incident photons will pass through each polarizing plate. In the app I created, the probability of passing these things was reflected in the transparency of the color of the polarizing plate. This imitated the actual appearance. The situation is shown in the figure below.
(2) Quantum key distribution based on quantum entanglement

The following app is an illustration of the quantum key distribution protocol Ekert. With this protocol, it is possible to reliably determine whether information between two parties has been intercepted by a third party. To prepare, we prepare a sufficiently long stream of entangled pairs of qubits. Alice receives one of each qubit pair, and Bob receives the other. For each pair, they independently select one of the three bases at random and perform measurements. After completing the reception of all streams, both parties will obtain a classical bit sequence (measurement result) and a selection basis sequence, respectively.

Obviously, the two bases do not match with probability 2/3. Bell's test shows that in an entangled state, results measured using different bases will agree with a probability of 1/4. However, this is the case where there is no eavesdropping by a third party.

On the other hand, suppose that a third party, Eve, eavesdrops on the quantum before Alice and Bob measure it. Eavesdropping always requires measurements on a suitable basis. As a result, the probability of coincidence of the measurement results increases to 3/8 due to the disappearance of the entangled state of the quantum pair of Alice and Bob. This allows wiretapping to be detected. The figure below shows an application that simulates this. It can be seen that the execution results match the theoretical values.
Summary

I have developed quantum circuit simulators and other mobile phone applications for learning the basics of quantum computing. All of these developments were done efficiently using only MIT App Inventor. I was able to reaffirm that App Inventor is extremely useful for developing apps for learning new fields like this.
If you need more details about the content omitted in this article, please refer to the blog articles below. A list of approximately 90 related articles is posted here:

In these developments, the books [1] , [2] and [3] written by three professors, Chirs Bernhardt, Barry Burd and Shigeru Nakayama, respectively, were very helpful.

References

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

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

[3] Shigeru Nakayama: Quantum app Qni, Gaia Education series 52, 2022. (In Japanese)

2024年3月9日土曜日

A List of my blog articles on Quantum Computing

 量子コンピューティングに関する私の記事の一覧です。約 90件あります。

Here is a list of my articles on quantum computing. There are about 90 titles in Japanese and English. English article titles have a or at the beginning.

➡️関連ビデオ(基本を学ぶ)は、ここに(Youtubeに)公開しました。
➡️関連ビデオ(スマホアプリ)は、ここに(Youtubeに)公開しました。

2024年1月26日金曜日
2023年4月28日金曜日
2023年2月22日水曜日
2022-11-28
2022-09-18
2022-05-24
2022-04-08

2024年3月6日水曜日

Limited release: My mobile quantum circuit simulator

I have developed several types of original quantum circuit simulators as mobile applications. Eventually, I decided to make two of them available to the public, albeit in beta version. All of these were efficiently developed using MIT App Inventor. In these developments, the books [1] , [2] and [3] written by three professors, Chirs Bernhardt, Barry Burd and Shigeru Nakayama, respectively, were very helpful.

If you are interested in these apps and would like to try them out, please contact me. I will provide you built files (.apk format) for Android phones. Below is a very simple document to help you use it.

Japanese summary "限定公開:私のモバイル量子回路シミュレータ"
私は、これまでに数種類の独自の量子回路シミュレータを、モバイルアプリとして開発してきた。そのうちに2つを、まだベータ版ではあるが、限定公開することにした。これらは、すべてMIT App Inventorによって、効率的に開発されました。これらの開発では、3人の教授Chirs BernhardtとBarry Burdと中山茂がそれぞれ執筆された著書[1]と[2]と[3]が非常に参考になりました。これらのアプリにご興味を持ち、試用してみたいという方がおられたら、私にご連絡ください。Android phone用のビルトファイル(.apk形式)をご提供します。それをご使用いただくために、以下に極簡単なドキュメントを示します。)

Two types of mobile quantum circuit simulator
    Q1_beta1: for a single-qubit
    Q3_beta2: for 3-qubit system

------- Q1_beta1 app ----
Q1_beta1 app - Setting quantum state using sliders

Applying quantum gates

------- Q3_beta2 app ----
Overview of the Q3_beta2 app

Phase Kickback

Grover’s Algorithm

Superdense Coding

Finding the frequency of coprime powers sequence(1)

Finding the frequency of coprime powers sequence(2)

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

[2] Barry Burd: Quantum Computing Algorithms -Discover how a little math goes a long way, Packt Publishing, 2023.
https://users.drew.edu/bburd/quantum/

[3] Shigeru Nakayama(中山 茂): Quantum app Qni, Gaia Education series 52, 2022. (In Japanese)

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年2月14日水曜日

Functional Programming in MIT App Inventor

Abstract: Functional programming blocks such as map, filter, reduce, and sort can be used in list operations in MIT App Inventor. These will allow you to create clean and easy-to-understand programs without using for loops or if-then-else. (This article has also been posted to the MIT App Inventor community. Have a look at this.)

要旨MIT App Inventorのリスト操作において、map、filter、reduce、sortなどの関数型プログラミングブロックが使える。これにより、forループやif-then-elseを使わず、すっきりとした分かりやすいプログラムが作れるであろう。(この記事は MIT App Inventor コミュニティにも投稿されています。 こちらをご覧下さい。)

MIT App InventorのFunctional List Operators
 
リスト処理において、図1に示すような関数型プログラミングのためのブロックが使える。すなわち、

  • map:要素に何らかの変換を施して新たなリストを作る。
  • filter:条件を満たす要素だけのリストを作る。
  • reduce:要素の最大値、最小値などを得るなど、縮約する。
  • sort:要素を条件に従ってソートし、新たなリストを作る。

 このような関数型プログラミングは、多くのプログラミング言語でサポートされている。しかし、MIT App Inventorのようなブロック型プログラミング環境にこれを取り入れるには多くの課題があったようである。この機能は、参考文献[1]で説明されているが、その元になる論文が[2]である。

 この論文[2]は、米国Wellesley大学(最難関の女子大らしい)の学生が2015年に発表したものであり、全98ページに及ぶ。表紙には、"Thesis advisor : Prof. Franklyn Turbak"と書かれているので(修士)学位論文のようだ。それだけ奥が深いものである。MIT App Inventorは、生徒学生を主な対象とした使いやすいプログラミング環境を提供しているが、関数型のような、高度なソフトウェア技術もしっかり取り入れて、強固なバックグラウンドを築いていることを改めて感じる。

例題1: 指定された年の"13日の金曜日"を全て求める
 よくある例題かも知れないが、"指定年の13日の金曜日"を全て求めるアプリを、上記の機能を使った書いてみた。図2はその全ブロックである。大きなブロック"get_13Fri_list"が主要部であるが、ここには、forループもif-then-elseも全く出てこない。これが典型的な関数型プログラミングの簡単な例である。
 このブロックには、関数型ブロックが3つ使われている。(a)では、mapにより、月のリストをその月の13日の日にちリストに変換している。(b)では、そのリストをfilterにより、"金曜日=曜日番号6"だけのリストに変換している。このリストの要素は日にちオブジェクトなので、それを(c)でのmapにより、通常の書式の日付に変換している。つまり、map - filter - mapを連続して適用して、一気に流れるように、答えの日付のリストを得ている。爽快感がある!

例題2: reduce(combining)も使ってみる
 上記のmapとfilterの他に、reduce(combining)も有用です。図3に、リスト要素をユークリッドノルムで正規化して新たなリストを作る例を示します。

参考文献

[1] MIT App Inventor Functional List Operators
https://ai2.appinventor.mit.edu/reference/concepts/pholo.html#map

[2] Soojin Kim, "Developing and Assessing New List Operators in App Inventor", 2015.
https://repository.wellesley.edu/object/ir558


2024年2月12日月曜日

数学グラフ計算機Desmosで偏光板アプリを作る

 以前のこのブログ記事で、量子計算に基づく、偏光板のスマホアプリを作成した。
これと同等機能のアプリを、最近知ったDesmos(2D3D数学グラフ計算機)を用いて作成してみた。Desmosの素晴らしさに感嘆!

 ノーコードで(というよりも、宣言的記述だけで)、インタラクティブに動作するアプリが簡単に作れてしまう。例えば、関数の計算式に未定義のパラメータ名が出てくると、そのパラメータを設定するスライダーが自動的に出てくる。それを動かすとパラメータを変化させたアニメーションも簡単に作れる。プログラミングにとらわれず、やりたいことを数学的に記述すれば良い、という感があって嬉しい。

 さらにwebで作成したアプリは、ほとんどそのままスマホアプリのように使うこともできる。
これで、ビジュアライゼーションの強力なツールを身につけられる!

 以下の図1、図2をご覧いただきたい。説明はほとんど不要であろう。
 以下の短いビデオもご覧ください。
 さらに、国際数学アートコンテストもやっていて、楽しく、ためになる。

 なお、上記はDemosの「幾何ツール」と呼ばれるものを使った。この中で、角度θに偏光した光子が軸角度αの偏光板を通過する確率p(θ, α)を使ったいるのだが、その3Dグラフを見たい場合は、別途「3Dgraph」で描いたものを参照できる。以下の例をご覧いただきたく。