4

I was going to use Braket in us-east1. I wanted to use qiskit, so I wanted to import the module, but an error occurred.

I referred to the following URL https://qiskit-community.github.io/qiskit-braket-provider/tutorials/3_tutorial_minimum_eigen_optimizer.html

Checking with !pip list, qiskit_algorithms is installed without any problem.

【code】

!pip install qiskit !pip install qiskit-optimization !pip install qiskit-algorithms from qiskit import QuantumCircuit, transpile from qiskit.quantum_info import SparsePauliOp from qiskit.primitives import Sampler from qiskit_aer import AerSimulator from qiskit_optimization import QuadraticProgram from qiskit_algorithms.minimum_eigensolvers import QAOA from qiskit_algorithms.optimizers import COBYLA from qiskit_algorithms import NumPyMinimumEigensolver from qiskit_optimization.algorithms import MinimumEigenOptimizer 

【output】

ModuleNotFoundError Traceback (most recent call last) Cell In[7], line 10 4 from qiskit_aer import AerSimulator 5 # from qiskit_optimization import QuadraticProgram 6 # from qiskit_algorithms.minimum_eigensolvers import QAOA 7 # from qiskit_algorithms.optimizers import COBYLA 8 # from qiskit_algorithms import NumPyMinimumEigensolver 9 # from qiskit_optimization.algorithms import MinimumEigenOptimizer ---> 10 import qiskit_optimization ModuleNotFoundError: No module named 'qiskit_optimization' 

How can I install qiskit_algorithms and qiskit_optimization without problems?

I have tried the following.

  • Verify that the module is installed with a !pip list
  • Restart kernel
  • Start another notebook
  • Verify that the installed path of the package is correct
  • Confirm that the module works fine in the local environment

2 Answers 2

1

In this case, can you run

%pip install --upgrade qiskit %pip install --upgrade qiskit-optimization %pip install --upgrade qiskit-algorithms 

This will install the library in the environment where the kernel is running instead of the base environment. The command !pip will use the base environment.

These commands should be ran from a Jupyter notebook. If you want to run pip installs from a terminal in the Braket instance, you can go to the terminal and activate the Braket environment using:

cd /home/ec2-user/ source ~/anaconda3/etc/profile.d/conda.sh conda activate Braket 

From there, you can then run the pip commands:

pip install --upgrade qiskit pip install --upgrade qiskit-optimization pip install --upgrade qiskit-algorithms 

In the case of Amazon Braket, qiskit is already installed as a direct dependency. However, it is also a dependency of the qiskit_braket_provider which is installed into the environment.

In the case of older versions of the qiskit-braket-provider, qiskit 1.x was not supported. However, new versions (as of this commit from around 2 weeks ago) of Braket Notebook Instances do have that support. If you do not wish for your full environment to get updated, you can run the lines:

# From a Jupyter notebook %pip install --upgrade qiskit_braket_provider %pip install --upgrade qiskit %pip install --upgrade qiskit-optimization %pip install --upgrade qiskit-algorithms 

There is also an option to stop and then start the notebook again. This will pull in the latest Braket examples and environment but will result in loss of work. For your use case, attempting the pip upgrades should unblock your issue.

Edit: After some testing, there is also an issue of incompatibility with moving to qiskit 1.x on Braket notebooks. The reason for this is that qiskit==0.46.x is currently installed. This version of qiskit installs qiskit-terra, the old name for the qiskit package for compatibility. Therefore, when running the upgrades and installing qiskit 1.x, qiskit-terra is kept. To work around this, try running:

%pip uninstall qiskit-terra %pip uninstall qiskit %pip install --upgrade qiskit %pip install --upgrade qiskit-optimization %pip install --upgrade qiskit-algorithms 
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your prompt response. I tried what you answered and got the following error. 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.
I have amended my answer to provide more context and details. Let me know if any of the sections need more explanation!
Sorry @yusuke, I have gone back for more testing on my answer and will add another edit to help with issues that may persist on older notebooks.
Have any of these been the correct answer? Hard to follow.
0

The error is straight up telling you that the module isn't installed, so we can work with that, or else there's some issue with how you've installed it. This can often be something like permissions or it not running on the same Python environment. Confirm you've got the latest upgrades.

!pip install --upgrade qiskit !pip install --upgrade qiskit-optimization !pip install --upgrade qiskit-algorithms 

If you need to install this on the environment where your kernel is running, try the following. This is worth trying as you are getting a clear error that the package isn't installing where it is trying to run, so let's rule this out.

%pip install --upgrade qiskit %pip install --upgrade qiskit-optimization %pip install --upgrade qiskit-algorithms 

Now keep it simple for sake of testing an import only the necessary modules.

from qiskit import QuantumCircuit, transpile from qiskit.quantum_info import SparsePauliOp from qiskit.aqua import QuantumInstance from qiskit.optimization import QuadraticProgram from qiskit.algorithms import QAOA, NumPyMinimumEigensolver from qiskit.optimization.algorithms import MinimumEigenOptimizer 

That should do it. Make sure to run it in an environment where you have the necessary permissions to install packages using pip. If you are using a Jupyter notebook, you might want restart the kernel and run the cells again to ensure that the new package is imported correctly. I've had this issue before so hopefully my fixes work for you too.

3 Comments

Thanks for replying. It is very reassuring. But I'm getting the same error. The installation appears to be correct. !which python Python Location: /home/ec2-user/anaconda3/envs/JupyterSystemEnv/bin/python !pip show qiskit-optimization qiskit-optimization Location: /home/ec2-user/anaconda3/envs/JupyterSystemEnv/lib/python3.10/site-packages Is IAM or Security Group involved?
It is likely that it's installing or running on a different python env. Try installing on the env specifically where your kernel is running. %pip install --upgrade qiskit %pip install --upgrade qiskit-optimization %pip install --upgrade qiskit-algorithms
Thank you so much for your continued support. When I tried the method you gave me, I got this error message. 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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.