Getting "tuple index out of range" error cant figure out why

import pennylane as qml

from pennylane import numpy as np

from pennylane.optimize import AdamOptimizer

from numpy import *  # get the "numpy" library for linear algebra

from math import *

import pandas as pd

from sklearn.model_selection import train_test_split

from pennylane.templates.layers import CVNeuralNetLayers

from pennylane.templates.embeddings import DisplacementEmbedding

dataset = pd.read_csv('train.csv')

dataset = dataset.head(20).copy()

drops =['ID', 'vendor+AF8-id', 'pickup+AF8-loc', 'drop+AF8-loc', 'driver+AF8-tip', 'mta+AF8-tax', 'pickup+AF8-time','drop+AF8-time', 'toll+AF8-amount','payment+AF8-method',  'rate+AF8-code', 'stored+AF8-flag', 'extra+AF8-charges', 'improvement+AF8-charge','num+AF8-passengers']

train_Data = dataset.drop(drops, axis=1)

X = train_Data['distance']

y = train_Data['total+AF8-amount']

from sklearn.preprocessing import MinMaxScaler

scaler = MinMaxScaler()

y = np.array(y)

y = y.reshape(-1,1)

y = scaler.fit_transform(y)

X = np.array(X)

X = X.reshape(-1,1)

X = scaler.fit_transform(X)

from sklearn.model_selection import train_test_split

xtr, xt, ytr, yt = train_test_split(X, y, test_size=0.3)


dev = qml.device("strawberryfields.fock", wires=3, cutoff_dim=50)

def circuit(*pars):

    CVNeuralNetLayers(*pars, wires=[0, 2])

@qml.qnode(dev)

def quantum_neural_net(*var, x1=None, x2=None, x3=None):

    # Encode input x into quantum state

    qml.Displacement(x1, 0.0, wires=0)

    qml.Displacement(x2, 0.0, wires=1)

    qml.Displacement(x3, 0.0, wires=2)

    for v in var:

        circuit(*v)

    return qml.expval(qml.X(0))

def square_loss(labels, predictions):

    loss = 0

    for l, p in zip(labels, predictions):

        loss = loss + (l - p) ** 2

    loss = loss / len(labels)

    return loss

def cost(var, features, labels):

    preds = [quantum_neural_net(var, x1=xlo[0]) for xlo in features]

    return square_loss(labels, press)

init_pars = CVNeuralNetLayers.shape(n_layers=2, n_wires=2)

print(init_pars)

opt = AdamOptimizer(0.01, beta1=0.9, beta2=0.999)

var = init_pars

 for it in range(50):
    var = opt.step(lambda v: cost(v, xtr,ytr), var)
    print("Iter: {:5d} | Cost: {:0.7f} ".format(it + 1, cost(var, xtr, ytr)))

predics = [quantum_neural_net(var, x1=xlo[0]) for xlo in xt]

I’m getting an error of “tuple index out of range” for var = opt.step(lambda v: cost(v, xtr,ytr), var)

Hey @TheHarshal30,

Welcome to the Forum!

Can you provide the full error message? Also, I can’t replicate your error because I don’t have the train.csv file that you’re importing. Can you provide a minimum working example that doesn’t rely on train.csv?

Full Error message:

train_Data:

Hey @TheHarshal30,

I’m having a hard time replicating your result, but here’s what I understand about your code:

vars define the parameters of your model. You want to take gradients of the cost function with respect to those parameters. In your code, vars is a list. To make sure PennyLane can differentiate with respect to vars, make vars a numpy array and set requires_grad = True. Check this video out for details.

It seems like the error message you’re getting is specifically related to vars being a list of tuples, but I’m not entirely sure.

Are you able to provide a more simple working example of your code?