Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 8 additions & 6 deletions src/qc_compiler/batching.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,9 @@ def _structural_batch(
"""Batch circuits with same depth on non-overlapping qubit subsets.

Circuits using different qubits of the same device can run
in parallel, maximizing qubit utilization.
in parallel, maximizing qubit utilization. Uses the device's
total qubit count (max_qubits) to determine how many circuits
can be placed without overlapping.

Args:
circuits: List of quantum circuits.
Expand All @@ -240,22 +242,22 @@ def _structural_batch(
batch_sizes = []

for depth, indices in depth_groups.items():
current_batch_qubits = set()
current_batch = []
total_qubits_used = 0

for idx in indices:
circuit = circuits[idx]
circuit_qubits = set(range(circuit.num_qubits))
n_qubits = circuit.num_qubits

if not current_batch_qubits & circuit_qubits:
if total_qubits_used + n_qubits <= self.max_qubits:
current_batch.append(circuit)
current_batch_qubits |= circuit_qubits
total_qubits_used += n_qubits
else:
if current_batch:
batches.append(current_batch)
batch_sizes.append(len(current_batch))
current_batch = [circuit]
current_batch_qubits = circuit_qubits.copy()
total_qubits_used = n_qubits

if current_batch:
batches.append(current_batch)
Expand Down
36 changes: 35 additions & 1 deletion tests/test_batching.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,4 +318,38 @@ def test_estimate_speedup_empty_groups(self, batcher):

def test_estimate_structural_speedup_empty(self, batcher):
speedup = batcher._estimate_structural_speedup([], [])
assert speedup == 1.0
assert speedup == 1.0


class TestStructuralBatchingDeviceQubits:
"""Regression tests for structural batching with device qubit count (issue #45)."""

def test_same_size_circuits_can_be_batched_on_large_device(self):
batcher = CircuitBatcher(cost_model=CostModel(), max_qubits=127)
circuits = [QuantumCircuit(3) for _ in range(4)]
for qc in circuits:
qc.h(0)
qc.cx(0, 1)
qc.cx(1, 2)
plan = batcher.create_batch_plan(circuits, strategy="structural")
assert plan.num_batches >= 1
assert plan.total_circuits == 4

def test_small_circuits_batch_together_on_device(self):
batcher = CircuitBatcher(cost_model=CostModel(), max_qubits=10)
circuits = [QuantumCircuit(3) for _ in range(3)]
for qc in circuits:
qc.h(0)
qc.cx(0, 1)
plan = batcher.create_batch_plan(circuits, strategy="structural")
assert plan.total_circuits == 3

def test_circuits_exceeding_device_capacity_split_into_batches(self):
batcher = CircuitBatcher(cost_model=CostModel(), max_qubits=5)
circuits = [QuantumCircuit(3) for _ in range(4)]
for qc in circuits:
qc.h(0)
qc.cx(0, 1)
plan = batcher.create_batch_plan(circuits, strategy="structural")
assert plan.total_circuits == 4
assert plan.num_batches >= 2
Loading