Skip to content

Commit

Permalink
Refactor output of OpenQASM 3 exporter to use fewer aliases (#10249)
Browse files Browse the repository at this point in the history
* Refactor output of OpenQASM 3 exporter to use fewer aliases

This removes spurious `_loose_bit` "registers" from the OpenQASM 3
output, and instead emits loose bits with individual `bit` and
`qubit` declarations.  Non-overlapping registers are emitted using
regular `bit[n]` and `qubit[n]` definitions when possible, and we only
resort to aliasing if we must to describe the structure.

This avoids introducing structure to the definitions that does not exist
in the original program, making round-trips and interactions with other
OQ3 consumers more straightforwards.  It's better not to use advanced
features that don't map to hardware particularly well when it's not
necessary.

On the technical side, all bits are now properly tracked in the symbol
table.  Previously, there was a lot of code duplication, internal state
tracking and magic inferences that attempted to "guess" how a
qubit/clbit should be referred to.  Instead, we just properly add them
as variables to the symbol table, which also drastically reduces the
number of objects that effectively reserve names that the user may not
use.

* Remove unnecessary empty init

* Fix typo

Co-authored-by: Ian Hincks <ian.hincks@ibm.com>

* Add explicit test of old option

---------

Co-authored-by: Ian Hincks <ian.hincks@ibm.com>
  • Loading branch information
jakelishman and ihincks authored Jul 18, 2023
1 parent 90c2fe6 commit 2194f55
Show file tree
Hide file tree
Showing 5 changed files with 315 additions and 364 deletions.
11 changes: 6 additions & 5 deletions qiskit/qasm3/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,6 @@ class QuantumInstruction(ASTNode):
| quantumBarrier
"""

def __init__(self):
pass


class ClassicalType(ASTNode):
"""Information about a classical type. This is just an abstract base for inheritance tests."""
Expand All @@ -135,6 +132,10 @@ def __init__(self, size: Optional[int] = None):
self.size = size


class BitType(ClassicalType):
"""Type information for a single bit."""


class BitArrayType(ClassicalType):
"""Type information for a sized number of classical bits."""

Expand Down Expand Up @@ -332,9 +333,9 @@ class AliasStatement(ASTNode):
: 'let' Identifier EQUALS indexIdentifier SEMICOLON
"""

def __init__(self, identifier: Identifier, concatenation: List[Identifier]):
def __init__(self, identifier: Identifier, value: Expression):
self.identifier = identifier
self.concatenation = concatenation
self.value = value


class QuantumGateModifierName(enum.Enum):
Expand Down
Loading

0 comments on commit 2194f55

Please sign in to comment.