A semi-agnostic ansatz with variable structure for variational quantum algorithms
This repository contains the official implementation of VAns (Variable Ansatz), a variable structure approach to build ansatzes for Variational Quantum Algorithms (VQAs). VAns applies a set of rules to both grow and remove quantum gates in an informed manner during optimization, making it ideally suited to mitigate trainability and noise-related issues by keeping the ansatz shallow.
If you use VAns in your research, please cite:
@article{bilkis2023semi,
title={A semi-agnostic ansatz with variable structure for variational quantum algorithms},
author={Bilkis, M. and Cerezo, M. and Verdon, G. and Coles, P. J. and Cincio, L.},
journal={Quantum Machine Intelligence},
volume={5},
number={43},
year={2023},
publisher={Springer},
doi={10.1007/s42484-023-00132-1},
arxiv={2103.06712}
}VAns explores the hyperspace of architectures of parametrized quantum circuits to create short-depth ansatzes for VQA applications. The algorithm:
- Starts with a (potentially non-trivial) initial circuit and optimizes its parameters until convergence
- Inserts blocks of gates initialized to the identity, so that ansatzes at contiguous steps belong to an equivalence class
- Simplifies the circuit by eliminating gates and finding the shortest circuit
- Accepts or rejects modifications based on cost function improvements
This mechanism prevents circuits from over-growing and gets the most out of available quantum resources.
VAns has been successfully applied to:
- Condensed Matter Systems: Transverse Field Ising Model (TFIM), XXZ model
- Quantum Chemistry: Hβ, Hβ, LiH molecules
- Finds lower energy states than fixed-structure ansatzes using the same resources
- Data compression for quantum states
- Optimizes circuit structure for efficient state encoding/decoding
- Compiles target unitaries into optimized quantum circuits
- Reduces circuit depth while maintaining fidelity
- Python 3.7-3.9 (for TensorFlow Quantum compatibility)
- pip or conda
-
Clone the repository:
git clone https://github.com/matibilkis/qvans.git cd qvans -
Create a virtual environment (recommended):
python3 -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install dependencies:
pip install -r requirements.txt
For different TensorFlow Quantum versions, see Version Management.
-
Run VAns:
python3 main.py --problem TFIM --J 0.6 --g 1.0 --n_qubits 4
For a Transverse Field Ising Model (TFIM) with 4 qubits, J=0.6, g=1.0:
python3 main.py --problem TFIM --J 0.6 --g 1.0 --n_qubits 4 \
--reps 150 --qepochs 10000 --qlr 0.01For Hβ molecule at bond length 1.5 Γ :
python3 main.py --n_qubits 4 --problem_config '{"problem": "H2", "geometry": [("H", (0., 0., 0.)), ("H", (0., 0., 1.5))], "multiplicity": 1, "charge": 0, "basis": "sto-3g"}' \
--reps 200 --qepochs 10000Use meta_main.py to sweep over parameters:
# Edit meta_main.py to customize sweeps
js = np.arange(0.5, 1.6, 0.1)
for J in js:
problem_config = dict_to_json({"problem": "TFIM", "g": 1.0, "J": J})
instruction = "python3 main.py --n_qubits 4 --problem_config {}".format(problem_config)
insts.append(instruction)Then run:
python3 meta_main.py--n_qubits: Number of qubits (default: 8)--reps: Number of VAns iterations (default: 150)--path_results: Path to save results (default: "../data-vans/")--problem_config: JSON string with problem configuration
--qepochs: Maximum training epochs per VQE (default: 10000)--qlr: Learning rate (default: 0.01)--training_patience: Early stopping patience (default: 1000)--optimizer: Optimizer choice: "adam", "sgd", "adagrad" (default: "adam")--max_vqe_time: Maximum time per VQE in seconds (default: 300)
--initialization: Initial ansatz: "hea", "separable", "xz" (default: "hea")--init_layers_hea: Number of initial HEA layers (default: 1)--acceptance_percentage: Energy improvement threshold for acceptance (default: 0.01)--rate_iids_per_step: Rate of identity insertions per step (default: 1.5)--selector_temperature: Temperature for gate selection (default: 10.0)--wait_to_get_back: Iterations before reverting to best circuit (default: 25)
Interactive Jupyter notebooks are available in the tutorials/ directory:
0_introduction.ipynb: Basic circuit construction and VQE1_VANs_methods.ipynb: VAns algorithm components2_VANs_loop.ipynb: Complete VAns workflow
To run tutorials:
jupyter notebook tutorials/For reproducibility, we recommend using the exact versions specified in requirements.txt:
pip install -r requirements.txtThe results from the paper can be reproduced using the default parameters. Key configurations:
- TFIM:
--problem TFIM --J 0.5 --g 1.0 --n_qubits 4 - XXZ:
--problem XXZ --J 1.0 --g 1.0 --n_qubits 4 - Hβ: See quantum chemistry example above
- Hβ:
--problem H4 --geometry [...] --n_qubits 8
| Component | Version | Notes |
|---|---|---|
| Python | 3.7-3.9 | Required for TFQ compatibility |
| TensorFlow | 2.3.1 | Compatible with TFQ 0.4.0 |
| TensorFlow Quantum | 0.4.0 | Primary version |
| Cirq | 0.9.1 | Quantum circuit library |
| OpenFermion | 1.0.0 | Quantum chemistry support |
qvans/
βββ main.py # Main VAns execution script
βββ meta_main.py # Parameter sweep script
βββ requirements.txt # Python dependencies
βββ utilities/ # Core VAns modules
β βββ variational.py # VQE and Autoencoder classes
β βββ evaluator.py # Circuit evaluation and acceptance
β βββ idinserter.py # Identity insertion (growth)
β βββ simplifier.py # Circuit simplification
β βββ unitary_killer.py # Gate removal
β βββ circuit_basics.py # Basic circuit operations
β βββ chemical.py # Quantum chemistry Hamiltonians
β βββ qmodels.py # Quantum neural network models
βββ tutorials/ # Jupyter notebook tutorials
βββ examples_repository/ # Example results
βββ multivans/ # Experimental: Quantum combs (see below)
The multivans/ directory contains an experimental extension of VAns for optimizing quantum combs with communication channels. This work extends VAns to multi-party quantum protocols.
Note: This code was previously maintained in a separate repository (github.com/matibilkis/multivans) but has been merged into this repository for convenience. The separate repository is no longer actively maintained.
- State: Unfinished research code (4+ years old)
- Framework: Written in PennyLane (older version)
- Compatibility: May not work with current dependencies
- Purpose: Research exploration of quantum combs optimization
- Quantum channel discrimination
- Multi-party quantum protocols
- Circuit database representation
- Template-based circuit construction
MultiVAns is provided for research purposes only. The codebase:
- Uses older PennyLane APIs
- May require manual dependency resolution
- Has incomplete documentation
- Is not actively maintained
To explore MultiVAns:
cd multivans
# Review README.md for specific setup instructions
# Note: May require older dependency versionsRun basic functionality tests:
python3 -c "from utilities.variational import VQE; print('VQE import successful')"
python3 -c "from utilities.evaluator import Evaluator; print('Evaluator import successful')"Example results are stored in examples_repository/. Each run generates:
- Circuit structures (
.pklfiles) - Energy evolution (
.npyfiles) - Training metadata
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
This software is provided for research and educational purposes. See the LICENSE file for details and disclaimers regarding government funding and use restrictions.
- Los Alamos National Laboratory (LANL)
- Sandbox@Alphabet for TPU-based Floq simulator access
- Port d'InformaciΓ³ CientΓfica, Barcelona for computational resources
For questions or issues, please open a GitHub issue or contact the authors.
- Bilkis, M., et al. "A semi-agnostic ansatz with variable structure for variational quantum algorithms." Quantum Machine Intelligence 5, 43 (2023).
- Preskill, J. "NISQ-era and beyond." Quantum 2, 79 (2018).
- Cerezo, M., et al. "Variational quantum algorithms." Nature Reviews Physics 3, 625-644 (2021).
Note: This implementation corresponds to the published paper. For the latest experimental features, see the multivans/ directory (experimental status).
