# Code in Variational Classifier

**URL:** <https://discuss.pennylane.ai/t/code-in-variational-classifier/3232>\
**Category:** PennyLane Help\
**Created:** [July 28, 2023, 5:47am UTC](https://discuss.pennylane.ai/t/code-in-variational-classifier/3232 "2023-07-28T05:47:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kshitij\_Dave](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/kshitij_dave/32/1826_2.png) [@Kshitij\_Dave](https://discuss.pennylane.ai/u/Kshitij_Dave)\
**Post date:** [July 28, 2023, 5:47am UTC](https://discuss.pennylane.ai/t/code-in-variational-classifier/3232/1 "2023-07-28T05:47:25Z")

</div>

In Variational Classifier Code, let’s say

```auto
 Y.shape = (200,)
index = np.random.permutation(range(len(Y)))
num_train = 150
# Y value (i.e., -1, 1) corresponding to array of # index of size 150. Also since, index contains # randomly permutated values from 0 to 200, # so selecting first 150 numbers possibly #have more than 150 in magnitude (i.e., 186, # 157, etc.)
Y_train = Y[index[:num_train]] 

# Now, let's define one more parameter,
batch_index = np.random.randint(0, num_train, (batch_size,))
# It takes 5 random values from 0 to 150 ,

# In optimization loop, here is the problem,
Y_train_batch = Y_train[batch_index]

```

batch\_ index is chosen randomly 5 values but in Y\_train is also randomly permutated values. In many cases if batch\_index contain value (example :128 as a value) but it didn’t have Y\_train. So many times it gives me this error :KeyError: ‘[117] not in index’.

Its not give me error if i choose ,

```auto
Y_train = Y[:150]
# rather than randomly generated between 0 to 200 and select 150.

```

Please, I tried my best explaining the difficulty i am facing. If someone understand the problem show me how can i fix it and where did i go wrong?

---

<div class="post-metadata">

**Author:** ![kevinkawchak](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/kevinkawchak/32/1801_2.png) [@kevinkawchak](https://discuss.pennylane.ai/u/kevinkawchak)\
**Post date:** [July 28, 2023, 7:35am UTC](https://discuss.pennylane.ai/t/code-in-variational-classifier/3232/2 "2023-07-28T07:35:42Z")

</div>

Hello @Kshitij_Dave,

Is this the model you are referring to? [Variational classifier](https://pennylane.ai/qml/demos/tutorial_variational_classifier) If so, could you provide more information on the changes you are trying to make. Best regards.

---

<div class="post-metadata">

**Author:** ![Kshitij\_Dave](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/kshitij_dave/32/1826_2.png) [@Kshitij\_Dave](https://discuss.pennylane.ai/u/Kshitij_Dave)\
**Post date:** [July 28, 2023, 8:37am UTC](https://discuss.pennylane.ai/t/code-in-variational-classifier/3232/3 "2023-07-28T08:37:26Z")

</div>

Sorry for late reply. Yes, this is tutorial I am referring to. If you try to implement it on any other datasets, it will probably don’t work in the first time because of the contradicting line of “code” they have used.  
I have shared link of jupyter notebook where i have distinguish the problematic part used in the Xanadu notebook . Please see it.

> **[heart\_VQC\_pennylane.ipynb](https://drive.google.com/file/d/1wlbINCyGzjYeJOv0giWfjwwO85nMhMcn/view?usp=drive_link)**
>
> Google Drive file.

The dataset i used,

> **[heart\_data (1).csv](https://drive.google.com/file/d/1ZQRSqcqwjXIp9I8YZxdy1x05-XK-dXLu/view?usp=sharing)**
>
> Google Drive file.

---

<div class="post-metadata">

**Author:** ![Guillermo\_Alonso](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/guillermo_alonso/32/816_2.png) [@Guillermo\_Alonso](https://discuss.pennylane.ai/u/Guillermo_Alonso)\
**Post date:** [July 28, 2023, 2:51pm UTC](https://discuss.pennylane.ai/t/code-in-variational-classifier/3232/4 "2023-07-28T14:51:06Z")

</div>

Hey! 😄  
It seems that the problem is in the dictionary management. Be careful how you use them or try to access their keys (it doesn’t behave the same as with arrays). If you make sure that before training, what you have are arrays, you will see that it works fine 🚀 If the error persists, let me know

---

<div class="post-metadata">

**Author:** ![Kshitij\_Dave](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/kshitij_dave/32/1826_2.png) [@Kshitij\_Dave](https://discuss.pennylane.ai/u/Kshitij_Dave)\
**Post date:** [July 28, 2023, 11:50pm UTC](https://discuss.pennylane.ai/t/code-in-variational-classifier/3232/5 "2023-07-28T23:50:51Z")

</div>

Sorry sir. But i didn’t quite understand what it means by dictionary management. I have given notebook where i perform same task as it is. Please could you clarify much more specifically?

---

<div class="post-metadata">

**Author:** ![Guillermo\_Alonso](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/guillermo_alonso/32/816_2.png) [@Guillermo\_Alonso](https://discuss.pennylane.ai/u/Guillermo_Alonso)\
**Post date:** [July 31, 2023, 12:34pm UTC](https://discuss.pennylane.ai/t/code-in-variational-classifier/3232/6 "2023-07-31T12:34:40Z")

</div>

If you put this line below `Y=Y[:200]` it should work.  
Normally it is better to work with arrays instead of dictionaries 🙂

 ![Captura de pantalla 2023-07-31 a las 8.32.30](https://canada1.discourse-cdn.com/flex012/uploads/pennylane/original/2X/3/3538bdcbc2c94ebc94ba5b2acb5a30e9e2cc9035.png)
