Skip to content

Commit

Permalink
Fix qasm3 exporter for std gates without stdgates.inc
Browse files Browse the repository at this point in the history
This commit fixes the handling of standard gates in Qiskit when the user
specifies excluding the use of the stdgates.inc file from the exported
qasm. Previously the object id of the standard gates were used to
maintain a lookup table of the global definitions for all the standard
gates explicitly in the file. However, the rust refactor means that
every time the exporter accesses `circuit.data[x].operation` a new
instance is returned. This means that on subsequent lookups for the
definition the gate definitions are never found. To correct this issue
this commit adds to the lookup table a fallback of the gate name +
parameters to do the lookup for. This should be unique for any standard
gate and not interfere with the previous logic that's still in place and
functional for other custom gate definitions.

While this fixes the logic in the exporter the test is still failing
because the test is asserting the object ids are the same in the qasm3
file, which isn't the case anymore. The test will be updated in a
subsequent commit to validate the qasm3 file is correct without using
a hardcoded object id.
  • Loading branch information
mtreinish committed May 25, 2024
1 parent 37c0780 commit a6e69ba
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions qiskit/qasm3/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ def __init__(self, includelist, basis_gates=()):
def __setitem__(self, name_str, instruction):
self._data[name_str] = instruction.base_class
self._data[id(instruction)] = name_str
self._data[f"{instruction.name}_{instruction.params}"] = name_str

def __getitem__(self, key):
if isinstance(key, Instruction):
Expand All @@ -262,6 +263,11 @@ def __getitem__(self, key):
pass
# Built-in gates.
if key.name not in self._data:
try:
# Registerd qiskit standard gate without stgates.inc
return self._data[f"{key.name}_{key.params}"]
except KeyError:
pass
raise KeyError(key)
return key.name
return self._data[key]
Expand Down

0 comments on commit a6e69ba

Please sign in to comment.