Repository: Qiskit/qiskit-terra (python)

Issue:
QASM 3 exporter does not respect non-standard basis gates
### Environment

- **Qiskit Terra version**: 0.20.2
- **Python version**: any
- **Operating system**: any


### What is happening?

The OpenQASM 3 exporter is not correctly handling non-standard (user-defined) gates put in `basis_gates`.  This is useful when the backend supplies some gates that Terra does not have decompositions for; one should be able to add the gate as an opaque gate, and use the `basis_gates` argument to cause Terra to treat it as having some external definition.  The transpiler does this correctly, but not the QASM 3 exporter.

### How can we reproduce the issue?

```python
from qiskit import QuantumCircuit, qasm3
from qiskit.circuit import Gate

qc = QuantumCircuit(1)
qc.x(0)
qc.append(Gate("my_gate", 1, []), [0], [])

qasm3.dumps(qc, basis_gates=["my_gate"])
```
results in an error from trying to export an opaque instruction:
```
---------------------------------------------------------------------------
QASM3ExporterError                        Traceback (most recent call last)
<ipython-input-2-5db45ede210a> in <module>
      6 qc.append(Gate("my_gate", 1, []), [0], [])
      7
----> 8 qasm3.dumps(qc, basis_gates=["my_gate"])

~/code/qiskit/terra/qiskit/qasm3/__init__.py in dumps(circuit, **kwargs)
     47         str: The OpenQASM3 serialization
     48     """
---> 49     return Exporter(**kwargs).dumps(circuit)
     50
     51

~/code/qiskit/terra/qiskit/qasm3/exporter.py in dumps(self, circuit)
    151         """Convert the circuit to QASM 3, returning the result as a string."""
    152         with io.StringIO() as stream:
--> 153             self.dump(circuit, stream)
    154             return stream.getvalue()
    155

~/code/qiskit/terra/qiskit/qasm3/exporter.py in dump(self, circuit, stream)
    163             alias_classical_registers=self.alias_classical_registers,
    164         )
--> 165         BasicPrinter(stream, indent=self.indent).visit(builder.build_program())
    166
    167

~/code/qiskit/terra/qiskit/qasm3/exporter.py in build_program(self)
    400         """Builds a Program"""
    401         self.hoist_declarations(self.global_scope(assert_=True).circuit.data)
--> 402         return ast.Program(self.build_header(), self.build_global_statements())
    403
    404     def hoist_declarations(self, instructions):

~/code/qiskit/terra/qiskit/qasm3/exporter.py in build_global_statements(self)
    518             ;
    519         """
--> 520         definitions = self.build_definitions()
    521         inputs, outputs, variables = self.build_variable_declarations()
    522         bit_declarations = self.build_classical_declarations()

~/code/qiskit/terra/qiskit/qasm3/exporter.py in build_definitions(self)
    548         ret = []
    549         for instruction in self._opaque_to_declare.values():
--> 550             ret.append(self.build_definition(instruction, self.build_opaque_definition))
    551         for instruction in self._subroutine_to_declare.values():
    552             ret.append(self.build_definition(instruction, self.build_subroutine_definition))

~/code/qiskit/terra/qiskit/qasm3/exporter.py in build_definition(self, instruction, builder)
    562             pass
    563         self._flat_reg = True
--> 564         definition = builder(instruction)
    565         self._flat_reg = False
    566         return definition

~/code/qiskit/terra/qiskit/qasm3/exporter.py in build_opaque_definition(self, instruction)
    569         """Builds an Opaque gate definition as a CalibrationDefinition"""
    570         # We can't do anything sensible with this yet, so it's better to loudly say that.
--> 571         raise QASM3ExporterError(
    572             "Exporting opaque instructions with pulse-level calibrations is not yet supported by"
    573             " the OpenQASM 3 exporter. Received this instruction, which appears opaque:"

QASM3ExporterError: "Exporting opaque instructions with pulse-level calibrations is not yet supported by the OpenQASM 3 exporter. Received this instruction, which appears opaque:\nInstruction(name='my_gate', num_qubits=1, num_clbits=0, params=[])"
```

### What should happen?

The error would be correct if the gate was truly opaque, but since it's passed in `basis_gates`, it should instead be output with no definition.

### Any suggestions?

The order of the logic in the exporter's `GlobalNamespace` is a bit out-of-order - it does the special handling for basis gates _after_ the check for regular instructions (which always returns a value for `Instruction` subclasses), so the check never triggered.

Pull request description:
Fix QASM 3 exporter not respecting unknown `basis_gates`
### Summary

Previously the logic in the QASM 3 exporter was out-of-order, and would
ignore non-standard names added to `basis_gates`.  This changes the
logic order to correctly assume that everything in `basis_gates` is
automatically defined.

<!--
⚠️ If you do not respect this template, your pull request will be closed.
⚠️ Your pull request title should be short detailed and understandable for all.
⚠️ Also, please add a release note file using reno if the change needs to be
  documented in the release notes.
⚠️ If your pull request fixes an open issue, please link to the issue.

- [ ] I have added the tests to cover my changes.
- [ ] I have updated the documentation accordingly.
- [ ] I have read the CONTRIBUTING document.
-->


### Details and comments

Fix #8159.



Patch:
diff --git a/qiskit/qasm3/exporter.py b/qiskit/qasm3/exporter.py
index 1d54b58b9..5a4a139f7 100644
--- a/qiskit/qasm3/exporter.py
+++ b/qiskit/qasm3/exporter.py
@@ -168,6 +168,8 @@ class Exporter:
 class GlobalNamespace:
     """Global namespace dict-like."""
 
+    BASIS_GATE = object()
+
     qiskit_gates = {
         "p": standard_gates.PhaseGate,
         "x": standard_gates.XGate,
@@ -205,7 +207,7 @@ class GlobalNamespace:
     include_paths = [abspath(join(dirname(__file__), "..", "qasm", "libs"))]
 
     def __init__(self, includelist, basis_gates=()):
-        self._data = {gate: None for gate in basis_gates}
+        self._data = {gate: self.BASIS_GATE for gate in basis_gates}
 
         for includefile in includelist:
             if includefile == "stdgates.inc":
@@ -240,13 +242,13 @@ class GlobalNamespace:
             return True
         if id(instruction) in self._data:
             return True
+        if self._data.get(instruction.name) is self.BASIS_GATE:
+            return True
         if type(instruction) in [Gate, Instruction]:  # user-defined instructions/gate
             return self._data.get(instruction.name, None) == instruction
-        if instruction.name in self._data:
-            if self._data.get(instruction.name) is None:  # it is a basis gate:
-                return True
-            if isinstance(instruction, self._data.get(instruction.name)):
-                return True
+        type_ = self._data.get(instruction.name)
+        if isinstance(type_, type) and isinstance(instruction, type_):
+            return True
         return False
 
     def register(self, instruction):
diff --git a/releasenotes/notes/qasm3-fix-basis-gates-c96a0b357dfdcb47.yaml b/releasenotes/notes/qasm3-fix-basis-gates-c96a0b357dfdcb47.yaml
new file mode 100644
index 000000000..c61a8a24c
--- /dev/null
+++ b/releasenotes/notes/qasm3-fix-basis-gates-c96a0b357dfdcb47.yaml
@@ -0,0 +1,5 @@
+---
+fixes:
+  - |
+    The OpenQASM 3 exporter (:mod:`qiskit.qasm3`) will no longer attempt to produce
+    definitions for non-standard gates in the `basis_gates` option.


Test patch:
diff --git a/test/python/circuit/test_circuit_qasm3.py b/test/python/circuit/test_circuit_qasm3.py
index e5dff6a25..d1857a9e7 100644
--- a/test/python/circuit/test_circuit_qasm3.py
+++ b/test/python/circuit/test_circuit_qasm3.py
@@ -22,7 +22,7 @@ import unittest
 from ddt import ddt, data
 
 from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit, transpile
-from qiskit.circuit import Parameter, Qubit, Clbit, Instruction
+from qiskit.circuit import Parameter, Qubit, Clbit, Instruction, Gate
 from qiskit.test import QiskitTestCase
 from qiskit.qasm3 import Exporter, dumps, dump, QASM3ExporterError
 from qiskit.qasm3.exporter import QASM3Builder
@@ -735,6 +735,27 @@ class TestCircuitQASM3(QiskitTestCase):
             expected_qasm,
         )
 
+    def test_opaque_instruction_in_basis_gates(self):
+        """Test that an instruction that is set in the basis gates is output verbatim with no
+        definition."""
+        qc = QuantumCircuit(1)
+        qc.x(0)
+        qc.append(Gate("my_gate", 1, []), [0], [])
+
+        basis_gates = ["my_gate", "x"]
+        transpiled = transpile(qc, initial_layout=[0])
+        expected_qasm = "\n".join(
+            [
+                "OPENQASM 3;",
+                "x $0;",
+                "my_gate $0;",
+                "",
+            ]
+        )
+        self.assertEqual(
+            Exporter(includes=[], basis_gates=basis_gates).dumps(transpiled), expected_qasm
+        )
+
     def test_reset_statement(self):
         """Test that a reset statement gets output into valid QASM 3.  This includes tests of reset
         operations on single qubits and in nested scopes."""