Help with Regression Model Training

Hello,

I am very new to Pennylane and have very little Python experience.

I am doing this for a school project and I just need help correcting the issue:
I am trying to do a regression of several Y variables vs one single X variable.

I am attempting to use Hybrid Quantum-Classical ML to a problem application.

Here is the code:

import pennylane as qml
from pennylane import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.model_selection import train_test_split

# Load the dataset
data = pd.read_csv('file.csv')


X = data['X'].values
Y = data['Y'].values

# Normalize or scale your features
X_normalized = (X - np.min(X)) / (np.max(X) - np.min(X))
Y_normalized = (Y - np.min(Y)) / (np.max(Y) - np.min(Y))

# Split into training and testing datasets
X_train, X_test, Y_train, Y_test = train_test_split(X_normalized, Y_normalized, test_size=0.2, random_state=42)

# Create the quantum device
dev = qml.device('default.qubit', wires=1)

# Define the quantum circuit
@qml.qnode(dev)
def quantum_circuit(datapoint, params):
    qml.RX(datapoint, wires=0)
    qml.Rot(params[0], params[1], params[2], wires=0)
    return qml.expval(qml.PauliZ(wires=0))

# Classical pre/postprocessing and loss function
def loss_func(predictions, targets):
    return np.mean((predictions - targets) ** 2)

# Define the cost function
def cost_fn(params, X, Y):
    predictions = [quantum_circuit(x, params) for x in X]
    return loss_func(predictions, Y)

# Define the optimizer
opt = qml.GradientDescentOptimizer(stepsize=0.3)

# Initialize parameters
params = np.array([0.1, 0.1, 0.1], requires_grad=True)

# Training loop
for i in range(100):
    print("Params shape:", params.shape)  # Debug: Check params shape
    params, prev_cost = opt.step_and_cost(lambda p: cost_fn(p, X_train, Y_train), params)
    if i % 10 == 0:
        print(f'Step {i}, Cost {prev_cost}')

# Generate predictions for testing
test_predictions = [quantum_circuit(x, params) for x in X_test]

# Plotting results
plt.scatter(X_train, Y_train, color='blue', label='Training Data')
plt.scatter(X_test, Y_test, color='red', label='Test Data')
plt.scatter(X_test, test_predictions, color='green', label='Predictions', marker='x')
plt.xlabel("Normalized X")
plt.ylabel("Normalized Y")
plt.title("Quantum Regression Model Results")
plt.legend()
plt.show()

Error:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) TypeError: float() argument must be a string or a real number, not ‘ArrayBox’ The above exception was the direct cause of the following exception: ValueError Traceback (most recent call last) Cell In[36], line 49 47 for i in range(100): 48 print(“Params shape:”, params.shape) # Debug: Check params shape —> 49 params, prev_cost = opt.step_and_cost(lambda p: cost_fn(p, X_train, Y_train), params) 50 if i % 10 == 0: 51 print(f’Step {i}, Cost {prev_cost}') File /opt/miniconda3/envs/HQML/lib/python3.12/site-packages/pennylane/optimize/gradient_descent.py:64, in GradientDescentOptimizer.step_and_cost(self, objective_fn, grad_fn, *args, **kwargs) 44 def step_and_cost(self, objective_fn, *args, grad_fn=None, **kwargs): 45 “”“Update trainable arguments with one step of the optimizer and return the corresponding 46 objective function value prior to the step. 47 (…) 61 If single arg is provided, list [array] is replaced by array. 62 “”” —> 64 g, forward = self.compute_grad(objective_fn, args, kwargs, grad_fn=grad_fn) 65 new_args = self.apply_grad(g, args) 67 if forward is None:

129 ret = ret.dtype.type(ret / rcount) 130 else: 131 ret = ret / rcount ValueError: setting an array element with a sequence.


Any help is appreciated.

Thank you!

Hi @fkajsfbksdjf ,

You were actually quite close! I didn’t have access to your data so I tested this with some random y=x data.
It seems that the issue was being caused by getting the square of (predictions-targets). In the code below I calculated the square for each pair (predictions[i]-targets[i]) and you no longer see the error you had before. Moreover, you see some nice predictions.

I noticed a lot of similarity in your code with the example at the end of my blog post on how to start learning quantum machine learning. Did you use it? Or did you find the base code somewhere else? I hope it helped you get started! Make sure to give it a quick mention or citation when using it. :grinning:

import pennylane as qml
from pennylane import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.model_selection import train_test_split

# Load the dataset
'''data = pd.read_csv('file.csv')


X = data['X'].values
Y = data['Y'].values'''
# Example data
X = np.random.random(100, requires_grad=False)
Y = X + np.random.random(100, requires_grad=False)/10

# Normalize or scale your features
X_normalized = (X - np.min(X)) / (np.max(X) - np.min(X))
Y_normalized = (Y - np.min(Y)) / (np.max(Y) - np.min(Y))

# Split into training and testing datasets
X_train, X_test, Y_train, Y_test = train_test_split(X_normalized, Y_normalized, test_size=0.2, random_state=42)

# Create the quantum device
dev = qml.device('default.qubit', wires=1)

# Define the quantum circuit
@qml.qnode(dev)
def quantum_circuit(datapoint, params):
    qml.RX(datapoint, wires=0)
    qml.Rot(params[0], params[1], params[2], wires=0)
    return qml.expval(qml.PauliZ(wires=0))

# Classical pre/postprocessing and loss function
def loss_func(predictions, targets):
    return np.mean(np.array([(predictions[i] - targets[i]) ** 2 for i in range(len(targets))]))#np.mean((predictions - targets) ** 2)

# Define the cost function
def cost_fn(params, X, Y):
    predictions = [quantum_circuit(x, params) for x in X]
    return loss_func(predictions, Y)

# Define the optimizer
opt = qml.GradientDescentOptimizer(stepsize=0.3)

# Initialize parameters
params = np.array([0.1, 0.1, 0.1], requires_grad=True)

# Training loop
for i in range(100):
    #print("Params shape:", params.shape)  # Debug: Check params shape
    params, prev_cost = opt.step_and_cost(lambda p: cost_fn(p, X_train, Y_train), params)
    if i % 10 == 0:
        print(f'Step {i}, Cost {prev_cost}')

# Generate predictions for testing
test_predictions = [quantum_circuit(x, params) for x in X_test]

# Plotting results
plt.scatter(X_train, Y_train, color='blue', label='Training Data')
plt.scatter(X_test, Y_test, color='red', label='Test Data')
plt.scatter(X_test, test_predictions, color='green', label='Predictions', marker='x')
plt.xlabel("Normalized X")
plt.ylabel("Normalized Y")
plt.title("Quantum Regression Model Results")
plt.legend()
plt.show()

image

Thank you. I will follow up when I test on real data. Yes I pattered this after your code and article. You are actually in my references and citations already :heart:

1 Like

Oh that’s great to hear @fkajsfbksdjf ! Thank you.