This project demonstrates how to build and train a Variational Quantum Classifier (VQC) using Qiskit. Variational Quantum Classifiers are hybrid quantum-classical machine learning models designed to classify data points into different categories using quantum circuits with trainable parameters.
A VQC leverages the power of quantum computers to process input data via parameterized quantum circuits. It uses a classical optimizer to iteratively adjust the quantum circuit's parameters to minimize a cost function (e.g., binary cross-entropy or mean squared error). VQCs are particularly promising for tasks in quantum machine learning, such as binary or multiclass classification.
- Parameterized Quantum Circuits (PQC): The quantum circuit with trainable parameters.
- Hybrid Optimization: A classical optimizer (e.g., COBYLA or L-BFGS-B) is used to minimize a cost function based on quantum measurements.
- Quantum Feature Encoding: Input data is encoded into the quantum state via rotations or other transformations.
- Quantum Measurement: Measurements of quantum states provide the predicted classification probabilities.
- Python 3.8 or higher
- Qiskit: For quantum circuit design and simulation
- SciPy: For classical optimization
- NumPy: For data manipulation and numerical operations
You can install all required dependencies using pip:
pip install qiskit scipy numpy
- Quantum Data Encoding: Encodes classical data into quantum states using feature maps.
- Customizable Ansatz: Uses the RyRz ansatz as the parameterized circuit, with options for entangling gates.
- Cost Function for Classification: Implements a binary cross-entropy loss function.
- Hybrid Optimization: Combines quantum circuit evaluation with classical optimization to train the model.
- Simulator Support: Uses Qiskit's Aer simulator to execute quantum circuits.
- Clone the repository:
git clone https://github.com/davitacols/Building-a-Variational-Quantum-Classifier-in-Python-with-Qiskit.git
cd variational-quantum-classifier
- Install dependencies:
pip install -r requirements.txt
- Run the script:
python vqc_classifier.py
- Input data (e.g., binary classification labels) is normalized and mapped to a quantum feature space.
- The RyRz ansatz is used as the parameterized quantum circuit. This circuit applies parameterized single-qubit rotations (RY, RZ) followed by entangling CNOT gates.
- The cost function is defined to quantify the error in classification. Binary cross-entropy is commonly used.
- A classical optimizer (e.g., L-BFGS-B, COBYLA) adjusts the circuit's parameters to minimize the cost function.
- Once trained, the model predicts labels for unseen data by evaluating the quantum circuit with optimized parameters.
Prepare a dataset with features and labels. For simplicity, the script supports synthetic datasets generated using NumPy.
# Example: Simple binary classification dataset
features = np.array([[0.1, 0.2], [0.9, 0.8], [0.4, 0.5]])
labels = np.array([0, 1, 0]) # Binary labels
Execute the training and evaluation pipeline by running the script:
python vqc_classifier.py
- Optimized parameters of the quantum circuit
- Training accuracy
- Classification results on a test dataset
For a simple binary classification dataset:
Training Accuracy: 95.0%
Test Accuracy: 90.0%
Optimized Parameters:
[2.345, 1.234, 3.567, ...]
Predicted Labels: [0, 1, 0]
Add a visualization to show decision boundaries or loss convergence during training.
If you have any questions, suggestions, or issues, feel free to raise an issue or submit a pull request. 🚀