ProjectQ-Framework / ProjectQ

ProjectQ: An open source software framework for quantum computing
https://projectq.ch
Apache License 2.0
893 stars 275 forks source link

not work on macbook with M1 Chip #418

Closed big91987 closed 2 years ago

big91987 commented 3 years ago

Hi everyone, I got a crash of projectq lib on macbook with M1 Chip , is there any solution for this chip?thx

(venv) ➜ test git:(master) ✗ python3 ./test1.py [1] 40975 illegal hardware instruction python3 ./test1.py (venv) ➜ test git:(master) ✗ cat test1.py from projectq import MainEngine # import the main compiler engine from projectq.ops import H, Measure # import the operations we want to perform␣

qengine = MainEngine() # create a default compiler (the back-end is a simulator) qubit = qengine.allocate_qubit() # allocate 1 qubit H | qubit # apply a Hadamard gate Measure | qubit # measure the qubit qengine.flush() # flush all gates (and execute measurements) print("Output {}".format(int(qubit))) # output measurement result%

Takishima commented 3 years ago

Mmm this is weird, the current setup.py should be able to compile the C++ version of the simulator without AVX2 intrinsics.

Could you please provide the full log of the installation?

Something like this:

python3 -m pip install -v projectq
CalMacCQ commented 2 years ago

Hi there, I seem to be having a related issue.

If I run import projectq or from projectq import MainEngine in python in the terminal I get illegal hardware instruction python3 if I do the same in a jupyter notebook environment then this command kills my kernel. I've encountered this with projectq 0.7.1 and 0.7.3.

I am using an M1 Mac with Monterey. My installation log is below if that helps. Thanks!

(pq) (base) callum@Callums-MBP Desktop % python -m pip install -v projectq Using pip 22.1.2 from /Users/callum/Desktop/pq/lib/python3.8/site-packages/pip (python 3.8) Collecting projectq Using cached projectq-0.7.3-cp38-cp38-macosx_10_9_x86_64.whl Collecting numpy Using cached numpy-1.22.4-cp38-cp38-macosx_10_15_x86_64.whl (17.6 MB) Collecting requests Using cached requests-2.27.1-py2.py3-none-any.whl (63 kB) Collecting scipy Using cached scipy-1.8.1-cp38-cp38-macosx_12_0_universal2.macosx_10_9_x86_64.whl (55.3 MB) Collecting networkx>=2 Using cached networkx-2.8.3-py3-none-any.whl (2.0 MB) Collecting matplotlib>=2.2.3 Using cached matplotlib-3.5.2-cp38-cp38-macosx_10_9_x86_64.whl (7.3 MB) Requirement already satisfied: python-dateutil>=2.7 in ./pq/lib/python3.8/site-packages (from matplotlib>=2.2.3->projectq) (2.8.2) Collecting fonttools>=4.22.0 Using cached fonttools-4.33.3-py3-none-any.whl (930 kB) Collecting kiwisolver>=1.0.1 Using cached kiwisolver-1.4.2-cp38-cp38-macosx_10_9_x86_64.whl (65 kB) Requirement already satisfied: packaging>=20.0 in ./pq/lib/python3.8/site-packages (from matplotlib>=2.2.3->projectq) (21.3) Collecting pillow>=6.2.0 Using cached Pillow-9.1.1-cp38-cp38-macosx_10_10_x86_64.whl (3.1 MB) Requirement already satisfied: pyparsing>=2.2.1 in ./pq/lib/python3.8/site-packages (from matplotlib>=2.2.3->projectq) (3.0.9) Collecting cycler>=0.10 Using cached cycler-0.11.0-py3-none-any.whl (6.4 kB) Collecting urllib3<1.27,>=1.21.1 Using cached urllib3-1.26.9-py2.py3-none-any.whl (138 kB) Collecting idna<4,>=2.5 Using cached idna-3.3-py3-none-any.whl (61 kB) Collecting charset-normalizer~=2.0.0 Using cached charset_normalizer-2.0.12-py3-none-any.whl (39 kB) Collecting certifi>=2017.4.17 Using cached certifi-2022.5.18.1-py3-none-any.whl (155 kB) Requirement already satisfied: six>=1.5 in ./pq/lib/python3.8/site-packages (from python-dateutil>=2.7->matplotlib>=2.2.3->projectq) (1.16.0) Installing collected packages: urllib3, pillow, numpy, networkx, kiwisolver, idna, fonttools, cycler, charset-normalizer, certifi, scipy, requests, matplotlib, projectq changing mode of /Users/callum/Desktop/pq/bin/f2py to 755 changing mode of /Users/callum/Desktop/pq/bin/f2py3 to 755 changing mode of /Users/callum/Desktop/pq/bin/f2py3.8 to 755 changing mode of /Users/callum/Desktop/pq/bin/fonttools to 755 changing mode of /Users/callum/Desktop/pq/bin/pyftmerge to 755 changing mode of /Users/callum/Desktop/pq/bin/pyftsubset to 755 changing mode of /Users/callum/Desktop/pq/bin/ttx to 755 changing mode of /Users/callum/Desktop/pq/bin/normalizer to 755 Successfully installed certifi-2022.5.18.1 charset-normalizer-2.0.12 cycler-0.11.0 fonttools-4.33.3 idna-3.3 kiwisolver-1.4.2 matplotlib-3.5.2 networkx-2.8.3 numpy-1.22.4 pillow-9.1.1 projectq-0.7.3 requests-2.27.1 scipy-1.8.1 urllib3-1.26.9

`

Takishima commented 2 years ago

@CalMacCQ Could you try giving me the full log of the following command?

python3 -m pip install -v projectq
CalMacCQ commented 2 years ago

Hi, Thanks for getting back to me. the output of the command was very long so I've written it to a txt file using python3 -m pip install -v projectq > pq_log.txt

Hope this helps. Possible I'm just missing something. pq_log.txt .

Takishima commented 2 years ago

Thanks. It appears that the ProjectQ wheel was not built this time around (most likely because pip cached it last time you installed ProjectQ). This means I don't have the information I need in this log file.

Could I ask you to run the following command?

python3 -m pip install --no-cache-dir -v projectq > pq_log.txt
CalMacCQ commented 2 years ago

Okay I've ran

python3 -m pip install --no-cache-dir -v projectq > pq_log1.txt

in the same directory. Let me know if you need anything else. Just in case its relevant I ran the command in my previous comment in a new virtual environment so I'm not sure there would be an issue with a previous installation.

Let me know if you need anything else. Thanks again.

pq_log1.txt

Takishima commented 2 years ago

Okay I've ran

python3 -m pip install --no-cache-dir -v projectq > pq_log1.txt

in the same directory. Let me know if you need anything else. Just in case its relevant I ran the command in my previous comment in a new virtual environment so I'm not sure there would be an issue with a previous installation.

Let me know if you need anything else. Thanks again.

pq_log1.txt

It does not matter if you ran the previous one in a virtual environment, that's perfectly fine. I suspect the issue comes from some compilation options that is passed to the C++ compiler.

My apologies but I am going to have to ask you to run the pip command once more because this time around ProjectQ was already installed and so basically nothing happened... 😅

python3 -m pip uninstall -y project
python3 -m pip install --no-cache-dir -v projectq > pq_log2.txt
CalMacCQ commented 2 years ago

Hi,

No worries :) I've ran the above commands and here is the txt file. Thanks pq_log2.txt .

Takishima commented 2 years ago

Ehm... there was a typo in the first of the commands I asked you to run... a 'q' was missing so you did not actually uninstall ProjectQ with the first one and reinstall it right afterwards...

I promise this time around it will be fine

python3 -m pip uninstall -y projectq
python3 -m pip install --no-cache-dir -v projectq > pq_log2.txt

NB: you can double check that the first of these two commands was successful if you get some output like this:

[damien@home]$ python3 -m pip uninstall -y projectq
Found existing installation: projectq 0.7.3
Uninstalling projectq-0.7.3:
  Successfully uninstalled projectq-0.7.3
CalMacCQ commented 2 years ago

Yes I suspected there was a typo and added the 'q' to the first command (I should've said) I received a confirmation that projectq was successfully uninstalled.

Takishima commented 2 years ago

Are you sure that you also included -v in your second command? Because in your log file there's no trace of the compiler tests that ProjectQ is doing during its setup.

Here's what I see when running python3 -m pip install --no-cache -v projectq on my Linux machine

[damien@home]$ python3 -m pip install --no-cache -v projectq
Using pip 22.1.2 from /home/[...]/pip (python 3.10)
Collecting projectq
  Downloading projectq-0.7.3.tar.gz (419 kB)
[...]
Building wheels for collected packages: projectq
  Running command Building wheel for projectq (pyproject.toml)
  /tmp/pip-build-env-z1f12_q7/overlay/lib/python3.10/site-packages/setuptools/config/setupcfg.py:463: SetuptoolsDeprecationWarning: The license_file parameter is deprecated, use license_files instead.
    warnings.warn(msg, warning_class)
  running bdist_wheel
[...]
  running build_ext
  creating tmp
  gcc -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -march=x86-64 -mtune=generic -O3 -pipe -fno-plt -fexceptions -Wp,-D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -fstack-clash-protection -fcf-protection -flto=auto -ffat-lto-objects -march=x86-64 -mtune=generic -O3 -pipe -fno-plt -fexceptions -Wp,-D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -fstack-clash-protection -fcf-protection -flto=auto -march=x86-64 -mtune=generic -O3 -pipe -fno-plt -fexceptions -Wp,-D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -fstack-clash-protection -fcf-protection -flto=auto -fPIC -I/home[...]/venv/include -I/usr/include/python3.10 -c /tmp/tmpzz7xnfwu.cpp -o tmp/tmpzz7xnfwu.o
  ---------------------------------------------------------------------------
  # INFO:  Configuring OpenMP
  ---------------------------------------------------------------------------
  gcc -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -march=x86-64 -mtune=generic -O3 -pipe -fno-plt -fexceptions -Wp,-D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -fstack-clash-protection -fcf-protection -flto=auto -ffat-lto-objects -march=x86-64 -mtune=generic -O3 -pipe -fno-plt -fexceptions -Wp,-D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -fstack-clash-protection -fcf-protection -flto=auto -march=x86-64 -mtune=generic -O3 -pipe -fno-plt -fexceptions -Wp,-D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -fstack-clash-protection -fcf-protection -flto=auto -fPIC -I/home/[...]venv/include -I/usr/include/python3.10 -c /tmp/tmpb7u_gn43.cpp -o tmp/tmpb7u_gn43.o -openmp
  gcc -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -march=x86-64 -mtune=generic -O3 -pipe -fno-plt -fexceptions -Wp,-D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -fstack-clash-protection -fcf-protection -flto=auto -ffat-lto-objects -march=x86-64 -mtune=generic -O3 -pipe -fno-plt -fexceptions -Wp,-D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -fstack-clash-protection -fcf-protection -flto=auto -march=x86-64 -mtune=generic -O3 -pipe -fno-plt -fexceptions -Wp,-D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -fstack-clash-protection -fcf-protection -flto=auto -fPIC -I/home/[...]venv/include -I/usr/include/python3.10 -c /tmp/tmpd_iqocqa.cpp -o tmp/tmpd_iqocqa.o -fopenmp
  gcc -shared -Wl,-O1,--sort-common,--as-needed,-z,relro,-z,now -flto=auto -Wl,-O1,--sort-common,--as-needed,-z,relro,-z,now -flto=auto tmp/tmpd_iqocqa.o -L/usr/lib -o /tmp/tmpvf2nquyh/libtest.so.so -fopenmp
  ---------------------------------------------------------------------------
  # INFO:  Configuring compiler intrinsics
  ---------------------------------------------------------------------------
  gcc -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -march=x86-64 -mtune=generic -O3 -pipe -fno-plt -fexceptions -Wp,-D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -fstack-clash-protection -fcf-protection -flto=auto -ffat-lto-objects -march=x86-64 -mtune=generic -O3 -pipe -fno-plt -fexceptions -Wp,-D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -fstack-clash-protection -fcf-protection -flto=auto -march=x86-64 -mtune=generic -O3 -pipe -fno-plt -fexceptions -Wp,-D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -fstack-clash-protection -fcf-protection -flto=auto -fPIC -I/home/[...]venv/include -I/usr/include/python3.10 -c /tmp/tmpwv7drc72.cpp -o tmp/tmpwv7drc72.o -march=native
  gcc -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -march=x86-64 -mtune=generic -O3 -pipe -fno-plt -fexceptions -Wp,-D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -fstack-clash-protection -fcf-protection -flto=auto -ffat-lto-objects -march=x86-64 -mtune=generic -O3 -pipe -fno-plt -fexceptions -Wp,-D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -fstack-clash-protection -fcf-protection -flto=auto -march=x86-64 -mtune=generic -O3 -pipe -fno-plt -fexceptions -Wp,-D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -fstack-clash-protection -fcf-protection -flto=auto -fPIC -DINTRIN -I/home/[...]venv/include -I/usr/include/python3.10 -c /tmp/tmp7cxaajr7.cpp -o tmp/tmp7cxaajr7.o -ffast-math
  ---------------------------------------------------------------------------
  # INFO:  Configuring C++ standard
  ---------------------------------------------------------------------------
  gcc -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -march=x86-64 -mtune=generic -O3 -pipe -fno-plt -fexceptions -Wp,-D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -fstack-clash-protection -fcf-protection -flto=auto -ffat-lto-objects -march=x86-64 -mtune=generic -O3 -pipe -fno-plt -fexceptions -Wp,-D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -fstack-clash-protection -fcf-protection -flto=auto -march=x86-64 -mtune=generic -O3 -pipe -fno-plt -fexceptions -Wp,-D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -fstack-clash-protection -fcf-protection -flto=auto -fPIC -DINTRIN -I/home/[...]venv/include -I/usr/include/python3.10 -c /tmp/tmpsic9o3nm.cpp -o tmp/tmpsic9o3nm.o -std=c++17
  ---------------------------------------------------------------------------
  # INFO:  Other compiler tests
  ---------------------------------------------------------------------------
  gcc -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -march=x86-64 -mtune=generic -O3 -pipe -fno-plt -fexceptions -Wp,-D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -fstack-clash-protection -fcf-protection -flto=auto -ffat-lto-objects -march=x86-64 -mtune=generic -O3 -pipe -fno-plt -fexceptions -Wp,-D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -fstack-clash-protection -fcf-protection -flto=auto -march=x86-64 -mtune=generic -O3 -pipe -fno-plt -fexceptions -Wp,-D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -fstack-clash-protection -fcf-protection -flto=auto -fPIC -DINTRIN -DVERSION_INFO=\"0.7.3\" -I/home/[...]env/include -I/usr/include/python3.10 -c /tmp/tmpyyycdud6.cpp -o tmp/tmpyyycdud6.o -fvisibility=hidden
  ---------------------------------------------------------------------------
  # INFO:  Finished configuring compiler!
  ---------------------------------------------------------------------------
[...]
Successfully built projectq
Installing collected packages: projectq

and here what I can see in your log file:

Using pip 20.2.3 from /Users/callum/Desktop/pqtest/pqenv/lib/python3.8/site-packages/pip (python 3.8)
Non-user install because user site-packages disabled
[...]
Building wheels for collected packages: projectq
  Created temporary directory: /private/var/folders/p1/hhrjrbd50hq35l9hgk8567tc0000gn/T/pip-wheel-wy2l1r65
  Destination directory: /private/var/folders/p1/hhrjrbd50hq35l9hgk8567tc0000gn/T/pip-wheel-wy2l1r65
  Building wheel for projectq (PEP 517): started
  Building wheel for projectq (PEP 517): finished with status 'done'
  Created wheel for projectq: filename=projectq-0.7.3-cp38-cp38-macosx_10_9_x86_64.whl size=575938 sha256=c7511307644f6bd6a41fcb4f6ada2db1e57cb44ed6e67928717d2ae7cf19af64
  Stored in directory: /private/var/folders/p1/hhrjrbd50hq35l9hgk8567tc0000gn/T/pip-ephem-wheel-cache-cnzfa7df/wheels/11/e6/3b/3b07762c059ac79e89afebf4c6470a685376759d9b542546cc
Successfully built projectq
Installing collected packages: projectq
[...]
CalMacCQ commented 2 years ago

Its possible I could've missed something else so I've reran both of these commands again (see screenshots) Looking in the pq_log3.txt file I also do not see the compiler tests or the other # INFO statements you have shared above so I'm not sure whats gone wrong here.

Screenshot 2022-06-21 at 21 42 44 Screenshot 2022-06-21 at 21 41 29

pq_log3.txt .

CalMacCQ commented 2 years ago

pq_log3.txt

txt file here.

Takishima commented 2 years ago

Could you try to update pip, setuptool and wheel ?

python3 -m pip install -U pip setuptools wheel

Without seeing the full log I cannot help much... essentially, I need to see the lines similar to the ones I have highlighted here

Try creating a new virtual environment and send the full log of the installation.

CalMacCQ commented 2 years ago

Hi again,

I have set up a new virtual environment and updated pip, setuptools and wheel.

python3 -m pip install --no-cache -v projectq --log log_file

Using this command seems to do a better job of logging the installation. I think the command I was using before was the wrong way to do this so apologies.

I can now see the compiler tests in the log and a warning about OpenMP.

I hope the log file is more helpful this time. Thanks

log_file.txt

Takishima commented 2 years ago

Thanks for the new log, this time I can see everything.

Unfortunately, I do not have an Apple M1 machine to test things on, but I am assuming that the issue comes from the fact that we are using AVX2 instructions in the C++ simulator.

The compiler test for it succeeds (since no error message):

...
2022-06-27T10:10:02,359   gcc -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/opt/anaconda3/include -arch x86_64 -I/opt/anaconda3/include -arch x86_64 -I/Users/callum/Desktop/new_test/test_env/include -I/opt/anaconda3/include/python3.8 -c /var/folders/p1/hhrjrbd50hq35l9hgk8567tc0000gn/T/tmpxxr456s8.cpp -o var/folders/p1/hhrjrbd50hq35l9hgk8567tc0000gn/T/tmpxxr456s8.o -mavx2
...

but then the Apple compatibility layer is unable to properly translates the AVX2 instructions when running the code it seems. I will submit a PR to address those issues in the coming days once I have figured out a good way around that.

Takishima commented 2 years ago

In the mean time, try updating your Python version to at least 3.9.1 since that was the first python version that officially supported the Apple M1 (according to https://isapplesiliconready.com/app/Python)

Takishima commented 2 years ago

Just mention that I have not forgotten about this issue and will try to come up with a fix soon.

CalMacCQ commented 2 years ago

Great :) apologies for the gap I got a bit bogged down upgrading my python version.

CalMacCQ commented 2 years ago

Just a note that in the above thread I was using my system python (version 3.8.8). I have now upgraded to python 3.10.6 using pyenv and I have no issues importing and using projectq.

I'm not sure what the issue was here but after upgrading it seems to have resolved itself.

Takishima commented 2 years ago

Ok, so it was indeed due to the fact that Python anterior to 3.9.1 are not officially supported on ARM-powered macs.

I will add a warning to that effect during the setup and potentially disable the C++ backend on ARM-powered macs.