-
Notifications
You must be signed in to change notification settings - Fork 1
Qompile pattern with Scheduler module #74
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
Merged
Merged
Changes from all commits
Commits
Show all changes
56 commits
Select commit
Hold shift + click to select a range
7227507
base implementation of scheduler
masa10-f 66d6147
implement on-the-fly scheduler
masa10-f 4bff8d6
add time slice grouping methods
masa10-f b7908c3
add scheduler attribute in qompiler func
masa10-f bc26c01
fix pyright warning
masa10-f d190f3b
add ortools as a requirements
masa10-f cddb743
implement mimize_space/minimize_time solvers
masa10-f 97115bc
update scheduler class
masa10-f 2999781
add schedule grouping method
masa10-f e7f393f
fix pyright warning
masa10-f fc3a1f2
scheduler generation with manual design
masa10-f 7a51968
rename
masa10-f 7f6989c
improve performance
masa10-f e77f9df
connect scheduler and solver
masa10-f 2af2c74
test for scheduler module
masa10-f 635ae8b
fix ruff error at test
masa10-f 3436b4a
fix qompiler generation order
masa10-f 0d6351c
add scheduled pattern generation demo
masa10-f c16f0bf
add documents
masa10-f 559c3a4
use general type
masa10-f ab94a3e
fix mypy error
masa10-f 9a82721
improve data pipeline in Scheduler
masa10-f 1e56b05
add schedule validator
masa10-f 5b89fa5
check schedule
masa10-f 3c3867b
implement constrained solver
masa10-f 00f3d92
update UI
masa10-f c765aad
update demo with new solvers
masa10-f 3071328
add docstrings
masa10-f c3f8b98
resolve pyright warnings
masa10-f 9e4612a
update unittest
masa10-f 341fe71
implement time compression method
masa10-f 2ef360c
add test
masa10-f b793a3d
use frozenset
masa10-f 5dff041
use and
masa10-f 8d556ac
replace and
masa10-f 7d1f635
fix docstring
masa10-f 33f3686
simplify code
masa10-f 5f9a960
fix missing type hint
masa10-f 2a139a3
unify MINIMIZE_TIME strategy
masa10-f 643a13b
update related files
masa10-f cf76464
ruff
masa10-f 55377f9
add missing branch
masa10-f 1cbd843
remove redundant set collections
masa10-f b910bbd
use defaultdict
masa10-f aa9dd24
change get_schedule() -> schedule
masa10-f c5c046d
avoid from_xxx
masa10-f 6b7423a
update related files
masa10-f 00b0385
use disjoint
masa10-f f4cc470
add test for schedule validation
masa10-f 2457b36
use collections type hint in private func
masa10-f 64b9332
separate compress_schedule from Scheduler class
masa10-f 58ced99
fix docstring
masa10-f 4a63230
change method name
masa10-f 3745bef
update qompile
masa10-f cc88c4f
update
masa10-f 4118d8f
add comment
masa10-f File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,3 +17,4 @@ Module reference | |
| pattern | ||
| pauli_frame | ||
| qompiler | ||
| scheduler | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| Scheduler Module | ||
| ================ | ||
|
|
||
|
|
||
| Scheduler | ||
| --------- | ||
|
|
||
| .. automodule:: graphix_zx.scheduler | ||
| :members: | ||
|
|
||
| Scheduling Solver | ||
| ----------------- | ||
|
|
||
| .. automodule:: graphix_zx.schedule_solver | ||
| :members: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,197 @@ | ||
| """ | ||
| Scheduler-based Pattern Generation Demo | ||
| ======================================= | ||
|
|
||
| This example demonstrates how to use the scheduler to generate optimized | ||
| measurement patterns from graph states using different scheduling strategies. | ||
| """ | ||
|
|
||
| # %% | ||
| from graphix_zx.pattern import Pattern, print_pattern | ||
| from graphix_zx.qompiler import qompile | ||
| from graphix_zx.random_objects import generate_random_flow_graph | ||
| from graphix_zx.schedule_solver import ScheduleConfig, Strategy | ||
| from graphix_zx.scheduler import Scheduler | ||
|
|
||
| # %% | ||
| # Create sample graph state and flow | ||
| print("=== Scheduler-based Pattern Generation Demo ===\n") | ||
| print("1. Creating sample graph state...") | ||
| graph, xflow = generate_random_flow_graph(width=3, depth=3, edge_p=0.7) | ||
| print(f" Graph has {len(graph.physical_nodes)} nodes") | ||
| print(f" Input nodes: {list(graph.input_node_indices.keys())}") | ||
| print(f" Output nodes: {list(graph.output_node_indices.keys())}") | ||
| print(f" Edges: {len(graph.physical_edges)}") | ||
|
|
||
| # %% | ||
| # Demonstrate space-optimized scheduling | ||
| print("\n2. Space-optimized Scheduling:") | ||
| print("=" * 40) | ||
|
|
||
| scheduler_space = Scheduler(graph, xflow) | ||
| space_config = ScheduleConfig(strategy=Strategy.MINIMIZE_SPACE) | ||
| success_space = scheduler_space.solve_schedule(space_config, timeout=10) | ||
| pattern_space = None | ||
|
|
||
| if success_space and scheduler_space.validate_schedule(): | ||
| print(" Scheduling successful!") | ||
| print(f" Number of time slices: {scheduler_space.num_slices()}") | ||
|
|
||
| prep_times = {k: v for k, v in scheduler_space.prepare_time.items() if v is not None} | ||
| if prep_times: | ||
| print(f" Preparation times: {prep_times}") | ||
|
|
||
| meas_times = {k: v for k, v in scheduler_space.measure_time.items() if v is not None} | ||
| if meas_times: | ||
| print(f" Measurement times: {meas_times}") | ||
|
|
||
| print("\n Generated Pattern (Space-optimized):") | ||
| pattern_space = qompile(graph, xflow, scheduler=scheduler_space) | ||
| print(f" Pattern has {len(pattern_space.commands)} commands") | ||
| print(f" Maximum space usage: {pattern_space.max_space} qubits") | ||
| print(f" Space usage over time: {pattern_space.space}") | ||
|
|
||
| print("\n Pattern commands:") | ||
| print_pattern(pattern_space, lim=10) | ||
| else: | ||
| print(" Failed to find solution for Space-optimized strategy") | ||
|
|
||
| # %% | ||
| # Demonstrate time-optimized scheduling | ||
| print("\n3. Time-optimized Scheduling:") | ||
| print("=" * 40) | ||
|
|
||
| scheduler_time = Scheduler(graph, xflow) | ||
| time_config = ScheduleConfig(strategy=Strategy.MINIMIZE_TIME) | ||
| success_time = scheduler_time.solve_schedule(time_config, timeout=10) | ||
| pattern_time = None | ||
|
|
||
| if success_time and scheduler_time.validate_schedule(): | ||
| print(" Scheduling successful!") | ||
| print(f" Number of time slices: {scheduler_time.num_slices()}") | ||
|
|
||
| prep_times = {k: v for k, v in scheduler_time.prepare_time.items() if v is not None} | ||
| if prep_times: | ||
| print(f" Preparation times: {prep_times}") | ||
|
|
||
| meas_times = {k: v for k, v in scheduler_time.measure_time.items() if v is not None} | ||
| if meas_times: | ||
| print(f" Measurement times: {meas_times}") | ||
|
|
||
| print("\n Generated Pattern (Time-optimized):") | ||
| pattern_time = qompile(graph, xflow, scheduler=scheduler_time) | ||
| print(f" Pattern has {len(pattern_time.commands)} commands") | ||
| print(f" Maximum space usage: {pattern_time.max_space} qubits") | ||
| print(f" Space usage over time: {pattern_time.space}") | ||
|
|
||
| print("\n Pattern commands:") | ||
| print_pattern(pattern_time, lim=10) | ||
| else: | ||
| print(" Failed to find solution for Time-optimized strategy") | ||
|
|
||
| # %% | ||
| # Demonstrate space-constrained time optimization using ScheduleConfig | ||
| print("\n4. Space-constrained Time-optimized Scheduling (ScheduleConfig):") | ||
| print("=" * 60) | ||
|
|
||
| # Use ScheduleConfig for more detailed control | ||
| max_qubits = 5 | ||
| constrained_config = ScheduleConfig( | ||
| strategy=Strategy.MINIMIZE_TIME, | ||
| max_qubit_count=max_qubits, | ||
| max_time=20, # Custom time limit | ||
| ) | ||
|
|
||
| scheduler_constrained = Scheduler(graph, xflow) | ||
| success_constrained = scheduler_constrained.solve_schedule(constrained_config, timeout=15) | ||
| pattern_constrained = None | ||
|
|
||
| if success_constrained and scheduler_constrained.validate_schedule(): | ||
| print(" Scheduling successful!") | ||
| print(f" Number of time slices: {scheduler_constrained.num_slices()}") | ||
| print(f" Max qubits constraint: {max_qubits}") | ||
|
|
||
| prep_times = {k: v for k, v in scheduler_constrained.prepare_time.items() if v is not None} | ||
| if prep_times: | ||
| print(f" Preparation times: {prep_times}") | ||
|
|
||
| meas_times = {k: v for k, v in scheduler_constrained.measure_time.items() if v is not None} | ||
| if meas_times: | ||
| print(f" Measurement times: {meas_times}") | ||
|
|
||
| print("\n Generated Pattern (Space-constrained Time-optimized):") | ||
| pattern_constrained = qompile(graph, xflow, scheduler=scheduler_constrained) | ||
| print(f" Pattern has {len(pattern_constrained.commands)} commands") | ||
| print(f" Maximum space usage: {pattern_constrained.max_space} qubits") | ||
| print(f" Space usage over time: {pattern_constrained.space}") | ||
|
|
||
| if pattern_constrained.max_space <= max_qubits: | ||
| print(f" ✓ Space constraint satisfied: {pattern_constrained.max_space} <= {max_qubits}") | ||
| else: | ||
| print(f" ⚠ Space constraint violated: {pattern_constrained.max_space} > {max_qubits}") | ||
|
|
||
| print("\n Pattern commands:") | ||
| print_pattern(pattern_constrained, lim=10) | ||
| else: | ||
| print(f" Failed to find solution with {max_qubits} qubits constraint") | ||
|
|
||
| # %% | ||
| # Demonstrate custom max_time using ScheduleConfig | ||
| print("\n5. Custom max_time Scheduling (ScheduleConfig):") | ||
| print("=" * 50) | ||
|
|
||
| custom_time_config = ScheduleConfig( | ||
| strategy=Strategy.MINIMIZE_SPACE, | ||
| max_time=15, # Smaller time horizon for faster solving | ||
| ) | ||
|
|
||
| scheduler_custom = Scheduler(graph, xflow) | ||
| success_custom = scheduler_custom.solve_schedule(custom_time_config, timeout=10) | ||
|
|
||
| if success_custom and scheduler_custom.validate_schedule(): | ||
| print(" Scheduling with custom max_time successful!") | ||
| print(f" Number of time slices: {scheduler_custom.num_slices()}") | ||
| print(f" Custom max_time: {custom_time_config.max_time}") | ||
|
|
||
| pattern_custom = qompile(graph, xflow, scheduler=scheduler_custom) | ||
| print(f" Maximum space usage: {pattern_custom.max_space} qubits") | ||
| else: | ||
| print(" Failed to find solution with custom max_time") | ||
|
|
||
| # %% | ||
| # Compare all strategies | ||
| print("\n6. Strategy Comparison:") | ||
| print("=" * 40) | ||
|
|
||
| all_results: list[tuple[str, Scheduler, Pattern]] = [] | ||
| if success_space and pattern_space: | ||
| all_results.append(("Space-optimized", scheduler_space, pattern_space)) | ||
| if success_time and pattern_time: | ||
| all_results.append(("Time-optimized", scheduler_time, pattern_time)) | ||
| if success_constrained and pattern_constrained: | ||
| all_results.append(("Space-constrained", scheduler_constrained, pattern_constrained)) | ||
|
|
||
| min_results_needed = 2 | ||
| if len(all_results) >= min_results_needed: | ||
| for name, scheduler, pattern in all_results: | ||
| print( | ||
| f" {name:18}: {scheduler.num_slices()} slices, " | ||
| f"{pattern.max_space} max qubits, {len(pattern.commands)} commands" | ||
| ) | ||
|
|
||
| print("\n Analysis:") | ||
| min_results_for_comparison = 2 | ||
| if len(all_results) >= min_results_for_comparison: | ||
| min_slices = min(s.num_slices() for _, s, _ in all_results) | ||
| min_qubits = min(p.max_space for _, _, p in all_results) | ||
|
|
||
| for name, scheduler, pattern in all_results: | ||
| notes: list[str] = [] | ||
| if scheduler.num_slices() == min_slices: | ||
| notes.append("fastest execution") | ||
| if pattern.max_space == min_qubits: | ||
| notes.append("lowest qubit usage") | ||
| if notes: | ||
| print(f" → {name}: {', '.join(notes)}") | ||
|
|
||
| print("\nDemo completed successfully!") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.