Skip to content

Commit adf55e1

Browse files
Bug fix in ElidePermutations (#13186)
* elide permutations fix * extending the test to check the final permutation
1 parent 65bb09e commit adf55e1

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

qiskit/transpiler/passes/optimization/elide_permutations.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ def _apply_mapping(qargs):
9696
elif isinstance(node.op, PermutationGate):
9797
starting_indices = [qubit_mapping[dag.find_bit(qarg).index] for qarg in node.qargs]
9898
pattern = node.op.params[0]
99-
pattern_indices = [qubit_mapping[idx] for idx in pattern]
100-
for i, j in zip(starting_indices, pattern_indices):
99+
updated_indices = [starting_indices[idx] for idx in pattern]
100+
for i, j in zip(starting_indices, updated_indices):
101101
qubit_mapping[i] = j
102102
input_qubit_mapping = {qubit: index for index, qubit in enumerate(dag.qubits)}
103103
self.property_set["original_layout"] = Layout(input_qubit_mapping)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
fixes:
3+
- |
4+
Fixed a bug in the transpiler pass :class:`~.ElidePermutations` where the
5+
qubit mapping was not updated correctly in the presence of :class:`.PermutationGate`\s.

test/python/transpiler/test_elide_permutations.py

+31
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,37 @@ def test_permutation_in_middle(self):
207207
res = self.swap_pass(qc)
208208
self.assertEqual(res, expected)
209209

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+
210241
def test_permutation_at_beginning(self):
211242
"""Test permutation in beginning of bell is elided."""
212243
qc = QuantumCircuit(3, 3)

0 commit comments

Comments
 (0)