Skip to content

Commit

Permalink
add qubits and clbits args to QuantumCircuit.from_instructions (Qiski…
Browse files Browse the repository at this point in the history
…t#9089)

* add qubits and clbits args to QuantumCircuit.from_instructions

* test classical bits too

* handle iterable correctly
  • Loading branch information
kevinsung authored Nov 8, 2022
1 parent 3ce1737 commit fbff44b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
14 changes: 14 additions & 0 deletions qiskit/circuit/quantumcircuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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.
Expand All @@ -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)
Expand Down
31 changes: 31 additions & 0 deletions test/python/circuit/test_circuit_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit fbff44b

Please sign in to comment.