dorahacksglobal / qc-classifier

AI-inspired Classification of Quantum Computers
GNU General Public License v2.0
2 stars 4 forks source link

Classification Model for General Quantum Circuits #2

Closed ShreyK7 closed 3 weeks ago

ShreyK7 commented 1 month ago

We are looking to expand/repurpose our classifier model to classify output data from generalized quantum circuits in the same way we did for QRNG circuits (i.e. not just quantum random number generation circuits, but any kind of x qubit quantum circuit and/or algorithm that can be run through a quantum device to generate data). Feel free to use any ML/AI models/techniques as well as quantum theory or data processing/extraction functions, including those we have already included in this repo for classifying specifically QNRG data. We have not yet attempted the task for general circuits, so there is no benchmark classification accuracy as of now - in other words, any positive results are valuable! Additionally, if needed, feel free to generate quantum training/test data from any sources possible.

AbdullahKazi500 commented 1 month ago

Hi @ShreyK7 made a PR #4 To address this issue it covers Generate Synthetic Data: The generate_quantum_data() function creates synthetic data mimicking the output of generalized quantum circuits. In this example, it generates random numerical data for the features (X) and binary labels (y).

Define the Classifier Model: The build_classifier() function defines the classifier model. In this example, it uses a Random Forest classifier with 100 trees.

Data Preparation: Set the number of samples (num_samples) and features (num_features). Then, generate synthetic quantum data using the generate_quantum_data() function.

Split Data: Use train_test_split() from scikit-learn to split the data into training and testing sets. Here, 80% of the data is used for training (X_train, y_train), and 20% for testing (X_test, y_test).

Build and Train Model: Create an instance of the classifier model using build_classifier() and train it on the training data (X_train, y_train) using the fit() method.

Prediction: Use the trained classifier to predict labels for the testing set (X_test) using the predict() method.

Evaluate Model: Calculate the accuracy of the model's predictions using accuracy_score() from scikit-learn.

accuracy - Accuracy: 0.49

schance995 commented 1 month ago

Hi! @0mwh and I would like to work on this too. What kind of general circuits are you looking for? Will benchmark circuits suffice, or should a more practical circuit like QAOA or quantum neural network be deployed?

AbdullahKazi500 commented 1 month ago

Hi @schance995 skylar it already has my PR So can you let the maintainers review it then @SID Can you let me know if this looks good

schance995 commented 1 month ago

@AbdullahKazi500 certainly, let's wait for the reviewers first.

sid-chava commented 1 month ago

Just merged - looks good. Keep in mind that that the python random module is psuedo random as opposed to the quantum random numbers generated in the training file. Do you guys have access to the original training circuit somewhere? if not I can upload it to this repository so let me know.

sid-chava commented 1 month ago

Hi! @0mWh and I would like to work on this too. What kind of general circuits are you looking for? Will benchmark circuits suffice, or should a more practical circuit like QAOA or quantum neural network be deployed?

Since we are effectively classifying the noise specific to select quantum computers, we are trying to see if we would be able to extend this to the output of other quantum circuits. So if you take other quantum circuits and run it over different quantum computers, would the model be able to separate the select quantum computers?

0mWh commented 1 month ago

Do you guys have access to the original training circuit somewhere? if not I can upload it to this repository so let me know.

no, I do not see any quantum circuits in the repository (are they somewhere else?)

sid-chava commented 1 month ago

Hi! @0mWh and I would like to work on this too. What kind of general circuits are you looking for? Will benchmark circuits suffice, or should a more practical circuit like QAOA or quantum neural network be deployed?

Since we are effectively classifying the noise specific to select quantum computers, we are trying to see if we would be able to extend this to the output of other quantum circuits. So if you take other quantum circuits and run it over different quantum computers, would the model be able to separate the select quantum computers?

@bernardyeszero should be able to provide more background on this when he gets online

sid-chava commented 1 month ago

Do you guys have access to the original training circuit somewhere? if not I can upload it to this repository so let me know.

no, I do not see any quantum circuits in the repository (are they somewhere else?)

https://github.com/dorahacksglobal/quantum-randomness-generator/blob/main/Rigetti_CHSH_pub.ipynb

I believe this repository should have most of the details that will be helpful for this part of the task. The Readme of this repository outlines the process of generating random numbers using the CHSH game in more detail.

ShreyK7 commented 1 month ago

Hi! @0mWh and I would like to work on this too. What kind of general circuits are you looking for? Will benchmark circuits suffice, or should a more practical circuit like QAOA or quantum neural network be deployed?

Just to add on - for now, the specific circuit itself doesn't matter, more so seeing if we can develop a classification model that can distinguish QC's (through circuit output data, most likely) through their device specific quantum noise. As @sid-chava mentioned, we are looking to see if we can classify any type of quantum circuit (beyond our preexisting QNRG generation circuit) with a prediction model. I believe @bernardyeszero will update the issue description sometime soon to add a little more detail (and I think he will specify some kind of 10 qubit general circuit), but for now, if it helps, feel free to use whatever quantum circuits you would like - the focus is classifying them and predicting which quantum computers they were run on.

ShreyK7 commented 1 month ago

Hi @ShreyK7 made a PR #4 To address this issue it covers Generate Synthetic Data: The generate_quantum_data() function creates synthetic data mimicking the output of generalized quantum circuits. In this example, it generates random numerical data for the features (X) and binary labels (y).

Define the Classifier Model: The build_classifier() function defines the classifier model. In this example, it uses a Random Forest classifier with 100 trees.

Data Preparation: Set the number of samples (num_samples) and features (num_features). Then, generate synthetic quantum data using the generate_quantum_data() function.

Split Data: Use train_test_split() from scikit-learn to split the data into training and testing sets. Here, 80% of the data is used for training (X_train, y_train), and 20% for testing (X_test, y_test).

Build and Train Model: Create an instance of the classifier model using build_classifier() and train it on the training data (X_train, y_train) using the fit() method.

Prediction: Use the trained classifier to predict labels for the testing set (X_test) using the predict() method.

Evaluate Model: Calculate the accuracy of the model's predictions using accuracy_score() from scikit-learn.

accuracy - Accuracy: 0.49

Yeah to add on to @sid-chava's point: we've already explored random forests in our QNRG classification notebook - your model looks well defined so can definitely build off of your initial methods but at the end of the day the important part of this task/issue/bounty is seeing what accuracy we can get when applying a ML/AI classification model to an actual general quantum circuit run through different QCs to generate output data.

sid-chava commented 1 month ago

Hi @ShreyK7 made a PR #4 To address this issue it covers Generate Synthetic Data: The generate_quantum_data() function creates synthetic data mimicking the output of generalized quantum circuits. In this example, it generates random numerical data for the features (X) and binary labels (y). Define the Classifier Model: The build_classifier() function defines the classifier model. In this example, it uses a Random Forest classifier with 100 trees. Data Preparation: Set the number of samples (num_samples) and features (num_features). Then, generate synthetic quantum data using the generate_quantum_data() function. Split Data: Use train_test_split() from scikit-learn to split the data into training and testing sets. Here, 80% of the data is used for training (X_train, y_train), and 20% for testing (X_test, y_test). Build and Train Model: Create an instance of the classifier model using build_classifier() and train it on the training data (X_train, y_train) using the fit() method. Prediction: Use the trained classifier to predict labels for the testing set (X_test) using the predict() method. Evaluate Model: Calculate the accuracy of the model's predictions using accuracy_score() from scikit-learn. accuracy - Accuracy: 0.49

Yeah to add on to @sid-chava's point: we've already explored random forests in our QNRG classification notebook - your model looks well defined so can definitely build off of your initial methods but at the end of the day the important part of this task/issue/bounty is seeing what accuracy we can get when applying a ML/AI classification model to an actual general quantum circuit run through different QCs to generate output data.

Apologies for not catching this earlier, but since we have this functionality described already in the original notebook, we will roll this back to avoid general confusion.

bernardyeszero commented 1 month ago

Hello Hackers,

Sorry for being late. I explored the comments and found that "general quantum circuits" confuse you. Here are some explanations.

As @ShreyK7 said in this comment, the ultimate goal of our curiosity is to develop an AI-driven model to distinguish data from quantum computers and classical computers for specific tasks. The significance lies in verifying some "quantumness" in the NISQ era.

For general circuits, you can generate with QuantumVolume function in Qiskit, and you can save this circuit using qpy.dump() function and reload it using qpy.load().

The exemplified workflow for this issue could be:

  1. Generate a general circuit using QuantumVolume function, and save it for future reuse or verification by others.
  2. Setup several quantum backends (Aer, Fake backends, or real backends, it's up to you).
  3. Run the circuits using different quantum backends and obtain output strings.
  4. Develop the model to distinguish the data from different backends. The figure of merit is the accuracy.

I also want to note that the output is not restricted to Z-basis measurements. It also can be from other Pauli-basis measurements.

AbdullahKazi500 commented 1 month ago

I am using pennylane but it doesnt has volume

AbdullahKazi500 commented 1 month ago

Hi @bernardyeszero and @sid-chava I gave training the classifier a try but the quantum volume function is not available in pennylane

bernardyeszero commented 1 month ago

@AbdullahKazi500 Hi, thanks for your question. I found two methods to create the QuantumVolume function in Pennylane. Before that, you can understand why we chose QuantumVolume to generate general circuits because it's a hardware-agnostic metric, see Wikipedia.

  1. The first way combines Pennylane and Strawberry Fields, as shown in this demo.
  2. The second way is converting the quantum circuits in Qiskit to the circuits in Pennylane. You can use qml.from_qiskit function, as shown in this documentation. Before that, you need to install Qiskit to use the QuantumVolume function.
AbdullahKazi500 commented 1 month ago

while using @bernardyeszero the backend I am getting an error IBMAccountError: 'No active IBM Q account, and no IBM Q token provided.' since all the IBM Cloud simulators have retired how can I use them

bernardyeszero commented 1 month ago

Hi @AbdullahKazi500, you can use the 'fake provider', see this doc.

AbdullahKazi500 commented 1 month ago

@bernardyeszero okay trying it on the pennylane hardware for now

AbdullahKazi500 commented 1 month ago

Hi @bernardyeszero @sid-chava I was able to follow the steps using quantum volume and also adding backends the code is working as of now i will try adding the backend in the place holder and trying it again for a run

AbdullahKazi500 commented 1 month ago

the building blocks of the code is ready and can be experimented @bernardyeszero can I get a review on the code

AbdullahKazi500 commented 1 month ago

Screenshot 2024-06-02 032625

AbdullahKazi500 commented 1 month ago

the weird thing is the accuracy comes up to be 1.0 even after experimenting with different quantum circuits

AbdullahKazi500 commented 1 month ago

qubitdora

AbdullahKazi500 commented 1 month ago

@bernardyeszero I tried different backend and used lightning gpu and kokkos

ShreyK7 commented 1 month ago

@AbdullahKazi500 submit a PR with your updated code and @bernardyeszero can then take a look at it

AbdullahKazi500 commented 1 month ago

@bernardyeszero @ShreyK7 made the PR

ShreyK7 commented 1 month ago

Thanks hopefully @bernardyeszero can take a closer look at your work soon. One thing I will say is that you theoretically should not be getting 100% accuracy each team (this implies that your circuit backend to generate training data is doing something wrong).

AbdullahKazi500 commented 1 month ago

@ShreyK7 I Don't know what might be the reason since the code is also not giving a traceback error or something like that

AbdullahKazi500 commented 1 month ago

@ShreyK7 I have also implemented a Multi-Layer Perceptron classifier model with pennylane backend it is also giving 1.0 accuracy with this output strings Epoch 1/5 Accuracy on test set: 1.0 Accuracy after training on 2400 samples: 1.0 Epoch 2/5 Accuracy on test set: 1.0 Accuracy after training on 2400 samples: 1.0 Epoch 3/5 Accuracy on test set: 1.0 Accuracy after training on 2400 samples: 1.0 Epoch 4/5 Accuracy on test set: 1.0 Accuracy after training on 2400 samples: 1.0 Epoch 5/5 Accuracy on test set: 1.0 Accuracy after training on 2400 samples: 1.0 Output strings for backend default.qubit: 001 0-10 100 -1-10 010 100 101 00-1 110 100 -100 0-10 0-10 0-11 -100 -100 0-11 0-10 0-10 001 00-1 0-10 011 100 010 110 0-1-1 010 00-1 01-1 101 -110 -100 1-10 0-10 001 101 011 1-11 01-1 100 010 010 0-10 -101 00-1 101 001 10-1 0-10 -100 01-1 -10-1 -100 -100 001 -1-10 00-1 01-1 100 001 001 -110 100 00-1 011 00-1 010 -101 -100 -100 01-1 0-10 01-1 -110 00-1 0-1-1 010 1-10 -10-1 1-10 0-1-1 110 -100 01-1 100 010 110 -101 -100 00-1 -10-1 010 -100 010 001 011 011 -10-1 -100 -1-10 -100 101 1-10 110 -101 -1-10 -100 100 010 -100 100 010 1-10 -100 010 -100 1-10 -110 010 1-10 010 -101 110 100 101 -101 001 0-10 0-1-1 001 1-10 110 100 01-1 0-10 00-1 001 -101 0-10 10-1 1-10 100 010 -110 010 001 110 -100 -100 0-10 100 100 101 -101 100 110 1-10 -100 -11-1 110 00-1 110 001 -100 -10-1 01-1 010 00-1 1-10 -1-10 0-10 0-10 011 101 010 0-11 0-1-1 110 0-10 00-1 -101 100 -10-1 010 -1-10 0-10 -110 001 -110 -100 1-10 -101 110 -110 -100 101 0-10 010 100 010 010 -101 100 1-10 010 0-10 10-1 100 -1-10 00-1 001 -100 1-10 100 010 00-1 0-10 0-10 1-10 -100 -110 -110 001 0-10 -110 1-10 0-10 -101 001 100 100 -110 00-1 010 010 0-10 -100 0-10 -101 100 110 1-10 -100 01-1 -1-10 -100 001 -10-1 100 -100 101 0-11 110 100 -100 001 011 0-10 110 0-10 0-10 010 0-1-1 -1-10 010 1-10 00-1 100 100 101 0-10 -100 001 010 1-11 0-11 110 0-10 0-1-1 011 01-1 -10-1 10-1 010 110 00-1 -110 0-10 -10-1 -100 011 001 -100 1-10 100 010 010 010 10-1 100 1-10 011 1-10 -100 0-11 0-1-1 110 1-10 010 100 001 -1-10 010 001 0-10 -1-10 100 001 001 0-10 001 00-1 100 100 -1-10 0-10 100 -100 -100 01-1 0-10 011 01-1 100 -100 001 100 -101 010 100 001 0-10 0-10 0-11 -100 -110 100 01-1 001 -100 010 -100 0-10 011 -110 010 001 -101 01-1 110 110 0-10 010 -110 0-1-1 010 100 100 100 10-1 1-10 110 -10-1 -100 011 001 001 00-1 1-10 -10-1 -110 100 0-10 1-10 -101 01-1 001 10-1 -100 -1-11 -1-10 -110 -100 100 0-11 010 010 110 -1-10 -110 110 10-1 1-10 010 100 1-10 010 010 100 110 100 -1-10 -1-10 0-10 00-1 10-1 1-11 110 0-10 100 0-11 001 -100 101 -100 10-1 1-10 0-10 00-1 -10-1 -100 0-10 100 100 001 001 1-10 0-10 010 -1-10 010 110 110 0-10 -100 001 001 110 00-1 01-1 011 -1-11 0-10 011 001 100 110 110 010 011 10-1 00-1 -101 0-10 0-10 -100 010 -100 110 -10-1 -110 0-10 100 010 011 110 011 010 110 010 100 1-10 0-10 001 001 101 0-10 -100 -100 00-1 -100 -1-10 001 0-10 -110 010 0-10 1-10 0-10 010 10-1 -101 101 100 -10-1 010 101 010 0-11 00-1 -11-1 0-10 0-11 010 001 01-1 1-10 011 -110 -10-1 -100 0-1-1 -101 010 001 -1-10 100 -101 1-10 0-1-1 -100 01-1 100 10-1 010 00-1 010 0-10 01-1 100 010 -1-10 0-10 -1-10 00-1

AbdullahKazi500 commented 1 month ago

I think I probably got some accuracy with the backend need to check I will submit a pr soon

AbdullahKazi500 commented 1 month ago

@bernardyeszero @ShreyK7 and @sid-chava can I get assigned to this bounty I have experimented with different backends, used different classifiers eg gradient boosting, Random forest and perceptron classifier I have also experimented with adding noise in the general circuit ' finally I also trained a QGAN With the classifier getting an accuracy of 0.33 % with QVolume I used the pennylane SDK for this for someone who still wants to do this forward as can use a different SDK, Different backend , different classifier models and a different approach I would love to work on this even after the hackathon

AbdullahKazi500 commented 1 month ago

qgan Please check PR #10 And more recent PR