-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add control flow support to QASM 3 exporter (#7299)
* Add control-flow support to QASM 3 exporter This teaches the OpenQASM 3 exporter, AST and printer to handle the new control-flow instructions, and output them to valid OpenQASM 3. There are nodes in the temporary AST to represent these, but significant extra changes were required as well to support scoping and scoped look-ups. This commit does not add a proper symbol table, nor a proper type system, which is how these look-ups should be handled, but instead makes a relatively quick hack to patch in looking up qubits and clbits from outer scopes. This is because the functionality is being added close to release of Terra 0.19, and we don't want to (nor have time to) make overarching changes to the whole infrastructure right now. The most notable change is that the 'context' management of the exporter is now a nested stack; the outer is for "outer" contexts, like the main program and subroutine/gate definitions (where we don't expect to share scoped variables or qubits), while the inner stacks are for control-flow scoping (where we do). To _very quickly_ support these look-ups, we add a `_Scope` struct that includes a look-up map of bits. While refactoring the bit-look-ups to support the scoped access, it became trivial to support loose bits, and to break the old assumption that all bits are always in registers. The exporter now supports loose classical bits and qubits, and registers with overlapping bits. For classical bits, the export will fail with the default settings if there are overlapping bits because there is little backend support for classical aliases, but the `alias_classical_registers` option will swap the handling to the alias form used for quantum registers, which can handle overlapping bits. * Use generic Enum, not IntEnum In this case, the values weren't meant to be interchangeable with integers anyway, it's just that the integer width is the only payload information in the type. In Python 3.6, the inheritance would break when using IntEnum with multiple inheritance (due to how it already inherits from int and Enum), so the solution is just to swap the type. * Build ast.FloatType lookup from enum values * Fix typos in tests * Swap skip to expectedFailure * Use loop parameter in for-loop tests * Use capital letters for enum constants * Clarify dangling-else comment * Fix missing enum name changes Co-authored-by: Kevin Krsulich <kevin.krsulich@ibm.com>
- Loading branch information
1 parent
6b6c683
commit 2c402dd
Showing
7 changed files
with
1,572 additions
and
549 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# This code is part of Qiskit. | ||
# | ||
# (C) Copyright IBM 2021. | ||
# | ||
# This code is licensed under the Apache License, Version 2.0. You may | ||
# obtain a copy of this license in the LICENSE.txt file in the root directory | ||
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. | ||
# | ||
# Any modifications or derivative works of this code must retain this | ||
# copyright notice, and modified files need to carry a notice indicating | ||
# that they have been altered from the originals. | ||
|
||
"""Exceptions that may be raised during processing OpenQASM 3.""" | ||
|
||
from qiskit.exceptions import QiskitError | ||
|
||
|
||
class QASM3Error(QiskitError): | ||
"""An error raised while working with OpenQASM 3 representations of circuits.""" | ||
|
||
|
||
class QASM3ExporterError(QASM3Error): | ||
"""An error raised during running the OpenQASM 3 exporter.""" |
Oops, something went wrong.