Repository: Qiskit/qiskit

Issue:
Add `qubits` and `clbits` arguments to QuantumCircuit.from_instructions
### What should we add?

These arguments can be used to initialize the QuantumCircuit. That way the user can control the ordering of `circuit.qubits`; otherwise, they will be in the order that the qubits are added.


Discussion:
Kevin and I discussed this offline before he opened the issue, and agreed on the names and behaviour.  This allows the sequencing behaviour that Kevin mentioned in the top, and in addition it can be used to ensure certain "spectator" qubits and clbits are also added in this alternate class constructor.

Resolving patch:
diff --git a/qiskit/circuit/quantumcircuit.py b/qiskit/circuit/quantumcircuit.py
--- a/qiskit/circuit/quantumcircuit.py
+++ b/qiskit/circuit/quantumcircuit.py
@@ -294,6 +294,8 @@ def from_instructions(
             | tuple[qiskit.circuit.Instruction, Iterable[Qubit], Iterable[Clbit]]
         ],
         *,
+        qubits: Iterable[Qubit] = (),
+        clbits: Iterable[Clbit] = (),
         name: Optional[str] = None,
         global_phase: ParameterValueType = 0,
         metadata: Optional[dict] = None,
@@ -302,6 +304,10 @@ def from_instructions(
 
         Args:
             instructions: The instructions to add to the circuit.
+            qubits: Any qubits to add to the circuit. This argument can be used,
+                for example, to enforce a particular ordering of qubits.
+            clbits: Any classical bits to add to the circuit. This argument can be used,
+                for example, to enforce a particular ordering of classical bits.
             name: The name of the circuit.
             global_phase: The global phase of the circuit in radians.
             metadata: Arbitrary key value metadata to associate with the circuit.
@@ -312,6 +318,14 @@ def from_instructions(
         circuit = QuantumCircuit(name=name, global_phase=global_phase, metadata=metadata)
         added_qubits = set()
         added_clbits = set()
+        if qubits:
+            qubits = list(qubits)
+            circuit.add_bits(qubits)
+            added_qubits.update(qubits)
+        if clbits:
+            clbits = list(clbits)
+            circuit.add_bits(clbits)
+            added_clbits.update(clbits)
         for instruction in instructions:
             if not isinstance(instruction, CircuitInstruction):
                 instruction = CircuitInstruction(*instruction)


Test patch:
diff --git a/test/python/circuit/test_circuit_operations.py b/test/python/circuit/test_circuit_operations.py
--- a/test/python/circuit/test_circuit_operations.py
+++ b/test/python/circuit/test_circuit_operations.py
@@ -1187,6 +1187,37 @@ def instruction_tuples_partial():
         self.assertEqual(circuit_tuples, expected)
         self.assertEqual(circuit_tuples_partial, expected)
 
+    def test_from_instructions_bit_order(self):
+        """Test from_instructions method bit order."""
+        qreg = QuantumRegister(2)
+        creg = ClassicalRegister(2)
+        a, b = qreg
+        c, d = creg
+
+        def instructions():
+            yield CircuitInstruction(HGate(), [b], [])
+            yield CircuitInstruction(CXGate(), [a, b], [])
+            yield CircuitInstruction(Measure(), [b], [d])
+            yield CircuitInstruction(Measure(), [a], [c])
+
+        circuit = QuantumCircuit.from_instructions(instructions())
+        self.assertEqual(circuit.qubits, [b, a])
+        self.assertEqual(circuit.clbits, [d, c])
+
+        circuit = QuantumCircuit.from_instructions(instructions(), qubits=qreg)
+        self.assertEqual(circuit.qubits, [a, b])
+        self.assertEqual(circuit.clbits, [d, c])
+
+        circuit = QuantumCircuit.from_instructions(instructions(), clbits=creg)
+        self.assertEqual(circuit.qubits, [b, a])
+        self.assertEqual(circuit.clbits, [c, d])
+
+        circuit = QuantumCircuit.from_instructions(
+            instructions(), qubits=iter([a, b]), clbits=[c, d]
+        )
+        self.assertEqual(circuit.qubits, [a, b])
+        self.assertEqual(circuit.clbits, [c, d])
+
     def test_from_instructions_metadata(self):
         """Test from_instructions method passes metadata."""
         qreg = QuantumRegister(2)