An essential part of supply chain management is predicting the backorders, because it helps the companies to address stock shortages and ensure products are available.
In this lab section, Hybrid Quantum Machine Learning is applied to tackle the challenge of predicting product backorders. In this module, we trained and compared a Random Forest Classifier and its hybrid quantum model to evaluate and compare their performance.
Install required libraries
Import required libraries
We are preprocessing the data here, first, we defined the feature columns and target column. Then we combined both the training and testing data. Later we scaled the feature data to standardize the dataset and encoded the target variable to numbers, and split the datasets into training and testing.
Mounting the Google Drive and accessing the dataset from the Google Drive.
These are the steps to mount the Google Drive through code.
First Upload the dataset into the Drive and make note of the dataset path.
When you run the above code cell this particular pop-up will appear. Click "Connect to Google Drive".
Then select the Google Drive account in which the dataset is uploaded.
Select "Continue"
Select "Continue" and the drive will be mounted.
We are training our model using a Random Forest Classifier testing it and printing out the accuracy of the model.
We are performing feature selection using Random forest classifier to identify the most important features.
We have setup the quantum device with 3 qubits and then defined the quantum circuit using a qnode. Then we defined the hybrid model and the cost function.
We are training the QML model by optimizing randomly initialized weights over 100 steps. Used the Adam Optimizer for optimization and printed the cost for every 10 steps.
Training cost Over Time:
In the early steps, there is a high initial cost with some fluctuations. Because the model begins to learn at the start. Later during the middle of the training, there is a cost reduction gradually which is a good sign. Later in the final training, it continued to decrease showing that the optimization is successful.
Training and testing the Hybrid Quantum Machine Learning Model and printing the accuracy of the model.
Plotting bar graph for showing the accuracies for classical model and Hybrid Quantum Machine Learning Model.
The orange bar shows the accuracy of the Random Forest Model and the green bar shows the accuracy of the Hybrid Quantum Machine Learning Model. Both have accuracies above 98% which is good. However, the Hybrid QML model has more accuracy than the Random Forest. This indicates that QML models give better performance than the normal Machine learning Models.
Link for the code: Code Link (Time took to run the code: 20 mins)
Link for Dataset: Dataset Link