tensorflow / quantum

An open-source Python framework for hybrid quantum-classical machine learning.
https://www.tensorflow.org/quantum
Apache License 2.0
1.81k stars 580 forks source link

Installation Issues on Apple M1 device #655

Open stfnmangini opened 2 years ago

stfnmangini commented 2 years ago

I'm having troubles installing tensorflow-quantum on an Apple M1 device.

Tensorflow installation is fine following Apple's official guide (https://developer.apple.com/metal/tensorflow-plugin/), though I am not able to install tfq, either via pip or building from source:


Step to reproduce the problem:

First install Tensorflow following Apple's guide, then follow tfq installation procedure https://github.com/tensorflow/quantum/blob/master/docs/install.md

  git clone https://github.com/tensorflow/tensorflow.git
  cd tensorflow
  git checkout v2.7.0 (same version obtained with   python -m pip tensorflow-macos)

  pip install -U pip six numpy wheel setuptools mock 'future>=0.17.1'
  pip install -U keras_applications --no-deps
  pip install -U keras_preprocessing --no-deps
  pip install numpy==1.19.5  (fails to compile wheels, must install with conda install numpy=1.19.5 instead)

  ./configure

WARNING: Package(s) not found: tensorflow
WARNING: Package(s) not found: tf-nightly
WARNING: Package(s) not found: tensorflow-cpu
WARNING: Package(s) not found: tf-nightly-cpu
Installing tensorflow .....\n
ERROR: Could not find a version that satisfies the requirement tensorflow (from versions: none)
ERROR: No matching distribution found for tensorflow

If modifying the configure.sh by having it look for tensorflow-macos (instead of tensorflow), then problems occur when building with Bazel (the required distribution 3.7.2 is not available for M1 architecture). Installing from source also Tensorflow itself comes with its own bag of installation issues (versions incompatibilities, problems with packages like numpy, grpcio, h5f, ...), and doesn't feel like the best way to proceed.

Is there a workaround or fix? How should one proceed? Thanks!

lockwo commented 2 years ago

I can't check what works (the only mac I have access to has an i9), but if the problem for pip installing is TF == 2.4.1, could you work with the nightly build? That has TF 2.7. The nightly pip install also break?

stfnmangini commented 2 years ago

I can't check what works (the only mac I have access to has an i9), but if the problem for pip installing is TF == 2.4.1, could you work with the nightly build? That has TF 2.7. The nightly pip install also break?

Hi Owen, thanks for the suggestion! I've just tried installing tfq-nightly using stadard pip install tfq-nightly in the conda environment with the M1-optimized tensorflow, but it yields the following error

ERROR: Could not find a version that satisfies the requirement tfq-nightly (from versions: none)
ERROR: No matching distribution found for tfq-nightly

Again, this must have something to do with tfq-nightly looking for package tensorflow instead of tensorflow-macos. Also, having a look at the tfq-nightly wheels (https://pypi.org/project/tfq-nightly/#files) they are for x86_64 architecture, and not M1-arm64 one. Though, I know very little about architectures, compilations and builds, so I may be getting something wrong.


However, after digging some more I found a way to install it using the x86_64 architecure! I don't know exactly if it is the minimal (or most efficient) solution, but it worked for me (at least for installation and import, haven't tested it in detail yet).

Solution: Installing tensorflow-quantum with x86_64 architecture on M1 device

  1. Install Rosetta: run on terminal softwareupdate --install-rosetta

  2. Create a Rosetta-Terminal: go to Application folder and duplicate the Terminal (or iTerm) app. Open the "Get info" tab of the duplicate terminal, and check the "Open with Rosetta" option. Rename the duplicate app (like "Rosetta-iTerm") to distinguish it from the original one (M1 arm64-based).

  3. Create Environment: create a python environment using the default python3 distribution (not using conda). Open the just created Rosetta-terminal, and run

    python3 -m venv ~/tensorflow-env
    source ~/tensorflow-env/bin/activate
    python -m pip install -U pip
  4. Install Tensorflow: following https://github.com/tensorflow/tensorflow/issues/46044#issuecomment-828347288, download the wheels for tensorflow v2.4.1 from the provided link, and run pip install /position/of/file/tensorflow-2.4.1-py3-none-any.whl

  5. Install Tensorflow Quantum: just pip install tensorflow-quantum

Note: always use the Rosetta enabled-terminal for activating the environment and using these tensorflow installations, otherwise you receive compatibility errors.

My default version of python3 is Python 3.8.9. Follwing these steps I could install all packages without errors, and import them as well. I still haven't tested in detail the performances and functionalities. Also, it remains to be understood how to set up tfq with the optimized M1 tensorflow distribution.

Hope this works for others as well!

owenonline commented 1 year ago

I followed these steps and still get the error:

ERROR: Could not find a version that satisfies the requirement tensorflow-quantum (from versions: none) ERROR: No matching distribution found for tensorflow-quantum

kyujin2228 commented 1 year ago

I got the same error, too.