5151from qiskit .transpiler .passes import (
5252 ApplyLayout ,
5353 BasisTranslator ,
54+ BasicSwap ,
5455 Collect2qBlocks ,
56+ CollectCliffords ,
5557 CommutativeCancellation ,
5658 CommutativeInverseCancellation ,
5759 ConsolidateBlocks ,
6769 OptimizeCliffords ,
6870 RemoveDiagonalGatesBeforeMeasure ,
6971 SabreLayout ,
72+ SabreSwap ,
7073 Size ,
74+ TrivialLayout ,
7175 UnitarySynthesis ,
7276 VF2Layout ,
7377 VF2PostLayout ,
8084 get_bqskit_native_gates ,
8185)
8286
87+ from mqt .predictor .rl .helper import SafeAIRouting , get_openqasm_gates
88+
8389if TYPE_CHECKING :
8490 from collections .abc import Callable
8591 from typing import Any
@@ -125,6 +131,7 @@ class Action:
125131 Callable [..., tuple [Any , ...] | Circuit ],
126132 ]
127133 )
134+ stochastic : bool = False
128135
129136
130137@dataclass
@@ -180,7 +187,7 @@ def remove_action(name: str) -> None:
180187 "Optimize1qGatesDecomposition" ,
181188 CompilationOrigin .QISKIT ,
182189 PassType .OPT ,
183- [Optimize1qGatesDecomposition ()],
190+ [Optimize1qGatesDecomposition (basis = get_openqasm_gates () )],
184191 )
185192)
186193
@@ -240,16 +247,18 @@ def remove_action(name: str) -> None:
240247 "OptimizeCliffords" ,
241248 CompilationOrigin .QISKIT ,
242249 PassType .OPT ,
243- [OptimizeCliffords ()],
250+ [CollectCliffords (), OptimizeCliffords ()],
244251 )
245252)
246253
247254register_action (
248- DeviceIndependentAction (
255+ DeviceDependentAction (
249256 "Opt2qBlocks" ,
250257 CompilationOrigin .QISKIT ,
251258 PassType .OPT ,
252- [Collect2qBlocks (), ConsolidateBlocks (), UnitarySynthesis ()],
259+ transpile_pass = lambda device : [Collect2qBlocks (),
260+ ConsolidateBlocks (basis_gates = device .operation_names ),
261+ UnitarySynthesis (basis_gates = device .operation_names , coupling_map = device .build_coupling_map ())],
253262 )
254263)
255264
@@ -393,9 +402,69 @@ def remove_action(name: str) -> None:
393402 "SabreMapping" ,
394403 CompilationOrigin .QISKIT ,
395404 PassType .MAPPING ,
396- transpile_pass = lambda device : [
397- SabreLayout (coupling_map = CouplingMap (device .build_coupling_map ()), skip_routing = False )
398- ],
405+ stochastic = True ,
406+ # Qiskit O3 by default uses (max_iterations, layout_trials, swap_trials) = (4, 20, 20)
407+ transpile_pass = lambda device , max_iteration = (20 ,20 ): [
408+ SabreLayout (
409+ coupling_map = CouplingMap (device .build_coupling_map ()),
410+ skip_routing = False ,
411+ layout_trials = max_iteration [0 ],
412+ swap_trials = max_iteration [1 ],
413+ max_iterations = 4 ,
414+ seed = None
415+ ),
416+ ],
417+ )
418+ )
419+
420+ register_action (
421+ DeviceDependentAction (
422+ "SabreLayout+BasicSwap" ,
423+ CompilationOrigin .QISKIT ,
424+ PassType .MAPPING ,
425+ stochastic = True ,
426+ transpile_pass = lambda device , max_iteration = (20 ,20 ): [
427+ SabreLayout (
428+ coupling_map = CouplingMap (device .build_coupling_map ()),
429+ skip_routing = True ,
430+ layout_trials = max_iteration [0 ],
431+ swap_trials = max_iteration [1 ],
432+ max_iterations = 4 ,
433+ seed = None
434+ ),
435+ FullAncillaAllocation (coupling_map = CouplingMap (device .build_coupling_map ())),
436+ EnlargeWithAncilla (),
437+ ApplyLayout (),
438+ BasicSwap (coupling_map = CouplingMap (device .build_coupling_map ())),
439+ ],
440+ )
441+ )
442+
443+ register_action (
444+ DeviceDependentAction (
445+ "SabreLayout+AIRouting" ,
446+ CompilationOrigin .QISKIT ,
447+ PassType .MAPPING ,
448+ stochastic = True ,
449+ transpile_pass = lambda device , max_iteration = (20 ,20 ): [
450+ SabreLayout (
451+ coupling_map = CouplingMap (device .build_coupling_map ()),
452+ skip_routing = True ,
453+ layout_trials = max_iteration [0 ],
454+ swap_trials = max_iteration [1 ],
455+ max_iterations = 4 ,
456+ seed = None
457+ ),
458+ FullAncillaAllocation (coupling_map = CouplingMap (device .build_coupling_map ())),
459+ EnlargeWithAncilla (),
460+ ApplyLayout (),
461+ SafeAIRouting (
462+ coupling_map = device .build_coupling_map (),
463+ optimization_level = 3 ,
464+ layout_mode = "optimize" ,
465+ local_mode = True
466+ ),
467+ ],
399468 )
400469)
401470
@@ -421,6 +490,194 @@ def remove_action(name: str) -> None:
421490 )
422491)
423492
493+ register_action (
494+ DeviceDependentAction (
495+ name = "DenseLayout+BasicSwap" ,
496+ origin = CompilationOrigin .QISKIT ,
497+ pass_type = PassType .MAPPING ,
498+ transpile_pass = lambda device : [
499+ DenseLayout (coupling_map = CouplingMap (device .build_coupling_map ())),
500+ FullAncillaAllocation (coupling_map = CouplingMap (device .build_coupling_map ())),
501+ EnlargeWithAncilla (),
502+ ApplyLayout (),
503+ BasicSwap (coupling_map = CouplingMap (device .build_coupling_map ())),
504+ ],
505+ )
506+ )
507+
508+ register_action (
509+ DeviceDependentAction (
510+ name = "DenseLayout+SabreSwap" ,
511+ origin = CompilationOrigin .QISKIT ,
512+ pass_type = PassType .MAPPING ,
513+ stochastic = True ,
514+ transpile_pass = lambda device , max_iteration = (20 ,20 ): [
515+ DenseLayout (coupling_map = CouplingMap (device .build_coupling_map ())),
516+ FullAncillaAllocation (coupling_map = CouplingMap (device .build_coupling_map ())),
517+ EnlargeWithAncilla (),
518+ ApplyLayout (),
519+ SabreSwap (coupling_map = CouplingMap (device .build_coupling_map ()), heuristic = "decay" , trials = max_iteration [1 ], seed = None ),
520+ ],
521+ )
522+ )
523+
524+ register_action (
525+ DeviceDependentAction (
526+ name = "DenseLayout+AIRouting" ,
527+ origin = CompilationOrigin .QISKIT ,
528+ pass_type = PassType .MAPPING ,
529+ stochastic = True ,
530+ transpile_pass = lambda device : [
531+ DenseLayout (coupling_map = CouplingMap (device .build_coupling_map ())),
532+ FullAncillaAllocation (coupling_map = CouplingMap (device .build_coupling_map ())),
533+ EnlargeWithAncilla (),
534+ ApplyLayout (),
535+ SafeAIRouting (
536+ coupling_map = device .build_coupling_map (),
537+ optimization_level = 3 ,
538+ layout_mode = "optimize" ,
539+ local_mode = True
540+ ),
541+ ],
542+ )
543+ )
544+
545+ register_action (
546+ DeviceDependentAction (
547+ name = "VF2Layout+BasicSwap" ,
548+ origin = CompilationOrigin .QISKIT ,
549+ pass_type = PassType .MAPPING ,
550+ transpile_pass = lambda device : [
551+ VF2Layout (
552+ coupling_map = CouplingMap (device .build_coupling_map ()),
553+ target = device ,
554+ ),
555+ ConditionalController (
556+ [
557+ FullAncillaAllocation (coupling_map = CouplingMap (device .build_coupling_map ())),
558+ EnlargeWithAncilla (),
559+ ApplyLayout (),
560+ ],
561+ condition = lambda property_set : property_set ["VF2Layout_stop_reason" ]
562+ == VF2LayoutStopReason .SOLUTION_FOUND ,
563+ ),
564+ ConditionalController (
565+ [
566+ TrivialLayout (coupling_map = CouplingMap (device .build_coupling_map ())),
567+ FullAncillaAllocation (coupling_map = CouplingMap (device .build_coupling_map ())),
568+ EnlargeWithAncilla (),
569+ ApplyLayout (),
570+ ],
571+ # Run if VF2Layout did not find a solution
572+ condition = lambda property_set : property_set ["VF2Layout_stop_reason" ] != VF2LayoutStopReason .SOLUTION_FOUND ,
573+ ),
574+ BasicSwap (coupling_map = CouplingMap (device .build_coupling_map ())),
575+ ],
576+ )
577+ )
578+
579+ register_action (
580+ DeviceDependentAction (
581+ name = "VF2Layout+SabreSwap" ,
582+ origin = CompilationOrigin .QISKIT ,
583+ pass_type = PassType .MAPPING ,
584+ stochastic = True ,
585+ transpile_pass = lambda device , max_iteration = (20 ,20 ): [
586+ VF2Layout (
587+ coupling_map = CouplingMap (device .build_coupling_map ()),
588+ target = device ,
589+ ),
590+ ConditionalController (
591+ [
592+ FullAncillaAllocation (coupling_map = CouplingMap (device .build_coupling_map ())),
593+ EnlargeWithAncilla (),
594+ ApplyLayout (),
595+ ],
596+ condition = lambda property_set : property_set ["VF2Layout_stop_reason" ]
597+ == VF2LayoutStopReason .SOLUTION_FOUND ,
598+ ),
599+ ConditionalController (
600+ [
601+ TrivialLayout (coupling_map = CouplingMap (device .build_coupling_map ())),
602+ FullAncillaAllocation (coupling_map = CouplingMap (device .build_coupling_map ())),
603+ EnlargeWithAncilla (),
604+ ApplyLayout (),
605+ ],
606+ # Run if VF2Layout did not find a solution
607+ condition = lambda property_set : property_set ["VF2Layout_stop_reason" ] != VF2LayoutStopReason .SOLUTION_FOUND ,
608+ ),
609+ SabreSwap (coupling_map = CouplingMap (device .build_coupling_map ()), heuristic = "decay" , trials = max_iteration [1 ], seed = None ),
610+ ],
611+ )
612+ )
613+
614+ register_action (
615+ DeviceDependentAction (
616+ name = "VF2Layout+AIRouting" ,
617+ origin = CompilationOrigin .QISKIT ,
618+ pass_type = PassType .MAPPING ,
619+ stochastic = True ,
620+ transpile_pass = lambda device :[
621+ VF2Layout (
622+ coupling_map = CouplingMap (device .build_coupling_map ()),
623+ target = device ,
624+ ),
625+ ConditionalController (
626+ [
627+ FullAncillaAllocation (coupling_map = CouplingMap (device .build_coupling_map ())),
628+ EnlargeWithAncilla (),
629+ ApplyLayout (),
630+ ],
631+ condition = lambda property_set : property_set ["VF2Layout_stop_reason" ]
632+ == VF2LayoutStopReason .SOLUTION_FOUND ,
633+ ),
634+ ConditionalController (
635+ [
636+ TrivialLayout (coupling_map = CouplingMap (device .build_coupling_map ())),
637+ FullAncillaAllocation (coupling_map = CouplingMap (device .build_coupling_map ())),
638+ EnlargeWithAncilla (),
639+ ApplyLayout (),
640+ ],
641+ # Run if VF2Layout did not find a solution
642+ condition = lambda property_set : property_set ["VF2Layout_stop_reason" ] != VF2LayoutStopReason .SOLUTION_FOUND ,
643+ ),
644+ SafeAIRouting (
645+ coupling_map = device .build_coupling_map (),
646+ optimization_level = 3 ,
647+ layout_mode = "optimize" ,
648+ local_mode = True
649+ ),
650+ ],
651+ )
652+ )
653+
654+ register_action (
655+ DeviceDependentAction (
656+ name = "SabreLayout+AIRouting" ,
657+ origin = CompilationOrigin .QISKIT ,
658+ stochastic = True ,
659+ pass_type = PassType .MAPPING ,
660+ transpile_pass = lambda device , max_iteration = (20 , 20 ): [
661+ SabreLayout (
662+ coupling_map = CouplingMap (device .build_coupling_map ()),
663+ skip_routing = True ,
664+ layout_trials = max_iteration [0 ],
665+ swap_trials = 1 ,
666+ max_iterations = 4 ,
667+ ),
668+ FullAncillaAllocation (coupling_map = CouplingMap (device .build_coupling_map ())),
669+ EnlargeWithAncilla (),
670+ ApplyLayout (),
671+ SafeAIRouting (
672+ coupling_map = device .build_coupling_map (),
673+ optimization_level = 3 ,
674+ layout_mode = "optimize" ,
675+ local_mode = True ,
676+ ),
677+ ],
678+ )
679+ )
680+
424681register_action (
425682 DeviceDependentAction (
426683 "BasisTranslator" ,
0 commit comments