Skip to content

Info for Google Colab users about TensorFlow Quantum 0.7.5 #959

@mhucka

Description

@mhucka

As noted in the release notes for TensorFlow Quantum (TFQ) 0.7.5, this version is compatible with TensorFlow 2.16 and Keras 2; however, Colab has version 2.19 preinstalled, and downgrading TensorFlow (TF) in Colab leads to some behaviors that may be confusing. This note is an attempt to provide some clarifications and guidance.

First, please make sure to install TF 2.16.2 and TFQ 0.7.5 with a shell command in a notebook cell early in your Colab notebook:

!pip install tensorflow==2.16.2 tensorflow-quantum==0.7.5

After that, and before you import TensorFlow or TensorFlow Quantum in your Python program, you need to set the environment variable TF_USE_LEGACY_KERAS=1. Here is a code snippet you can place before the first import of tensorflow or tensorflow_quantum:

# Configure use of Keras 2 before importing TensorFlow or TensorFlow Quantum:
import os
os.environ["TF_USE_LEGACY_KERAS"] = "1"

If your code imports Keras directly, also alias keras to tf_keras:

import sys
import tf_keras
sys.modules["keras"] = tf_keras

When you run your notebook, immediately after Colab finishes the !pip install … command, two things will happen:

  1. Pip will print warnings about package version compatibilities. The warnings will look scary but the important part to focus on is it successfully installed Cirq, TFQ, and other packages:
Image
  1. Colab will prompt you to restart the session runtime. You must restart the session before continuing with the rest of your notebook, or else errors will result. (These usually involve NumPy data types.)

After that, things should work normally. You can find examples of using the commands above in the tutorial notebooks provided with TensorFlow Quantum.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/dependenciesInvolves libraries or other software that TFQ depends onarea/docsInvolves documentation – problems, ideas, requestsarea/kerasInvolves Keras, Keras-TFQ interfaces, or related topicsarea/tensorflowInvolves TensorFlowplatform/colabGoogle Colab issues and PRsplatform/linuxLinux issues and PRs.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions