QamomileにおけるQiskitTranspilerの使用方法#

このチュートリアルでは、QamomileにおけるQiskitTranspilerの使い方を紹介し、ユーザーが効果的に活用するための主要な例を示します。

ハミルトニアンのQiskitへの変換#

まずはテスト用のハミルトニアンを定義し、それを変換器(トランスパイラ)を用いてQiskit互換の形式に変換します。このステップでは、私たちのライブラリで定義されたハミルトニアンが、Qiskit に認識される演算子へとスムーズに変換される様子を示します。

import jijmodeling as jm
import matplotlib.pyplot as plt
import networkx as nx
import numpy as np
import qiskit.primitives as qk_pr
from scipy.optimize import minimize

import qamomile
from qamomile.core.operator import Hamiltonian, Pauli, X, Y, Z
from qamomile.core.circuit import QuantumCircuit as QamomileCircuit
from qamomile.core.circuit import (
    QuantumCircuit,
    SingleQubitGate,
    TwoQubitGate,
    ParametricSingleQubitGate,
    ParametricTwoQubitGate,
    SingleQubitGateType,
    TwoQubitGateType,
    ParametricSingleQubitGateType,
    ParametricTwoQubitGateType,
    Parameter
)
from qamomile.core.circuit.drawer import plot_quantum_circuit
from qamomile.qiskit.transpiler import QiskitTranspiler

このスニペットでは、X・Y・Z などのパウリ演算子を使って独自に定義したハミルトニアンから始め、QiskitTranspilerを用いてQiskitに直接対応する形式へと変換します。 qiskit_hamiltonianを出力することで、その変換が正しく行われたかを確認できます。

hamiltonian = Hamiltonian()
hamiltonian += X(0)*Z(1)
hamiltonian += Y(0)*Y(1)*Z(2)*X(3)*X(4)

transpiler = QiskitTranspiler()
qiskit_hamiltonian = transpiler.transpile_hamiltonian(hamiltonian)

qiskit_hamiltonian
SparsePauliOp(['IIIZX', 'XXZYY'],
              coeffs=[1.+0.j, 1.+0.j])

パラメータ化された量子回路の構築#

次に、QamomileCircuitを使ってパラメータ化された量子回路を構築します。ここでは、1量子ビット回転(rx, ry, rz)やその制御付きバージョン(crx, crz, cry)、さらに2量子ビットのエンタングルゲート(rxx, ryy, rzz)を含めます。パラメータ(theta, beta, gamma)を使うことで、変分的な柔軟な調整が可能になります。

qc = QamomileCircuit(3)
theta = Parameter("theta")
beta = Parameter("beta")
gamma = Parameter("gamma")

qc.rx(theta, 0)
qc.ry(beta, 1)
qc.rz(gamma, 2)
qc.crx(gamma, 0 ,1)
qc.crz(theta, 1 ,2)
qc.cry(beta, 2 ,0)
qc.rxx(gamma, 0 ,1)
qc.ryy(theta, 1 ,2)
qc.rzz(beta, 2 ,0)

transpiler = QiskitTranspiler()
qk_circuit = transpiler.transpile_circuit(qc)

MaxCut問題の定式化と量子形式への変換#

以下では、古典的な最適化問題であるMaxCutをどのようにしてイジング形式のハミルトニアンにエンコードするかを示します。次に、QAOAスタイルのアンザッツ回路を構築し、それを実行および最適化することで、MaxCutインスタンスの解を求めようとします。

def maxcut_problem() -> jm.Problem:
    V = jm.Placeholder("V")
    E = jm.Placeholder("E", ndim=2)
    x = jm.BinaryVar("x", shape=(V,))
    e = jm.Element("e", belong_to=E)
    i = jm.Element("i", belong_to=V)
    j = jm.Element("j", belong_to=V)

    problem = jm.Problem("Maxcut", sense=jm.ProblemSense.MAXIMIZE)
    si = 2 * x[e[0]] - 1
    sj = 2 * x[e[1]] - 1
    si.set_latex("s_{e[0]}")
    sj.set_latex("s_{e[1]}")
    obj = 1 / 2 * jm.sum(e, (1 - si * sj))
    problem += obj
    return problem

def maxcut_instance():
    # MaxCutインスタンスのために簡単なグラフを構築します。
    G = nx.Graph()
    G.add_nodes_from([0, 1, 2, 3, 4])
    G.add_edges_from([(0, 1), (0, 4), (1, 2), (1, 3), (2, 3), (3, 4)])
    E = [list(edge) for edge in G.edges]
    instance_data = {"V": G.number_of_nodes(), "E": E}
    pos = {0: (1, 1), 1: (0, 1), 2: (-1, 0.5), 3: (0, 0), 4: (1, 0)}
    nx.draw_networkx(G, pos, node_size=500)
    return instance_data

problem = maxcut_problem()
instance_data = maxcut_instance()
interpreter = jm.Interpreter(instance_data)
compiled_instance = interpreter.eval_problem(problem)

# コンパイルした問題をQAOA形式へ変換する
qaoa_converter = qamomile.core.qaoa.QAOAConverter(compiled_instance)
qaoa_converter.ising_encode()

p = 5
qaoa_hamiltonian = qaoa_converter.get_cost_hamiltonian()
qaoa_circuit = qaoa_converter.get_qaoa_ansatz(p=p)
../../_images/56c1f0b128695a82df847d86d91244673b58c15477567e34c02d6ba211d371cd.png
plot_quantum_circuit(qaoa_circuit)
../../_images/21b8b00fc60533d1684518de75aa64c3f25e1bd80e028c50b617b6e9ad8458e2.png

これで、MaxCut問題をQAOAのようなアルゴリズムに適したコストハミルトニアンに変換しました。パラメータpは、問題ハミルトニアンとミキサーハミルトニアンのレイヤー数を決定します。各レイヤーのパラメータは変分的であり、期待値を最小化するように調整されます。理想的には、これによりMaxCutインスタンスの良い解が得られます。

QiskitでのQAOA回路のトランスパイルと実行#

QAOA回路とハミルトニアンが定義されたので、再びトランスパイラを使用して、QAOA回路とコストハミルトニアンをQiskit形式に変換します:

transpiler = QiskitTranspiler()
# QAOA回路をQiskit用へ変換する
qk_circuit = transpiler.transpile_circuit(qaoa_circuit)

# QAOAハミルトニアンをQiskit用へ変換する
qk_hamiltonian = transpiler.transpile_hamiltonian(qaoa_hamiltonian)
qk_hamiltonian
SparsePauliOp(['IIIZZ', 'ZIIIZ', 'IIZZI', 'IZIZI', 'IZZII', 'ZZIII', 'IIIII'],
              coeffs=[ 0.5+0.j,  0.5+0.j,  0.5+0.j,  0.5+0.j,  0.5+0.j,  0.5+0.j, -3. +0.j])

ここで、qk_circuitはQamomileのQAOAアンザッツから生成されたQiskit回路であり、qk_hamiltonianは数理モデルに基づいてQamomileのハミルトニアンから構築されたものです。

パラメータの最適化#

最後に、変分パラメータを最適化し、より良い結果(例えば、MaxCut目的関数に対する低コスト)をもたらすパラメータを見つけることを試みます。

cost_history = []
# コスト推定関数
estimator = qk_pr.StatevectorEstimator()


def estimate_cost(param_values):
    try:
        job = estimator.run([(qk_circuit, qk_hamiltonian, param_values)])
        result = job.result()[0]
        cost = result.data["evs"]
        cost_history.append(cost)
        return cost
    except Exception as e:
        print(f"Error during cost estimation: {e}")
        return np.inf


# 初期パラメータを作成する
initial_params = np.random.uniform(low=-np.pi / 4, high=np.pi / 4, size=2 * p)

# QAOA最適化を実行する
result = minimize(
    estimate_cost,
    initial_params,
    method="COBYLA",
    options={"maxiter": 2000},
)
print(result)
 message: Return from COBYLA because the objective function has been evaluated MAXFUN times.
 success: False
  status: 3
     fun: -4.826405374825612
       x: [ 3.991e-02  1.035e-01  6.439e-01 -1.082e+00 -2.025e-01
           -4.758e-01 -2.785e-01  1.494e+00 -2.191e-01  1.312e+00]
    nfev: 2000
   maxcv: 0.0

結果の可視化#

plt.title("Change of Cost", fontsize=15)
plt.xlabel("Iteration", fontsize=15)
plt.ylabel("Cost", fontsize=15)
plt.xscale("log")
plt.xlim(1, result.nfev)
plt.plot(cost_history, label="Cost", color="#2696EB")
plt.show()
../../_images/ebb6ab9db758e878d6cacb081acf81f2facd8a739b89bc2afe0970e030ab43ec.png

最適化されたパラメータが得られたら、QiskitのStatevectorSamplerを使用して、パラメータ化された量子回路からサンプリングし、回路の測定結果(カウント)を取得します。

# 最適化されたQAOA回路を実行する
sampler = qk_pr.StatevectorSampler()
qk_circuit.measure_all()
job = sampler.run([(qk_circuit, result.x)], shots=10000)
job_result = job.result()[0]
qaoa_counts = job_result.data["meas"]
# プロットのためのデータを準備する
keys = list(qaoa_counts.get_counts().keys())
values = list(qaoa_counts.get_counts().values())

# プロットする
plt.figure(figsize=(12, 6))
plt.bar(keys, values, width=0.6)
plt.xlabel("State", fontsize=12)
plt.ylabel("Frequency", fontsize=12)
plt.title("Distribution of State", fontsize=14)
plt.xticks(rotation=90, fontsize=10)
plt.tight_layout()
plt.show()
../../_images/a2858cea309e8fa7f87cf6450e28fc1ddbedf52b5ae7543972533490d61372a8.png

結果の評価#

前に得られた qaoa_countsから、qaoa_converter.decodeを使ってサンプルセットに変換することができます。このサンプルセットから実行可能な解のみを選択し、目的関数の値の分布を調べます。

sampleset = qaoa_converter.decode(transpiler, qaoa_counts) 
feasible_ids = sampleset.summary.query("feasible == True").index
energies = []
frequencies = []
# Create a dictionary to group energies and count their frequencies
from collections import defaultdict
energy_freq = defaultdict(int)

for sample_id in sampleset.sample_ids:
    sample = sampleset.get(sample_id)
    if sample_id in feasible_ids:
        energy_freq[sample.objective] += 1

energies = list(energy_freq.keys())
frequencies = list(energy_freq.values())

plt.bar(energies, frequencies, width=0.5)
plt.title("cost distribution of feasible solution", fontsize=15)
plt.ylabel("Frequency", fontsize=15)
plt.xlabel("Energy", fontsize=15)
plt.show()
../../_images/5725fd404bdc20cabb65d80977655e5f7460541ec4419a9cfa268b85390c69ed.png