User Tools

Site Tools


Sidebar

python:qiskit

This is an old revision of the document!


概要

Qiskit 1.0を入れた記録

WSL

QiskitをWSLからJupyterNoteで作業できるようにする。 インストールにはPIPを使いました。

$ sudo apt install python3-pip

Jupyter Labの導入

  1. Jupyter Labを入れます。
$ pip install jupyterlab
  1. $HOME/.local/binに導入されるので、パスを通します
$ sed -i '$aexport PATH="$HOME/.local/bin:$PATH"' .bashrc
$ source ~/.bashrc
  1. WindowsのブラウザでJupyter Labが開けるようにします。設定ファイルを作成します。
$ jupyter lab --generate-config
  1. 設定ファイルを開きます
$ vi ~/.jupyter/jupyter_lab_config.py
  1. 以下の文を追加します。
# 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'
  1. Jupyter Labの起動は次のようにします
$ jupyter lab
  • Windowsのブラウザが開いて、Jupyter Labが実行されていることを確認してください。
  • 終了は、コマンドラインで Ctrl+c です。

Qiskitの導入

  1. Qiskitと、その他必要なものを入れます。
$ pip install 'qiskit>=1' pandas matplotlib pylatexenc

サンプルプログラム

Qiskitを実際に動かしてみます。 Jupyter Labを開いて、新しいノートブックを作成します。

  1. Qiskitを呼び出します。
from qiskit import *
  1. 2量子ビットの量子レジスタと古典レジスタを作成します
qr=QuantumRegister(2)
cr=ClassicalRegister(2)
circuit=QuantumCircuit(qr,cr)
  1. 0番目の量子レジスタにアダマールゲートを作用させたあと、0番目の量子レジスタを制御ビットとするCNOTゲートを作用させます
circuit.h(qr[0])
circuit.cx(qr[0], qr[1]);
circuit.draw(output='mpl')
  1. Matplotlibで量子回路をプロットしてみます
%matplotlib inline
circuit.draw(output='mpl')

うまくいっていれば、量子回路が表示されるはずです。

python/qiskit.1710359498.txt.gz · Last modified: 2024/03/14 04:51 by koudai