1
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.

PennyLaneからqiskit simulator / ibmq を動かす

Posted at

PennyLaneからqiskit simulator / ibmq を動かす

PennyLaneからqiskitのsimulatorやibmq実機を操作できます。

やり方

まず、プラグインを入れます。
pip install pennylane-qiskit

simulator

simulatorの場合は簡単です。

import pennylane as qml
dev = qml.device('qiskit.aer', wires=2, shots=1024)

とします。(backendも指定できます。unitary simulatorとか)

デモ回路を作ります。Bell状態です。


@qml.qnode(dev)
def circuit():
    qml.Hadamard(wires=0)
    qml.CNOT(wires=[0,1])
    return qml.sample(qml.PauliZ(0)), qml.sample(qml.PauliZ(1))

circuit()
とすると、

array([[ 1, 1, -1, ..., 1, -1, -1], [ 1, 1, -1, ..., 1, -1, -1]])
と返りました。第一量子ビットと第二量子ビットの出力が相関していますね。(Bell状態)

確率分布を出力することもできます。 returnを変えます。

@qml.qnode(dev)
def circuit():
    qml.Hadamard(wires=0)
    qml.CNOT(wires=[0,1])
    return qml.probs(wires=[0,1])

circuit()

tensor([0.46679688, 0. , 0. , 0.53320312], requires_grad=False)

となります。tensorオブジェクトで返ってきた場合は、 .numpy() とかやればarrayに出来ます。

実機

実機の場合も要領は同じです。

最初にibmqアカウントをqiskitでロードします。qiskitをinstallした状態で、

from qiskit import IBMQ
IBMQ.load_account()

です。事前にIBMQ.save_account をしておく必要があります。
その上で、
dev = qml.device('qiskit.ibmq', wires=2, backend='ibmqx2')

のようにしてやればOKです。なお、save/load accountを使わずに、tokenをこの場で指定して

dev = qml.device('qiskit.ibmq', wires=2, backend='ibmqx2', ibmqx_token="Your token")

としてもOKです。

実行してみると、

c:\users\hogehoge\appdata\local\programs\python\python38\lib\site-packages\qiskit\providers\ibmq\api\clients\account.py:376: RuntimeWarning: coroutine 'WebsocketClient.get_job_status' was never awaited logger.info('Error checking job status using websocket, ' RuntimeWarning: Enable tracemalloc to get the object allocation traceback

なんか怒られますが、実行は出来ていますので、のんびり待ちましょう。

まとめ

PennyLaneからqiskitのsimulatorも実機も操作できる

  • qiskitのdraw('mpl')が使いたいのだが、使い方がわからん。
1
0
0

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
1
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?