Repository: Qiskit/qiskit

Issue:
Calling `synth(registerless=False)` on a classical function synth labels the qubits wrongly
### Environment

- **Qiskit Terra version**:  0.23.0.dev0+dcd7bea
- **Python version**:  3.9

### What is happening?

In https://github.com/Qiskit/qiskit-terra/pull/9337#discussion_r1067055542 @Cryoris noticed that `classical_function` synthesis labels the qubits in the reversed order.

### How can we reproduce the issue?

:

```python
from qiskit.circuit.classicalfunction import classical_function
from qiskit.circuit.classicalfunction.types import Int1

@classical_function
def grover_oracle(a: Int1, b: Int1, c: Int1) -> Int1:
    return (a and b and not c)

quantum_circuit = grover_oracle.synth(registerless=False)
print(quantum_circuit.draw())
```
```
     c: ──■──
          │  
     b: ──■──
          │  
     a: ──o──
        ┌─┴─┐
return: ┤ X ├
        └───┘
```

### What should happen?

in that example, the open control is on `c`, not on `a`.

### Any suggestions?

_No response_


Discussion:
I'm not sure here what's expected. It could be either:
```
     a: ──■──
          │  
     b: ──■──
          │  
     c: ──o──
        ┌─┴─┐
return: ┤ X ├
        └───┘
```
or
```
     c: ──o──
          │  
     b: ──■──
          │  
     a: ──■──
        ┌─┴─┐
return: ┤ X ├
        └───┘
```

Tweedledum seems to prefer the first option.

Resolving patch:
diff --git a/qiskit/circuit/classicalfunction/classicalfunction.py b/qiskit/circuit/classicalfunction/classicalfunction.py
--- a/qiskit/circuit/classicalfunction/classicalfunction.py
+++ b/qiskit/circuit/classicalfunction/classicalfunction.py
@@ -168,7 +168,6 @@ def _define(self):
     def qregs(self):
         """The list of qregs used by the classicalfunction"""
         qregs = [QuantumRegister(1, name=arg) for arg in self.args if self.types[0][arg] == "Int1"]
-        qregs.reverse()
         if self.types[0]["return"] == "Int1":
             qregs.append(QuantumRegister(1, name="return"))
         return qregs
diff --git a/qiskit/circuit/classicalfunction/utils.py b/qiskit/circuit/classicalfunction/utils.py
--- a/qiskit/circuit/classicalfunction/utils.py
+++ b/qiskit/circuit/classicalfunction/utils.py
@@ -75,8 +75,7 @@ def tweedledum2qiskit(tweedledum_circuit, name=None, qregs=None):
         QuantumCircuit: The Tweedledum circuit converted to a Qiskit circuit.
 
     Raises:
-        ClassicalFunctionCompilerError: If there a gate in the Tweedledum circuit has no Qiskit
-        equivalent.
+        ClassicalFunctionCompilerError: If a gate in the Tweedledum circuit has no Qiskit equivalent.
     """
     if qregs:
         qiskit_qc = QuantumCircuit(*qregs, name=name)


Test patch:
diff --git a/test/python/classical_function_compiler/test_synthesis.py b/test/python/classical_function_compiler/test_synthesis.py
--- a/test/python/classical_function_compiler/test_synthesis.py
+++ b/test/python/classical_function_compiler/test_synthesis.py
@@ -49,10 +49,10 @@ def test_grover_oracle_arg_regs(self):
         qr_c = QuantumRegister(1, "c")
         qr_d = QuantumRegister(1, "d")
         qr_return = QuantumRegister(1, "return")
-        expected = QuantumCircuit(qr_d, qr_c, qr_b, qr_a, qr_return)
+        expected = QuantumCircuit(qr_a, qr_b, qr_c, qr_d, qr_return)
         expected.append(
             XGate().control(4, ctrl_state="1010"),
-            [qr_d[0], qr_c[0], qr_b[0], qr_a[0], qr_return[0]],
+            [qr_a[0], qr_b[0], qr_c[0], qr_d[0], qr_return[0]],
         )
 
         self.assertEqual(quantum_circuit.name, "grover_oracle")
diff --git a/test/python/visualization/test_circuit_text_drawer.py b/test/python/visualization/test_circuit_text_drawer.py
--- a/test/python/visualization/test_circuit_text_drawer.py
+++ b/test/python/visualization/test_circuit_text_drawer.py
@@ -48,8 +48,13 @@
     CPhaseGate,
 )
 from qiskit.transpiler.passes import ApplyLayout
+from qiskit.utils.optionals import HAS_TWEEDLEDUM
 from .visualization import path_to_diagram_reference, QiskitVisualizationTestCase
 
+if HAS_TWEEDLEDUM:
+    from qiskit.circuit.classicalfunction import classical_function
+    from qiskit.circuit.classicalfunction.types import Int1
+
 
 class TestTextDrawerElement(QiskitTestCase):
     """Draw each element"""
@@ -1216,6 +1221,31 @@ def test_text_spacing_2378(self):
         circuit.rz(11111, qr[2])
         self.assertEqual(str(_text_circuit_drawer(circuit)), expected)
 
+    @unittest.skipUnless(HAS_TWEEDLEDUM, "Tweedledum is required for these tests.")
+    def test_text_synth_no_registerless(self):
+        """Test synthesis's label when registerless=False.
+        See https://github.com/Qiskit/qiskit-terra/issues/9363"""
+        expected = "\n".join(
+            [
+                "                ",
+                "     a: |0>──■──",
+                "             │  ",
+                "     b: |0>──■──",
+                "             │  ",
+                "     c: |0>──o──",
+                "           ┌─┴─┐",
+                "return: |0>┤ X ├",
+                "           └───┘",
+            ]
+        )
+
+        @classical_function
+        def grover_oracle(a: Int1, b: Int1, c: Int1) -> Int1:
+            return a and b and not c
+
+        circuit = grover_oracle.synth(registerless=False)
+        self.assertEqual(str(_text_circuit_drawer(circuit)), expected)
+
 
 class TestTextDrawerLabels(QiskitTestCase):
     """Gates with labels."""