概要
- Qiskit について, サンプルプログラムを動かしてみた.
- 動作環境は, Google Colaboratory で行った
- 実行プログラム, 解説は, 下記の参照サイトをご覧ください
感想
- 本家のサイトにあるように, 量子ゲートの解説が, 非常に豊富だと驚いた.
- 時間を見つけて, 今後も, ドキュメントの残りの部分を進めていこうと思う.
Operators
- operators
- operators addition and subtraction etc
- closer look
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
[編集内容(operators)] from qiskit.aqua.operators import I, X, Y, Z from qiskit.aqua.operators import CX, S, H, T, Swap, CZ from qiskit.aqua.operators import Zero, One, Plus, Minus # One qubit Pauli operators. print(I, X, Y, Z) # Clifford + T etc. print(CX) print(S) print(H) print(T) print(Swap) print(CZ) # One qubit states. print(Zero) print(One) print(Plus) print(Minus) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
[出力結果] I X Y Z q_0: ──■── ┌─┴─┐ q_1: ┤ X ├ └───┘ ┌───┐ q_0: ┤ S ├ └───┘ ┌───┐ q_0: ┤ H ├ └───┘ ┌───┐ q_0: ┤ T ├ └───┘ q_0: ─X─ │ q_1: ─X─ q_0: ─■─ │ q_1: ─■─ DictStateFn({'0': 1}) DictStateFn({'1': 1}) CircuitStateFn( ┌───┐ q_0: ┤ H ├ └───┘ ) CircuitStateFn( ┌───┐┌───┐ q_0: ┤ X ├┤ H ├ └───┘└───┘ ) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
[編集内容(operators addition and subtraction etc)] from qiskit.aqua.operators import I, X, Y, Z # addition. print(X + Y) # subtraction. print(X - Y) # multiply. # ValueError: Operators can only be scalar multiplied by float or complex, # not Y of type <class 'qiskit.aqua.operators.primitive_ops.pauli_op.PauliOp'>. # print(X * Y) # division. # TypeError: unsupported operand type(s) for /: 'int' and 'PauliOp' # print(X / Y) # tensor products print(X ^ Y) # composition print(X @ Y) # other print(2 * (3 * X) @ (4 * Y) ^ (5 * Z)) print(I @ I @ I) print(I ^ I ^ I) print((X + Y - Z) @ (Y + Z - X) ^ (Z + X - Y)) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
[出力結果] SummedOp([ X, Y ]) SummedOp([ X, -1.0 * Y ]) XY 1j * Z 120j * ZZ I III TensoredOp([ ComposedOp([ SummedOp([ X, Y, -1.0 * Z ]), SummedOp([ Y, Z, -1.0 * X ]) ]), SummedOp([ Z, X, -1.0 * Y ]) ]) |
1 2 3 4 5 6 7 |
[編集内容(closer look)] from qiskit.aqua.operators import I, X, Y, Z # コメントアウトの箇所を変えながら確認. # (1 * I, 2 * X, 3 * Y, 4 * Z) # (X + Y, Y @ Z, Z ^ X) # (2 * I + 3 * X - 4 * Y - 5 * Z, X ^ Y @ Z) |
1 2 3 4 5 6 7 8 9 10 |
[出力結果] (PauliOp(Pauli('I'), coeff=1.0), PauliOp(Pauli('X'), coeff=2.0), PauliOp(Pauli('Y'), coeff=3.0), PauliOp(Pauli('Z'), coeff=4.0)) (SummedOp([PauliOp(Pauli('X'), coeff=1.0), PauliOp(Pauli('Y'), coeff=1.0)], coeff=1.0, abelian=False), PauliOp(Pauli('X'), coeff=1j), PauliOp(Pauli('ZX'), coeff=1.0)) (SummedOp([PauliOp(Pauli('I'), coeff=2.0), PauliOp(Pauli('X'), coeff=3.0), PauliOp(Pauli('Y'), coeff=-4.0), PauliOp(Pauli('Z'), coeff=-5.0)], coeff=1.0, abelian=False), PauliOp(Pauli('XX'), coeff=1j)) |
State Functions and Measurements
- StateFn etc
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
[編集内容(StateFn etc)] from qiskit.aqua.operators import (StateFn, Zero, One, Plus, Minus, H, DictStateFn, VectorStateFn, CircuitStateFn, OperatorStateFn) # One qubit states. print(Zero) print(One) print(Plus) print(Minus) # eval. print(Zero.eval('0')) print(Zero.eval('1')) print(Zero.eval('2')) print(One.eval('0')) print(One.eval('1')) print(One.eval('2')) print(Plus.eval('0')) print(Plus.eval('1')) # print(Plus.eval('2')) # ValueError: invalid literal for int() with base 2: '2' print(Minus.eval('0')) print(Minus.eval('1')) # print(Minus.eval('2')) # ValueError: invalid literal for int() with base 2: '2' # adjoint. # コメントアウトの箇所を変えながら確認. # Zero.adjoint() # equal to ~Zero # One.adjoint() # equal to ~One # Plus.adjoint() # Minus.adjoint() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
[出力結果] DictStateFn({'0': 1}) DictStateFn({'1': 1}) CircuitStateFn( ┌───┐ q_0: ┤ H ├ └───┘ ) CircuitStateFn( ┌───┐┌───┐ q_0: ┤ X ├┤ H ├ └───┘└───┘ ) 1.0 0.0 0.0 0.0 1.0 0.0 (0.7071067811865475+0j) (0.7071067811865475+0j) (0.7071067811865475-8.7e-17j) (-0.7071067811865475+8.7e-17j) DictStateFn({'0': 1}, coeff=1.0, is_measurement=True) DictStateFn({'1': 1}, coeff=1.0, is_measurement=True) CircuitStateFn(<qiskit.circuit.quantumcircuit.QuantumCircuit object at 0x7fc0b997f8d0>, coeff=1.0, is_measurement=True) CircuitStateFn(<qiskit.circuit.quantumcircuit.QuantumCircuit object at 0x7fc0b9990690>, coeff=1.0, is_measurement=True) |