Quantum Transfer Learning Question

Hi @andreamari, I have some questions regarding the ImageNet dataset.

Has the Hymenoptera dataset already been trained on the truncated network? i.e is it unseen data for the entire network or just the dressed circuit?

Also, why is q_delta set to 0.01?



Hi @James_Ellis,

the truncated network (ResNet18) is pre-trained on the full ImageNet dataset. Hymenoptera is a very small subset of ImageNet.

So, ResNet18 has seen the Hymenoptera dataset a little bit. However it is not optimized on Hymenoptera but on the full ImageNet .

Note that this is not necessary and (classical or quantum) transfer learning can be applied also to a completely new dataset. You can find an example of this scenario in this notebook, where we replace the Hymenoptera dataset with CIFAR (which is unrelated to ImageNet).

Thanks for the reply! I appreciate the help.

Is there any reason why q_delta = 0.01?


Sorry, I forgot to reply about this.

No, there isn’t any particular reason for the choice of q_delta.
This can be considered as one of the hyper-parameters of the model.

The only thing that one can say is that it shouldn’t be be too small (risk of making the initial gradient null) and it shouldn’t be too large (risk of numerical instability of the optimization algorithm).

What is the purpose of apply Hadamard gates to each wire before embedding data? What would happen if we didn’t take this approach.

Thanks for your help!

Hi @James_Ellis,
since in our model we measure in the Z basis, the purpose of the Hadamard gates is to initialize all the qubits in a state which is not biased towards Z=+1 or Z=-1. Indeed the effect of H is to prepare the |+> state, for which the expectation value of Z is zero.

What would happen if we remove the first layer of H gates? Probably the final result would be similar but maybe the training time would be longer because of the different initial condition. This is just a guess, but the best way to know is to try and see how it goes.


Dear andreamari.

Thank you for your explanations. How can I use/upload my own image dataset instead of Hymenoptera? In what format do they need to be?

Hello @_risto,
in this example we used PyTorch to load the dataset.
So if you want to use a different dataset you can have a look
at the PyTorch documentation of torchvision.datasetslink. In particular, in our example we used the torchvision.datasets.ImageFolder data loader link. In this case the dataset is loaded from a folder which contains images organized in subfolders associated to the different classes. For example, the Hymenoptera folder has this structure.

1 Like

Thank you for the prompt answer andreamari. Does this apply for loading a custom dataset for the following algorithm as well: https://pennylane.ai/qml/demos/tutorial_embeddings_metric_learning.html ?

Hi @_risto,

The embeddings and metric learning demo uses the Numpy interface instead of PyTorch, so data is loaded there using the function numpy.loadtxt() (you can see this in the “Data” subsection).

However, one could also implement that example fully using PyTorch. In that case, you could use PyTorch’s built-in data loaders, as @andreamari describes above.

1 Like

Hello andreamari.

Is it possible to run this algorithm with 3d pictures / 3d object recontruction dataset?

Hi @_risto,

in principle, it is possible to apply the quantum transfer learning trick to any classical problem. One has to find a good classical network which is good for the task of interest (or a similar generic task): e.g. 3D object reconstruction, 3D image classification, etc. Then one must replace some of the final layers of the classical network with a variational quantum circuit which can be trained for the specific task of interest.

1 Like

Hi andreamari.

What would be the minimum amount of data needed to train this network (let’s say I am using either a ResNet18, ResNet50 or resNet151 network)? I have 159 cases, for each case 3 different modality of picture, which I was planning to divide roughly in - training : validation : testing = 7 : 2 : 1 ratio.

Hi @_risto,

transfer learning requires training only the final weights of the full network and so it is particularly convenient when the dataset is small. In your case the dataset very small :slight_smile: but it may still work. If data is not enough you may try reducing the complexity of the quantum layer, e.g., by using less qubits or less variational layers.

1 Like

Hi again.

While trying to run the code in the 1st paragraph of Dataset loading, I get the following error:

NameError Traceback (most recent call last)
1 data_transforms = {
----> 2 “train”: transforms.Compose([
3 # transforms.RandomResizedCrop(224), # uncomment for data augmentation
4 # transforms.RandomHorizontalFlip(), # uncomment for data augmentation
5 transforms.Resize(256),

NameError: name ‘transforms’ is not defined

I’ve followed all the previous steps. I wanted to recreate the same experiment, just to see, how it goes.

Hi @_risto,
did you run the previous code blocks too?
It looks like the line from torchvision import datasets, transforms was not executed.

Hi andreamari.
Yes, I did and have installed torchvision before, although I’ve done it in jupyter notebook. Should I use some other program, like pycharm to run the whole code?

Hi @_risto,
it should work both with a jupyter notebook and with a standard Python script. I would suggest you to open a Python shell (or a Jupyter notebook ) and execute, one by one, the following 2 instructions (in a Jupyter notebook you can write them in 2 separate cells):

  1. from torchvision import transforms
  2. t = transforms

Do you get any error?
If you get an error, there is a problem with the installation of torch or torchvision.
Otherwise, you can try to execute each line of code from the tutorial, one by one, to see if and where you get the first error.

Hey andremari.

No error. I did go step by step. Now I got" NameError: name ‘datasets’ is not defined", repeated what you have written but changed transforms to datasets, run the code again, get “NameError: name ‘os’ is not defined”. I went step by step. Is the whole code really 100% fully written on the page?

Hi @_risto,
I am quite sure that this tutorial is complete (no code missing).

The errors that you mention seem to be related to the non-execution of one or more import instructions. But if you are sure that this is not the case, I don’t know how to help.

One last suggestion that just came to my mind is that you could try to download this Python script which is equivalent the tutorial (actually the html page of the tutorial is automatically generated from this file).

After downloading the script, you can try to execute it by running on a terminal:
python tutorial_quantum_transfer_learning.py.

Depending on how Python was installed in your computer, you may need to use the command python3 instead of python.

1 Like