A hands-on introduction to Quantum Computing and Quantum Machine Learning using PennyLane — from building your first quantum circuit to training a Variational Quantum Classifier (VQC) on real data.
PennyLane/
│
├── simple_circuit.py # Bell state: Hadamard + CNOT entanglement
├── param_circuit.py # Parameterized rotations + expectation values
├── vqc_train.py # Variational Quantum Classifier on make_moons dataset
├── .gitignore
└── README.md
Create a fresh virtual environment to keep your dependencies isolated:
python -m venv qml-envActivate it:
| Platform | Command |
|---|---|
| 🪟 Windows | qml-env\Scripts\activate |
| 🍎 Mac/Linux | source qml-env/bin/activate |
You should now see (qml-env) at the start of your terminal prompt.
With the virtual environment active, install the required packages:
pip install pennylane
pip install matplotlib
pip install scikit-learn💡 All at once (optional):
pip install pennylane matplotlib scikit-learn
Test that PennyLane installed correctly:
python -c "import pennylane as qml; print(qml.__version__)"If a version number prints (e.g., 0.38.0) → you're all set! ⚡
Creates a 2-qubit Bell state using a Hadamard gate and a CNOT gate — the classic quantum entanglement circuit.
python simple_circuit.pyWhat it does:
- Applies
H(Hadamard) to qubit 0 → puts it in superposition - Applies
CNOTwith qubit 0 as control, qubit 1 as target → entangles them - Returns the full quantum state vector
Expected output:
[0.70710678+0.j 0. +0.j 0. +0.j 0.70710678+0.j]
Demonstrates a parameterized circuit using rotation gates with a trainable angle θ.
python param_circuit.pyWhat it does:
- Applies
RX(θ)to qubit 0 andRY(θ)to qubit 1 - Entangles them with
CNOT - Measures the expectation value of the Pauli-Z operator on qubit 0
Expected output:
Expectation value: 0.7071067811865476
Trains a Variational Quantum Classifier on the make_moons dataset from scikit-learn — a real quantum machine learning example!
python vqc_train.pyWhat it does:
- Loads the
make_moonsdataset (100 samples, non-linearly separable) - Encodes classical data into quantum states via
RX/RYrotations - Adds trainable weight layers (
RX,RY+CNOT) - Optimizes using quantum gradient descent (PennyLane's
GradientDescentOptimizer) - Runs for 50 training steps and reports loss every 10 steps
Expected output:
Step 0, Loss: 0.9523...
Step 10, Loss: 0.7841...
Step 20, Loss: 0.6102...
Step 30, Loss: 0.4987...
Step 40, Loss: 0.4213...
Final weights: [...]
The loss decreases across steps, showing the quantum model is learning! 🧠
| Concept | Script |
|---|---|
| Quantum gates (H, CNOT, RX, RY) | simple_circuit.py, param_circuit.py |
| Quantum entanglement (Bell state) | simple_circuit.py |
| Parameterized quantum circuits | param_circuit.py |
| Expectation values (Pauli-Z) | param_circuit.py |
| Data encoding into qubit rotations | vqc_train.py |
| Quantum gradient descent | vqc_train.py |
| Variational Quantum Classifier | vqc_train.py |
| Package | Purpose |
|---|---|
pennylane |
Quantum circuit simulation & ML |
scikit-learn |
make_moons dataset generation |
matplotlib |
(Optional) Plotting |
numpy |
Numerical operations (bundled with PennyLane) |
- 📖 PennyLane Documentation
- 🎓 PennyLane Demos & Tutorials
- 🔬 Quantum Machine Learning Paper (Biamonte et al.)
- 💻 PennyLane GitHub
Pull requests are welcome! For major changes, please open an issue first.
This project is licensed under the MIT License.