LoginSignup
25
30

More than 3 years have passed since last update.

量子コンピューティングを試してみた

Posted at

AI Academyの量子コンピューター入門編に沿っていろいろと試してみた。
※なので、今回使ったのはQiskitではなく、blueqat

# 準備
from blueqat import Circuit

# Xゲートを0番目の量子につなぐ
# Xゲート:反転
Circuit().x[0].run()

array([0.+0.j, 1.+0.j])

反転したので、|1>になっている。


# H:アダマールゲート:重ね合わせの状態を作る
Circuit().h[0].run()

array([0.70710678+0.j, 0.70710678+0.j])

# 測定
# Circuit().m[:].run(shots=100)
print(Circuit().x[0].m[:].run(shots=100))
print(Circuit().y[0].m[:].run(shots=100))
print(Circuit().z[0].m[:].run(shots=100))
print(Circuit().h[0].m[:].run(shots=100))

Counter({'1': 100})
Counter({'1': 100})
Counter({'0': 100})
Counter({'1': 58, '0': 42})

重ね合わせると、0,1が半々ぐらいになっている。

ちなみにいくつか調べてみるとblueqatで結果表示でlast_resultを使うってあったりするが、
https://qiita.com/gyu-don/items/9db7a585a126ffd1b994 によると
last_resultみたいな使い方はやめたとのこと。

# 2量子ビット
# CXゲート:制御ビットが1の時だけ、標的ビットを反転
Circuit().cx[0,1].m[:].run(shots=1)

Counter({'00': 1})

# CXゲートのその他パターン
c1 = Circuit(2).x[0]
print("{} -> {}".format(c1.m[:].run(shots=1), c1.cx[0,1].m[:].run(shots=1)))

c2 = Circuit(2).x[0].x[1]
print("{} -> {}".format(c2.m[:].run(shots=1), c2.cx[0,1].m[:].run(shots=1)))

c3 = Circuit(2).x[1]
print("{} -> {}".format(c3.m[:].run(shots=1), c3.cx[0,1].m[:].run(shots=1)))

Counter({'10': 1}) -> Counter({'11': 1})
Counter({'11': 1}) -> Counter({'10': 1})
Counter({'01': 1}) -> Counter({'01': 1})

制御ビット(0番ビット)が1の場合だけ、標的ビット(1番ビット)が反転しているのがわかる

# CCXゲート:制御ビットが両方1の時、標的ビットを反転する
# 真面目に書くと長くなるが、これ用の関数が用意されているのでそれを使えばOK
print(Circuit().x[:2].ccx[0,1,2].m[:].run(shots=1))
print(Circuit().x[:1].ccx[0,1,2].m[:].run(shots=1))
print(Circuit().x[0].ccx[0,1,2].m[:].run(shots=1))
print(Circuit().x[1].ccx[0,1,2].m[:].run(shots=1))
print(Circuit().x[0].x[1].ccx[0,1,2].m[:].run(shots=1))
print(Circuit().x[0].x[1].x[2].ccx[0,1,2].m[:].run(shots=1))

Counter({'111': 1})
Counter({'100': 1})
Counter({'100': 1})
Counter({'010': 1})
Counter({'111': 1})
Counter({'110': 1})

制御ビット(0番,1番ビット)の両方が1の場合だけ、標的ビット(2番ビット)が反転しているのがわかる

所感

Xゲートはわかりやすい。YゲートやZゲートは意味はわかるんだが、ちゃんと理解できているとも思えない。それでも、前になんとなくしかわからなかったゲート型のやつがなんとなくわかったような気がする。
とはいえ、これをどうやって使えばいいのかどうかが全然わからない。

あと、アニーリングも試してみたいんだけど、その前に数理モデルをたしなんでみないとそもそも定式化ができないんだよな。
あと、同じようなものをQiskitでも試してみたい。

今回作ったものは以下に。
https://github.com/RyoNakamae/quantum/blob/master/quantum.ipynb

25
30
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
25
30