概要
- Python の 練習を兼ねて, 量子コンピュータのフレームワーク(Google社)のチュートリアルを確認してみた
- 動作環境は, Google Colaboratory で行った
感想
- 量子ゲートに, たくさん種類があるので, だいぶ混乱したように思う.
- 時間を見つけて, 今後も, チュートリアルの残りの部分を進めていこうと思う.
Gates and operations
- Gates
- Operation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
[編集内容(Gates)] import cirq # Example gates hadamard_gate, not_gate, toffoli_gate, swap_gate, cz_gate = cirq.H, cirq.CNOT, cirq.CCNOT, cirq.SWAP, cirq.CZ s_gate, t_gate = cirq.S, cirq.T pauli_x, pauli_y, pauli_z = cirq.X, cirq.Y, cirq.Z # Using exponentiation to get square root gates sqrt_x_gate, sqrt_y_gate, sqrt_z_gate = cirq.X**0.5, cirq.Y**0.5, cirq.Z**0.5 sqrt_iswap = cirq.ISWAP**0.5 # Some gates can also take parameters sqrt_sqrt_x, sqrt_sqrt_y, sqrt_sqrt_z = cirq.XPowGate(exponent=0.25), cirq.YPowGate(exponent=0.25), cirq.ZPowGate(exponent=0.25) print(hadamard_gate, not_gate, toffoli_gate, swap_gate, cz_gate) print(s_gate, t_gate) print(pauli_x, pauli_y, pauli_z) print(sqrt_x_gate, sqrt_y_gate, sqrt_z_gate) print(sqrt_iswap) print(sqrt_sqrt_x, sqrt_sqrt_y, sqrt_sqrt_z) |
1 2 3 4 5 6 7 |
[出力結果] H CNOT TOFFOLI SWAP CZ S T X Y Z X**0.5 Y**0.5 S ISWAP**0.5 X**0.25 Y**0.25 T |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
[編集内容(Operation)] import cirq # Example operations q0, q1, q2, q3, q4 = cirq.LineQubit.range(5) x_op, y_op, z_op, h_op, s_op = cirq.X(q0), cirq.Y(q1), cirq.Z(q2), cirq.H(q3), cirq.S(q4) # https://quantumai.google/reference/python/cirq/ops/CNOT # -> cirq.CNOT は, [[1 . . .], [. 1 . .], [. . . 1], [. . 1 .]] に 等しいとのこと. not_op = cirq.CNOT(q0, q1) # https://quantumai.google/reference/python/cirq/ops/SWAP # -> cirq.SWAP は, [[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1]] に等しいとのこと. swap_op = cirq.SWAP(q2, q3) # https://quantumai.google/reference/python/cirq/ops/ISWAP # -> cirq.ISWAP は, [[1, 0, 0, 0], [0, 0, i, 0], [0, i, 0, 0], [0, 0, 0, 1]] に等しいとのこと. iswap_op, sqrt_iswap_op = cirq.ISWAP(q0, q1), sqrt_iswap(q3, q4) print(q0, q1, q2, q3, q4) print(x_op, y_op, z_op, h_op, s_op) print(not_op) print(swap_op) print(iswap_op, sqrt_iswap_op) |
1 2 3 4 5 6 |
[出力結果] 0 1 2 3 4 X(0) Y(1) Z(2) H(3) S(4) CNOT(0, 1) SWAP(2, 3) ISWAP(0, 1) ISWAP**0.5(3, 4) |
Circuits and moments
- Hadamard
- SWAP
- Moment
- Pauli X
- Pauli Y
- Pauli Z
- Controlled Z
- Controlled NOT
- Controlled ISWAP
- Controlled TOFFOLI
- Controlled CCZ
1 2 3 4 5 6 7 8 9 10 |
[編集内容(cirq.H)] import cirq circuit = cirq.Circuit() # You can create a circuit by appending to it circuit.append(cirq.H(q) for q in cirq.LineQubit.range(3)) # All of the gates are put into the same Moment since none overlap print(circuit) |
1 2 3 4 5 6 |
[出力結果] 0: ───H─── 1: ───H─── 2: ───H─── |
1 2 3 4 5 6 7 |
[編集内容(cirq.SWAP)] import cirq circuit = cirq.Circuit() # We can also create a circuit directly as well: print(cirq.Circuit(cirq.SWAP(q, q + 1) for q in cirq.LineQubit.range(3))) |
1 2 3 4 5 6 7 8 9 |
[出力結果] ※ 実際の出力を, Copy & Paste すると, 縦線がズレたため手動で修正(以降, 縦線ズレた場合は同様に対応). 0: ───×─────────── │ 1: ───×───×─────── │ 2: ───────×───×─── │ 3: ───────────×─── |
1 2 3 4 5 6 7 |
[編集内容(cirq.Moment)] import cirq circuit = cirq.Circuit() # Creates each gate in a separate moment. print(cirq.Circuit(cirq.Moment([cirq.H(q)]) for q in cirq.LineQubit.range(3))) |
1 2 3 4 5 6 7 |
[出力結果] ※ Moment 使うと, 右方向にスライドしていくらしい. 0: ───H─────────── 1: ───────H─────── 2: ───────────H─── |
1 2 3 4 5 6 |
[編集内容(cirq.X)] import cirq circuit = cirq.Circuit() circuit.append(cirq.X(q) for q in cirq.LineQubit.range(5)) print(circuit) |
1 2 3 4 5 6 7 8 9 10 |
[出力結果] 0: ───X─── 1: ───X─── 2: ───X─── 3: ───X─── 4: ───X─── |
1 2 3 4 5 6 |
[編集内容(cirq.Y)] import cirq circuit = cirq.Circuit() circuit.append(cirq.X(q) for q in cirq.LineQubit.range(0)) print(circuit) |
1 2 |
[出力結果] ※なし |
1 2 3 4 5 6 |
[編集内容(cirq.Z)] import cirq circuit = cirq.Circuit() circuit.append(cirq.Z(q) for q in cirq.LineQubit.range(1)) print(circuit) |
1 2 |
[出力結果] 0: ───Z─── |
1 2 3 4 5 6 |
[編集内容(cirq.CZ)] import cirq circuit = cirq.Circuit() circuit.append(cirq.CZ(q, q + 1) for q in cirq.LineQubit.range(2)) print(circuit) |
1 2 3 4 5 6 |
[出力結果] 0: ───@─────── │ 1: ───@───@─── │ 2: ───────@─── |
1 2 3 4 5 |
[編集内容(cirq.CNOT)] import cirq circuit = cirq.Circuit() print(cirq.Circuit(cirq.CNOT(q, q + 1) for q in cirq.LineQubit.range(2))) |
1 2 3 4 5 6 |
[出力結果] 0: ───@─────── │ 1: ───X───@─── │ 2: ───────X─── |
1 2 3 4 5 |
[編集内容(cirq.ISWAP)] import cirq circuit = cirq.Circuit() print(cirq.Circuit(cirq.ISWAP(q, q + 1) for q in cirq.LineQubit.range(7))) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
[出力結果] 0: ───iSwap─────────────────────────────────────────────────── │ 1: ───iSwap───iSwap─────────────────────────────────────────── │ 2: ───────────iSwap───iSwap─────────────────────────────────── │ 3: ───────────────────iSwap───iSwap─────────────────────────── │ 4: ───────────────────────────iSwap───iSwap─────────────────── │ 5: ───────────────────────────────────iSwap───iSwap─────────── │ 6: ───────────────────────────────────────────iSwap───iSwap─── │ 7: ───────────────────────────────────────────────────iSwap─── |
1 2 3 4 5 |
[編集内容(cirq.CCNOT)] import cirq circuit = cirq.Circuit() print(cirq.Circuit(cirq.CCNOT(q, q + 1, q + 2) for q in cirq.LineQubit.range(5))) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
[出力結果] 0: ───@─────────────────── │ 1: ───@───@─────────────── │ │ 2: ───X───@───@─────────── │ │ 3: ───────X───@───@─────── │ │ 4: ───────────X───@───@─── │ │ 5: ───────────────X───@─── │ 6: ───────────────────X─── |
1 2 3 4 5 |
[編集内容(cirq.CCZ)] import cirq circuit = cirq.Circuit() print(cirq.Circuit(cirq.CCZ(q, q + 1, q + 2) for q in cirq.LineQubit.range(3))) |
1 2 3 4 5 6 7 8 9 10 |
[出力結果] 0: ───@─────────── │ 1: ───@───@─────── │ │ 2: ───@───@───@─── │ │ 3: ───────@───@─── │ 4: ───────────@─── |