2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

QiskitでGPUを使用して期待値を求めてみた!

Posted at

はじめに

QiskitでGPUを用いて高速に計算をしようというのがコンセプトです。
本記事で作成したコードはGoogleColaboratoryにあります。

# 下準備

installの処理

install関係は以下のようになってます。

pip install qiskit==0.25.2
pip install qiskit-aer-gpu

importの処理

import numpy as np

import qiskit
from qiskit import QuantumRegister, QuantumCircuit, ClassicalRegister, Aer, execute
from qiskit.providers.aer import QasmSimulator, StatevectorSimulator
from qiskit.aqua.utils import tensorproduct
from qiskit.quantum_info.analysis import average_data
from qiskit.quantum_info import Statevector
from qiskit.quantum_info.operators import Operator, Pauli

コード

回路の準備

theta = [2.0 * np.pi * np.random.rand() for i in range(9)]
theta

qc = QuantumCircuit(2)

for j in range(4):
    for i in range(2):
        qc.ry(theta[i], i)
        qc.rz(theta[i], i)
    qc.cx(0, 1)

GPUによる回路の実行

backend = StatevectorSimulator(precision='single', device='GPU')

results = execute(qc, backend=backend).result()
result_statevec = Statevector(results.get_statevector(qc))

print(result_statevec)
Statevector([ 0.26311904+0.56951183j,  0.0355426 +0.18207325j,
             -0.49505538+0.52376759j, -0.03841634+0.22610325j],
            dims=(2, 2))

Observable 作成

pauli0 = Pauli(label='IZ')
pauli1 = Pauli(label='IZ')

obs_op = Operator(pauli0)
print(obs_op)
Operator([[ 1.+0.j,  0.+0.j,  0.+0.j,  0.+0.j],
          [ 0.+0.j, -1.+0.j,  0.+0.j,  0.+0.j],
          [ 0.+0.j,  0.+0.j,  1.+0.j,  0.+0.j],
          [ 0.+0.j,  0.+0.j,  0.+0.j, -1.+0.j]],
         input_dims=(2, 2), output_dims=(2, 2))

期待値計算

expectation = result_statevec.expectation_value(obs_op)
print(expectation)
(0.8259752337379496+0j)
2
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?