Skip to content

[FEATURE] Implement Circuit Segmentation via .segment() Method #215

@TheGupta2012

Description

@TheGupta2012

Feature Description

  • Introduce a .segment() method to pyqasm.QasmModule that partitions quantum circuits into independent segments where no entanglement exists between segments.

  • Each segment is returned as a standalone QasmModule object containing:

    • Remapped qubit registers (e.g., PYQASM_SEGMENT_0[i])
    • Gates acting exclusively on the segment's qubits
  • Key constraints:

    • We use entangling gates to define an "independently executable segment". Raises an error if the circuit contains measurements or classical controls (unsupported in v1).
    • Qubit order in new registers follows first occurrence in the original circuit.

Motivation

  • Parallel Execution: Enables independent segment execution across multiple QPUs.
  • Resource Optimization: Splits large circuits for execution on smaller devices.
  • Error Containment: Isolates quantum errors to individual segments.

Implementation (Optional)

Preprocessing:

  • Validate absence of measurements/classical controls.

  • Construct a qubit connectivity graph using multi-qubit gates (edges connect interacting qubits).

Segmentation Algorithm:

  • Identify connected components in the graph → segments. Should essentially be a disjoint set union amongst all the qubits present in our program. Inspired by the BlockSplitter in qiskit's dag_circuit.collect_blocks

  • For each segment:

    • Create a new register qubit[N] __PYQASM_SEGMENT_<idx>__ (N = segment qubit count).

    • Remap original qubits to new register indices ordered by first gate occurrence.

Module Generation:

  • Clone original module headers (OPENQASM 3.0, includes).
  • Replace original gates with remapped operations.
  • Preserve gate order within segments.

Edge Cases:

  • All Single-qubit gates → Divide into a default segment count (say 10) as all qubits are disjoint
  • Empty circuits → No OP

Example flow

from pyqasm import loads

qasm_code = """
OPENQASM 3.0;
qubit[4] q;
h q[0]; 
cx q[0], q[1];
h q[2];
cx q[2], q[3];
"""

module = loads(qasm_code)
segments = module.segment()  # Returns 2 QasmModules

print(segments[0])
# Output:
# OPENQASM 3.0;
# qubit[2] __PYQASM_SEGMENT_0__;
# h __PYQASM_SEGMENT_0__[0];
# cx __PYQASM_SEGMENT_0__[0], __PYQASM_SEGMENT_0__[1];

print(segments[1])
# Output:
# OPENQASM 3.0;
# qubit[2] __PYQASM_SEGMENT_1__;
# h __PYQASM_SEGMENT_1__[0];
# cx __PYQASM_SEGMENT_1__[0], __PYQASM_SEGMENT_1__[1];

This is a first draft where we return a list of modules but can potentially be changed in-place addition of these segments to existing module

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestllm-assistedUsed LLMs to fine tune issue description.transformationRelated to qasm program transformation

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions