@@ -881,6 +881,10 @@ def scalar(complex):
881
881
0 , 1 , 0 , 0 ,
882
882
0 , 0 , 0 , 1 ,
883
883
0 , 0 , 1 , 0 ], _dagger = None )
884
+ CZ = Gate ('CZ' , 2 , [1 , 0 , 0 , 0 ,
885
+ 0 , 1 , 0 , 0 ,
886
+ 0 , 0 , 1 , 0 ,
887
+ 0 , 0 , 0 , - 1 ], _dagger = None )
884
888
H = Gate ('H' , 1 , 1 / np .sqrt (2 ) * np .array ([1 , 1 , 1 , - 1 ]), _dagger = None )
885
889
S = Gate ('S' , 1 , [1 , 0 , 0 , 1j ])
886
890
T = Gate ('T' , 1 , [1 , 0 , 0 , np .exp (1j * np .pi / 4 )])
@@ -940,3 +944,20 @@ def IQPansatz(n, params):
940
944
for i in range (1 , depth ):
941
945
ansatz = ansatz >> IQPlayer (params [i ])
942
946
return ansatz
947
+
948
+
949
+ def Perm (perm ):
950
+ """ Constructs a permutation
951
+
952
+ >>> assert Perm([2, 1, 0]) == Perm([2, 0, 1]) >> Perm([0, 2, 1])
953
+ """
954
+ assert set (range (len (perm ))) == set (perm )
955
+ gates , offsets = [], []
956
+ frame = perm .copy ()
957
+ for i in range (len (perm )):
958
+ if i < frame [i ]:
959
+ num_swaps = frame [i ] - i
960
+ gates += [SWAP for x in range (num_swaps )]
961
+ offsets += range (i , frame [i ])[::- 1 ]
962
+ frame [i : i + num_swaps ] = [x + 1 for x in frame [i : i + num_swaps ]]
963
+ return Circuit (len (perm ), len (perm ), gates , offsets )
0 commit comments