← Quantum Store · Guides · Lessons · Glossary · Packages · About · Türkçe
Xanadu
VQE and quantum machine learning with PennyLane
Official source: PennyLaneAI/pennylane
Download ZIP (free)PennyLane, Xanadu's differentiable quantum programming library: a tested VQE example trained with gradients that converges to within 1e-3 of the exact solution.
What is PennyLane?
PennyLane is a library that lets you write quantum circuits like ordinary Python functions and train them with automatic differentiation (autodiff). It works with NumPy, PyTorch and JAX, which is why it is widely used for hybrid quantum-classical algorithms and quantum machine learning.
When to use it
- When writing parameterised (variational) algorithms such as VQE and QAOA.
- When you want to train a quantum circuit like a layer of a neural network.
- When you want to run the same code on different hardware and simulators (through plugins).
The example in this package
vqe_ornegi.py defines a two-qubit Hamiltonian, trains a circuit with 4
parameters using the Adam optimiser and compares the energy it finds with the
result of exact diagonalisation. In testing the difference stayed below 1e-3.
Note
If JAX is installed in the environment, PennyLane tries to load it too. On Windows we saw JAX give a DLL error; installing PennyLane in its own clean virtual environment is the safest option.
Next steps
- Build the Hamiltonian of a real molecule with the
qml.qchemmodule. - Move on to the quantum classifier examples in the PennyLane demos.
Installation and running
pip install pennylane
python vqe_ornegi.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
vqe_ornegi.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.
vqe_ornegi.py
"""PennyLane ile küçük bir VQE (varyasyonel kuantum özdeğer çözücü).
İki kübitlik bir Hamiltonyenin taban enerjisini gradyan inişiyle bulur ve
tam köşegenleştirme sonucuyla karşılaştırır.
Çalıştırma:
pip install pennylane
python vqe_ornegi.py
"""
import pennylane as qml
from pennylane import numpy as pnp
H = 0.5 * qml.Z(0) @ qml.Z(1) - 0.8 * qml.X(0) + 0.3 * qml.X(1)
dev = qml.device("default.qubit", wires=2)
@qml.qnode(dev)
def energy(params):
qml.RY(params[0], wires=0)
qml.RY(params[1], wires=1)
qml.CNOT(wires=[0, 1])
qml.RY(params[2], wires=0)
qml.RY(params[3], wires=1)
return qml.expval(H)
opt = qml.AdamOptimizer(stepsize=0.1)
params = pnp.array([0.1, 0.2, 0.3, 0.4], requires_grad=True)
for step in range(300):
params = opt.step(energy, params)
if step % 50 == 0:
print(f"adım {step:3d} enerji = {float(energy(params)):.6f}")
vqe = float(energy(params))
exact = float(min(pnp.linalg.eigvalsh(qml.matrix(H, wire_order=[0, 1]))))
print(f"\nVQE enerjisi : {vqe:.6f}")
print(f"Tam değer : {exact:.6f}")
assert abs(vqe - exact) < 1e-3, (vqe, exact)
print("Tamam: VQE tam değere 1e-3 içinde yakınsadı.")
Related concepts
- VQE (variational quantum eigensolver) · VQE is a hybrid quantum algorithm that approximates the ground-state energy of a molecule by tuning a parameterised quantum circuit in a classical optimisation loop.
- Qiskit · Qiskit is IBM’s open-source Python library for quantum programming; it lets you build circuits, run them on simulators and send them to IBM quantum computers.