Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Issue 11026: SparsePauliOp.apply_layout should do nothing if given None #11041

Merged
merged 7 commits into from
Nov 16, 2023
Merged
8 changes: 6 additions & 2 deletions qiskit/quantum_info/operators/symplectic/sparse_pauli_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -1109,12 +1109,13 @@ def assign_parameters(
return None if inplace else bound

def apply_layout(
self, layout: TranspileLayout | List[int], num_qubits: int | None = None
self, layout: TranspileLayout | List[int] | None, num_qubits: int | None = None
) -> SparsePauliOp:
"""Apply a transpiler layout to this :class:`~.SparsePauliOp`

Args:
layout: Either a :class:`~.TranspileLayout` or a list of integers.
layout: Either a :class:`~.TranspileLayout`, a list of integers or None.
If layout is None, then no action is applied.
num_qubits: The number of qubits to expand the operator to. If not
provided then if ``layout`` is a :class:`~.TranspileLayout` the
number of the transpiler output circuit qubits will be used by
Expand All @@ -1127,6 +1128,9 @@ def apply_layout(
"""
from qiskit.transpiler.layout import TranspileLayout

if layout is None:
return self.copy()

n_qubits = self.num_qubits
if isinstance(layout, TranspileLayout):
n_qubits = len(layout._output_qubit_list)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1088,6 +1088,12 @@ def test_apply_layout_layout_list_and_num_qubits(self):
res = op.apply_layout([4, 0], 5)
self.assertEqual(SparsePauliOp.from_list([("IIIIY", 2), ("IIIIX", 1)]), res)

def test_apply_layout_null_layout(self):
"""Test apply_layout with a null layout"""
op = SparsePauliOp.from_list([("II", 1), ("IZ", 2), ("XI", 3)])
res = op.apply_layout(layout=None, num_qubits=5)
self.assertEqual(op, res)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test looks odd to me: it specifically asks for num_qubits=5, but the output operator then asserted to be 2q (by comparison to the input op, which is 2q). Certainly I think that that should be invalid, but it's not clear to me whether it should be an error during SparsePauliOp.apply_layout, or whether that method should do something to expand the operator. I expect it should be an error, but I've opened a discussion in the initial thread: #11026 (comment)



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