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.9, 3.10, and 3.11 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.15.0

Install the package

Install the latest stable release of TensorFlow Quantum:

  pip3 install -U tensorflow-quantum

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.10 development tools.

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

2. Create a virtual environment

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

  python3.10 -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.15.0. To ensure compatibility we use bazel version 6.5.0. To remove any existing version of Bazel:

  sudo apt-get remove bazel

Download and install bazel version 6.5.0:

  wget https://github.com/bazelbuild/bazel/releases/download/6.5.0/bazel_6.5.0-linux-x86_64.deb

  sudo dpkg -i bazel_6.5.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.15.0.

Download the TensorFlow source code:

  git clone https://github.com/tensorflow/tensorflow.git
  cd tensorflow
  git checkout v2.15.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.23.5
  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

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

Build the TensorFlow Quantum pip package and install:

  ./configure.sh
  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 has successfully been installed, you can run the tests:

  ./scripts/test_all.sh