@@ -36,8 +36,6 @@ use qiskit_circuit::{BitType, Clbit, Qubit};
3636use crate :: unitary_compose;
3737use crate :: QiskitError ;
3838
39- const TWOPI : f64 = 2.0 * std:: f64:: consts:: PI ;
40-
4139// These gates do not commute with other gates, we do not check them.
4240static SKIPPED_NAMES : [ & str ; 4 ] = [ "measure" , "reset" , "delay" , "initialize" ] ;
4341
@@ -50,23 +48,38 @@ static SUPPORTED_OP: Lazy<HashSet<&str>> = Lazy::new(|| {
5048 ] )
5149} ) ;
5250
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 ( || {
5657 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+ ) ,
7083 ] )
7184} ) ;
7285
@@ -636,12 +649,14 @@ fn map_rotation<'a>(
636649 tol : f64 ,
637650) -> ( & ' a OperationRef < ' a > , & ' a [ Param ] , bool ) {
638651 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) {
640654 // If the rotation angle is below the tolerance, the gate is assumed to
641655 // commute with everything, and we simply return the operation with the flag that
642656 // it commutes trivially.
643657 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 {
645660 return ( op, params, true ) ;
646661 } ;
647662 } ;
0 commit comments