-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Add Rust-based OpenQASM 2 converter #9784
Merged
Merged
Commits on Mar 13, 2023
-
Add Rust-based OpenQASM 2 converter
This is a vendored version of qiskit-qasm2 (https://pypi.org/project/qiskit-qasm2), with this initial commit being equivalent (barring some naming / documentation / testing conversions to match Qiskit's style) to version 0.5.3 of that package. This adds a new translation layer from OpenQASM 2 to Qiskit, which is around an order of magnitude faster than the existing version in Python, while being more type safe (in terms of disallowing invalid OpenQASM 2 programs rather than attempting to construction `QuantumCircuit`s that are not correct) and more extensible. The core logic is a hand-written lexer and parser combination written in Rust, which emits a bytecode stream across the PyO3 boundary to a small Python interpreter loop. The main bulk of the parsing logic is a simple LL(1) recursive-descent algorithm, which delegates to more specific recursive Pratt-based algorithm for handling classical expressions. Many of the design decisions made (including why the lexer is written by hand) are because the project originally started life as a way for me to learn about implementations of the different parts of a parser stack; this is the principal reason there are very few external crates used. There are a few inefficiencies in this implementation, for example: - the string interner in the lexer allocates twice for each stored string (but zero times for a lookup). It may be possible to completely eliminate allocations when parsing a string (or a file if it's read into memory as a whole), but realistically there's only a fairly small number of different tokens seen in most OpenQASM 2 programs, so it shouldn't be too big a deal. - the hand-off from Rust to Python transfers small objects frequently. It might be more efficient to have a secondary buffered iterator in Python space, transferring more bytecode instructions at a time and letting Python resolve them. This form could also be made asynchronous, since for the most part, the Rust components only need to acquire the CPython GIL at the API boundary. - there are too many points within the lexer that can return a failure result that needs unwrapping at every site. Since there are no tokens that can span multiple lines, it should be possible to refactor so that almost all of the byte-getter and -peeker routines cannot return error statuses, at the cost of the main lexer loop becoming responsible for advancing the line buffer, and moving the non-ASCII error handling into each token constructor. I'll probably keep playing with some of those in the `qiskit-qasm2` package itself when I have free time, but at some point I needed to draw the line and vendor the package. It's still ~10x faster than the existing one: In [1]: import qiskit.qasm2 ...: prog = """ ...: OPENQASM 2.0; ...: include "qelib1.inc"; ...: qreg q[2]; ...: """ ...: prog += "rz(pi * 2) q[0];\ncx q[0], q[1];\n"*100_000 ...: %timeit qiskit.qasm2.loads(prog) ...: %timeit qiskit.QuantumCircuit.from_qasm_str(prog) 2.26 s ± 39.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 22.5 s ± 106 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) `cx`-heavy programs like this one are actually the ones that the new parser is (comparatively) slowest on, because the construction time of `CXGate` is higher than most gates, and this dominates the execution time for the Rust-based parser.
Configuration menu - View commit details
-
Copy full SHA for fccd043 - Browse repository at this point
Copy the full SHA fccd043View commit details
Commits on Mar 14, 2023
-
Work around docs failure on Sphinx 5.3, Python 3.9
The version of Sphinx that we're constrained to use in the docs build can't handle the `Unpack` operator, so as a temporary measure we can just relax the type hint a little.
Configuration menu - View commit details
-
Copy full SHA for 5723864 - Browse repository at this point
Copy the full SHA 5723864View commit details
Commits on Mar 15, 2023
-
Configuration menu - View commit details
-
Copy full SHA for ea9d7b8 - Browse repository at this point
Copy the full SHA ea9d7b8View commit details -
Configuration menu - View commit details
-
Copy full SHA for d730709 - Browse repository at this point
Copy the full SHA d730709View commit details
Commits on Mar 17, 2023
-
Configuration menu - View commit details
-
Copy full SHA for 02b3934 - Browse repository at this point
Copy the full SHA 02b3934View commit details -
Configuration menu - View commit details
-
Copy full SHA for 203c7bb - Browse repository at this point
Copy the full SHA 203c7bbView commit details -
Configuration menu - View commit details
-
Copy full SHA for f74c872 - Browse repository at this point
Copy the full SHA f74c872View commit details -
Configuration menu - View commit details
-
Copy full SHA for 2fffb06 - Browse repository at this point
Copy the full SHA 2fffb06View commit details -
Configuration menu - View commit details
-
Copy full SHA for dedb720 - Browse repository at this point
Copy the full SHA dedb720View commit details
Commits on Mar 18, 2023
-
Configuration menu - View commit details
-
Copy full SHA for b9b1276 - Browse repository at this point
Copy the full SHA b9b1276View commit details -
This should have no impact on runtime or on memory usage, since each of the new types has the same bit width and alignment as the `usize` values they replace.
Configuration menu - View commit details
-
Copy full SHA for a95e8e4 - Browse repository at this point
Copy the full SHA a95e8e4View commit details
Commits on Mar 30, 2023
-
Configuration menu - View commit details
-
Copy full SHA for 6048257 - Browse repository at this point
Copy the full SHA 6048257View commit details -
Configuration menu - View commit details
-
Copy full SHA for 9c28c1e - Browse repository at this point
Copy the full SHA 9c28c1eView commit details -
Configuration menu - View commit details
-
Copy full SHA for 2eb339a - Browse repository at this point
Copy the full SHA 2eb339aView commit details -
Configuration menu - View commit details
-
Copy full SHA for d5d7115 - Browse repository at this point
Copy the full SHA d5d7115View commit details -
Configuration menu - View commit details
-
Copy full SHA for 3c0db61 - Browse repository at this point
Copy the full SHA 3c0db61View commit details -
Configuration menu - View commit details
-
Copy full SHA for 46a62ea - Browse repository at this point
Copy the full SHA 46a62eaView commit details -
Configuration menu - View commit details
-
Copy full SHA for fdc6a9d - Browse repository at this point
Copy the full SHA fdc6a9dView commit details -
Configuration menu - View commit details
-
Copy full SHA for e9ffd41 - Browse repository at this point
Copy the full SHA e9ffd41View commit details -
Co-authored-by: Luciano Bello <[email protected]> Co-authored-by: Kevin Hartman <[email protected]> Co-authored-by: Eric Arellano <[email protected]>
Configuration menu - View commit details
-
Copy full SHA for af56d2b - Browse repository at this point
Copy the full SHA af56d2bView commit details
Commits on Apr 3, 2023
-
Configuration menu - View commit details
-
Copy full SHA for aebb86e - Browse repository at this point
Copy the full SHA aebb86eView commit details
Commits on Apr 4, 2023
-
Refactor custom built-in gate definitions
The previous system was quite confusing, and required all accesses to the global symbol table to know that the `Gate` symbol could be present but overridable. This led to confusing logic, various bugs and unnecessary constraints, such as it previously being (erroneously) possible to provide re-definitions for any "built-in" gate. Instead, we keep a separate store of instructions that may be redefined. This allows the logic to be centralised to only to the place responsible for performing those overrides, and remains accessible for error-message builders to query in order to provide better diagnostics.
Configuration menu - View commit details
-
Copy full SHA for 0816bfb - Browse repository at this point
Copy the full SHA 0816bfbView commit details -
Configuration menu - View commit details
-
Copy full SHA for 1072db2 - Browse repository at this point
Copy the full SHA 1072db2View commit details -
Co-authored-by: Alexander Ivrii <[email protected]>
Configuration menu - View commit details
-
Copy full SHA for eaa0aab - Browse repository at this point
Copy the full SHA eaa0aabView commit details
Commits on Apr 12, 2023
-
Configuration menu - View commit details
-
Copy full SHA for 2c54a1a - Browse repository at this point
Copy the full SHA 2c54a1aView commit details -
Co-authored-by: Matthew Treinish <[email protected]>
Configuration menu - View commit details
-
Copy full SHA for bdd373e - Browse repository at this point
Copy the full SHA bdd373eView commit details -
Remove dependency on
lazy_static
For a hashset of only 6 elements that is only checked once, there's not really any point to pull in an extra dependency or use a hash set at all.
Configuration menu - View commit details
-
Copy full SHA for 5f74f1e - Browse repository at this point
Copy the full SHA 5f74f1eView commit details -
Configuration menu - View commit details
-
Copy full SHA for f6f980b - Browse repository at this point
Copy the full SHA f6f980bView commit details
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.