LoginSignup
o_suke49
@o_suke49

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

Anacondaの仮想環境でQiskitの実行ができない

理論、プログラムにおいて初学者です。わかりにくいところがあれば申し訳ないのですが、お尋ねください。

解決したいこと

anaconda jupyternotebookを利用してQiskitの例ファイルを実行したい。
具体的にはAnaconda上で仮想環境を作成し、そこでQiskitを利用したい。

例)
最終的にはHHLアルゴリズムを利用することを目指しています。

環境

・MacOS intel corei5
・Anaconda Navigater version2.6.0
・python3.9
・qiskit0.46.1
・jupyternotebook 7.*

参考にしたサイト

  1. IBMが提供しているチューリアルのプログラムです。
    https://docs.quantum.ibm.com/start/hello-world
Quantum_Circuit_output.ipynb
from qiskit import QuantumCircuit
from qiskit.quantum_info import SparsePauliOp
from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager
from qiskit_ibm_runtime import QiskitRuntimeService, EstimatorV2 as Estimator
 
# Create a new circuit with two qubits
qc = QuantumCircuit(2)
 
# Add a Hadamard gate to qubit 0
qc.h(0)
 
# Perform a controlled-X gate on qubit 1, controlled by qubit 0
qc.cx(0, 1)
 
# Return a drawing of the circuit using MatPlotLib ("mpl"). This is the
# last line of the cell, so the drawing appears in the cell output.
# Remove the "mpl" argument to get a text drawing.
qc.draw("mpl")

output
image.png

  1. MacでのQiskit環境作成において参考にしました。
    https://qiita.com/John_John_Katy/items/3f3ea10362b353baef4d

  2. 後述のエラーについて調べた結果が載っていたので参考にしました。
    https://quantumcomputing.stackexchange.com/questions/37042/ibmq-import-error

まず参考サイト2に載っていた通り仮想環境(python3.11, qiskit1.0)を作成しその中で参考サイト1(Quantum_Circuit_output.ipynb)を実行した結果以下のエラーが検出されました。

発生している問題・エラー

どうやら参考サイト2からもqiskit1.0が新たにリリースされており、改めて環境を構築した際に以前のバージョンと混合しているとのこと。

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
Cell In[1], line 1
----> 1 from qiskit import QuantumCircuit
      2 from qiskit.quantum_info import SparsePauliOp
      3 from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager

File ~/.pyenv/versions/3.11.0/lib/python3.11/site-packages/qiskit/__init__.py:36
     34     _suppress_error = os.environ.get("QISKIT_SUPPRESS_1_0_IMPORT_ERROR", False) == "1"
     35     if not _suppress_error and _has_tools:
---> 36         raise ImportError(
     37             "Qiskit is installed in an invalid environment that has both Qiskit >=1.0"
     38             " and an earlier version."
     39             " You should create a new virtual environment, and ensure that you do not mix"
     40             " dependencies between Qiskit <1.0 and >=1.0."
     41             " Any packages that depend on 'qiskit-terra' are not compatible with Qiskit 1.0 and"
     42             " will need to be updated."
     43             " Qiskit unfortunately cannot enforce this requirement during environment resolution."
     44             " See https://qisk.it/packaging-1-0 for more detail."
     45         )
     47 import qiskit._accelerate
     50 # Globally define compiled submodules. The normal import mechanism will not find compiled submodules
     51 # in _accelerate because it relies on file paths, but PyO3 generates only one shared library file.
     52 # We manually define them on import so people can directly import qiskit._accelerate.* submodules
     53 # and not have to rely on attribute access.  No action needed for top-level extension packages.

ImportError: Qiskit is installed in an invalid environment that has both Qiskit >=1.0 and an earlier version. You should create a new virtual environment, and ensure that you do not mix dependencies between Qiskit <1.0 and >=1.0. Any packages that depend on 'qiskit-terra' are not compatible with Qiskit 1.0 and will need to be updated. Qiskit unfortunately cannot enforce this requirement during environment resolution. See https://qisk.it/packaging-1-0 for more detail.

そこで改めて仮想環境の中に(python3.9, qiskit0.4.6)を入れ直し再びコードを実行すると先ほどと同じエラーが出力されました。

私の考えではAnaconda下で実行されたファイルが仮想環境に入れたpython3.9ではなく仮想環境外のpython3.11を参照しているかも。

ここで ~/.zshrcファイルのpathを見てみると
/Users/myname/.pyenv/versions/3.11.0/lib/python3.11/site-packages
を参照していることに気づきました。
これを
export PATH=$PATH:/Users/myname/anaconda3/envs/qiskit_env2/lib/python3.9/site-packages
に修正し再度実行をしてみてもまた同様のエラーです。

自分で試したこと

先述した通りです。
・python3.9とそれに対応したqiskit0.4.6をAnaconda上の仮想環境に入れた。
・/Users/myname/.pyenv/versions/3.11.0/lib/python3.11/site-packages
 の3.11.0の部分にpython3.9のファイルをコピーして無理やりパスを通してみた。
→コードが実行されなくなった。

お聞きしたいこと

・Anacondaの仮想環境で実行する際にパスを指定する方法があれば教えていただきたいです。
・qiskitに詳しい方、現在のバージョンでHHLを実行できるのでしょうか。以前とバージョンが変わりなくなったという噂を耳にしたもので。

0

No Answers yet.

Your answer might help someone💌