@@ -207,6 +207,37 @@ def test_permutation_in_middle(self):
207
207
res = self .swap_pass (qc )
208
208
self .assertEqual (res , expected )
209
209
210
+ def test_partial_permutation_in_middle (self ):
211
+ """Test with a permutation gate in the middle of a circuit,
212
+ with the permutation gate defined only on a subset of qubits.
213
+ """
214
+ qc = QuantumCircuit (5 )
215
+ qc .cx (0 , 1 )
216
+ qc .append (PermutationGate ([1 , 2 , 0 ]), [0 , 2 , 4 ])
217
+ qc .cx (2 , 3 )
218
+
219
+ # The permutation corresponding to the permutation gate maps
220
+ # 2 -> 0, 4 -> 2, 0 -> 4, and 1 -> 1, 3 -> 3.
221
+ # Instead of the permutation gate, we can relabel the qubits
222
+ # 0 -> 2, 1 -> 1, 2 -> 4, 3 -> 3, 4 -> 2.
223
+ # Hence cx(2, 3) becomes cx(4, 3).
224
+ expected = QuantumCircuit (5 )
225
+ expected .cx (0 , 1 )
226
+ expected .cx (4 , 3 )
227
+
228
+ pass_ = ElidePermutations ()
229
+ res = pass_ (qc )
230
+
231
+ # Make sure that the pass removes the permutation gate and remaps
232
+ # the following gate
233
+ self .assertEqual (res , expected )
234
+
235
+ # Make sure that the transpiled circuit *with* the final permutation
236
+ # is equivalent to the original circuit
237
+ perm = pass_ .property_set ["virtual_permutation_layout" ].to_permutation (qc .qubits )
238
+ res .append (PermutationGate (perm ), [0 , 1 , 2 , 3 , 4 ])
239
+ self .assertEqual (Operator (res ), Operator (qc ))
240
+
210
241
def test_permutation_at_beginning (self ):
211
242
"""Test permutation in beginning of bell is elided."""
212
243
qc = QuantumCircuit (3 , 3 )
0 commit comments