Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add qubits and clbits args to QuantumCircuit.from_instructions #9089

Merged
merged 3 commits into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 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: Optional[Iterable[Qubit]] = None,
clbits: Optional[Iterable[Qubit]] = None,
kevinsung marked this conversation as resolved.
Show resolved Hide resolved
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,12 @@ def from_instructions(
circuit = QuantumCircuit(name=name, global_phase=global_phase, metadata=metadata)
added_qubits = set()
added_clbits = set()
if qubits:
circuit.add_bits(qubits)
added_qubits.update(qubits)
if clbits:
circuit.add_bits(clbits)
added_clbits.update(clbits)
jakelishman marked this conversation as resolved.
Show resolved Hide resolved
for instruction in instructions:
if not isinstance(instruction, CircuitInstruction):
instruction = CircuitInstruction(*instruction)
Expand Down
29 changes: 29 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,35 @@ 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=[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