# Quantum NLP Transfer Learning

**URL:** <https://discuss.pennylane.ai/t/quantum-nlp-transfer-learning/1270>\
**Category:** PennyLane Help\
**Created:** [August 11, 2021, 2:32pm UTC](https://discuss.pennylane.ai/t/quantum-nlp-transfer-learning/1270 "2021-08-11T14:32:27Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![dancbeaulieu](https://avatars.discourse-cdn.com/v4/letter/d/7cd45c/32.png) [@dancbeaulieu](https://discuss.pennylane.ai/u/dancbeaulieu)\
**Post date:** [August 11, 2021, 2:32pm UTC](https://discuss.pennylane.ai/t/quantum-nlp-transfer-learning/1270/1 "2021-08-11T14:32:28Z")

</div>

Hello,

I created a QNLP transfer learning program using Pytorch and DistilBERT, and it runs correctly when I run it classically on a local simulator. However, I cannot get it to run on either IBMQ or AWS Braket. I have been trying to diagnose and fix this issue for over a month, and just can’t figure out what is incorrect. The issue is that when I try to run on IBMQ, it doesn’t run on the IBMQ network and just runs locally, despite what I set the IBMQ device to. It runs, creates results but no log on the backend of the service. As for when I run this code on AWS Braket, it holds forever on the training phase (line starting runner.train()) and doesn’t show up in the Braket “tasks” log. It doesn’t work on quantum hardware or in the simulator.

Here is a link to the data: [https://github.com/dancbeaulieu/publicshare/blob/main/sdg\_classification.zip](https://github.com/dancbeaulieu/publicshare/blob/main/sdg_classification.zip)

Here is my code:  
#!/usr/bin/env python

# coding: utf-8

path\_to\_data= ‘data/sdg\_classification’  
train\_filename= ‘train\_set\_sdg\_1\_7\_8\_12\_13\_toy.csv’  
validation\_filename= ‘val\_set\_sdg\_1\_7\_8\_12\_13\_toy.csv’  
test\_filename= ‘eval\_set\_sdg\_1\_7\_8\_12\_13\_curated\_journals\_toy.csv’  
text\_field\_name= ‘title\_keywords\_abstract’  
label\_field\_name= ‘sdg\_id’  
path\_to\_test\_pred\_scores= ‘data/output/pred.txt’

#model:  
model\_name=‘distilbert-base-uncased’ # pretrained model from Transformers  
max\_seq\_length= 256 # depends on your available GPU memory (in combination with batch size)  
num\_classes= 5  
n\_in = 5 #input into quantum layer

#training:  
learn\_rate= 3e-5 # learning rate is typically ~1e-5 for transformers  
num\_epochs= 2 # smth around 2-6 epochs is typically fine when finetuning transformers  
accum\_steps= 4 # one optimization step for that many backward passes  
batch\_size= 8 # depends on your available GPU memory (in combination with max seq length)  
log\_dir=‘logdir’ # for training logs and tensorboard visualizations  
fp16\_params= None # fp16 support  
q\_depth = 2 # Depth of the quantum circuit (number of variational layers)  
n\_qubits = 2 # Number of qubits  
step = 0.0004 # Learning rate

import time  
#general:  
seed=17 # random seed for reproducibility  
gamma\_lr\_scheduler = 0.1 # Learning rate reduction applied every 10 epochs.  
q\_delta = 0.01 # Initial spread of random quantum weights  
start\_time = time.time() # Start of the computation timer

train\_samples = 100  
test\_samples = 50  
#data\_dir = “texts/.arxiv/”  
validation\_split = .2  
shuffle\_dataset = True

import logging  
from pathlib import Path  
from typing import List, Mapping, Tuple

# PyTorch

import torch  
import torch.nn as nn  
import torch.optim as optim  
from torch.optim import lr\_scheduler  
#import torchvision  
#from torchvision import datasets, transforms  
from torch.utils.data import DataLoader, Dataset

import pandas as pd  
import torch

from transformers import AutoTokenizer  
#from catalyst.utils import set\_global\_seed  
import torch.nn as nn  
from transformers import AutoConfig, AutoModel, AutoTokenizer, BertForSequenceClassification, AutoModelForSequenceClassification, Trainer, TrainingArguments

from pathlib import Path  
from torch.optim import lr\_scheduler  
from torch.utils.data.sampler import SubsetRandomSampler  
import torch.optim as optim  
import numpy as np  
import torch  
import yaml  
#from catalyst.dl import SupervisedRunner  
import pennylane as qml  
import copy  
import qiskit  
from qiskit import IBMQ

#Catalyst version 21.X breaks all 20.0 imports  
from catalyst.dl import SupervisedRunner  
from catalyst.dl.callbacks import (  
AccuracyCallback,  
CheckpointCallback,  
InferCallback,  
OptimizerCallback,  
)  
from catalyst.utils import prepare\_cudnn, set\_global\_seed

#FOR CONNECTING TO IBMQ, REPLACED HUB, GROUP & PROJECT WITH FAKE INFO

#token = ‘FAKE TOKEN’  
#IBMQ.save\_account(token)

#IBMQ.load\_account()  
#provider = IBMQ.get\_provider(hub=‘HUB’, group=‘GROUP’, project=‘PROJECT’)  
#dev = qml.device(“qiskit.ibmq”, wires=n\_qubits, backend=“ibmq\_bogota”, provider=provider)

#dev = qml.device(“default.qubit”, wires=n\_qubits)  
#device = torch.device(“cuda:0” if torch.cuda.is\_available() else “cpu”)

# Please enter the S3 bucket you created during onboarding

# (or any other S3 bucket starting with ‘amazon-braket-’ in your account) in the code below

#CHANGED BUCKET NAME TO PROTECT MY INFORMATION  
my\_bucket = “braket-bucket-fake” # the name of the bucket  
my\_prefix = “braket” # the name of the folder in the bucket  
s3\_folder = (my\_bucket, my\_prefix)

#Pennylane Hardware config  
#device\_arn = “arn:aws:braket:::device/quantum-simulator/amazon/sv1”  
#device\_arn = “arn:aws:braket:::device/qpu/ionq/ionQdevice”  
device\_arn = “arn:aws:braket:::device/qpu/rigetti/Aspen-9”  
dev\_remote = qml.device(  
“braket.aws.qubit”,  
device\_arn=device\_arn,  
wires=n\_qubits,  
s3\_destination\_folder=s3\_folder,  
parallel=True,  
)

device = torch.device(“cuda:0” if torch.cuda.is\_available() else “cpu”)

def get\_project\_root() -\> Path:  
return Path( **file** ).parent.parent

#data  
class TextClassificationDataset(Dataset):  
“”"  
Wrapper around Torch Dataset to perform text classification  
“”"

```
def __init__ (
    self,
    texts: List[str],
    labels: List[str] = None,
    label_dict: Mapping[str, int] = None,
    max_seq_length: int = 512,
    model_name: str = "distilbert-base-uncased",
):
    """
    Args:
        texts (List[str]): a list with texts to classify or to train the
            classifier on
        labels List[str]: a list with classification labels (optional)
        label_dict (dict): a dictionary mapping class names to class ids,
            to be passed to the validation data (optional)
        max_seq_length (int): maximal sequence length in tokens,
            texts will be stripped to this length
        model_name (str): transformer model name, needed to perform
            appropriate tokenization

    """

    self.texts = texts
    self.labels = labels
    self.label_dict = label_dict
    self.max_seq_length = max_seq_length

    if self.label_dict is None and labels is not None:
        # {'class1': 0, 'class2': 1, 'class3': 2, ...}
        # using this instead of `sklearn.preprocessing.LabelEncoder`
        # no easily handle unknown target values
        self.label_dict = dict(zip(sorted(set(labels)), range(len(set(labels)))))

    self.tokenizer = AutoTokenizer.from_pretrained(model_name)
    # suppresses tokenizer warnings
    logging.getLogger("transformers.tokenization_utils").setLevel(logging.FATAL)

    # special tokens for transformers
    # in the simplest case a [CLS] token is added in the beginning
    # and [SEP] token is added in the end of a piece of text
    # [CLS] <indexes text tokens> [SEP] .. <[PAD]>
    self.sep_vid = self.tokenizer.vocab["[SEP]"]
    self.cls_vid = self.tokenizer.vocab["[CLS]"]
    self.pad_vid = self.tokenizer.vocab["[PAD]"]

def __len__ (self) -> int:
    """
    Returns:
        int: length of the dataset
    """
    return len(self.texts)

def __getitem__ (self, index) -> Mapping[str, torch.Tensor]:
    """Gets element of the dataset

    Args:
        index (int): index of the element in the dataset
    Returns:
        Single element by index
    """

    # encoding the text
    x = self.texts[index]

    # a dictionary with `input_ids` and `attention_mask` as keys
    output_dict = self.tokenizer.encode_plus(
        x,
        add_special_tokens=True,
        padding="max_length",
        max_length=self.max_seq_length,
        return_tensors="pt",
        truncation=True,
        return_attention_mask=True,
    )

    # for Catalyst, there needs to be a key called features
    output_dict["features"] = output_dict["input_ids"].squeeze(0)
    del output_dict["input_ids"]

    # encoding target
    if self.labels is not None:
        y = self.labels[index]
        y_encoded = torch.Tensor([self.label_dict.get(y, -1)]).long().squeeze(0)
        output_dict["targets"] = y_encoded

    return output_dict

```

def read\_data(params: dict) -\> Tuple[dict, dict]:  
“”"  
A custom function that reads data from CSV files, creates PyTorch datasets and  
data loaders. The output is provided to be easily used with Catalyst

```
:param params: a dictionary read from the config.yml file
:return: a tuple with 2 dictionaries
"""
# reading CSV files to Pandas dataframes
train_df = pd.read_csv(
    Path(params["data"]["path_to_data"]) / params["data"]["train_filename"]
)
valid_df = pd.read_csv(
    Path(params["data"]["path_to_data"]) / params["data"]["validation_filename"]
)
test_df = pd.read_csv(
    Path(params["data"]["path_to_data"]) / params["data"]["test_filename"]
)

# creating PyTorch Datasets
train_dataset = TextClassificationDataset(
    texts=train_df[params["data"]["text_field_name"]].values.tolist(),
    labels=train_df[params["data"]["label_field_name"]].values,
    max_seq_length=params["model"]["max_seq_length"],
    model_name=params["model"]["model_name"],
)

valid_dataset = TextClassificationDataset(
    texts=valid_df[params["data"]["text_field_name"]].values.tolist(),
    labels=valid_df[params["data"]["label_field_name"]].values,
    max_seq_length=params["model"]["max_seq_length"],
    model_name=params["model"]["model_name"],
)

test_dataset = TextClassificationDataset(
    texts=test_df[params["data"]["text_field_name"]].values.tolist(),
    labels=test_df[params["data"]["label_field_name"]].values,
    max_seq_length=params["model"]["max_seq_length"],
    model_name=params["model"]["model_name"],
)

set_global_seed(params["general"]["seed"])

# creating PyTorch data loaders and placing them in dictionaries (for Catalyst)
train_val_loaders = {
    "train": DataLoader(
        dataset=train_dataset,
        batch_size=params["training"]["batch_size"],
        shuffle=True,
    ),
    "valid": DataLoader(
        dataset=valid_dataset,
        batch_size=params["training"]["batch_size"],
        shuffle=False,
    ),
}

test_loaders = {
    "test": DataLoader(
        dataset=test_dataset,
        batch_size=params["training"]["batch_size"],
        shuffle=False,
    )
}

return train_val_loaders, test_loaders, train_dataset, valid_dataset, test_dataset

```

class BertForSequenceClassification(nn.Module):  
“”"  
Simplified version of the same class by HuggingFace.  
See transformers/modeling\_distilbert.py in the transformers repository.  
“”"

```
def __init__ (
    self, pretrained_model_name: str, num_classes: int = None, dropout: float = 0.3
):
    """
    Args:
        pretrained_model_name (str): HuggingFace model name.
            See transformers/modeling_auto.py
        num_classes (int): the number of class labels
            in the classification task
    """
    super(). __init__ ()

    config = AutoConfig.from_pretrained(
        pretrained_model_name, num_labels=num_classes
    )

    self.model = AutoModel.from_pretrained(pretrained_model_name, config=config)
    #for param in self.parameters():
    # param.requires_grad = False
    self.classifier = nn.Linear(config.hidden_size, num_classes)
    self.dropout = nn.Dropout(dropout)

def forward(self, features, attention_mask=None, head_mask=None):
    """Compute class probabilities for the input sequence.

    Args:
        features (torch.Tensor): ids of each token,
            size ([bs, seq_length]
        attention_mask (torch.Tensor): binary tensor, used to select
            tokens which are used to compute attention scores
            in the self-attention heads, size [bs, seq_length]
        head_mask (torch.Tensor): 1.0 in head_mask indicates that
            we keep the head, size: [num_heads]
            or [num_hidden_layers x num_heads]
    Returns:
        PyTorch Tensor with predicted class scores
    """
    assert attention_mask is not None, "attention mask is none"

    # taking BERTModel output
    # see https://huggingface.co/transformers/model_doc/bert.html#transformers.BertModel
    bert_output = self.model(
        input_ids=features, attention_mask=attention_mask, head_mask=head_mask
    )
    # we only need the hidden state here and don't need
    # transformer output, so index 0
    seq_output = bert_output[0] # (bs, seq_len, dim)
    # mean pooling, i.e. getting average representation of all tokens
    pooled_output = seq_output.mean(axis=1) # (bs, dim)
    pooled_output = self.dropout(pooled_output) # (bs, dim)
    scores = self.classifier(pooled_output) # (bs, num_classes)

    return scores

```

with open(str(“config.yml”)) as f:  
params = yaml.load(f, Loader=yaml.FullLoader)

def H\_layer(nqubits):  
“”“Layer of single-qubit Hadamard gates.  
“””  
for idx in range(nqubits):  
qml.Hadamard(wires=idx)

def RY\_layer(w):  
“”“Layer of parametrized qubit rotations around the y axis.  
“””  
for idx, element in enumerate(w):  
#print(‘w in RY\_layer’, type(w))  
qml.RY(element, wires=idx)

def RX\_layer(w):  
“”“Layer of parametrized qubit rotations around the X axis.  
“””  
for idx, element in enumerate(w):  
#print(‘w in RY\_layer’, type(w))  
qml.RX(element, wires=idx)

def RZ\_layer(w):  
“”“Layer of parametrized qubit rotations around the Z axis.  
“””  
for idx, element in enumerate(w):  
#print(‘w in RY\_layer’, type(w))  
qml.RZ(element, wires=idx)

def entangling\_layer(nqubits):  
“”“Layer of CNOTs followed by another shifted layer of CNOT.  
“””  
# In other words it should apply something like :  
# CNOT CNOT CNOT CNOT… CNOT  
# CNOT CNOT CNOT… CNOT  
for i in range(0, nqubits - 1, 2): # Loop over even indices: i=0,2,…N-2  
qml.CNOT(wires=[i, i + 1])  
for i in range(1, nqubits - 1, 2): # Loop over odd indices: i=1,3,…N-3  
qml.CNOT(wires=[i, i + 1])

def entangling\_layer\_alt(nqubits):  
“”“Layer of CNOTs followed by another shifted layer of CNOT.  
“””  
# In other words it should apply something like :  
# CNOT CNOT CNOT CNOT… CNOT  
# CNOT CNOT CNOT… CNOT  
for i in range(0, nqubits - 1, 2): # Loop over even indices: i=0,2,…N-2  
qml.CNOT(wires=[i, i + 1])  
for i in range(1, nqubits - 1, 2): # Loop over odd indices: i=1,3,…N-3  
qml.CNOT(wires=[i, i + 1])

#Original version of quantum circuit used in Mari et al 2019  
@qml.qnode(dev\_remote, interface=“torch”)  
def quantum\_net(q\_input\_features, q\_weights\_flat):  
“”"  
The variational quantum circuit.  
“”"

```
# Reshape weights
q_weights = q_weights_flat.reshape(q_depth, n_qubits)

# Start from state |+> , unbiased w.r.t. |0> and |1>
H_layer(n_qubits)

# Embed features in the quantum node
RY_layer(q_input_features)

# Sequence of trainable variational layers
for k in range(q_depth):
    entangling_layer(n_qubits)
    RY_layer(q_weights[k])

# Expectation values in the Z basis
exp_vals = [qml.expval(qml.PauliZ(position)) for position in range(n_qubits)]
return tuple(exp_vals)

```

# read and process data

train\_val\_loaders, test\_loaders, train\_dataset, valid\_dataset, test\_dataset = read\_data(params)

# reproducibility

set\_global\_seed(seed)  
prepare\_cudnn(deterministic=True)

class DressedQuantumNet(nn.Module):  
“”"  
Torch module implementing the _dressed_ quantum net.  
“”"

```
def __init__ (self):
    """
    Definition of the *dressed* layout.
    """

    super(). __init__ ()
    self.pre_net = nn.Linear(num_classes, n_qubits)
    self.q_params = nn.Parameter(q_delta * torch.randn(q_depth * n_qubits))       
    self.post_net = nn.Linear(n_qubits, 2)

def forward(self, input_features):
    """
    Defining how tensors are supposed to move through the *dressed* quantum
    net.
    """

    # obtain the input features for the quantum circuit
    # by reducing the feature dimension from 512 to 4
    pre_out = self.pre_net(input_features)
    q_in = torch.tanh(pre_out) * np.pi / 2.0

    # Apply the quantum circuit to each element of the batch and append to q_out
    q_out = torch.Tensor(0, n_qubits)
    q_out = q_out.to(device)
    for elem in q_in:
        q_out_elem = quantum_net(elem, self.q_params).float().unsqueeze(0)
        #quantum_net(elem, self.q_params).float().unsqueeze(0)
        q_out = torch.cat((q_out, q_out_elem))

    # return the two-dimensional prediction from the postprocessing layer
    return self.post_net(q_out)

```

class BertForSequenceClassification\_qnlp(nn.Module):  
“”"  
Simplified version of the same class by HuggingFace.  
See transformers/modeling\_distilbert.py in the transformers repository.  
“”"

```
def __init__ (
    self, pretrained_model_name: str, num_classes: int = None, dropout: float = 0.3
):
    """
    Args:
        pretrained_model_name (str): HuggingFace model name.
            See transformers/modeling_auto.py
        num_classes (int): the number of class labels
            in the classification task
    """
    super(). __init__ ()

    config = AutoConfig.from_pretrained(
        pretrained_model_name, num_labels=num_classes
    )

    self.model = AutoModel.from_pretrained(pretrained_model_name, config=config)

    #for param in self.parameters():
    # param.requires_grad = False 

    self.classifier = nn.Linear(config.hidden_size, num_classes)
    self.linear = DressedQuantumNet()
    self.dropout = nn.Dropout(dropout)

def forward(self, features, attention_mask=None, head_mask=None):
    """Compute class probabilities for the input sequence.

    Args:
        features (torch.Tensor): ids of each token,
            size ([bs, seq_length]
        attention_mask (torch.Tensor): binary tensor, used to select
            tokens which are used to compute attention scores
            in the self-attention heads, size [bs, seq_length]
        head_mask (torch.Tensor): 1.0 in head_mask indicates that
            we keep the head, size: [num_heads]
            or [num_hidden_layers x num_heads]
    Returns:
        PyTorch Tensor with predicted class scores
    """
    assert attention_mask is not None, "attention mask is none"

    # taking BERTModel output
    # see https://huggingface.co/transformers/model_doc/bert.html#transformers.BertModel
    bert_output = self.model(
        input_ids=features, attention_mask=attention_mask, head_mask=head_mask
    )
    # we only need the hidden state here and don't need
    # transformer output, so index 0
    seq_output = bert_output[0] # (bs, seq_len, dim)
    # mean pooling, i.e. getting average representation of all tokens
    pooled_output = seq_output.mean(axis=1) # (bs, dim)
    pooled_output = self.dropout(pooled_output) # (bs, dim)
    scores = self.classifier(pooled_output) # (bs, num_classes)

    return scores

```

model\_dressnet = BertForSequenceClassification\_qnlp(  
pretrained\_model\_name=model\_name ,  
num\_classes=num\_classes  
)

# Use CUDA or CPU according to the “device” object.

model\_dressnet = model\_dressnet.to(device)

# specify criterion for the multi-class classification task, optimizer and scheduler

criterion = torch.nn.CrossEntropyLoss()  
optimizer = torch.optim.Adam(  
model\_dressnet.parameters(), lr=float(learn\_rate)  
)  
scheduler = torch.optim.lr\_scheduler.ReduceLROnPlateau(optimizer)

# here we specify that we pass masks to the runner. So model’s forward method will be called with

# these arguments passed to it.

runner = SupervisedRunner(input\_key=(“features”, “attention\_mask”))

#quantum\_net\_alt, original alternate version based on randomized quantum circuit

## finally, training the model with Catalyst

runner.train(  
#input\_key=(“features”, “attention\_mask”),  
model=model\_dressnet,  
criterion=criterion,  
optimizer=optimizer,  
scheduler=scheduler,  
loaders=train\_val\_loaders,  
callbacks=[  
AccuracyCallback(num\_classes=num\_classes),  
OptimizerCallback(accumulation\_steps=accum\_steps),  
],  
logdir=log\_dir,  
num\_epochs=num\_epochs,  
verbose=True  
)

---

<div class="post-metadata">

**Author:** ![christina](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/christina/32/583_2.png) [@christina](https://discuss.pennylane.ai/u/christina)\
**Post date:** [August 12, 2021, 12:50pm UTC](https://discuss.pennylane.ai/t/quantum-nlp-transfer-learning/1270/2 "2021-08-12T12:50:44Z")

</div>

Edit: correcting for typo. Sorry for that…

Thanks for your question.

Have you been able to run any individual simple test circuits on either of the platforms?

Before getting the full workflow evaluated, I often find it helpful to make sure I have everything set up correctly with a qnode like

```auto
@qml.qnode(dev)
def circuit(x):
    qml.RX(x,wires=0)
    return qml.expval(qml.PauliZ(0))

circuit(0.1)

```

Then I know for sure its the remote communication that’s the problem and not anything in a long chunk of code.

---

<div class="post-metadata">

**Author:** ![dancbeaulieu](https://avatars.discourse-cdn.com/v4/letter/d/7cd45c/32.png) [@dancbeaulieu](https://discuss.pennylane.ai/u/dancbeaulieu)\
**Post date:** [August 12, 2021, 6:42pm UTC](https://discuss.pennylane.ai/t/quantum-nlp-transfer-learning/1270/3 "2021-08-12T18:42:48Z")

</div>

Hello! I had to modify the code to get it to work, had to make it “@qml.qnode(dev)” then it worked. Gave me the result “tensor(0.99500417, requires\_grad=True)”. So it looks like the problem is the connection to quantum hardware in the code. I can also connect to the IBMQ system “ibmq\_casablanca” and run the code successfully. So I can run on my local system and on IBMQ quantum hardware.

---

<div class="post-metadata">

**Author:** ![gojiita\_ku](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/gojiita_ku/32/566_2.png) [@gojiita\_ku](https://discuss.pennylane.ai/u/gojiita_ku)\
**Post date:** [August 13, 2021, 3:08am UTC](https://discuss.pennylane.ai/t/quantum-nlp-transfer-learning/1270/4 "2021-08-13T03:08:36Z")

</div>

Hi @dancbeaulieu,

Your work looks fantastic! So did you also compare the performance of this quantum Bert model with the original Bert model? For example, did this quantum model achieve a better performance in terms of classification accuracy and F1 score on the same dataset?

---

<div class="post-metadata">

**Author:** ![dancbeaulieu](https://avatars.discourse-cdn.com/v4/letter/d/7cd45c/32.png) [@dancbeaulieu](https://discuss.pennylane.ai/u/dancbeaulieu)\
**Post date:** [August 17, 2021, 1:08pm UTC](https://discuss.pennylane.ai/t/quantum-nlp-transfer-learning/1270/5 "2021-08-17T13:08:56Z")

</div>

I can’t get the model to work on any quantum hardware or simulator. Only on my local system. I need assistance with this and help figuring out why it doesn’t run on quantum computers using pennylane.

---

<div class="post-metadata">

**Author:** ![Maria\_Schuld](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/maria_schuld/32/166_2.png) [@Maria\_Schuld](https://discuss.pennylane.ai/u/Maria_Schuld)\
**Post date:** [August 18, 2021, 6:04am UTC](https://discuss.pennylane.ai/t/quantum-nlp-transfer-learning/1270/6 "2021-08-18T06:04:12Z")

</div>

Hey @dancbeaulieu,

If you want help figuring out how to run your code on quantum hardware, the easiest will be if you open a new thread with a minimum working example of what does not work. It may not be related to the transfer learning idea at all!

Making an MWE (i.e. reproducing the error you see with just a few lines of code) is crucial - it will speed up our reply, make it more targeted, and in 90% of the cases _creating_ the MWE makes you solve the issue 🙂 (at least that is my experience).

---

<div class="post-metadata">

**Author:** ![\_risto](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/_risto/32/237_2.png) [@\_risto](https://discuss.pennylane.ai/u/_risto)\
**Post date:** [September 12, 2021, 4:16am UTC](https://discuss.pennylane.ai/t/quantum-nlp-transfer-learning/1270/7 "2021-09-12T04:16:19Z")

</div>

Hi @dancbeaulieu

I could not get it run on IBMQ lab. Did you manage to run it?

---

<div class="post-metadata">

**Author:** ![CatalinaAlbornoz](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/catalinaalbornoz/32/1196_2.png) [@CatalinaAlbornoz](https://discuss.pennylane.ai/u/CatalinaAlbornoz)\
**Post date:** [September 14, 2021, 1:08am UTC](https://discuss.pennylane.ai/t/quantum-nlp-transfer-learning/1270/8 "2021-09-14T01:08:04Z")

</div>

Hi @_risto!

Could you share your code and error message so that we can take a look into it?

Thanks!

---

<div class="post-metadata">

**Author:** ![\_risto](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/_risto/32/237_2.png) [@\_risto](https://discuss.pennylane.ai/u/_risto)\
**Post date:** [September 14, 2021, 5:29pm UTC](https://discuss.pennylane.ai/t/quantum-nlp-transfer-learning/1270/9 "2021-09-14T17:29:57Z")

</div>

Hi @CatalinaAlbornoz

When I run the [https://pennylane.ai/qml/demos/tutorial\_quantum\_transfer\_learning.html](https://pennylane.ai/qml/demos/tutorial_quantum_transfer_learning.html) in IBMQ lab and when importing data: data dir = os.path.abspath(rPATH), I get: FileNotFoundError: [Errno 2] No such file or directory: '/home/jovyan/PATH.

---

<div class="post-metadata">

**Author:** ![Diego](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/diego/32/1663_2.png) [@Diego](https://discuss.pennylane.ai/u/Diego)\
**Post date:** [September 14, 2021, 9:20pm UTC](https://discuss.pennylane.ai/t/quantum-nlp-transfer-learning/1270/10 "2021-09-14T21:20:35Z")

</div>

Hi @_risto,

Does the code work when running locally?

Could you verify that `data_dir` points to the folder `hymenoptera_data`?

---

<div class="post-metadata">

**Author:** ![\_risto](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/_risto/32/237_2.png) [@\_risto](https://discuss.pennylane.ai/u/_risto)\
**Post date:** [September 14, 2021, 9:28pm UTC](https://discuss.pennylane.ai/t/quantum-nlp-transfer-learning/1270/11 "2021-09-14T21:28:08Z")

</div>

Hi @Diego

When I run the code on the notebook from my terminal it works.

---

<div class="post-metadata">

**Author:** ![CatalinaAlbornoz](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/catalinaalbornoz/32/1196_2.png) [@CatalinaAlbornoz](https://discuss.pennylane.ai/u/CatalinaAlbornoz)\
**Post date:** [September 15, 2021, 8:45pm UTC](https://discuss.pennylane.ai/t/quantum-nlp-transfer-learning/1270/12 "2021-09-15T20:45:05Z")

</div>

Hi @_risto, I’m not being able to reproduce your error. Are you sure that you have all of your images in the right folder? If your notebook is in the IBMQ lab outside any folder, you can then create the hymenoptera\_data folder (with all of its contents) and change the line `data_dir = "../_data/hymenoptera_data"` to `data_dir = "./hymenoptera_data"`

Please let me know if this works for you!

 ![32%20PM](https://canada1.discourse-cdn.com/flex012/uploads/pennylane/original/1X/db7646e18f1d12f59ae112456d8f9d73e1008033.png)

---

<div class="post-metadata">

**Author:** ![\_risto](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/_risto/32/237_2.png) [@\_risto](https://discuss.pennylane.ai/u/_risto)\
**Post date:** [September 15, 2021, 8:51pm UTC](https://discuss.pennylane.ai/t/quantum-nlp-transfer-learning/1270/13 "2021-09-15T20:51:23Z")

</div>

Hi @CatalinaAlbornoz

When importing pennylane and qml I get:  
Name: PennyLane  
Version: 0.17.0  
Summary: PennyLane is a Python quantum machine learning library by Xanadu Inc.  
Home-page: [GitHub - PennyLaneAI/pennylane: PennyLane is a cross-platform Python library for differentiable programming of quantum computers. Train a quantum computer the same way as a neural network.](https://github.com/XanaduAI/pennylane)  
Author: None  
Author-email: None  
License: Apache License 2.0  
**Location: /opt/conda/lib/python3.8/site-packages** → I can’t find this folder on my laptop.  
This is why I get:

 ![image](https://canada1.discourse-cdn.com/flex012/uploads/pennylane/original/1X/166e9f0f554fef0fa0e057a4275d0cb4ce266681.png)

Do I need to install IBM MQ?

---

<div class="post-metadata">

**Author:** ![CatalinaAlbornoz](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/catalinaalbornoz/32/1196_2.png) [@CatalinaAlbornoz](https://discuss.pennylane.ai/u/CatalinaAlbornoz)\
**Post date:** [September 15, 2021, 9:05pm UTC](https://discuss.pennylane.ai/t/quantum-nlp-transfer-learning/1270/14 "2021-09-15T21:05:01Z")

</div>

Hi @_risto ! I think I might have misunderstood the question. Are you trying run this notebook in the IBMQ Lab platform? Or locally on your computer but using an IBM device?  
Don’t worry about finding the **Python** folder in your laptop, I think the problem is somewhere else. Could you send the full error message?

---

<div class="post-metadata">

**Author:** ![\_risto](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/_risto/32/237_2.png) [@\_risto](https://discuss.pennylane.ai/u/_risto)\
**Post date:** [September 17, 2021, 4:29pm UTC](https://discuss.pennylane.ai/t/quantum-nlp-transfer-learning/1270/15 "2021-09-17T16:29:45Z")

</div>

Hi @CatalinaAlbornoz

If I run it locally (so to call the IBMQ as device) or on IBMQ lab both times the hymenoptera data can not be found. The difference is, that when I do it through the later, I noticed that the file being executed is located at **/opt/conda/lib/python3.8/site-packages**

---

<div class="post-metadata">

**Author:** ![CatalinaAlbornoz](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/catalinaalbornoz/32/1196_2.png) [@CatalinaAlbornoz](https://discuss.pennylane.ai/u/CatalinaAlbornoz)\
**Post date:** [September 18, 2021, 12:11am UTC](https://discuss.pennylane.ai/t/quantum-nlp-transfer-learning/1270/16 "2021-09-18T00:11:03Z")

</div>

Hi @_risto, that’s very strange. Could you send the location of your notebook, and the location of the data folder?

---

<div class="post-metadata">

**Author:** ![dancbeaulieu](https://avatars.discourse-cdn.com/v4/letter/d/7cd45c/32.png) [@dancbeaulieu](https://discuss.pennylane.ai/u/dancbeaulieu)\
**Post date:** [October 5, 2021, 7:49pm UTC](https://discuss.pennylane.ai/t/quantum-nlp-transfer-learning/1270/17 "2021-10-05T19:49:48Z")

</div>

Thanks, I took some time to make a new program and create a minimum example as clean as possible with an aim to eliminate unnecessary packages. Opening a new discussion.

---

<div class="post-metadata">

**Author:** ![CatalinaAlbornoz](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/catalinaalbornoz/32/1196_2.png) [@CatalinaAlbornoz](https://discuss.pennylane.ai/u/CatalinaAlbornoz)\
**Post date:** [October 5, 2021, 11:26pm UTC](https://discuss.pennylane.ai/t/quantum-nlp-transfer-learning/1270/18 "2021-10-05T23:26:16Z")

</div>

Thank you @dancbeaulieu!

---

<div class="post-metadata">

**Author:** ![\_risto](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/_risto/32/237_2.png) [@\_risto](https://discuss.pennylane.ai/u/_risto)\
**Post date:** [November 16, 2021, 11:36pm UTC](https://discuss.pennylane.ai/t/quantum-nlp-transfer-learning/1270/19 "2021-11-16T23:36:04Z")

</div>

Hi @CatalinaAlbornoz

I went again to run the QTL tutorial, step by step, but I get:

RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cpu and cuda:0! (when checking argument for argument tensors in method wrapper\_\_\_cat)

This has not happened before. I have updated pennylane and pip.

---

<div class="post-metadata">

**Author:** ![CatalinaAlbornoz](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/catalinaalbornoz/32/1196_2.png) [@CatalinaAlbornoz](https://discuss.pennylane.ai/u/CatalinaAlbornoz)\
**Post date:** [November 17, 2021, 4:20pm UTC](https://discuss.pennylane.ai/t/quantum-nlp-transfer-learning/1270/20 "2021-11-17T16:20:11Z")

</div>

Hi @_risto, the last line before Dataset Loading is where we configure the Pytorch device. This is how it should look. `device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")`

Is this how it looks for you? If it does then it’s probably a problem with torch.

I would suggest that you try to uninstall and reinstall like it’s shown [here](https://pytorch.org/get-started/locally/#start-locally). I’m running it in my cpu and it’s working.

Please let me know if this works for you!
