-
Notifications
You must be signed in to change notification settings - Fork 45
Add quantum exact lanczos #398
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
wsttiger
wants to merge
6
commits into
NVIDIA:main
Choose a base branch
from
wsttiger:add_quantum_exact_lanczos
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Implements block encoding for quantum Hamiltonians using the Pauli Linear Combination of Unitaries (LCU) approach. This is a fundamental building block for quantum algorithms such as Quantum Eigenvalue Learning (QEL), Quantum Singular Value Transformation (QSVT), and Hamiltonian simulation. Key additions: - Abstract block_encoding base class defining the PREPARE, SELECT, and UNPREPARE operations - pauli_lcu concrete implementation optimized for Pauli Hamiltonians from quantum chemistry (e.g., molecular Hamiltonians) - State preparation tree using controlled rotations with binary control patterns - Controlled Pauli operations indexed by ancilla register state - Support for Hamiltonians with up to 1024 terms (10 ancilla qubits) - Comprehensive unit tests covering various Hamiltonian types The implementation uses log₂(# terms) ancilla qubits and achieves α = ||H||₁ normalization. Quantum kernels are implemented as stateless functors compatible with the CUDA-Q compiler. Files modified: - libs/solvers/include/cudaq/solvers/operators/block_encoding.h (new) - libs/solvers/lib/operators/block_encoding.cpp (new) - libs/solvers/unittests/test_block_encoding.cpp (new) - libs/solvers/include/cudaq/solvers/operators.h - libs/solvers/lib/CMakeLists.txt - libs/solvers/unittests/CMakeLists.txt Signed-off-by: Scott Thornton <[email protected]>
Implements the Quantum Exact Lanczos method for computing ground state energies of quantum Hamiltonians. QEL uses block encoding and amplitude amplification to build a Krylov subspace, then returns the Krylov matrices for eigenvalue extraction via classical linear algebra. Key features: - Implements PREPARE-SELECT-REFLECT quantum circuits for moment collection - Builds Krylov Hamiltonian (H) and overlap (S) matrices from 2k moments - Returns matrices to user for solving H|v⟩ = E·S|v⟩ in Python - Clean free-function API following VQE pattern - Observable construction utilities for ancilla projectors - Support for Hamiltonians with up to 1024 terms (10 ancilla qubits) Algorithm components: - qel_reflection_kernel: Amplitude amplification via PREPARE†-X-CZ-X-PREPARE - qel_state_prep_kernel: Odd moment measurement circuits - qel_measure_even_kernel: Even moment measurement circuits - build_R_observable: R = 2P₀ - I for even moments - build_U_observable: Σᵢ sign(cᵢ)Pᵢ⊗Pᵢ for odd moments - build_krylov_matrices: Constructs H and S from moment vector Design decisions: - No C++ eigenvalue solver dependency (user handles in Python/NumPy) - Built on pauli_lcu block encoding infrastructure - Stateless quantum kernels compatible with CUDA-Q compiler - Row-major matrix flattening for easy NumPy integration Files added: - libs/solvers/include/cudaq/solvers/quantum_exact_lanczos.h - libs/solvers/lib/quantum_exact_lanczos.cpp - libs/solvers/unittests/test_quantum_exact_lanczos.cpp All unit tests passing (H2 molecule, simple operators, metadata checks). Signed-off-by: Scott Thornton <[email protected]>
Add comprehensive Python bindings for QEL algorithm enabling hybrid quantum-classical ground state energy calculations. Exposed Classes: - BlockEncoding: Abstract base class (prepare, select, apply methods) - PauliLCU: Pauli LCU implementation with NumPy array accessors - QELResult: Result container returning Krylov matrices as NumPy arrays - quantum_exact_lanczos(): Main function with krylov_dim/shots/verbose kwargs Includes: - py_block_encoding.cpp: Full pybind11 bindings (379 lines) - test_quantum_exact_lanczos.py: 7 test cases (all passing) - qel_h2_example.py: 5 working examples with H2 molecule - README_QEL.md: Complete API reference and usage guide Usage: result = solvers.quantum_exact_lanczos(h2, num_qubits=4, n_electrons=2) H = result.get_hamiltonian_matrix() S = result.get_overlap_matrix() eigenvalues = scipy.linalg.eigh(H, S) Follows existing VQE/QAOA binding patterns. Integrates with cudaq.SpinOperator via type casters. Enables flexible eigenvalue extraction at Python level. 7 files changed, 1287 insertions(+) Signed-off-by: Scott Thornton <[email protected]>
Expand block encoding support and add comprehensive LiH testing with proper constant term handling for accurate molecular energy calculations. Block Encoding Expansion: - Increase ancilla qubit support from 10 to 20 in PauliLCU preparation - Now handles Hamiltonians with up to 2^20 (~1M) Pauli terms - Added explicit controlled-RY cases for layers 10-19 - Added error handling for >20 ancilla with informative message - Enables large molecular systems like LiH (630 terms, 10 ancilla) LiH Molecule Support: - Add lih_hamiltonian_data.py with 630 Pauli terms from STO-3G basis - LiH geometry: 1.595 Å bond length, 12 qubits, 4 electrons - FCI target energy: -7.882402 Ha, HF energy: -7.862024 Ha - Standalone data file, no runtime PySCF/OpenFermion dependency QEL Testing Improvements: - Add test_quantum_exact_lanczos_lih_molecule() with krylov_dim=8 - Fix critical constant term handling bug in H2 and LiH tests - Constant terms now kept separate from Hamiltonian to preserve 1-norm - Energy calculation: eigenvalue * normalization + constant (manual) Bug Fix - Constant Term Handling: - Previously: Adding constant to Hamiltonian inflated 1-norm incorrectly * H2: 1.887 → 1.984 (includes |const|) * LiH: 12.342 → 16.477 (includes |const|) - Fixed: Keep constant separate, add after eigenvalue extraction - Results: H2 error 0.00 mHa (exact), LiH error 1.44 mHa (excellent) Performance: - LiH test runtime: ~206 seconds (22 qubits: 12 system + 10 ancilla) - Both H2 and LiH achieve chemical accuracy (<2.5 mHa error) - C++ implementation 8% faster than pure Python equivalent Testing: pytest libs/solvers/python/tests/test_quantum_exact_lanczos.py::test_quantum_exact_lanczos_h2_molecule pytest libs/solvers/python/tests/test_quantum_exact_lanczos.py::test_quantum_exact_lanczos_lih_molecule 3 files changed, 965 insertions(+), 5 deletions(-) Signed-off-by: Scott Thornton <[email protected]>
Extends the QEL test suite with two additional molecular systems using
pre-extracted Hamiltonians for reproducibility. This brings the total
molecular test coverage to four systems (H2, LiH, N2, H2O) spanning
different chemical bonding types and system sizes.
New Test Cases:
- N2 molecule: 8 qubits (4e, 4o), 80 Pauli terms, 0.33 mHa accuracy
- H2O molecule: 8 qubits (4e, 4o), 104 Pauli terms, 0.22 mHa accuracy
Implementation Details:
- Pre-extracted Hamiltonian data files ensure reproducibility and avoid
PySCF/chemistry library variability
- Enhanced parse_pauli_term() to handle both space-separated ("Z0 Z1")
and concatenated ("Z0Z1") Pauli string formats using regex
- Both tests use CASCI reference energies from STO-3G calculations
- Tests include S-matrix filtering for numerical stability
Test Results:
- All 11 QEL tests passing (6 unit tests + 4 molecular tests + 1 API test)
- Total test runtime: ~3.5 minutes
- Molecular test accuracies: H2 (0.11 mHa), LiH (0.66 mHa),
N2 (0.33 mHa), H2O (0.22 mHa)
Files:
- libs/solvers/python/tests/n2_hamiltonian_data.py: N2 Hamiltonian
extracted via cudaq_solvers.create_molecule()
- libs/solvers/python/tests/h2o_hamiltonian_data.py: H2O Hamiltonian
with (4e, 4o) active space
- libs/solvers/python/tests/test_quantum_exact_lanczos.py: Added
test_quantum_exact_lanczos_n2_molecule() and
test_quantum_exact_lanczos_h2o_molecule()
Signed-off-by: Scott Thornton <[email protected]>
Extends QEL molecular test suite with benzene, demonstrating the algorithm's capability on aromatic π-systems. Uses a reduced (4e, 4o) active space to maintain computational tractability while capturing essential π-orbital chemistry. New Test Case: - Benzene (C6H6): 8 qubits, 160 Pauli terms, 0.76 mHa accuracy - Active space: (4e, 4o) captures HOMO-1, HOMO, LUMO, LUMO+1 π-orbitals - CASCI reference energy: -227.945110 Ha (STO-3G basis) - Test runtime: ~8.5 seconds Implementation: - libs/solvers/python/tests/benzene_hamiltonian_data.py: Pre-extracted Hamiltonian using cudaq_solvers.create_molecule() for reproducibility - libs/solvers/python/tests/test_quantum_exact_lanczos.py: Added test_quantum_exact_lanczos_benzene_molecule() with S-matrix filtering - docs/qel_test_molecules.md: Added benzene to implemented tests table and documented active space selection rationale Design Note: Full (6e, 6o) π-system would yield ~1000-3000 Pauli terms, making tests prohibitively slow. The (4e, 4o) truncation provides a good balance between chemical relevance and computational efficiency. Test Results: - All 12 QEL tests passing (7 unit + 5 molecular) - Total test suite runtime: ~3.6 minutes - Molecular test accuracies: H2 (0.11 mHa), LiH (0.66 mHa), N2 (0.33 mHa), H2O (0.22 mHa), Benzene (0.76 mHa) Signed-off-by: Scott Thornton <[email protected]>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Add Quantum Exact Lanczos (QEL) Algorithm to CUDA-Q Solvers
This PR introduces a complete implementation of the Quantum Exact Lanczos (QEL) algorithm for computing ground state energies of molecular Hamiltonians using hybrid quantum-classical methods.
Overview
QEL uses block encoding and amplitude amplification to build a Krylov subspace from quantum moment measurements, then solves a generalized eigenvalue problem classically to extract ground state energies. This implementation provides both C++ and Python interfaces with comprehensive testing on five molecular systems.
Key Components
Core Algorithm (
quantum_exact_lanczos.cpp, 340 lines)Block Encoding Support (
block_encoding.cpp)Python Bindings (
py_block_encoding.cpp, 379 lines)BlockEncodingabstract base classPauliLCUwith NumPy array accessors for debug/inspectionQELResultcontainer withget_hamiltonian_matrix(),get_overlap_matrix()quantum_exact_lanczos()function withkrylov_dim,shots,verbosekwargsComprehensive Testing
Molecular Test Coverage
All tests achieve sub-mHa accuracy (chemical accuracy is 2.5 mHa).
Design Highlights
1. Proper Constant Term Handling
SpinOperatorinflates normalization incorrectlyE = eigenvalue * α + constant(constant added manually)2. S-Matrix Filtering for Numerical Stability
3. Pre-Extracted Hamiltonians
cudaq_solvers.create_molecule()4. Scalable Architecture
Example Usage
Documentation