User Tools

Site Tools


python:qiskit:インストール

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

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