python:qiskit
This is an old revision of the document!
Table of Contents
概要
Qiskit 1.0を入れた記録
WSL
WSLにインストールしたJupyter Labで作業できるようにします。 インストールにはPIPを使いました。
$ sudo apt install python3-pip
Jupyter Labの導入
- Jupyter Labを入れます。
$ pip install jupyterlab
- $HOME/.local/binに導入されるので、パスを通します
$ sed -i '$aexport PATH="$HOME/.local/bin:$PATH"' .bashrc $ source ~/.bashrc
- WindowsのブラウザでJupyter Labが開けるようにします。設定ファイルを作成します。
$ jupyter lab --generate-config
- 設定ファイルを開きます
$ vi ~/.jupyter/jupyter_lab_config.py
- 以下の文を追加します。
# c.ServerApp.use_redirect_file = True c.ServerApp.use_redirect_file = False # c.ServerApp.browser = '' c.ServerApp.browser = '/mnt/c/Program\ Files\ \(x86\)/Microsoft/Edge/Application/msedge.exe %s'
- Jupyter Labの起動は次のようにします
$ jupyter lab
- Windowsのブラウザが自動的に開き、Jupyter Labが実行されます。
- 終了は、コマンドラインで Ctrl+c です。
Qiskitの導入
- Qiskitと、その他必要なものを入れます。
$ pip install 'qiskit>=1' pandas matplotlib pylatexenc
サンプルプログラム
Qiskitを実際に動かしてみます。 Jupyter Labを開いて、新しいノートブックを作成します。
- Qiskitを呼び出します。
from qiskit import *
- 2量子ビットの量子レジスタと古典レジスタを作成します
qr=QuantumRegister(2) cr=ClassicalRegister(2) circuit=QuantumCircuit(qr,cr)
- 0番目の量子レジスタにアダマールゲートを作用させたあと、0番目の量子レジスタを制御ビットとするCNOTゲートを作用させ、最後に測定する量子回路を考えます
circuit.h(qr[0]) circuit.cx(qr[0], qr[1]) circuit.measure(qr,cr)
- Matplotlibで量子回路をプロットしてみます
%matplotlib inline circuit.draw(output='mpl')
うまくいっていれば、量子回路が表示されるはずです。
python/qiskit.1710361278.txt.gz · Last modified: 2024/03/14 05:21 by koudai