Skip to content

Conversation

@Vivek1106-04
Copy link
Contributor

@Vivek1106-04 Vivek1106-04 commented Dec 17, 2025

Add Directed Graph Support to RouteCQC

Fixes #5863

This PR adds support for directed device graphs to Cirq’s RouteCQC router.
The router now handles unidirectional couplings using a Hadamard-based decomposition when required.


Summary of Changes

1️⃣ Removed Directed Graph Restriction

File: route_circuit_cqc.py

  • Updated __init__ to accept both directed and undirected graphs
  • Removed the ValueError previously raised for directed graphs
  • Updated docstring to reflect expanded device support

2️⃣ Implemented _emit_swap (Direction-Aware SWAP Emission)

File: route_circuit_cqc.py

Added a new classmethod _emit_swap that emits SWAP operations based on edge directionality:

  • Case A: Bidirectional edge

    • Uses standard SWAP
    • Automatically decomposed by Cirq into 3 CNOTs
  • Case B: Unidirectional edge (q1 → q2)

    • Uses Hadamard-based decomposition:
      CNOT(q1, q2)
      H(q1), H(q2)
      CNOT(q1, q2)
      H(q1), H(q2)
      CNOT(q1, q2)
      
    • Basis change allows effective CNOT direction reversal
  • Case C: Unidirectional edge (q2 → q1)

    • Same logic as Case B with qubits flipped
  • Case D: No edge

    • Raises ValueError (should not occur for valid routing)

3️⃣ Updated Routing Logic

File: route_circuit_cqc.py

  • Added device_graph parameter to _route
  • Updated route_circuit to pass self.device_graph
  • Replaced direct SWAP insertion with _emit_swap

4️⃣ Updated Tests

File: route_circuit_cqc_test.py

  • test_directed_device

    • Now accepts directed graphs
    • Verifies routing of basic CNOT circuits
  • test_directed_device_swap_decomposition (NEW)

    • Routes a SWAP on a directed ring device
    • Asserts presence of Hadamard gates
    • Confirms unidirectional edge handling is exercised

@Vivek1106-04 Vivek1106-04 requested review from a team and vtomole as code owners December 17, 2025 07:26
@Vivek1106-04 Vivek1106-04 requested a review from maffoo December 17, 2025 07:26
@github-actions github-actions bot added size: M 50< lines changed <250 size: L 250< lines changed <1000 and removed size: M 50< lines changed <250 labels Dec 17, 2025
@Vivek1106-04
Copy link
Contributor Author

Added additional tests to cover edge cases for directed device graphs and the tag_inserted_swaps parameter.

@codecov
Copy link

codecov bot commented Dec 18, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.57%. Comparing base (a8efbc5) to head (e71ea5f).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7810      +/-   ##
==========================================
- Coverage   99.57%   99.57%   -0.01%     
==========================================
  Files        1102     1102              
  Lines       98772    98878     +106     
==========================================
+ Hits        98351    98456     +105     
- Misses        421      422       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Vivek1106-04 Vivek1106-04 force-pushed the directed-device-graph branch from bced992 to e71ea5f Compare January 9, 2026 16:38
@Vivek1106-04
Copy link
Contributor Author

Introduced RoutingConfig, SwapSearchContext, and CircuitOps dataclasses to bundle related parameters and reduce function argument counts, resolving pylint too-many-arguments errors without using disable comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size: L 250< lines changed <1000

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Routing] Add support for directed device graphs.

1 participant