← Quantum Store · Guides · Lessons · Glossary · Packages · About · Türkçe
Xanadu and the PennyLane community
A quantum machine learning classifier with PennyLane
Official source: PennyLaneAI/pennylane
Download ZIP (free)A quantum machine learning example that trains a two-qubit variational circuit like a neural network to separate two classes of data. 100% accuracy on unseen test points.
What is quantum machine learning?
In a classical neural network we train the model by adjusting weights. In quantum machine learning (QML) the weights are replaced by the rotation angles in a quantum circuit. A classical optimisation loop changes these angles, and the quantum circuit produces the prediction. This structure is called a hybrid algorithm.
The example in this package
kuantum_siniflandirici.py:
- Generates random points split into two groups (40 for training, 50 for testing).
- Embeds the two coordinates of each point into the rotation angles of two
qubits (
AngleEmbedding). - Applies a learnable two-layer entangling circuit
(
StronglyEntanglingLayers). - Predicts the class from the sign of the measured expectation value of the first qubit.
- Trains for 40 steps with the Adam optimiser.
On our test machine the accuracy was 46% before training (no better than a coin flip) and 100% after training, on 50 points never seen in training. The run takes about a minute.
An honest note
A classical model also separates such a simple dataset easily. The purpose of the example is not to show a quantum advantage but to show how QML works in its smallest form. Which problems quantum machine learning will give a real advantage on is still a research question.
Next steps
- Move the clouds closer together (
0.3instead of0.8); see how the accuracy drops. - Set the number of layers (
n_layers) to 1 and to 4 and compare. - Move on to a real dataset with PennyLane's
qml.datamodule.
Installation and running
pip install pennylane
python kuantum_siniflandirici.py
Tested with: pennylane 0.45.1, Python 3.12.
Official source
- Repository: PennyLaneAI/pennylane
- Latest release: v0.45.1 — 26 June 2026
- Licence: Apache-2.0
- Stars: 3,494
- Default branch commit when the catalogue was built:
6c78a9882a5e(13 October 2026)
Links
Package contents
kuantum_siniflandirici.pyREADME.en.md— this fileREADME.md— the same guide in TurkishLICENSE— the licence of the files in this packageKAYNAK.json— a machine-readable summary of the official sourceUPSTREAM-LICENSE.txt— the licence text of the official project
Licence
The example code and the guide in this package were written by the Quantum Store editors and are MIT licensed. The project it uses is subject to its own licence (Apache-2.0).
Quantum Store — https://hakanerdogan.com/en
Example code
The example shipped in the package, run and checked. Comments and printed messages are in Turkish; the code is the same in any language. Everything is in the ZIP.
kuantum_siniflandirici.py
"""PennyLane ile kuantum makine öğrenmesi: iki sınıflı noktaları ayıran değişken devre.
Çalıştırma:
pip install pennylane
python kuantum_siniflandirici.py
"""
import numpy as np
import pennylane as qml
from pennylane import numpy as pnp
rng = np.random.default_rng(3)
def veri(n: int) -> tuple[np.ndarray, np.ndarray]:
# İki bulut: (-0.8, -0.8) çevresindeki noktalar -1, (0.8, 0.8) çevresindekiler +1.
a = rng.normal([-0.8, -0.8], 0.35, (n, 2))
b = rng.normal([0.8, 0.8], 0.35, (n, 2))
return np.vstack([a, b]), np.array([-1] * n + [1] * n)
X_egitim, y_egitim = veri(20)
X_test, y_test = veri(25)
dev = qml.device("default.qubit", wires=2)
@qml.qnode(dev)
def devre(agirlik, x):
qml.AngleEmbedding(x * np.pi / 2, wires=[0, 1]) # Veriyi döndürme açılarına göm.
qml.StronglyEntanglingLayers(agirlik, wires=[0, 1]) # Öğrenilen kısım.
return qml.expval(qml.PauliZ(0)) # -1 ile +1 arasında bir sayı.
def tahmin(agirlik, sapma, x):
return devre(agirlik, x) + sapma
def maliyet(agirlik, sapma):
hatalar = [(tahmin(agirlik, sapma, x) - y) ** 2 for x, y in zip(X_egitim, y_egitim)]
return sum(hatalar) / len(hatalar)
def dogruluk(agirlik, sapma, X, y) -> float:
return float(np.mean([np.sign(tahmin(agirlik, sapma, x)) == t for x, t in zip(X, y)]))
sekil = qml.StronglyEntanglingLayers.shape(n_layers=2, n_wires=2)
agirlik = pnp.array(rng.normal(0, 0.1, sekil), requires_grad=True)
sapma = pnp.array(0.0, requires_grad=True)
opt = qml.AdamOptimizer(0.1)
print(f"Eğitim öncesi test doğruluğu: {dogruluk(agirlik, sapma, X_test, y_test):.0%}")
for adim in range(1, 41):
(agirlik, sapma), kayip = opt.step_and_cost(maliyet, agirlik, sapma)
if adim % 10 == 0:
print(f"adım {adim:2d} maliyet {float(kayip):.3f}")
sonuc = dogruluk(agirlik, sapma, X_test, y_test)
print(f"Eğitim sonrası test doğruluğu: {sonuc:.0%} ({len(y_test)} görülmemiş nokta)")
assert sonuc >= 0.9, sonuc
print("Tamam: devre, eğitimde görmediği noktaları doğru sınıflandırdı.")