Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

リソース推定

量子カーネルを実機で実行する前に、必要なリソース(量子ビット数、ゲート数等)を把握しておきたい場合や、そもそも定義した量子カーネルを実行するために必要なリソースを知りたい場合があります。Qamomileのestimate_resources()量子カーネルを実行せずにリソース推定が可能です。具体的な(パラメータ固定の)量子カーネルにも、シンボリック(パラメータ付き)な量子カーネルにも対応しています。

この章では以下を扱います:

  • 固定量子カーネルの基本的なリソース推定

  • パラメータ付き量子カーネルのシンボリックなリソース推定

  • ResourceEstimateフィールドリファレンス

  • .substitute()によるスケーリング分析

# 最新のQamomileをpipからインストールします!
# !pip install qamomile
import qamomile.circuit as qmc

固定量子カーネルのリソース推定

パラメータを持たない量子カーネルに対しては、estimate_resources()は具体的な数値を返します。

@qmc.qkernel
def fixed_circuit() -> qmc.Vector[qmc.Bit]:
    q = qmc.qubit_array(3, name="q")

    q[0] = qmc.h(q[0])
    q[0], q[1] = qmc.cx(q[0], q[1])
    q[1], q[2] = qmc.cx(q[1], q[2])

    return qmc.measure(q)
fixed_circuit.draw()
<Figure size 785.5x256 with 1 Axes>
est = fixed_circuit.estimate_resources()
print("qubits:", est.qubits)
print("total gates:", est.gates.total)
print("single-qubit gates:", est.gates.single_qubit)
print("two-qubit gates:", est.gates.two_qubit)
qubits: 3
total gates: 3
single-qubit gates: 1
two-qubit gates: 2

シンボリックなリソース推定

量子カーネルに未バインドのパラメータ(例:n: qmc.UInt)がある場合、estimate_resources()SymPy式を返します。特定の値を選ばなくてもコストのスケーリングが分かります。

@qmc.qkernel
def scalable_circuit(n: qmc.UInt, theta: qmc.Float) -> qmc.Vector[qmc.Bit]:
    q = qmc.qubit_array(n, name="q")

    for i in qmc.range(n):
        q[i] = qmc.h(q[i])
        q[i] = qmc.ry(q[i], theta)

    for i in qmc.range(n - 1):
        q[i], q[i + 1] = qmc.cx(q[i], q[i + 1])

    return qmc.measure(q)
scalable_circuit.draw(n=4, fold_loops=False)
<Figure size 1044.5x336 with 1 Axes>
est = scalable_circuit.estimate_resources()
print("qubits:", est.qubits)
print("total gates:", est.gates.total)
print("single-qubit gates:", est.gates.single_qubit)
print("two-qubit gates:", est.gates.two_qubit)
print("rotation gates:", est.gates.rotation_gates)
print("parameters:", est.parameters)
qubits: n
total gates: 3*n - 1
single-qubit gates: 2*n
two-qubit gates: n - 1
rotation gates: n
parameters: {'n': n}

出力には、量子ビット数を表すnや総ゲート数を表す3*n - 1のようなSymPy式が含まれます。これらは近似ではなく厳密な値です。

ResourceEstimateフィールドリファレンス

フィールド説明
est.qubits論理量子ビット数
est.gates.total総ゲート数
est.gates.single_qubit単一量子ビットゲート数
est.gates.two_qubit2量子ビットゲート数
est.gates.multi_qubit多量子ビットゲート数(3量子ビット以上)
est.gates.t_gatesTゲート数
est.gates.clifford_gatesCliffordゲート数
est.gates.rotation_gates回転ゲート数
est.gates.oracle_callsオラクル呼び出し回数(名前別の辞書)
est.parametersシンボル名からSymPyシンボルへの辞書

すべてのフィールドはSymPy式です。固定量子カーネルの場合は通常の整数に評価されます。

.substitute()によるスケーリング分析

シンボリック式は数式を示してくれますが、特定のサイズでの具体的な数値も確認したい場合.substitute()で評価できます:

for n_val in [4, 8, 16, 32]:
    c = est.substitute(n=n_val)
    print(
        f"n={n_val:2d}: {int(c.gates.total):>3} gates total, {int(c.gates.two_qubit):>2} two-qubit"
    )
n= 4:  11 gates total,  3 two-qubit
n= 8:  23 gates total,  7 two-qubit
n=16:  47 gates total, 15 two-qubit
n=32:  95 gates total, 31 two-qubit

まとめ

  • estimate_resources()は実行せずに量子ビット数とゲートコストを算出します。

  • パラメータ付き量子カーネルでは、結果は厳密なスケーリングを示すSymPy式になります。

  • .substitute(n=...)で特定のサイズに代入し、実行可能性を確認できます。

次へ実行モデルsample()run()、オブザーバブル、ビット順序について。