Skip to content

TensorFlow device configuration fails after importing tensorflow_quantum #911

@aapo-kossi

Description

@aapo-kossi

TensorFlow device configuration fails after importing tensorflow_quantum

With the current version of tfq, importing the package before setting TensorFlow device configuration (e.g., enabling memory growth) results in a runtime error:

RuntimeError: Physical devices cannot be modified after being initialized

Expected Behavior
I would expect that importing tensorflow_quantum should not need to trigger device initialization yet, since I have not at this point created any tensors on any device.

Reproducer

import tensorflow as tf
import tensorflow_quantum as tfq

gpus = tf.config.list_physical_devices('GPU')
tf.config.experimental.set_memory_growth(gpus[0], True)  # RuntimeError here

Workaround
Set device configuration before importing tensorflow_quantum. This is fine for me, but since this is unexpected behaviour to me I am documenting it here.

Environment

  • TensorFlow version: 2.15.0.post1
  • TensorFlow Quantum version: 0.7.3
  • Platform: Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/bug-reportSomething doesn't seem to work

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions