Skip to content

Commit

Permalink
Fix wrong argument supplied to _identity_op() (Qiskit#9201)
Browse files Browse the repository at this point in the history
* Fix wrong argument supplied to _identity_op() Qiskit#9197

* 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 <jake.lishman@ibm.com>

* Add release note

---------

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
Co-authored-by: Jake Lishman <jake.lishman@ibm.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
4 people authored and king-p3nguin committed May 22, 2023
1 parent 6b8d5a9 commit d7e397d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion qiskit/circuit/commutation_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <https://github.com/Qiskit/qiskit-terra/issues/9197>`__
8 changes: 7 additions & 1 deletion test/python/circuit/test_commutation_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()

0 comments on commit d7e397d

Please sign in to comment.