← Quantum Store · Guides · Lessons · Glossary · Packages · About · Türkçe

Xanadu

VQE and quantum machine learning with PennyLane

SoftwareQuantum machine learningLicence: Apache-2.0Version: 0.45.18.7 KB

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

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

  1. Build the Hamiltonian of a real molecule with the qml.qchem module.
  2. 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

Links

Package contents

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

← All packages

Share: X · LinkedIn