Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Tensorflow backend #596

Merged
merged 20 commits into from
Jul 4, 2022
Merged

Add Tensorflow backend #596

merged 20 commits into from
Jul 4, 2022

Conversation

stavros11
Copy link
Member

This is an attempt to add the tensorflow backend on top of numpy with as little additional code as possible. It is based on tensorflow.experimental.numpy and the idea is to use self.np (instead of np) in every numpy call used in the numpy backend and then replace this with self.np = tnp for the tensorflow backend. It seems to work fairly well and it includes GPU, since the tnp ND array is an alias for tf.Tensor and not a numpy array. I have not tested backpropagation but at least according to their docs it is expected to work.

One potential disadvantage with this approach is that it requires to have a reference to the numpy module in the numpy backend, by doing self.np = np. This breaks the pickle serialization of the backend object because Python's pickle cannot pickle modules. Note that dill does not have this limitation (related stackoverflow discussion), however I am not sure how efficient this is, mostly in terms of the pkl file size. Moreover, this breaks parallel.py because multiprocessing uses pickle.

@stavros11 stavros11 requested a review from scarrazza June 7, 2022 10:26
@stavros11 stavros11 mentioned this pull request Jun 8, 2022
23 tasks
@scarrazza
Copy link
Member

@stavros11 could you please fix conflicts?

Copy link
Member

@scarrazza scarrazza left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stavros11 thanks, I have 2 comments:

  • in this branch qibo does not show information about the backend in the first import, only after typing qibo.set_backend("tensorflow").
  • I cannot switch from CPU to GPU, the code prints "Using tensorflow backend on /CPU:0" but performance looks like GPU.

@stavros11
Copy link
Member Author

@stavros11 could you please fix conflicts?

Thank you for reviewing, done.

  • in this branch qibo does not show information about the backend in the first import, only after typing qibo.set_backend("tensorflow").

This is normal after the refactoring because no backend is initialized during import. If you do an operation that requires backend, such as executing a circuit or creating a Hamiltonian, then the GlobalBackend singleton will be created and the info message backend will appear. Creating a circuit and adding gates will not trigger backend initialization as these objects are now backend agnostic.

  • I cannot switch from CPU to GPU, the code prints "Using tensorflow backend on /CPU:0" but performance looks like GPU.

Indeed the device switcher and default device identification was not implemented here. It should work properly after the latest push.

@scarrazza scarrazza changed the base branch from abstrchannels to abstractions July 4, 2022 06:29
@scarrazza scarrazza merged commit d1effb0 into abstractions Jul 4, 2022
@stavros11 stavros11 deleted the abstrtnp branch July 5, 2022 12:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants