This project implements a Quantum Generative Eigensolver (GQE) using PennyLane and PyTorch. The model aims to estimate molecular energies by applying a Transformer-based neural network to quantum chemical systems. The project combines quantum mechanics, variational principles, and machine learning to optimize and predict molecular energies efficiently.
Inspiration: This project was inspired by the PennyLane GQE training demo.
The core of this project is a GPT-like Transformer model that generates sequences of quantum operators (excitations) and evaluates their impact on molecular systems. It leverages a Quantum Generative Eigensolver (GQE) to minimize the difference between predicted and true molecular energies, providing an efficient method for simulating quantum systems such as molecules.
-
Quantum Generative Eigensolver (GQE): The GQE framework is inspired by classical eigensolvers, but it employs a quantum neural network to generate the operator sequences that simulate the evolution of a quantum state. The aim is to find the ground-state energy of a molecule by minimizing the loss function, which is defined based on the cumulative energy of operator sequences.
-
UCC Operator Pool: The project uses Unitary Coupled Cluster (UCC) operators to describe the excitations in molecular systems, forming a pool of operators applied to generate quantum states.
-
Hamiltonian and Expectation Values: The energy of a quantum system is described by the expectation value of its Hamiltonian:
$E(\theta) = \left\langle \psi(\theta) \middle| H \middle| \psi(\theta) \right\rangle$ where
$\lvert \psi(\theta)\rangle$ is the parameterized quantum state generated by the model, and$H$ is the Hamiltonian of the system. -
Subsequence Energy Calculation: The model generates sequences of quantum operators and applies them to an initial quantum state. The energy for each subsequence of operators is computed using:
$E_{\text{subsequence}} = \left\langle \psi_{\text{init}} \middle| U^\dagger H U \middle| \psi_{\text{init}} \right\rangle$
The quantum mechanical Hamiltonian of a molecular system describes the energy of electrons and nuclei. For a molecule like
where
The core mathematical concept is minimizing the loss function, which is defined by the mean-squared error (MSE) between predicted cumulative energies and the true molecular energy:
This is optimized using stochastic gradient descent (AdamW optimizer).
- Quantum Operator Pool: Generates and applies UCC (Unitary Coupled Cluster) and Lie Algebra-based operators to quantum states.
- Transformer-based Architecture: Uses a GPT-like neural network to predict energy sequences.
- Quantum Simulation: Calculates molecular energies using PennyLane and a qubit-based quantum simulator.
- Custom Loss Function: Minimizes the difference between predicted energy sequences and true molecular energies using variational methods.
To set up the project, follow these steps:
-
Clone the repository:
git clone https://github.com/JakeKitchen/Q-GESolver cd Q-GESolver -
Install the dependencies:
pip install -r requirements.txt
-
(Optional) Create and activate a virtual environment:
python -m venv venv source venv/bin/activate # For Windows: venv\Scripts\activate
Once you have set up the environment, you can train the Quantum Generative Eigensolver as follows:
- Run the training script:
python main.py
This will begin training the model, generating sequences of quantum operators and optimizing their energies to match the ground-state energy of a molecule (like
An example of the energy prediction and loss calculation is shown below:
from models.gpt import GPTQE, GPTConfig
gptqe = GPTQE(GPTConfig(
vocab_size=1000, # Depends on operator pool size
block_size=6,
n_layer=12,
n_head=12,
n_embd=768,
dropout=0.1
)).to("cuda")
# Example input: tokens and energy
tokens = torch.from_numpy(train_token_seq).to("cuda")
energies = torch.from_numpy(train_sub_seq_en).to("cuda")
# Forward pass
logits = gptqe(tokens)
loss = gptqe.calculate_loss(tokens, energies)
print(f"Loss: {loss.item()}")The GQE model predicts molecular energies with high accuracy, as shown by the following metrics:
- Mean Absolute Error (MAE): Measures how close the predictions are to the true energy values.
- Root Mean Squared Error (RMSE): Provides an average measure of the magnitude of the prediction error.
- Mean Absolute Percentage Error (MAPE): Quantifies the error as a percentage of the true energy.
The results demonstrate that the model converges towards the ground-state energy of the molecule with minimal error.
We welcome contributions to improve this project. Feel free to submit issues, create pull requests, or propose new features.
This project is licensed under the MIT License. See the LICENSE file for more details.