Skip to main content
IBM Quantum Platform

回路を可視化する

  • このページのコードは、以下の要件に基づいて開発された。 これらのバージョンまたは新しいバージョンの使用をお勧めします。

    qiskit[all]~=2.5.1
    

自分が作っている回路を見ることは、しばしば役に立つ。 Qiskit回路を表示するには、以下のオプションを使用します。

from qiskit import QuantumCircuit

量子回路を描く

QuantumCircuit クラスは、 draw() メソッドによる回路の描画や、回路オブジェクトの印刷をサポートしています。 デフォルトでは、どちらも回路図のアスキーアートバージョンをレンダリングする。

printNone を返しますが、ダイアグラムを印刷するという副作用があります。一方、 QuantumCircuit.draw は副作用なしにダイアグラムを返します。 Jupyterノートブックは各セルの最終行の出力を表示するので、同じ効果があるように見える。

# Build a quantum circuit
circuit = QuantumCircuit(3, 3)
circuit.x(1)
circuit.h(range(3))
circuit.cx(0, 1)
circuit.measure(range(3), range(3));
print(circuit)

Output:

     ┌───┐          ┌─┐   
q_0: ┤ H ├───────■──┤M├───
     ├───┤┌───┐┌─┴─┐└╥┘┌─┐
q_1: ┤ X ├┤ H ├┤ X ├─╫─┤M├
     ├───┤└┬─┬┘└───┘ ║ └╥┘
q_2: ┤ H ├─┤M├───────╫──╫─
     └───┘ └╥┘       ║  ║ 
c: 3/═══════╩════════╩══╩═
            2        0  1 
circuit.draw()

Output:

     ┌───┐          ┌─┐   
q_0: ┤ H ├───────■──┤M├───
     ├───┤┌───┐┌─┴─┐└╥┘┌─┐
q_1: ┤ X ├┤ H ├┤ X ├─╫─┤M├
     ├───┤└┬─┬┘└───┘ ║ └╥┘
q_2: ┤ H ├─┤M├───────╫──╫─
     └───┘ └╥┘       ║  ║ 
c: 3/═══════╩════════╩══╩═
            2        0  1 

代替レンダラー

テキスト出力は、回路開発中に出力を素早く確認するのに便利だが、柔軟性に欠ける。 量子回路の出力レンダラーには2つの選択肢がある。 一方は Matplotlib もう一方は LaTeX. LaTeX レンダラーには qcircuit パッケージが必要です。 output "引数に文字列 mpllatex を設定して、これらのレンダラーを選択する。

Tip

OSXユーザーは、必要な LaTeX パッケージを mactexパッケージから入手できる。

# Matplotlib drawing
circuit.draw(output="mpl")

Output:

Output of the previous code cell
# Latex drawing
circuit.draw(output="latex")

Output:

Output of the previous code cell

出力の保存

Jupyterノートブックで大規模な回路をインラインで描くと、時間がかかったり、読めなかったりすることがある。 図を直接ファイルに保存し、画像ビューアで開いて必要に応じて拡大することができます。

# Save as an image using the Matplotlib drawer
circuit.draw(output="mpl", filename="circuit-mpl.jpeg")

Output:

Output of the previous code cell
# Or save a LaTeX rendering
circuit.draw(output="latex", filename="circuit-latex.pdf")

Output:

Output of the previous code cell

制御回路図

デフォルトでは、 draw() メソッドはレンダリングした画像をオブジェクトとして返し、何も出力しません。 返される正確なクラスは、指定された出力によって異なる: 'text' (デフォルト)は TextDrawer オブジェクトを返し、 'mpl'matplotlib.Figure オブジェクトを返し、 latexPIL.Image オブジェクトを返す。 Jupyterノートブックはこれらのreturn typeを理解し、適切にレンダリングするが、Jupyterの外で実行する場合、画像は自動的に表示されない。

draw() メソッドには、出力を表示または保存するためのオプション引数がある。 指定された場合、 filename kwargはレンダリング出力を保存するパスを取ります。 また、 mpl または latex 出力を使っている場合は、 interactive kwarg を使って画像を新しいウィンドウで開くこともできます(これはノートブック内から常に機能するわけではありません)。

出力のカスタマイズ

出力によっては、回路図をカスタマイズするオプションもある。

プロットバリアを無効化し、ビット順序を反転する

最初の2つのオプションは3つのバックエンドで共有される。 ビットオーダーとバリアーを描くかどうかの両方を設定できる。 これらはそれぞれ、 reverse_bits kwargと plot_barriers kwargで設定できる。 以下の例は、どの出力レンダラーでも動作する。ここでは簡潔にするために mpl

from qiskit import QuantumRegister, ClassicalRegister

# Draw a new circuit with barriers and more registers
q_a = QuantumRegister(3, name="a")
q_b = QuantumRegister(5, name="b")
c_a = ClassicalRegister(3)
c_b = ClassicalRegister(5)

circuit = QuantumCircuit(q_a, q_b, c_a, c_b)
circuit.x(q_a[1])
circuit.x(q_b[1])
circuit.x(q_b[2])
circuit.x(q_b[4])
circuit.barrier()
circuit.h(q_a)
circuit.barrier(q_a)
circuit.h(q_b)
circuit.cswap(q_b[0], q_b[1], q_b[2])
circuit.cswap(q_b[2], q_b[3], q_b[4])
circuit.cswap(q_b[3], q_b[4], q_b[0])
circuit.barrier(q_b)
circuit.measure(q_a, c_a)
circuit.measure(q_b, c_b);
# Draw the circuit
circuit.draw(output="mpl")

Output:

Output of the previous code cell
# Draw the circuit with reversed bit order
circuit.draw(output="mpl", reverse_bits=True)

Output:

Output of the previous code cell
# Draw the circuit without barriers
circuit.draw(output="mpl", plot_barriers=False)

Output:

Output of the previous code cell

レンダラー固有のカスタマイズ

利用可能なカスタマイズオプションの中には、レンダラー固有のものもあります。

fold 引数は出力の最大幅を設定する。 text レンダラーでは、次の行に折り返す前のダイアグラムの行の長さを設定します。 mpl'レンダラーを使用する場合、これは次の行に折り返す前の(視覚的な)レイヤー数である。

mpl レンダラーには style kwarg があり、色とアウトラインを変更する。 詳細は APIドキュメントを参照のこと。

scale オプションは、 mpllatex レンダラーの出力をスケーリングする。

circuit = QuantumCircuit(1)
for _ in range(10):
    circuit.h(0)
# limit line length to 40 characters
circuit.draw(output="text", fold=40)

Output:

   ┌───┐┌───┐┌───┐┌───┐┌───┐┌───┐┌───┐»
q: ┤ H ├┤ H ├┤ H ├┤ H ├┤ H ├┤ H ├┤ H ├»
   └───┘└───┘└───┘└───┘└───┘└───┘└───┘»
«   ┌───┐┌───┐┌───┐
«q: ┤ H ├┤ H ├┤ H ├
«   └───┘└───┘└───┘
# Change the background color in mpl

style = {"backgroundcolor": "lightgreen"}
circuit.draw(output="mpl", style=style)

Output:

Output of the previous code cell
# Scale the mpl output to 1/2 the normal size
circuit.draw(output="mpl", scale=0.5)

Output:

Output of the previous code cell

スタンドアロンの回路図作成機能

回路オブジェクトのメソッドとしてではなく、自己完結型の関数で回路を描画したいアプリケーションでは、 qiskit.visualization からの public stable インターフェイスの一部である circuit_drawer() 関数を直接使用できます。 この関数は、必須引数として回路オブジェクトを受け取る以外は、 circuit.draw() メソッドと同じ動作をする。

from qiskit.visualization import circuit_drawer

circuit_drawer(circuit, output="mpl", plot_barriers=False)

Output:

Output of the previous code cell

次のステップ

推奨事項
このページは役に立ちましたか?
バグや誤字の報告、またはコンテンツの要求はGitHubで行ってください。