Path: blob/master/site/en-snapshot/quantum/tutorials/quantum_data.ipynb
25118 views
Copyright 2020 The TensorFlow Authors.
Quantum data
Building off of the comparisons made in the MNIST tutorial, this tutorial explores the recent work of Huang et al. that shows how different datasets affect performance comparisons. In the work, the authors seek to understand how and when classical machine learning models can learn as well as (or better than) quantum models. The work also showcases an empirical performance separation between classical and quantum machine learning model via a carefully crafted dataset. You will:
Prepare a reduced dimension Fashion-MNIST dataset.
Use quantum circuits to re-label the dataset and compute Projected Quantum Kernel features (PQK).
Train a classical neural network on the re-labeled dataset and compare the performance with a model that has access to the PQK features.
Setup
WARNING: pip is being invoked by an old script wrapper. This will fail in a future version of pip.
Please see https://github.com/pypa/pip/issues/5599 for advice on fixing the underlying issue.
To avoid this problem you can invoke Python with '-m pip' instead of running pip directly.
1. Data preparation
You will begin by preparing the fashion-MNIST dataset for running on a quantum computer.
1.1 Download fashion-MNIST
The first step is to get the traditional fashion-mnist dataset. This can be done using the tf.keras.datasets
module.
Filter the dataset to keep just the T-shirts/tops and dresses, remove the other classes. At the same time convert the label, y
, to boolean: True for 0 and False for 3.
1.2 Downscale the images
Just like the MNIST example, you will need to downscale these images in order to be within the boundaries for current quantum computers. This time however you will use a PCA transformation to reduce the dimensions instead of a tf.image.resize
operation.
The last step is to reduce the size of the dataset to just 1000 training datapoints and 200 testing datapoints.
2. Relabeling and computing PQK features
You will now prepare a "stilted" quantum dataset by incorporating quantum components and re-labeling the truncated fashion-MNIST dataset you've created above. In order to get the most seperation between quantum and classical methods, you will first prepare the PQK features and then relabel outputs based on their values.
2.1 Quantum encoding and PQK features
You will create a new set of features, based on x_train
, y_train
, x_test
and y_test
that is defined to be the 1-RDM on all qubits of:
Where is a wall of single qubit rotations and
First, you can generate the wall of single qubit rotations:
You can quickly verify this works by looking at the circuit:
Next you can prepare with the help of tfq.util.exponential
which can exponentiate any commuting cirq.PauliSum
objects:
This circuit might be a little bit harder to verify by looking at, but you can still examine a two qubit case to see what is happening:
Now you have all the building blocks you need to put your full encoding circuits together:
Choose some qubits and prepare the data encoding circuits:
Next, compute the PQK features based on the 1-RDM of the dataset circuits above and store the results in rdm
, a tf.Tensor
with shape [n_points, n_qubits, 3]
. The entries in rdm[i][j][k]
= where i
indexes over datapoints, j
indexes over qubits and k
indexes over .
2.2 Re-labeling based on PQK features
Now that you have these quantum generated features in x_train_pqk
and x_test_pqk
, it is time to re-label the dataset. To achieve maximum seperation between quantum and classical performance you can re-label the dataset based on the spectrum information found in x_train_pqk
and x_test_pqk
.
Note: This preparation of your dataset to explicitly maximize the seperation in performance between the classical and quantum models might feel like cheating, but it provides a very important proof of existance for datasets that are hard for classical computers and easy for quantum computers to model. There would be no point in searching for quantum advantage in QML if you couldn't first create something like this to demonstrate advantage.
Now you have everything you need to re-label the dataset! Now you can consult with the flowchart to better understand how to maximize performance seperation when re-labeling the dataset:
In order to maximize the seperation between quantum and classical models, you will attempt to maximize the geometric difference between the original dataset and the PQK features kernel matrices using S_pqk, V_pqk
and S_original, V_original
. A large value of ensures that you initially move to the right in the flowchart down towards a prediction advantage in the quantum case.
Note: Computing quantities for and are also very useful when looking to better understand performance seperations. In this case ensuring a large value is enough to see performance seperation.
3. Comparing models
Now that you have prepared your dataset it is time to compare model performance. You will create two small feedforward neural networks and compare performance when they are given access to the PQK features found in x_train_pqk
.
3.1 Create PQK enhanced model
Using standard tf.keras
library features you can now create and a train a model on the x_train_pqk
and y_train_new
datapoints:
3.2 Create a classical model
Similar to the code above you can now also create a classical model that doesn't have access to the PQK features in your stilted dataset. This model can be trained using x_train
and y_label_new
.
3.3 Compare performance
Now that you have trained the two models you can quickly plot the performance gaps in the validation data between the two. Typically both models will achieve > 0.9 accuaracy on the training data. However on the validation data it becomes clear that only the information found in the PQK features is enough to make the model generalize well to unseen instances.
Success: You have engineered a stilted quantum dataset that can intentionally defeat classical models in a fair (but contrived) setting. Try comparing results using other types of classical models. The next step is to try and see if you can find new and interesting datasets that can defeat classical models without needing to engineer them yourself!
4. Important conclusions
There are several important conclusions you can draw from this and the MNIST experiments:
It's very unlikely that the quantum models of today will beat classical model performance on classical data. Especially on today's classical datasets that can have upwards of a million datapoints.
Just because the data might come from a hard to classically simulate quantum circuit, doesn't necessarily make the data hard to learn for a classical model.
Datasets (ultimately quantum in nature) that are easy for quantum models to learn and hard for classical models to learn do exist, regardless of model architecture or training algorithms used.