In this module, K Nearest Neighbors, Hybrid Quantum K Nearest Neighbors, and Quantum Machine Learning will be applied to predict patient flow situations.
Install required libraries
Import required libraries
Mount the drive and read the dataset.
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.
Select the required features, fill the string values with NAN, and fill the NANs with mean. Separate the features and target. Use one hot encoder to encode categorical data to numerical and split the data to train and test.
We are training our model using a K Nearest Neighbors Classifier testing it and printing out the model's accuracy.
We are performing feature selection using a KNN classifier to identify the most important features.
We have setup the quantum device with qubits the same as the size of the features and then defined the quantum circuit.
Defining quantum circuit using a qnode. Then, we defined the hybrid model and the cost function. Initializing the weights and the optimizer. Printed the cost for every ten steps. Plotted the graph and printed the accuracy.
In the initial steps, the cost is relatively high with noticeable fluctuations, which is expected as the model starts to learn and adjust its parameters. As training progresses, particularly in the middle stages, there is a gradual reduction in the cost, indicating that the model is learning effectively and optimizing towards a better solution. Toward the later stages of training, the cost continues to decrease but exhibits fluctuations, suggesting that while the optimization process was largely successful, it encountered minor instability, potentially due to small oscillations around a local minimum or plateau.
Preprocessing by scaling the features and combining the train and test. Fixing the qubits.
This code defines a quantum variational circuit with parameterized layers and entanglement to classify input data, using the expectation value of a quantum observable as the output. A regularized cost function combines the model's prediction error (square loss) with an L2 regularization term to optimize the model's weights.
Initializing the weights and the optimizer. Printed the cost for every ten steps. Plotted the graph and printed the accuracy.
The cost function graph shows a decreasing trend with fluctuations, indicating successful optimization as the model learns over time. The steady reduction in cost suggests the model is progressively improving its performance.