Amazon Braket の量子タスクの例 - Amazon Braket

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

Amazon Braket の量子タスクの例

このセクションでは、デバイスの選択から結果の表示まで、量子タスクの例を実行する段階について説明します。Amazon Braket のベストプラクティスとして、まず などのシミュレーターで回路を実行することをお勧めしますSV1。

デバイスを指定する

まず、量子タスクのデバイスを選択して指定します。この例では、シミュレーター を選択する方法を示しますSV1。

# choose the on-demand simulator to run the circuit from braket.aws import AwsDevice device = AwsDevice("arn:aws:braket:::device/quantum-simulator/amazon/sv1")

このデバイスのプロパティの一部は、次のように表示できます。

print (device.name) for iter in device.properties.action['braket.ir.jaqcd.program']: print(iter)
SV1 ('version', ['1.0', '1.1']) ('actionType', <DeviceActionType.JAQCD: 'braket.ir.jaqcd.program'>) ('supportedOperations', ['ccnot', 'cnot', 'cphaseshift', 'cphaseshift00', 'cphaseshift01', 'cphaseshift10', 'cswap', 'cy', 'cz', 'h', 'i', 'iswap', 'pswap', 'phaseshift', 'rx', 'ry', 'rz', 's', 'si', 'swap', 't', 'ti', 'unitary', 'v', 'vi', 'x', 'xx', 'xy', 'y', 'yy', 'z', 'zz']) ('supportedResultTypes', [ResultType(name='Sample', observables=['x', 'y', 'z', 'h', 'i', 'hermitian'], minShots=1, maxShots=100000), ResultType(name='Expectation', observables=['x', 'y', 'z', 'h', 'i', 'hermitian'], minShots=0, maxShots=100000), ResultType(name='Variance', observables=['x', 'y', 'z', 'h', 'i', 'hermitian'], minShots=0, maxShots=100000), ResultType(name='Probability', observables=None, minShots=1, maxShots=100000), ResultType(name='Amplitude', observables=None, minShots=0, maxShots=0)])

量子タスクの例を送信する

オンデマンドシミュレーターで実行する量子タスクの例を送信します。

# create a circuit with a result type circ = Circuit().rx(0, 1).ry(1, 0.2).cnot(0,2).variance(observable=Observable.Z(), target=0) # add another result type circ.probability(target=[0, 2]) # set up S3 bucket (where results are stored) my_bucket = "amzn-s3-demo-bucket" # the name of the bucket my_prefix = "your-folder-name" # the name of the folder in the bucket s3_location = (my_bucket, my_prefix) # submit the quantum task to run my_task = device.run(circ, s3_location, shots=1000, poll_timeout_seconds = 100, poll_interval_seconds = 10) # the positional argument for the S3 bucket is optional if you want to specify a bucket other than the default # get results of the quantum task result = my_task.result()

device.run() コマンドは、 CreateQuantumTask API を使用して量子タスクを作成します。初期化時間が短いと、量子タスクは、デバイスで量子タスクを実行する容量が存在するまでキューに入れられます。この場合、デバイスは ですSV1。デバイスが計算を完了すると、AmazonBraket は呼び出しで指定された Amazon S3 の場所に結果を書き込みます。位置引数 s3_location はローカルシミュレーターを除くすべてのデバイスで必要です。

注記

Braket 量子タスクアクションのサイズは 3MB に制限されています。

パラメータ化されたタスクを送信する

Amazon Braket オンデマンドシミュレーター、ローカルシミュレーター、QPUsもサポートされています。これを行うには、次の例に示すようにdevice.run()、 の inputs引数を使用します。は文字列と浮動小数点のペアのディクショナリinputsである必要があります。ここで、キーはパラメータ名です。

パラメトリックコンパイルは、特定の QPUs。サポートされている QPU に量子タスクとしてパラメトリック回路を送信すると、Braket は回路を 1 回コンパイルし、結果をキャッシュします。同じ回路への後続のパラメータ更新の再コンパイルは行われないため、同じ回路を使用するタスクの実行時間が短縮されます。Braket は、回路をコンパイルするときに、ハードウェアプロバイダーから更新されたキャリブレーションデータを自動的に使用して、最高品質の結果を確保します。

注記

パラメトリックコンパイルは、パルスレベルプログラムRigetti Computingを除き、 からのすべての超電導ゲートベースの QPUs でサポートされています。

from braket.circuits import Circuit, FreeParameter, Observable # create the free parameters alpha = FreeParameter('alpha') beta = FreeParameter('beta') # create a circuit with a result type circ = Circuit().rx(0, alpha).ry(1, alpha).cnot(0,2).xx(0, 2, beta) circ.variance(observable=Observable.Z(), target=0) # add another result type circ.probability(target=[0, 2]) # submit the quantum task to run my_task = device.run(circ, inputs={'alpha': 0.1, 'beta':0.2})

shots を指定する

shots 引数は、必要な測定 の数を参照しますshots。などのシミュレーターは、2 つのシミュレーションモードSV1をサポートしています。

  • shots = 0 の場合、シミュレーターは正確なシミュレーションを実行し、すべての結果タイプの真の値を返します。( では使用できません)TN1。

  • のゼロ以外の値の場合shots、シミュレーターは出力ディストリビューションからサンプリングして、実際の QPU のshotノイズをエミュレートします。 QPUs QPU デバイスは shots > 0 のみを許可します。

量子タスクあたりの最大ショット数については、「Braket Quotas」を参照してください。

結果のポーリング

を実行するとmy_task.result()、SDK は量子タスクの作成時に定義したパラメータを使用して結果のポーリングを開始します。

  • poll_timeout_seconds は、オンデマンドシミュレーターや QPU デバイスで量子タスクを実行するときにタイムアウトするまでに量子タスクをポーリングする秒数です。デフォルト値は 432,000 秒 (5 日) です。

  • 注: Rigettiや などの QPU デバイスの場合はIonQ、数日かかることをお勧めします。ポーリングタイムアウトが短すぎると、ポーリング時間内に結果が返されないことがあります。例えば、QPU が利用できない場合、ローカルタイムアウトエラーが返されます。

  • poll_interval_seconds は、量子タスクがポーリングされる頻度です。オンデマンドシミュレーターおよび QPU デバイスで量子タスクが実行されたときに、Braket を呼び出してステータスAPIを取得する頻度を指定します。デフォルト値は 1 秒です。

この非同期実行により、常に使用できるとは限らない QPU デバイスの操作が容易になります。例えば、通常のメンテナンス期間中はデバイスを使用できない場合があります。

返される結果には、量子タスクに関連付けられたメタデータの範囲が含まれます。測定結果は、次のコマンドで確認できます。

print('Measurement results:\n',result.measurements) print('Counts for collapsed states:\n',result.measurement_counts) print('Probabilities for collapsed states:\n',result.measurement_probabilities)
Measurement results: [[1 0 1] [0 0 0] [1 0 1] ... [0 0 0] [0 0 0] [0 0 0]] Counts for collapsed states: Counter({'000': 761, '101': 226, '010': 10, '111': 3}) Probabilities for collapsed states: {'101': 0.226, '000': 0.761, '111': 0.003, '010': 0.01}

サンプル結果の表示

ResultType も指定したので、返される結果を表示できます。結果タイプは、回路に追加された順に表示されます。

print('Result types include:\n', result.result_types) print('Variance=',result.values[0]) print('Probability=',result.values[1]) # you can plot the result and do some analysis import matplotlib.pyplot as plt plt.bar(result.measurement_counts.keys(), result.measurement_counts.values()); plt.xlabel('bitstrings'); plt.ylabel('counts');
Result types include: [ResultTypeValue(type={'observable': ['z'], 'targets': [0], 'type': 'variance'}, value=0.7062359999999999), ResultTypeValue(type={'targets': [0, 2], 'type': 'probability'}, value=array([0.771, 0. , 0. , 0.229]))] Variance= 0.7062359999999999 Probability= [0.771 0. 0. 0.229]
異なるビット文字列に対するカウント数を示す棒グラフ。「000」の最も高いバーには 700 個を超えるカウントが含まれます。