@@ -36,8 +36,6 @@ use qiskit_circuit::{BitType, Clbit, Qubit};
36
36
use crate :: unitary_compose;
37
37
use crate :: QiskitError ;
38
38
39
- const TWOPI : f64 = 2.0 * std:: f64:: consts:: PI ;
40
-
41
39
// These gates do not commute with other gates, we do not check them.
42
40
static SKIPPED_NAMES : [ & str ; 4 ] = [ "measure" , "reset" , "delay" , "initialize" ] ;
43
41
@@ -50,23 +48,38 @@ static SUPPORTED_OP: Lazy<HashSet<&str>> = Lazy::new(|| {
50
48
] )
51
49
} ) ;
52
50
53
- // Map rotation gates to their generators, or to ``None`` if we cannot currently efficiently
54
- // represent the generator in Rust and store the commutation relation in the commutation dictionary
55
- static SUPPORTED_ROTATIONS : Lazy < HashMap < & str , Option < OperationRef > > > = Lazy :: new ( || {
51
+ // Map rotation gates to their generators (or to ``None`` if we cannot currently efficiently
52
+ // represent the generator in Rust and store the commutation relation in the commutation dictionary)
53
+ // and their pi-periodicity. Here we mean a gate is n-pi periodic, if for angles that are
54
+ // multiples of n*pi, the gate is equal to the identity up to a global phase.
55
+ // E.g. RX is generated by X and 2-pi periodic, while CRX is generated by CX and 4-pi periodic.
56
+ static SUPPORTED_ROTATIONS : Lazy < HashMap < & str , ( u8 , Option < OperationRef > ) > > = Lazy :: new ( || {
56
57
HashMap :: from ( [
57
- ( "rx" , Some ( OperationRef :: Standard ( StandardGate :: XGate ) ) ) ,
58
- ( "ry" , Some ( OperationRef :: Standard ( StandardGate :: YGate ) ) ) ,
59
- ( "rz" , Some ( OperationRef :: Standard ( StandardGate :: ZGate ) ) ) ,
60
- ( "p" , Some ( OperationRef :: Standard ( StandardGate :: ZGate ) ) ) ,
61
- ( "u1" , Some ( OperationRef :: Standard ( StandardGate :: ZGate ) ) ) ,
62
- ( "crx" , Some ( OperationRef :: Standard ( StandardGate :: CXGate ) ) ) ,
63
- ( "cry" , Some ( OperationRef :: Standard ( StandardGate :: CYGate ) ) ) ,
64
- ( "crz" , Some ( OperationRef :: Standard ( StandardGate :: CZGate ) ) ) ,
65
- ( "cp" , Some ( OperationRef :: Standard ( StandardGate :: CZGate ) ) ) ,
66
- ( "rxx" , None ) , // None means the gate is in the commutation dictionary
67
- ( "ryy" , None ) ,
68
- ( "rzx" , None ) ,
69
- ( "rzz" , None ) ,
58
+ ( "rx" , ( 2 , Some ( OperationRef :: Standard ( StandardGate :: XGate ) ) ) ) ,
59
+ ( "ry" , ( 2 , Some ( OperationRef :: Standard ( StandardGate :: YGate ) ) ) ) ,
60
+ ( "rz" , ( 2 , Some ( OperationRef :: Standard ( StandardGate :: ZGate ) ) ) ) ,
61
+ ( "p" , ( 2 , Some ( OperationRef :: Standard ( StandardGate :: ZGate ) ) ) ) ,
62
+ ( "u1" , ( 2 , Some ( OperationRef :: Standard ( StandardGate :: ZGate ) ) ) ) ,
63
+ ( "rxx" , ( 2 , None ) ) , // None means the gate is in the commutation dictionary
64
+ ( "ryy" , ( 2 , None ) ) ,
65
+ ( "rzx" , ( 2 , None ) ) ,
66
+ ( "rzz" , ( 2 , None ) ) ,
67
+ (
68
+ "crx" ,
69
+ ( 4 , Some ( OperationRef :: Standard ( StandardGate :: CXGate ) ) ) ,
70
+ ) ,
71
+ (
72
+ "cry" ,
73
+ ( 4 , Some ( OperationRef :: Standard ( StandardGate :: CYGate ) ) ) ,
74
+ ) ,
75
+ (
76
+ "crz" ,
77
+ ( 4 , Some ( OperationRef :: Standard ( StandardGate :: CZGate ) ) ) ,
78
+ ) ,
79
+ (
80
+ "cp" ,
81
+ ( 2 , Some ( OperationRef :: Standard ( StandardGate :: CZGate ) ) ) ,
82
+ ) ,
70
83
] )
71
84
} ) ;
72
85
@@ -636,12 +649,14 @@ fn map_rotation<'a>(
636
649
tol : f64 ,
637
650
) -> ( & ' a OperationRef < ' a > , & ' a [ Param ] , bool ) {
638
651
let name = op. name ( ) ;
639
- if let Some ( generator) = SUPPORTED_ROTATIONS . get ( name) {
652
+
653
+ if let Some ( ( pi_multiple, generator) ) = SUPPORTED_ROTATIONS . get ( name) {
640
654
// If the rotation angle is below the tolerance, the gate is assumed to
641
655
// commute with everything, and we simply return the operation with the flag that
642
656
// it commutes trivially.
643
657
if let Param :: Float ( angle) = params[ 0 ] {
644
- if ( angle % TWOPI ) . abs ( ) < tol {
658
+ let periodicity = ( * pi_multiple as f64 ) * :: std:: f64:: consts:: PI ;
659
+ if ( angle % periodicity) . abs ( ) < tol {
645
660
return ( op, params, true ) ;
646
661
} ;
647
662
} ;
0 commit comments