From 662cb44c30c76a5a596a07a3dfa6e495e382f022 Mon Sep 17 00:00:00 2001 From: mstnb <25572273+mstnb@users.noreply.github.com> Date: Thu, 24 Nov 2022 19:41:50 +0100 Subject: [PATCH 1/3] Fix wrong argument supplied to _identity_op() qiskit/qiskit-terra#9197 --- qiskit/circuit/commutation_checker.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qiskit/circuit/commutation_checker.py b/qiskit/circuit/commutation_checker.py index cb3e40685bee..2a809bf7e348 100644 --- a/qiskit/circuit/commutation_checker.py +++ b/qiskit/circuit/commutation_checker.py @@ -144,7 +144,7 @@ def commute( # being the lowest possible indices so the identity can be tensored before it. extra_qarg2 = num_qubits - len(qarg1) if extra_qarg2: - id_op = _identity_op(2**extra_qarg2) + id_op = _identity_op(extra_qarg2) operator_1 = id_op.tensor(operator_1) op12 = operator_1.compose(operator_2, qargs=qarg2, front=False) op21 = operator_1.compose(operator_2, qargs=qarg2, front=True) From af735006e5ae7f19082fd5f2c793a652ac965a02 Mon Sep 17 00:00:00 2001 From: Matthew Treinish Date: Wed, 1 Mar 2023 12:43:20 -0500 Subject: [PATCH 2/3] Add test case with large qubit gate This commit adds a test case to ensure we get the correct result with a large number of qubits. This also implicitly tests that we've fixed the excessive memory consumption because the memory requirements for an 8 qubit gate with the bug still present would not be runnable on most current systems. Co-authored-by: Jake Lishman --- test/python/circuit/test_commutation_checker.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/test/python/circuit/test_commutation_checker.py b/test/python/circuit/test_commutation_checker.py index c44252a64f3e..6ab74e484ae6 100644 --- a/test/python/circuit/test_commutation_checker.py +++ b/test/python/circuit/test_commutation_checker.py @@ -18,7 +18,7 @@ from qiskit import ClassicalRegister from qiskit.test import QiskitTestCase -from qiskit.circuit import QuantumRegister, Parameter +from qiskit.circuit import QuantumRegister, Parameter, Qubit from qiskit.circuit import CommutationChecker from qiskit.circuit.library import ( ZGate, @@ -357,6 +357,12 @@ def test_complex_gates(self): res = comm_checker.commute(lf3, [0, 1, 2], [], lf4, [0, 1, 2], []) self.assertTrue(res) + def test_c7x_gate(self): + """Test wide gate works correctly.""" + qargs = [Qubit() for _ in [None] * 8] + res = CommutationChecker().commute(XGate(), qargs[:1], [], XGate().control(7), qargs, []) + self.assertFalse(res) + if __name__ == "__main__": unittest.main() From cf2be87423cca817f742891d57cd6913cbfcc83c Mon Sep 17 00:00:00 2001 From: Matthew Treinish Date: Wed, 1 Mar 2023 12:45:42 -0500 Subject: [PATCH 3/3] Add release note --- .../fix-memory-commutation-checker-dbb441de68706b6f.yaml | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 releasenotes/notes/fix-memory-commutation-checker-dbb441de68706b6f.yaml diff --git a/releasenotes/notes/fix-memory-commutation-checker-dbb441de68706b6f.yaml b/releasenotes/notes/fix-memory-commutation-checker-dbb441de68706b6f.yaml new file mode 100644 index 000000000000..dee3704aae08 --- /dev/null +++ b/releasenotes/notes/fix-memory-commutation-checker-dbb441de68706b6f.yaml @@ -0,0 +1,9 @@ +--- +fixes: + - | + Fixed an issue with the :class:`~.CommutationChecker` class where it would + attempt to internally allocate an array for :math:`2^{n}` qubits when it + only needed an array to represent :math:`n` qubits. This could cause + an excessive amount of memory for wide gates, for example a 4 qubit + gate would require 32 gigabytes instead of 2 kilobytes. + Fixed `#9197 `__