0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Qiskit環境構築 [uv/poetry共通] (4) シミュレータでの量子回路実行(qiskit primitives)

Last updated at Posted at 2025-01-19

概要

 ・Windows11/WSL2でのQiskit環境構築手順は需要がありそうなので記載します。
 ・IBM Community Japanの2024年ナレッジモール研究に参加した際に使用したQiskit環境の構築手順となります。
  IBM Community Japan / ナレッジモール研究
 ・Qiitaの書き方がまだわかっておらず練習を兼ねているため、今後も修正をしていきます。

本ドキュメントの範囲

 シンプルな量子回路の作成とシミュレータ実行
   (1) vscodeでファイルの作成
   (2) ライブラリのimport
   (3) シンプルな量子回路を作成
   (4) 量子回路の実機(qiskit primitives)実行
   (5) 実行結果を確率分布図で確認

(1) vscodeでファイルの作成

vscodeで新規のipynbファイルを作成したり、コードブロックを追加したり
コードを入力していきます。
qiskit001.png

(2) ライブラリのimport

最小限のライブラリをimportします。

ライブラリのimport
# ライブラリのimport
from qiskit import QuantumCircuit                   # 量子回路作成のために必要
from qiskit.primitives import StatevectorSampler    # シミュレータ実行のために必要
from qiskit.visualization import plot_distribution  # 確率分布図を表示するために必要

(3) シンプルな量子回路を作成

量子ビットq0とq1がもつれます。

量子回路の作成
# 量子回路の初期化
# 量子ビット2個 古典ビット2個を準備
circuit = QuantumCircuit(2) 

# 量子回路の組み立て
circuit.h(0)    # アダマールゲートを適用
circuit.cx(0,1) # CNOTゲートを適用

# 測定
# measure_allですべての量子ビットを測定する。
# 量子ビットq0の測定結果を古典ビットc0、q1の測定結果をc1にそれぞれ代入。
circuit.measure_all() 

# 量子回路の素描
# 量子回路をmpl形式で表示
circuit.draw(output="mpl")

aefb67db-3326-4990-a189-a7540bbdc96d.png

(4) 量子回路のシミュレータ(qiskit primitives)実行

量子回路をシミュレータ実行します。

# 実行と結果取得
# StatevectorSamplerのインスタンスを生成
sampler = StatevectorSampler()   

# 量子プログラムを実行。量子回路を1024回(デフォルト設定)を繰り返し実行する。
job = sampler.run([circuit])    

# PrimitiveResult形式の結果を取得
result = job.result()     

# PrimitiveResultから回路実行結果を抽出
counts = result[0].data.meas.get_counts()    
print(counts)

コードブロックの下に実行結果が出力されます。
qiskit002.png

(5) 実行結果を確率分布図で確認

確率分布図の表示
# 確率分布図を表示   
plot_distribution(counts)

8ddc950a-b3b8-4751-9bd0-7d0f36599f25.png

その他

いろいろ表示
# Qiskitバージョン情報
import qiskit
print(f'qiskit version : {qiskit.__version__}')

# Pythonおよび実行環境情報
import sys, platform
print(f'python version : {sys.version}')
print(f'platform : {platform.uname()}')

qiskit003.png

続きます。

Qiskit環境構築
【uv版】
(1) Linuxの導入/設定
(2) uvの導入/Python仮想環境の作成

【poetry版】
(1) Linuxの導入/設定
(2) Python仮想環境の作成

【uv/poetry共通】
(3) vscodeからのPython仮想環境接続
(4) シミュレータでの量子回路実行
(5) IBMQ実機での量子回路実行
(6) メンテナンス

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?