sQUlearn / squlearn

scikit-learn interface for quantum algorithms
https://squlearn.github.io
Apache License 2.0
57 stars 18 forks source link

qiskit version requirement #299

Open MazenAli opened 2 days ago

MazenAli commented 2 days ago

The qiskit requirement "qiskit>=0.43.0,<1.1.0" in pyproject.toml is incompatible with pre-stable release versions of qiskit (the rest of the qiskit packages are 0.x) and with itself (1.0 is not backwards compatible with 0.x). Maybe change to "qiskit>=0.43.0,<1.0"?

Generally, the version requirements for all packages are too loose and do not ensure backwards compatibility.

Error on my device:

Traceback (most recent call last):
  File "/opt/project/use_case/examples/qkrenelgeo.py", line 8, in <module>
    from squlearn import Executor
  File "/usr/local/lib/python3.9/site-packages/squlearn/__init__.py", line 3, in <module>
    from .util import Executor
  File "/usr/local/lib/python3.9/site-packages/squlearn/util/__init__.py", line 1, in <module>
    from .executor import Executor
  File "/usr/local/lib/python3.9/site-packages/squlearn/util/executor.py", line 15, in <module>
    import qiskit
  File "/usr/local/lib/python3.9/site-packages/qiskit/__init__.py", line 36, in <module>
    raise ImportError(
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.

Installed libs and versions

qiskit                  1.0.2
qiskit-aer              0.14.2
qiskit-algorithms       0.3.1
qiskit-ibm-runtime      0.23.0
qiskit-machine-learning 0.6.1
qiskit-terra            0.46.3
QuantLib                1.33
requests                2.32.3
requests_ntlm           1.3.0
rustworkx               0.15.1
scikit-learn            1.0.2
scipy                   1.13.1
seaborn                 0.12.2
setuptools              58.1.0
six                     1.16.0
smmap                   5.0.1
squlearn                0.7.6
David-Kreplin commented 2 days ago

Hi Mazen,

Thanks for opening the issue!

Have you tried installing Qiskit 1.0.2 in a fresh environment? I suspect that the existing qiskit-terra installation might be causing the problem. Starting from Qiskit 1.0, the qiskit-terra meta-package is no longer part of a new Qiskit installation (see the message at PyPI).

Based on our tests, sQUlearn should work with Qiskit <=1.1, but not with Qiskit 1.2. We are currently working on adding support for Qiskit 1.2 and are optimistic about releasing version 0.8 with Qiskit 1.2 support by the end of the week.

MazenAli commented 2 days ago

qiskit-machine-learning>=0.6.1 is also incorrect, TrainableFidelityStatevectorKernel was introduced in 0.7.0: https://github.com/qiskit-community/qiskit-machine-learning/releases

MazenAli commented 2 days ago

Hi David. I can fix the issue myself, ofc, by manually installing and deinstalling until it works, but that's not the point. Out of the box pip install in a clean docker environment does not guarantee a runnable squlearn because of version numbers incompatibilities.

MazenAli commented 2 days ago

If you don't have time for this rn, I can see if I can work on it next week and submit a PR, if you guys are open to it.

David-Kreplin commented 2 days ago

Yes, it looks like we haven't updated the requirements in a while; qiskit-machine-learning>=0.7.0 is definitely required. There is also a version conflict with the latest update addressing the issue with Bayesian optimization (see #300). So far, pip install has always worked for us (also in different docker set-ups), which is why we haven't done extensive testing on the dependencies. We will try to fix everything in the next release this week, and we should add a test job for the lowest requirements.

In general, if you'd like to contribute, we appreciate any support and contributions!

David-Kreplin commented 1 day ago

We are still unsure how you could end up with both qiskit-terra and qiskit==1.0.2 installed in the same environment. The only explanation we have is that there might be some automatic dependency resolution running in the background.

If qiskit <1.0 is installed first and later upgraded to qiskit >1.0, this could lead to such an environment. Unfortunately, this issue cannot be resolved by simply adjusting our dependencies. It appears that qiskit can only work with either qiskit >=0.43.0, <1.0 or qiskit >=1.0. To run everything on IBM Quantum, we are required to upgrade to the 1.x version.

MazenAli commented 1 day ago

Here is another example before and after a fresh install. Before adding squlearn to requirements, this is my pip list:

bandit            1.7.10
black             24.3.0
click             8.1.7
coverage          7.6.4
cycler            0.12.1
exceptiongroup    1.2.2
flake8            7.1.1
fonttools         4.54.1
iniconfig         2.0.0
isort             5.13.2
jax               0.4.35
jaxlib            0.4.35
joblib            1.4.2
kiwisolver        1.4.7
markdown-it-py    3.0.0
matplotlib        3.5.3
mccabe            0.7.0
mdurl             0.1.2
ml_dtypes         0.5.0
mypy              1.13.0
mypy-extensions   1.0.0
numpy             1.26.3
opt_einsum        3.4.0
packaging         24.1
pandas            1.3.5
pathspec          0.12.1
pbr               6.1.0
pillow            11.0.0
pip               24.2
platformdirs      4.3.6
pluggy            1.5.0
pycodestyle       2.12.1
pyflakes          3.2.0
Pygments          2.18.0
pyparsing         3.2.0
pytest            8.3.3
python-dateutil   2.9.0.post0
pytz              2024.2
PyYAML            6.0.2
QuantLib          1.33
rich              13.9.3
scikit-learn      1.0.2
scipy             1.14.1
seaborn           0.12.2
setuptools        65.5.1
six               1.16.0
stevedore         5.3.0
teneva            0.14.9
teneva-jax        0.1.1
threadpoolctl     3.5.0
tomli             2.0.2
torch             1.13.1
torchTT           2.0
typing_extensions 4.12.2
wheel             0.44.0

After adding squlearn and rebuilding from scratch:

annotated-types         0.7.0
appdirs                 1.4.4
autograd                1.7.0
autoray                 0.7.0
bandit                  1.7.10
bayesian-optimization   2.0.0
black                   24.3.0
cachetools              5.5.0
certifi                 2024.8.30
cffi                    1.17.1
charset-normalizer      3.4.0
click                   8.1.7
colorama                0.4.6
coverage                7.6.4
cryptography            43.0.3
cycler                  0.12.1
dill                    0.3.9
exceptiongroup          1.2.2
fastdtw                 0.3.4
flake8                  7.1.1
fonttools               4.54.1
ibm-cloud-sdk-core      3.22.0
ibm-platform-services   0.57.2
idna                    3.10
iniconfig               2.0.0
isort                   5.13.2
jax                     0.4.35
jaxlib                  0.4.35
joblib                  1.4.2
kiwisolver              1.4.7
markdown-it-py          3.0.0
matplotlib              3.5.3
mccabe                  0.7.0
mdurl                   0.1.2
ml_dtypes               0.5.0
mpmath                  1.3.0
mypy                    1.13.0
mypy-extensions         1.0.0
networkx                3.4.2
numpy                   1.26.3
opt_einsum              3.4.0
packaging               24.1
pandas                  1.3.5
pathspec                0.12.1
pbr                     6.1.0
PennyLane               0.38.0
PennyLane_Lightning     0.38.0
pillow                  11.0.0
pip                     24.2
platformdirs            4.3.6
pluggy                  1.5.0
ply                     3.11
psutil                  6.1.0
pycodestyle             2.12.1
pycparser               2.22
pydantic                2.9.2
pydantic_core           2.23.4
pyflakes                3.2.0
Pygments                2.18.0
PyJWT                   2.9.0
pyparsing               3.2.0
pyspnego                0.11.1
pytest                  8.3.3
python-dateutil         2.9.0.post0
pytz                    2024.2
PyYAML                  6.0.2
qiskit                  1.0.2
qiskit-aer              0.14.2
qiskit-algorithms       0.3.1
qiskit-ibm-runtime      0.23.0
qiskit-machine-learning 0.6.1
qiskit-terra            0.46.3
QuantLib                1.33
requests                2.32.3
requests_ntlm           1.3.0
rich                    13.9.3
rustworkx               0.15.1
scikit-learn            1.0.2
scipy                   1.14.1
seaborn                 0.12.2
setuptools              65.5.1
six                     1.16.0
squlearn                0.7.6
stevedore               5.3.0
symengine               0.13.0
sympy                   1.13.3
teneva                  0.14.9
teneva-jax              0.1.1
threadpoolctl           3.5.0
toml                    0.10.2
tomli                   2.0.2
torch                   1.13.1
torchTT                 2.0
tqdm                    4.66.5
typing_extensions       4.12.2
urllib3                 2.2.3
websocket-client        1.8.0
wheel                   0.44.0

With the resulting error when trying to import squlearn as before. The issue is definitely not a pre-existing qiskit install.

MazenAli commented 1 day ago

What currently works for me (at least to build and import) is adding this explicitly to the requirements

qiskit~=0.43
qiskit-machine-learning~=0.7
bayesian-optimization~=1.4
squlearn~=0.7

which results in

bayesian-optimization   1.5.1
qiskit                  0.46.3
qiskit-aer              0.14.2
qiskit-algorithms       0.3.1
qiskit-ibm-runtime      0.20.0
qiskit-machine-learning 0.7.2
qiskit-terra            0.46.3
squlearn                0.7.6
David-Kreplin commented 1 day ago

Ok, interesting. What happens if you add qiskit==1.1 instead of squlearn to the requirements file? It looks to me like there's a dependency conflict that it's trying to resolve, leading to different versions of Qiskit being installed, which results in both qiskit-terra and qiskit==1.0.2 being installed.

MazenAli commented 1 day ago

Removed squlearn, added qiskit==1.1 instead. My pip list now:

bandit                  1.7.10
bayesian-optimization   1.5.1
black                   24.3.0
click                   8.1.7
colorama                0.4.6
contourpy               1.3.0
coverage                7.6.4
cycler                  0.12.1
dill                    0.3.9
exceptiongroup          1.2.2
fastdtw                 0.3.4
flake8                  7.1.1
fonttools               4.54.1
iniconfig               2.0.0
isort                   5.13.2
jax                     0.4.35
jaxlib                  0.4.35
joblib                  1.4.2
kiwisolver              1.4.7
markdown-it-py          3.0.0
matplotlib              3.9.2
mccabe                  0.7.0
mdurl                   0.1.2
ml_dtypes               0.5.0
mpmath                  1.3.0
mypy                    1.13.0
mypy-extensions         1.0.0
numpy                   1.26.4
opt_einsum              3.4.0
packaging               24.1
pandas                  1.5.3
pathspec                0.12.1
pbr                     6.1.0
pillow                  11.0.0
pip                     24.2
platformdirs            4.3.6
pluggy                  1.5.0
psutil                  6.1.0
pycodestyle             2.12.1
pyflakes                3.2.0
Pygments                2.18.0
pylatexenc              2.10
pyparsing               3.2.0
pytest                  8.3.3
python-dateutil         2.9.0.post0
pytz                    2024.2
PyYAML                  6.0.2
qiskit                  1.1.0
qiskit-algorithms       0.3.1
qiskit-machine-learning 0.7.2
QuantLib                1.36
rich                    13.9.3
rustworkx               0.15.1
scikit-learn            1.5.2
scipy                   1.14.1
seaborn                 0.13.2
setuptools              65.5.1
six                     1.16.0
stevedore               5.3.0
symengine               0.13.0
sympy                   1.13.3
teneva                  0.14.9
teneva-jax              0.1.1
threadpoolctl           3.5.0
tomli                   2.0.2
torch                   1.13.1
torchTT                 2.0
typing_extensions       4.12.2
wheel                   0.44.0

(no qiskit-terra)

My requirements.txt:

torch~=1.13
numpy~=1.26
pandas~=1.3
matplotlib~=3.5
scikit-learn~=1.0
seaborn~=0.12
QuantLib~=1.33
scipy
teneva
teneva-jax
qiskit==1.1
qiskit-machine-learning~=0.7
bayesian-optimization~=1.4
pylatexenc
David-Kreplin commented 1 day ago

Ok, so plain Qiskit is not causing the problem. We will check different Squlearn dependencies tomorrow. Thank you very much for providing the requirements; with that information, it should be possible to find the origin of the problem.

David-Kreplin commented 1 day ago

What python version did you use? I was not able to install your pip list with both python 3.9 and 3.10 on linux and windows.

For python=3.9

ERROR: Ignored the following yanked versions: 0.2.23, 0.3.18, 0.4.0, 0.4.15
ERROR: Ignored the following versions that require a different python version: 0.4.31 Requires-Python >=3.10; 0.4.32 Requires-Python >=3.10; 0.4.33 Requires-Python >=3.10; 0.4.34 Requires-Python >=3.10; 0.4.35 Requires-Python >=3.10
ERROR: Could not find a version that satisfies the requirement jax==0.4.35 (from versions: 0.0, 0.1, 0.1.1, 0.1.2, 0.1.3, 0.1.4, 0.1.5, 0.1.6, 0.1.7, 0.1.8, 0.1.9, 0.1.10, 0.1.11, 0.1.12, 0.1.13, 0.1.14, 0.1.15, 0.1.16, 0.1.18, 0.1.19, 0.1.20, 0.1.21, 0.1.22, 0.1.23, 0.1.24, 0.1.25, 0.1.26, 0.1.27, 0.1.28, 0.1.29, 0.1.30, 0.1.31, 0.1.32, 0.1.33, 0.1.34, 0.1.35, 0.1.36, 0.1.37, 0.1.38, 0.1.39, 0.1.40, 0.1.41, 0.1.42, 0.1.43, 0.1.44, 0.1.45, 0.1.46, 0.1.47, 0.1.48, 0.1.49, 0.1.50, 0.1.51, 0.1.52, 0.1.53, 0.1.54, 0.1.55, 0.1.56, 0.1.57, 0.1.58, 0.1.59, 0.1.60, 0.1.61, 0.1.62, 0.1.63, 0.1.64, 0.1.65, 0.1.66, 0.1.67, 0.1.68, 0.1.69, 0.1.70, 0.1.71, 
0.1.72, 0.1.73, 0.1.74, 0.1.75, 0.1.76, 0.1.77, 0.2.0, 0.2.1, 0.2.2, 0.2.3, 0.2.4, 0.2.5, 0.2.6, 0.2.7, 0.2.8, 0.2.9, 0.2.10, 0.2.11, 0.2.12, 0.2.13, 0.2.14, 0.2.15, 0.2.16, 0.2.17, 0.2.18, 0.2.19, 0.2.20, 0.2.21, 0.2.22, 0.2.24, 0.2.25, 0.2.26, 0.2.27, 0.2.28, 0.3.0, 0.3.1, 0.3.2, 0.3.3, 0.3.4, 0.3.5, 0.3.6, 0.3.7, 0.3.8, 0.3.9, 0.3.10, 0.3.11, 0.3.12, 0.3.13, 0.3.14, 0.3.15, 0.3.16, 0.3.17, 0.3.19, 0.3.20, 0.3.21, 0.3.22, 0.3.23, 0.3.24, 0.3.25, 0.4.1, 0.4.2, 0.4.3, 0.4.4, 0.4.5, 0.4.6, 0.4.7, 0.4.8, 0.4.9, 0.4.10, 0.4.11, 0.4.12, 0.4.13, 0.4.14, 0.4.16, 0.4.17, 0.4.18, 0.4.19, 0.4.20, 0.4.21, 0.4.22, 0.4.23, 0.4.24, 0.4.25, 0.4.26, 0.4.27, 0.4.28, 0.4.29, 0.4.30)
ERROR: No matching distribution found for jax==0.4.35

For python==3.10:

ERROR: Ignored the following versions that require a different python version: 1.6.2 Requires-Python >=3.7,<3.10; 1.6.3 Requires-Python >=3.7,<3.10; 1.7.0 Requires-Python >=3.7,<3.10; 1.7.1 Requires-Python >=3.7,<3.10
ERROR: Could not find a version that satisfies the requirement torchTT==2.0 (from versions: none)
ERROR: No matching distribution found for torchTT==2.0
MazenAli commented 20 hours ago

I use python 3.10, neither 3.11 nor 3.9 work. For 3.10, I think the issue might be how you are installing torchTT (needs to be pulled from github). So here are my Dockerfile, and all the requirement files for complete reproducibility:

Dockerfile:

FROM python:3.10-slim

RUN mkdir /opt/project
WORKDIR /opt/project
COPY requirements* /opt/project
ENV PYTHONPATH "/opt/project/use_case"

RUN apt-get update && \
    apt-get install -y --no-install-recommends && \
    apt-get install -y make jq git

RUN pip install --upgrade pip
RUN pip install --no-cache-dir -r requirements-dev.txt
RUN pip install --no-cache-dir -r requirements.txt
RUN pip install --no-cache-dir -r requirements-repos.txt

requirements-dev.txt:

flake8~=7.0
mypy~=1.13
black~=24.3.0
isort~=5.13
coverage~=7.6
bandit~=1.7.4

requirements.txt

torch~=1.13
numpy~=1.26
pandas~=1.3
matplotlib~=3.5
scikit-learn~=1.0
seaborn~=0.12
QuantLib~=1.33
scipy
teneva
teneva-jax
qiskit~=0.43
qiskit-machine-learning~=0.7
bayesian-optimization~=1.4
squlearn~=0.7
pylatexenc

and requirements-repos.txt:

git+https://github.com/ion-g-ion/torchTT
David-Kreplin commented 1 hour ago

Thank you very much! I think with this, we can investigate what's going wrong.

However, my guess is that we cannot fix this easily if we want to support both Qiskit < 1.0 and Qiskit > 1.0, as the problem lies in the handling of the Qiskit meta-package. :/

The easiest solution for you is probably to install Qiskit 1.0 (or lower) first and then install scikit-learn afterwards.

We have fixed and checked our minimal requirements; the update will be ready by Monday morning. :)