From a6e69ba4c99cdd2fa50ed10399cada322bc88903 Mon Sep 17 00:00:00 2001 From: Matthew Treinish Date: Sat, 25 May 2024 16:17:21 -0400 Subject: [PATCH] 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. --- qiskit/qasm3/exporter.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/qiskit/qasm3/exporter.py b/qiskit/qasm3/exporter.py index a25d25805220..03602ca69857 100644 --- a/qiskit/qasm3/exporter.py +++ b/qiskit/qasm3/exporter.py @@ -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): @@ -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]