FPGA開発日記

カテゴリ別記事インデックス https://msyksphinz.github.io/github_pages , English Version https://fpgadevdiary.hatenadiary.com/

Googleの量子コンピュータフレームワークCirqを使ってみる(7. Cirqのチュートリアルで量子フーリエ変換を試す)

量子コンピュータの勉強を続けており、Cirqの資料をひたすら読み進めている。

Cirqのサンプルプログラムでも、量子フーリエ変換の例が挙げられている。

以下の論文を参考にしているらしい。

  • Implementation of Shor’s Algorithm on a Linear Nearest Neighbour Qubit Array

https://arxiv.org/pdf/quant-ph/0402196.pdf

def generate_2x2_grid_qft_circuit():
    # Define a 2*2 square grid of qubits.
    a,b,c,d = [cirq.GridQubit(0, 0), cirq.GridQubit(0, 1),
               cirq.GridQubit(1, 1), cirq.GridQubit(1, 0)]

    circuit = cirq.Circuit.from_ops(
        cirq.H(a),
        _cz_and_swap(a, b, 0.5),
        _cz_and_swap(b, c, 0.25),
        _cz_and_swap(c, d, 0.125),
        cirq.H(a),
        _cz_and_swap(a, b, 0.5),
        _cz_and_swap(b, c, 0.25),
        cirq.H(a),
        _cz_and_swap(a, b, 0.5),
        cirq.H(a),
        strategy=cirq.InsertStrategy.EARLIEST
    )
    return circuit

私の使っているサンプルプログラムと微妙に違うなあ...?

とりあえず実行してみる。

$ python3 quantum_fourier_transform.py
Circuit:
(0, 0): ───H───@───────×───H────────────@─────────×───H────────────@───────×───H───
               │       │                │         │                │       │
(0, 1): ───────@^0.5───×───@────────×───@^0.5─────×───@────────×───@^0.5───×───────
                           │        │                 │        │
(1, 0): ───────────────────┼────────┼───@─────────×───┼────────┼───────────────────
                           │        │   │         │   │        │
(1, 1): ───────────────────@^0.25───×───@^0.125───×───@^0.25───×───────────────────

FinalState
[0.25+0.j 0.25+0.j 0.25+0.j 0.25+0.j 0.25+0.j 0.25+0.j 0.25+0.j 0.25+0.j
 0.25+0.j 0.25+0.j 0.25+0.j 0.25-0.j 0.25+0.j 0.25+0.j 0.25+0.j 0.25+0.j]

すべての状態で量子の確率が同一になった。わたしの最初に実装したケースと違うなあ。

初期量子状態を少し変更してみる。 周期が0(すべての量子状態の確率が同一)の状態を作ってみる。

diff --git a/examples/quantum_fourier_transform.py b/examples/quantum_fourier_transform.py
index 893298a..bf850f4 100644
--- a/examples/quantum_fourier_transform.py
+++ b/examples/quantum_fourier_transform.py
@@ -38,7 +38,8 @@ def main():
     print()
     print('FinalState')
     print(np.around(result.final_state, 3))
-
+    print(np.abs(np.around(result.final_state, 3)))
+
 def _cz_and_swap(q0, q1, rot):
     yield cirq.CZ(q0, q1)**rot
     yield cirq.SWAP(q0,q1)
@@ -52,6 +53,10 @@ def generate_2x2_grid_qft_circuit():

     circuit = cirq.Circuit.from_ops(
+        cirq.H(a),
+        cirq.H(b),
+        cirq.H(d),
+        cirq.H(c),
         cirq.H(a),
         _cz_and_swap(a, b, 0.5),
         _cz_and_swap(b, c, 0.25),
         _cz_and_swap(c, d, 0.125),

実行結果。これは自作量子シミュレータと一緒だ。

$ python3 quantum_fourier_transform.py
Circuit:
(0, 0): ───H───H───@───────×───H────────────@─────────×───H────────────@───────×───H───
                   │       │                │         │                │       │
(0, 1): ───H───────@^0.5───×───@────────×───@^0.5─────×───@────────×───@^0.5───×───────
                               │        │                 │        │
(1, 0): ───H───────────────────┼────────┼───@─────────×───┼────────┼───────────────────
                               │        │   │         │   │        │
(1, 1): ───H───────────────────@^0.25───×───@^0.125───×───@^0.25───×───────────────────

FinalState
[ 1.+0.j -0.-0.j -0.-0.j  0.+0.j  0.+0.j  0.+0.j -0.+0.j  0.-0.j -0.+0.j
  0.+0.j -0.-0.j  0.-0.j -0.-0.j -0.+0.j -0.-0.j -0.-0.j]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]

もう一つ周期を1つ増やしてみる。

+#        cirq.H(a),
+        cirq.H(b),
+        cirq.H(d),
+        cirq.H(c),
$ python3 quantum_fourier_transform.py
Circuit:
(0, 0): ───H───@───────×───H────────────@─────────×───H────────────@───────×───H───
               │       │                │         │                │       │
(0, 1): ───H───@^0.5───×───@────────×───@^0.5─────×───@────────×───@^0.5───×───────
                           │        │                 │        │
(1, 0): ───H───────────────┼────────┼───@─────────×───┼────────┼───────────────────
                           │        │   │         │   │        │
(1, 1): ───H───────────────@^0.25───×───@^0.125───×───@^0.25───×───────────────────

FinalState
[-0.   +0.707j  0.   -0.j    -0.444+0.088j -0.132+0.088j  0.   -0.j
 -0.   -0.j    -0.059+0.088j -0.018+0.088j -0.   -0.j     0.   -0.j
  0.018+0.088j  0.059+0.088j -0.   -0.j    -0.   -0.j     0.132+0.088j
  0.444+0.088j]
[0.707      0.         0.45263672 0.15864426 0.         0.
 0.1059481  0.08982205 0.         0.         0.08982205 0.1059481
 0.         0.         0.15864426 0.45263672]

うーん、自作量子シミュレータど結果と少し違うかなあ? 色々見てみたけどよく分からん。。。

f:id:msyksphinz:20180926011313p:plain