From 92bf92678d6c385923f8caee6b44473369e8610b Mon Sep 17 00:00:00 2001 From: "Kevin J. Sung" Date: Tue, 8 Nov 2022 07:50:45 -0500 Subject: [PATCH] add qubits and clbits args to QuantumCircuit.from_instructions (#9089) * add qubits and clbits args to QuantumCircuit.from_instructions * test classical bits too * handle iterable correctly --- qiskit/circuit/quantumcircuit.py | 14 +++++++++ .../python/circuit/test_circuit_operations.py | 31 +++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/qiskit/circuit/quantumcircuit.py b/qiskit/circuit/quantumcircuit.py index e982a8046c35..5c1f4134a90b 100644 --- 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) diff --git a/test/python/circuit/test_circuit_operations.py b/test/python/circuit/test_circuit_operations.py index 4b36555023fb..8b956482f3af 100644 --- 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)