Qafny is a programming language for quantum programs. Its intended use is as a high-level representation to verify and compile quantum circuits, but we have also used it to implement verified versions of several quantum algorithms.
We first presented Qafny and its Dafny verification techniques in our paper Qafny: A Quantum Program Verifier at ECOOP 2024. We provided additional details in our paper Embedding Quantum Program Verification into Dafny presented at OOPSLA 2025.
This repository contains our Python implementation of Qafny as well as several verified quantum algorithms.
If you are interested in learning more about formal verification of quantum programs in general, we recommend Robert Rand's Verfied Quantum Computing tutorial.
To run Qafny, you will need Python v3.11.5 and the python packages antlr4-python3-runtime version 4.9.2 and rich version 13.9.4. The python packages are also listed in requirements.txt.
Assuming you have Python and Pip installed, the following script will clone this repository, setup a virtual environment and download the required dependencies.
# repository clone
git clone https://github.com/qafny/qafny_impl.git qafny
cd qafny
# environment setup
python -m venv .venv
python -m pip install -r ./src/requirements.txtOnce the repository has been downloaded and the python depedencies are setup, running python . in the src/ directory will run qafny. When this script is provided a filename, such as in python . supercoding.qfy it will attempt to convert and verify supercoding.qfy as a Qafny file. When the script is run without a filename, it will attempt to verify the test files in test/Qafny.
OPTIONS:
The main script also accepts a few options, including:
-d,--print-dafny: Outputs the generated Dafny code before verifying.-o [out_filename],--output [out_filename]: Writes the Dafny code to a file before verifying.-x,--skip-verify: Skips verification. Useful for inspecting the Dafny code without verifying it or manually running dafny.
Contains all the code associated with Qafny. The transformation from Qafny to Dafny involves parsing the file with ExpParser.py, transforming the ANTLR AST into a cleaner AST with ProgramTransformer.py, converting the Qafny AST to Dafny with ProgramTransfer.py and ultimately piping the Dafny AST into Dafny for verification.
Important files include:
__main__.py: The main Qafny file. Outlines the process for transforming Qafny to Dafny.
Exp.g4, ExpParser.py, ExpVisitor.py, etc.: Exp.g4 defines the Qafny grammar in ANTLR v4 syntax. All classes/files with the Exp prefix are generated from this grammar file.
Programmer.py: Defines an intermediate Qafny grammar.
ProgramTransformer.py: Converts an AST generated by ANTLR into the AST defined in Programmer.py.
ProgramTransfer.py: Converts a Qafny AST into a Dafny one.
Contains classes relating to printing formatted and stylized source code on the terminal.
Contains internal testing code for various classes.
Formal verifications of quantum algorithms written in Qafny code. All qafny code has the suffix .qfy.
test[1-16].qfy: smaller files that test single steps in quantum circuits.BellPair.qfy: An example of creating a bell pair in Qafny.Superdense.qfy: An implementation of superdense encoding.Teleportation.qfy: An implementation of Quantum teleportation.GHZ.qfy: Creates a Greenberger-Horne-Zeilinger entangled quantum state.Grovers.qfy: An implementation of Grover's algorithm in Qafny.Shors.qfy: An implementation of Shor's algorithm in Qafny.simon.qfy: An implementation of Simon's algorithm in Qafny.HammingWeight.qfy: An implementation of a hamming weight calculation in Qafny.HSG.qfy: Abelian sub-group problems. Based off of Andrew Childs' lecture notes.HSP.qfy: An implementation of the hidden shift problem. Based on Quantum Algorithms for some Hidden Shift Problems.BHSP.qfy: An implementation for the Boolean hidden shift problem. Based off of Quantum algorithm for the Boolean hidden shift problem.BHT.qfy: An implementation of the Brassard-Høyer-Tapp algorithm, a quantum algorithm that solves the collision problem.BV.qfy: A proof of the Bernstein–Vazirani algorithm in Qafny.LCU.qfy: A proof for the linear combination of unitaries in Qafny.LongDistanceEntangle.qfy: A proof of long-distance entanglement to show that the teleportation process preserves the entanglement information.NonBoolean.qfy: An implementation of non-boolean amplitude amplification. Based on Non-Boolean Quantum Amplitude Amplification and Quantum Mean Estimation.QFTModQ.qfy: Algorithm for quantum fourier transform with modulus. Based on section 0.3 "QFT mod Q" from Vazirani's lecture notes.QPE.qfy: Quantum phase estimation.QuantumFixedPoint.qfy: An implementation of quantum fixed point search.DeutschJozsa.qfy: A proof of the Deutsch-Jozsa algorithm in Qafny.DiscreteLog.qfy: An implementation of discrete logarithm in Qafny.Stabilizer.qfy: An implementation of the abelian stabilizer problem / Kitaev's algorithm. Based on Quantum measurements and the Abelian Stabilizer Problem.StateDistinguishing.qfy: An implementation of quantum fingerprinting without shared keys. Based on Quantum fingerprinting.SWAPTest.qfy: The swap test from the same Quantum fingerpriting paper.FirstAmpEstimate.qfy: Amplitude estimation without phase estimation. Based on Amplitude Estimation without Phase Estimation.FixedPtSearch.qfy: An implementation of fixed point quantum search.FOQA.qfy: An implementation of non-boolean quantum amplitude amplification. Based on Non-Boolean Quantum Amplitude Amplification and Quantum Mean Estimation.AmpAmp.qfy: Amplitude estimation without phase estimation. Based off of Quantum Amplitude Amplification and Estimation.AmplitudeEstimation.qfy: An algorithm for amplitude estimation. Based off of The Quantum Amplitude Estimation Algorithms on Near-Term Devices: A Practical Guide and Iterative quantum amplitude estimation.SimpleAmpEstimate.qfy: An implementation of amplitude estimation and counting. Based on Quantum Amplitude Amplification and Estimation.AppxCounting.qfy: Amplitude estimation with counting. Based off of Quantum Amplitude Amplification and Estimation and Quantum Existence Testing and Its Application for Finding Extreme Values in Unsorted Databases.
This project is the result of the efforts of many people. The primary contacts for this project are Dr. Liyi Li (@liyili2) and Feifei Cheng (@jump-orange). The full list of contributors include:
- Liyi Li
- Feifei Cheng
- Sushen Vangeepuram
- Seyed Mohammad Reza Jafari
- Alex Potanin
- Henry Allard
- Mingwei Zhu
- Rance Cleaveland
- Alexander Nicolellis
- Yi Lee
- Le Chang
- Xiaodi Wu
If you use Qafny in your work, please cite our papers:
@InProceedings{li_et_al:LIPIcs.ECOOP.2024.24,
author = {Li, Liyi and Zhu, Mingwei and Cleaveland, Rance and Nicolellis, Alexander and Lee, Yi and Chang, Le and Wu, Xiaodi},
title = {{Qafny: A Quantum-Program Verifier}},
booktitle = {38th European Conference on Object-Oriented Programming (ECOOP 2024)},
pages = {24:1--24:31},
series = {Leibniz International Proceedings in Informatics (LIPIcs)},
ISBN = {978-3-95977-341-6},
ISSN = {1868-8969},
year = {2024},
volume = {313},
editor = {Aldrich, Jonathan and Salvaneschi, Guido},
publisher = {Schloss Dagstuhl -- Leibniz-Zentrum f{\"u}r Informatik},
address = {Dagstuhl, Germany},
URL = {https://drops.dagstuhl.de/entities/document/10.4230/LIPIcs.ECOOP.2024.24},
URN = {urn:nbn:de:0030-drops-208735},
doi = {10.4230/LIPIcs.ECOOP.2024.24},
annote = {Keywords: Quantum Computing, Automated Verification, Separation Logic}
note = {ECOOP '24}
}@InProceedings {cheng_et_al:
doi = {10.1145/3763157},
}Alternatively, you can cite our repository using the information in CITATION.cff.