Skip to content

Latest commit

 

History

History
240 lines (178 loc) · 9.94 KB

install.md

File metadata and controls

240 lines (178 loc) · 9.94 KB

Install TensorFlow Quantum

There are a few ways to set up your environment to use TensorFlow Quantum (TFQ):

  • The easiest way to learn and use TFQ requires no installation—run the TensorFlow Quantum tutorials directly in your browser using Google Colab.
  • To use TensorFlow Quantum on a local machine, install the TFQ package using Python's pip package manager.
  • Or build TensorFlow Quantum from source.

TensorFlow Quantum is supported on Python 3.7, 3.8, and 3.9 and depends directly on Cirq.

Pip package

Requirements

See the TensorFlow install guide to set up your Python development environment and an (optional) virtual environment.

Upgrade pip and install TensorFlow

  pip3 install --upgrade pip
  pip3 install tensorflow==2.11.0

Install the package

Install the latest stable release of TensorFlow Quantum:

  pip3 install -U tensorflow-quantum

Success: TensorFlow Quantum is now installed.

Nightly builds which might depend on newer version of TensorFlow can be installed with:

  pip3 install -U tfq-nightly

Build from source

The following steps are tested for Ubuntu-like systems.

1. Set up a Python 3 development environment

First we need the Python 3.8 development tools.

  sudo apt update
  sudo apt-get install pkg-config zip g++ zlib1g-dev unzip python3.8
  sudo apt install python3.8 python3.8-dev python3.8-venv python3-pip
  python3.8 -m pip install --upgrade pip

2. Create a virtual environment

Go to your workspace directory and make a virtual environment for TFQ development.

  python3.8 -m venv quantum_env
  source quantum_env/bin/activate

3. Install Bazel

As noted in the TensorFlow build from source guide, the Bazel build system will be required.

Our latest source builds use TensorFlow 2.11.0. To ensure compatibility we use bazel version 5.3.0. To remove any existing version of Bazel:

  sudo apt-get remove bazel

Download and install bazel version 5.3.0:

  wget https://github.com/bazelbuild/bazel/releases/download/5.3.0/bazel_5.3.0-linux-x86_64.deb

  sudo dpkg -i bazel_5.3.0-linux-x86_64.deb

To prevent automatic updating of bazel to an incompatible version, run the following:

  sudo apt-mark hold bazel

Finally, confirm installation of the correct bazel version:

  bazel --version

4. Build TensorFlow from source

Here we adapt instructions from the TensorFlow build from source guide, see the link for further details. TensorFlow Quantum is compatible with TensorFlow version 2.11.0.

Download the TensorFlow source code:

  git clone https://github.com/tensorflow/tensorflow.git
  cd tensorflow
  git checkout v2.11.0

Be sure the virtual environment you created in step 2 is activated. Then, install the TensorFlow dependencies:

  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.24.2
  pip install packaging requests

Configure the TensorFlow build. When asked for the Python interpreter and library locations, be sure to specify locations inside your virtual environment folder. The remaining options can be left at default values.

  ./configure

Build the TensorFlow package (Since TF v2.8, _GLIBCXX_USE_CXX11_ABI is set to 1, and the c++ codes are all compiled with -std=c++17):

  bazel build -c opt --cxxopt="-O3" --cxxopt="-march=native" --cxxopt="-std=c++17" --cxxopt="-D_GLIBCXX_USE_CXX11_ABI=1" //tensorflow/tools/pip_package:build_pip_package

Note: It may take over an hour to build the package.

After the build is complete, install the package and leave the TensorFlow directory:

  ./bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
  pip install /tmp/tensorflow_pkg/name_of_generated_wheel.whl
  cd ..

5. Download TensorFlow Quantum

We use the standard fork and pull request workflow for contributions. After forking from the TensorFlow Quantum GitHub page, download the source of your fork and install the requirements:

  git clone https://github.com/username/quantum.git
  cd quantum
  pip install -r requirements.txt

6. Build the TensorFlow Quantum pip package for CPU

Build the TensorFlow Quantum pip package and install:

  ./configure.sh  # Type 'Y' for the first question.
  bazel build -c opt --cxxopt="-O3" --cxxopt="-march=native" --cxxopt="-std=c++17" --cxxopt="-D_GLIBCXX_USE_CXX11_ABI=1" release:build_pip_package
  bazel-bin/release/build_pip_package /tmp/tfquantum/
  python3 -m pip install /tmp/tfquantum/name_of_generated_wheel.whl

To confirm that TensorFlow Quantum for CPU has successfully been installed, you can run the tests:

  ./scripts/test_all.sh

Success: TensorFlow Quantum for CPU is now installed.

7. Build the TensorFlow Quantum pip package for GPU

To enable GPU (cuQuantum) backend, cuStatevec must be installed, see installation guide for details. Importantly, we require that the CUQUANTUM_ROOT environment variable has been set by running the following with your installation path.

  export CUQUANTUM_ROOT=/path/to/cuquantum/installation/dir 

Build the TensorFlow Quantum GPU pip package and install:

  bazel clean --expunge  # If you got stuck `.so` related issue, please clean the cache.
  ./configure.sh  # Type 'n' for the second question.
  bazel build -c opt --config=cuda --cxxopt="-O3" --cxxopt="-march=native" --cxxopt="-std=c++17" --cxxopt="-D_GLIBCXX_USE_CXX11_ABI=1" release:build_pip_package
  bazel-bin/release/build_pip_package /tmp/tfquantum_gpu/
  python3 -m pip install /tmp/tfquantum_gpu/name_of_generated_wheel.whl

To confirm that TensorFlow Quantum for GPU has successfully been installed, you can run the tests:

  ./scripts/test_all.sh gpu

Success: TensorFlow Quantum for GPU is now installed.