@@ -71,6 +71,7 @@ class ConstantGauge(Gauge):
71
71
post_q1 : Tuple [ops .Gate , ...] = field (
72
72
default = (), converter = lambda g : (g ,) if isinstance (g , ops .Gate ) else tuple (g )
73
73
)
74
+ swap_qubits : bool = False
74
75
75
76
def sample (self , gate : ops .Gate , prng : np .random .Generator ) -> "ConstantGauge" :
76
77
return self
@@ -85,6 +86,41 @@ def post(self) -> Tuple[Tuple[ops.Gate, ...], Tuple[ops.Gate, ...]]:
85
86
"""A tuple (ops to apply to q0, ops to apply to q1)."""
86
87
return self .post_q0 , self .post_q1
87
88
89
+ def on (self , q0 : ops .Qid , q1 : ops .Qid ) -> ops .Operation :
90
+ """Returns the operation that replaces the two qubit gate."""
91
+ if self .swap_qubits :
92
+ return self .two_qubit_gate (q1 , q0 )
93
+ return self .two_qubit_gate (q0 , q1 )
94
+
95
+
96
+ @frozen
97
+ class SameGateGauge (Gauge ):
98
+ """Same as ConstantGauge but the new two-qubit gate equals the old gate."""
99
+
100
+ pre_q0 : Tuple [ops .Gate , ...] = field (
101
+ default = (), converter = lambda g : (g ,) if isinstance (g , ops .Gate ) else tuple (g )
102
+ )
103
+ pre_q1 : Tuple [ops .Gate , ...] = field (
104
+ default = (), converter = lambda g : (g ,) if isinstance (g , ops .Gate ) else tuple (g )
105
+ )
106
+ post_q0 : Tuple [ops .Gate , ...] = field (
107
+ default = (), converter = lambda g : (g ,) if isinstance (g , ops .Gate ) else tuple (g )
108
+ )
109
+ post_q1 : Tuple [ops .Gate , ...] = field (
110
+ default = (), converter = lambda g : (g ,) if isinstance (g , ops .Gate ) else tuple (g )
111
+ )
112
+ swap_qubits : bool = False
113
+
114
+ def sample (self , gate : ops .Gate , prng : np .random .Generator ) -> ConstantGauge :
115
+ return ConstantGauge (
116
+ two_qubit_gate = gate ,
117
+ pre_q0 = self .pre_q0 ,
118
+ pre_q1 = self .pre_q1 ,
119
+ post_q0 = self .post_q0 ,
120
+ post_q1 = self .post_q1 ,
121
+ swap_qubits = self .swap_qubits ,
122
+ )
123
+
88
124
89
125
def _select (choices : Sequence [Gauge ], probabilites : np .ndarray , prng : np .random .Generator ) -> Gauge :
90
126
return choices [prng .choice (len (choices ), p = probabilites )]
@@ -154,7 +190,7 @@ def __call__(
154
190
gauge = self .gauge_selector (rng ).sample (op .gate , rng )
155
191
q0 , q1 = op .qubits
156
192
left .extend ([g (q ) for g in gs ] for q , gs in zip (op .qubits , gauge .pre ))
157
- center .append (gauge .two_qubit_gate (q0 , q1 ))
193
+ center .append (gauge .on (q0 , q1 ))
158
194
right .extend ([g (q ) for g in gs ] for q , gs in zip (op .qubits , gauge .post ))
159
195
else :
160
196
center .append (op )
0 commit comments