量子回路の構築#

このチュートリアルでは、Qamomileを使った量子回路の構築について学びます。Qamomileは主に量子最適化のために設計されていますが、任意の量子回路を構築するための強力な機能も備えています。このチュートリアルでは、Qamomile独自の中間表現を用いて、量子回路を作成・操作する方法を紹介します。

前提条件#

始める前に、以下がPython環境にインストールされていることを確認してください:

  • Qamomile

はじめに#

まずは、Qamomileから必要なモジュールをインポートしましょう:

import qamomile.core as qm

量子回路の作成#

Qamomileで量子回路を作成するには、まず量子レジスタと古典レジスタを初期化し、それらを用いてQuantumCircuitオブジェクトを作成します:

circuit = qm.circuit.QuantumCircuit(2)  # 2量子ビットの量子回路を作成する

回路へのゲートの追加#

Qamomileは多くの種類の量子ゲートをサポートしています。以下では、一般的なゲートを回路に追加してみましょう:

# 第一量子ビットにアダマールゲートをかける
circuit.h(0)

# 第一量子ビットを制御、第二量子ビットを標的としてCNOTゲートをかける
circuit.cx(0, 1)

# 第二量子ビットにXゲートをかける
circuit.x(1)

Qamomile がサポートしている量子ゲートは、以下のリンクから確認できます:
https://jij-inc.github.io/Qamomile/autoapi/core/circuit/circuit/index.html#core.circuit.circuit.QuantumCircuit

測定#

Qamomileで測定を行うには:

circuit.measure_all()

回路の可視化#

Qamomileは量子回路を可視化するためのメソッドを提供しています:

from qamomile.core.circuit.drawer import plot_quantum_circuit

plot_quantum_circuit(circuit)
../../_images/3074864574d5626597f392983924ef7cf0d92125b3ddfd22ebb08d4d089d3303.png

回路の合成#

より複雑な回路を作成するために、小さな回路を組み合わせることができます:

# 二つの独立した量子回路を作成する
circuit1 = qm.circuit.QuantumCircuit(2)
circuit1.h(0)
circuit1.h(1)

circuit2 = qm.circuit.QuantumCircuit(2)
circuit2.cx(0, 1)


circuit1.append(circuit2)

plot_quantum_circuit(circuit1)
../../_images/c723f06624e97c2f252e5b403292e876c74c810380afbbdbf884c7278466ac87.png

decompose_levelを指定することで、回路をより詳細に表示することができます。

plot_quantum_circuit(circuit1, decompose_level=1)
../../_images/b5f19f2e7a1cc18105bb316b87cc70731631fed7ff6609794167e7ff149d8da0.png

量子回路の実行#

Qamomile自体には量子回路を実行する機能はありませんが、回路を他の量子SDKに変換して実行することができます。

Qiskitバックエンド#

以下はQamomileの回路をQiskitを用いて実行する方法です:

1- まず、必要なモジュールをインポートします:

from qamomile.qiskit import QiskitTranspiler
import qiskit.primitives as qk_pr

2- QiskitTranspilerオブジェクトを作成します:

qk_transpiler = QiskitTranspiler()

3- Qamomileの回路をQiskitの回路に変換(トランスパイル)

qk_circuit = qk_transpiler.transpile_circuit(circuit)

4- 次に、Qiskitの機能を使用して回路を実行できます。たとえば、シミュレーター上で回路を実行するには:

simulator = qk_pr.StatevectorSampler()
job = simulator.run([qk_circuit], shots=1000)
result = job.result()

5- 結果はQiskitのツールを使って処理および解析できます:

result[0].data['c'].get_counts()
{'01': 479, '10': 521}

QURI-PartsとQulacsバックエンド#

以下は、QURI-PartsとQulacsを使用してQamomileの量子回路を実行する方法です。

Qiskitと同様に、回路を変換して量子回路を実行します。

from qamomile.quri_parts import QuriPartsTranspiler

# 量子回路をQURI-Partsへ変換する
qp_transpiler = QuriPartsTranspiler()
qp_circuit = qp_transpiler.transpile_circuit(circuit)

これで、Qulacsを使って回路を実行できます。

from quri_parts.core.state import quantum_state, apply_circuit
from quri_parts.qulacs.sampler import create_qulacs_vector_sampler

sampler = create_qulacs_vector_sampler()
sampling_result = sampler(qp_circuit, 1000)
sampling_result
Counter({2: 518, 1: 482})

Pennylaneバックエンド#

以下は、Qamomileの回路をPennylaneを使って実行する方法です。

Qiskitと同様に、量子回路を変換して実行します。

from qamomile.pennylane import PennylaneTranspiler

# 量子回路をPennylaneへ変換する
qml_transpiler = PennylaneTranspiler()
qml_circuit_func = qml_transpiler.transpile_circuit(circuit)
import pennylane as qml

dev = qml.device("default.qubit", wires=circuit.num_qubits, shots=1000)
@qml.qnode(dev)
def qml_circuit(params):
    qml_circuit_func(params)
        
    return qml.counts()
qml_circuit(None)
{np.str_('01'): np.int64(493), np.str_('10'): np.int64(507)}

結論#

このチュートリアルでは、Qamomileの中間表現を用いた量子回路の構築方法と、それらをQiskitやQURI-Partsを使って実行する方法について紹介しました。Qamomileは主に量子最適化に焦点を当てていますが、これらの回路構築機能によって、さまざまな量子計算タスクに対応する柔軟性が得られます。Qamomileでの量子コンピューティングをお楽しみください!