Skip to content

Commit

Permalink
fix Pauli.apply_layout
Browse files Browse the repository at this point in the history
  • Loading branch information
t-imamichi committed May 13, 2024
1 parent 49e96fa commit 23dde22
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
3 changes: 3 additions & 0 deletions qiskit/quantum_info/operators/symplectic/pauli.py
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,9 @@ def apply_layout(
raise QiskitError("Provided layout contains indices outside the number of qubits.")
if len(set(layout)) != len(layout):
raise QiskitError("Provided layout contains duplicate indices.")
if self.num_qubits == 0:
phases = ["", "-i", "-", "i"]
return type(self)(phases[self.phase] + "I" * n_qubits)
new_op = type(self)("I" * n_qubits)
return new_op.compose(self, qargs=layout)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
fixes:
- |
Fixed :meth:`~.SparsePauliOp.apply_layout` to work correctly with zero-qubit operators.
Fixed :meth:`.SparsePauliOp.apply_layout` and :meth:`.Pauli.apply_layout`
to work correctly with zero-qubit operators.
23 changes: 23 additions & 0 deletions test/python/quantum_info/operators/symplectic/test_pauli.py
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,29 @@ def test_apply_layout_duplicate_indices(self):
with self.assertRaises(QiskitError):
op.apply_layout(layout=[0, 0], num_qubits=3)

def test_apply_layout_zero_qubit(self):
"""Test apply_layout with a zero-qubit operator"""
with self.subTest("default"):
op = Pauli("")
res = op.apply_layout(layout=None, num_qubits=5)
self.assertEqual(Pauli("IIIII"), res)
with self.subTest("phase -1j"):
op = Pauli("-i")
res = op.apply_layout(layout=None, num_qubits=5)
self.assertEqual(Pauli("-iIIIII"), res)
with self.subTest("phase -1"):
op = Pauli("-")
res = op.apply_layout(layout=None, num_qubits=5)
self.assertEqual(Pauli("-IIIII"), res)
with self.subTest("phase 1j"):
op = Pauli("i")
res = op.apply_layout(layout=None, num_qubits=5)
self.assertEqual(Pauli("iIIIII"), res)
with self.subTest("layout"):
op = Pauli("")
res = op.apply_layout(layout=[], num_qubits=5)
self.assertEqual(Pauli("IIIII"), res)


if __name__ == "__main__":
unittest.main()

0 comments on commit 23dde22

Please sign in to comment.