1
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?

More than 1 year has passed since last update.

量子機械学習 PennyLane の量子回路の表示方法

Last updated at Posted at 2023-09-24

はじめに

量子機械学習 PennyLane を使う上で、量子回路を図示するのは最初に一歩となりますので、その方法について紹介します。

このページの例は google colab 上で再現できます。

ご本家の例

qml.drawer.use_style という関数が定義されていて、この使い方のサンプルの下記のページに書かれています。

import pennylane as qml
qml.drawer.use_style('black_white')

@qml.qnode(qml.device('lightning.qubit', wires=(0,1,2,3)))
def circuit(x, z):
    qml.QFT(wires=(0,1,2,3))
    qml.Toffoli(wires=(0,1,2))
    qml.CSWAP(wires=(0,2,3))
    qml.RX(x, wires=0)
    qml.CRZ(z, wires=(3,0))
    return qml.expval(qml.PauliZ(0) @ qml.PauliZ(2))

fig, ax = qml.draw_mpl(circuit)(1.2345,1.2345)
fig.show()

基本的にはこれを使えばOKです。次のような図が生成されます。

qiita_qml_drawer_sample.png

スタイルと見た目の関係

いくつかのスタイルが用意されているので、その一覧をここでは紹介します。

用意されているスタイルは、

# 使用可能なスタイルを確認する
qml.drawer.available_styles()
('black_white',
 'black_white_dark',
 'sketch',
 'pennylane',
 'sketch_dark',
 'solarized_light',
 'solarized_dark',
 'default')

の8つあります(2023.9.25時点)。

これらを、

import pennylane as qml

for style in qml.drawer.available_styles():
  qml.drawer.use_style(style)

  @qml.qnode(qml.device('lightning.qubit', wires=(0,1,2,3)))
  def circuit(x, z):
      qml.QFT(wires=(0,1,2,3))
      qml.Toffoli(wires=(0,1,2))
      qml.CSWAP(wires=(0,2,3))
      qml.RX(x, wires=0)
      qml.CRZ(z, wires=(3,0))
      return qml.expval(qml.PauliZ(0) @ qml.PauliZ(2))

  fig, ax = qml.draw_mpl(circuit)(1.2345,1.2345)
  ax.set_title(style)
  fig.savefig(style+".png")
  fig.show()

を実行して、図を生成します。

結果を整理すると、下記のようなスタイルと図の関係になりますので、好きなデザインでプロットしましょう。

qiita_qml_drawer.png

1
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
1
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?