Skip to content

Latest commit

 

History

History
113 lines (81 loc) · 2.94 KB

development_Mac.md

File metadata and controls

113 lines (81 loc) · 2.94 KB

Tensorcircuit Installation Guide on MacOS

Contributed by Mark (Zixuan) Song

Apple has updated Tensorflow (for MacOS) so that installation on M-series (until M2) and Intel-series Mac can follow the exact same procedure.

Starting From Scratch

For completely new Macos or Macos without Xcode installed.

If you have Xcode installed, skip to Install TC backends.

Install Xcode Command Line Tools

Need graphical access to the machine.

Run xcode-select --install to install if on optimal internet.

Or Download it from Apple Command Line Tools installation image then install it if the internet connection is weak.

Install TC Backends

There are four backends to choose from, Numpy, Tensorflow, Jax, and Torch.

Install Jax, Pytorch (Optional)

pip install [Package Name]

Install Tensorflow (Optional - Recommended)

Install Miniconda (Optional - Recommended)

If you wish to install Tensorflow optimized for MacOS (tensorflow-macos) or Tensorflow GPU optimized (tensorflow-metal) please install miniconda.

If you wish to install Vanilla Tensorflow developed by Google (tensorflow) please skip this step.

curl -o ~/miniconda.sh https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-arm64.sh
bash ~/miniconda.sh -b -p $HOME/miniconda
source ~/miniconda/bin/activate
conda install -c apple tensorflow-deps

Installation

pip install tensorflow

If you wish to use tensorflow-metal PluggableDevice, then continue install (not recommended):

pip install tensorflow-metal

Verify Tensorflow Installation

import tensorflow as tf

cifar = tf.keras.datasets.cifar100
(x_train, y_train), (x_test, y_test) = cifar.load_data()
model = tf.keras.applications.ResNet50(
    include_top=True,
    weights=None,
    input_shape=(32, 32, 3),
    classes=100,)

loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
model.compile(optimizer="adam", loss=loss_fn, metrics=["accuracy"])
model.fit(x_train, y_train, epochs=5, batch_size=64)

Install Tensorcircuit

pip install tensorcircuit

Benchmarking

This data is collected by running benchmarks/scripts/vqe_tc.py 10 times and average results.

Vanilla Tensorflow Apple Tensorflow Apple Tensorflow with Metal Plugin
Construction Time 11.49241641s 11.31878941s 11.6103961s
Iteration time 0.002313011s 0.002333004s 0.046412581s
Total time 11.72371747s 11.55208979s 16.25165417s

Until July 2023, this has been tested on Intel Macs running Ventura, M1 Macs running Ventura, M2 Macs running Ventura, and M2 Macs running Sonoma beta.