Skip to content

Commit a6e69ba

Browse files
committed
Fix qasm3 exporter for std gates without stdgates.inc
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.
1 parent 37c0780 commit a6e69ba

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

qiskit/qasm3/exporter.py

+6
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ def __init__(self, includelist, basis_gates=()):
252252
def __setitem__(self, name_str, instruction):
253253
self._data[name_str] = instruction.base_class
254254
self._data[id(instruction)] = name_str
255+
self._data[f"{instruction.name}_{instruction.params}"] = name_str
255256

256257
def __getitem__(self, key):
257258
if isinstance(key, Instruction):
@@ -262,6 +263,11 @@ def __getitem__(self, key):
262263
pass
263264
# Built-in gates.
264265
if key.name not in self._data:
266+
try:
267+
# Registerd qiskit standard gate without stgates.inc
268+
return self._data[f"{key.name}_{key.params}"]
269+
except KeyError:
270+
pass
265271
raise KeyError(key)
266272
return key.name
267273
return self._data[key]

0 commit comments

Comments
 (0)