Skip to content

Commit

Permalink
Rewrite OpenQASM 3 exporter symbol table
Browse files Browse the repository at this point in the history
This rewrites the symbol handling of the OpenQASM 3 exporter to decouple
object identities from the necessary object identifiers.  As part of
this, we use the same trick of standard-gate reparametrisation to
produce gate-definition sources for Qiskit built-ins, which fixes many
cases of bad parametrisation of gates like `rzx`.

This kind of rewrite was necessary to fix the now-bad assumption within
the OQ3 exporter that "gate identity" is always static within a circuit.
Since standard gate `Gate` instances are now only generated on demand,
there is no guarantee of stability of them.  The fix to the definition
source for these makes them independent of object identity.
User-defined gates can still use the identity, as these are still
guaranteed static.

This commit fixes almost all of the "bad parametrisation" tests in the
test suite.  There are several other changes in the test suite
necessary:

* since the uniqueness of the identifier is now independent of how the
  lookup of a Qiskit object works, there is no need to include the
  highly non-deterministic `id` in the generated symbols for user gates.
  Several tests changed to use the new, simple count-based unique names.

* the escaping and uniqueness rules now apply uniformly to all gate
  definitions, fixing several bad test cases that previously were
  testing invalid OpenQASM 3.

* the escaping rules changed slightly for naming collisions with
  keywords, making them slightly more consistent with how other renaming
  rules worked.
  • Loading branch information
jakelishman committed Jul 22, 2024
1 parent c3b468c commit ec8a321
Show file tree
Hide file tree
Showing 6 changed files with 807 additions and 939 deletions.
10 changes: 10 additions & 0 deletions crates/circuit/src/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,16 @@ impl StandardGate {
pub fn get_name(&self) -> &str {
self.name()
}

pub fn __eq__(&self, other: &Bound<PyAny>) -> Py<PyAny> {
let py = other.py();
let Ok(other) = other.extract::<Self>() else { return py.NotImplemented() };
(*self == other).into_py(py)
}

pub fn __hash__(&self) -> isize {
*self as isize
}
}

// This must be kept up-to-date with `StandardGate` when adding or removing
Expand Down
32 changes: 8 additions & 24 deletions qiskit/qasm3/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,39 +456,23 @@ class SubroutineBlock(ProgramBlock):
pass


class QuantumArgument(QuantumDeclaration):
"""
quantumArgument
: 'qreg' Identifier designator? | 'qubit' designator? Identifier
"""


class QuantumGateSignature(ASTNode):
class QuantumGateDefinition(Statement):
"""
quantumGateSignature
: quantumGateName ( LPAREN identifierList? RPAREN )? identifierList
quantumGateDefinition
: 'gate' quantumGateSignature quantumBlock
"""

def __init__(
self,
name: Identifier,
qargList: List[Identifier],
params: Optional[List[Expression]] = None,
params: Tuple[Identifier, ...],
qubits: Tuple[Identifier, ...],
body: QuantumBlock,
):
self.name = name
self.qargList = qargList
self.params = params


class QuantumGateDefinition(Statement):
"""
quantumGateDefinition
: 'gate' quantumGateSignature quantumBlock
"""

def __init__(self, quantumGateSignature: QuantumGateSignature, quantumBlock: QuantumBlock):
self.quantumGateSignature = quantumGateSignature
self.quantumBlock = quantumBlock
self.qubits = qubits
self.body = body


class SubroutineDefinition(Statement):
Expand Down
Loading

0 comments on commit ec8a321

Please sign in to comment.