Skip to main content

A user-friendly wrapper for setting up and running VQE calculations with Qiskit.

Project description

Easy VQE

PyPI version License: Apache-2.0

A user-friendly Python package for setting up and running Variational Quantum Eigensolver (VQE) calculations using Qiskit. Define Hamiltonians as simple strings and ansatz circuits using nested lists/tuples.

Features

  • Simple Hamiltonian Definition: Define Hamiltonians using intuitive strings (e.g., "1.0 * XX - 0.5 * ZI + YZ"). Handles various formats, signs, and spacing.
  • Flexible Ansatz Creation: Build parameterized circuits using a list-based structure with standard Qiskit gate names. Auto-generates parameters.
  • Automated VQE Workflow: Handles parameter management, basis rotations, measurement simulation, expectation value calculation, and optimization loop.
  • Built-in Optimization: Uses scipy.optimize.minimize with configurable methods (COBYLA, Nelder-Mead, etc.) and options.
  • Simulation Backend: Leverages qiskit-aer for efficient classical simulation.
  • Convergence Plotting: Optionally save plots of the energy convergence during optimization.
  • Flexible Initial Parameters: Supports random initialization, zero initialization, or providing specific lists/NumPy arrays for starting parameters.
  • Result Summarization: Includes helper functions to print a clean summary of the VQE results.
  • Theoretical Energy Calculation: Provides a utility to compute the exact ground state energy via matrix diagonalization for comparison.
  • Circuit Visualization: Includes a helper function to draw the final ansatz circuit with the optimized parameters bound.

Installation

You can install easy_vqe using pip:

pip install easy-vqe

Or, for development, clone this repository and install in editable mode:

git clone https://github.com/7Abdoman7/easy_vqe.git 
cd easy_vqe
pip install -e .[dev] 

Quick Start

Here's a basic example to find the ground state energy of a 3-qubit Hamiltonian:

import numpy as np
from easy_vqe import find_ground_state

# 1. Define the Hamiltonian string
hamiltonian_str = "-1.0 * ZZI + 0.9 * ZIZ - 0.5 * IZZ + 0.2 * XXX"

# 2. Define the Ansatz Structure
ansatz_block = [
    ('ry', [0, 1, 2]), # Ry on qubits 0, 1, 2 (each gets a parameter)
    ('cx', [0, 1]),    # CNOT 0 -> 1
    ('cx', [1, 2]),    # CNOT 1 -> 2
    ('rz', [0, 1, 2]), # Rz on qubits 0, 1, 2 (each gets a parameter)
]
# Build the full ansatz: Hadamard layer, block, CNOT, block
ansatz_structure = [
    ('h', [0, 1, 2]),
    ansatz_block,
    ('cx', [0, 2]),
    ('barrier', []), # Optional barrier for visual separation
    ansatz_block,
]

# 3. Run the VQE algorithm
results = find_ground_state(
    ansatz_structure=ansatz_structure,
    hamiltonian_expression=hamiltonian_str,
    optimizer_method='COBYLA',
    optimizer_options={'maxiter': 200, 'tol': 1e-5}, # Optimizer settings
    n_shots=4096, # Number of shots for expectation value
    plot_filename="vqe_convergence.png" # Save the plot
)

# 4. Print Results
if 'error' not in results:
    print(f"Optimization Successful: {results['success']}")
    print(f"Optimizer Message: {results['message']}")
    print(f"Minimum Energy Found: {results['optimal_value']:.6f}")
    print(f"Optimal Parameters: {np.round(results['optimal_params'], 4)}")
else:
    print(f"VQE Failed: {results['error']}")

See the examples/ directory for more usage scenarios.

Contributing

Contributions are welcome! Please feel free to submit issues or pull requests. (Add more details here if you like - code style, testing requirements etc.)

License

This project is licensed under the Apache License 2.0 - see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

easy_vqe-1.1.0.tar.gz (23.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

easy_vqe-1.1.0-py3-none-any.whl (23.2 kB view details)

Uploaded Python 3

File details

Details for the file easy_vqe-1.1.0.tar.gz.

File metadata

  • Download URL: easy_vqe-1.1.0.tar.gz
  • Upload date:
  • Size: 23.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for easy_vqe-1.1.0.tar.gz
Algorithm Hash digest
SHA256 da79f7975afb902dac1ea4c6640c03496eec5e3a6d9fccbad94735f308334a52
MD5 4aa567434078e96a14cfb5134a7f82f4
BLAKE2b-256 c75702218a56d4f02cc3c6f4a1bf9f770c27a92ff60dbc150cac8de2ff8e7790

See more details on using hashes here.

File details

Details for the file easy_vqe-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: easy_vqe-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 23.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for easy_vqe-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 642406a1644cca5aefce55909a62b79e5198be50d39f0d335c78624441ca6904
MD5 2292e9832de93cd2d7dae2d923299545
BLAKE2b-256 c037bb0ee9a38addcdb15825b7794eb260346f336cc3950cb381cfe9a9298954

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page