Skip to content

Commit

Permalink
Fix use of node.op in Split2QUnitaries (backport #13015) (#13062)
Browse files Browse the repository at this point in the history
* Fix  use of `node.op` in `Split2QUnitaries` (#13015)

* Replace node.op with node.matrix

* Add docstring to test gate

* Fix black

(cherry picked from commit 623415c)

* Update test/python/transpiler/test_split_2q_unitaries.py

---------

Co-authored-by: Elena Peña Tapia <57907331+ElePT@users.noreply.github.com>
Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
  • Loading branch information
3 people authored Aug 30, 2024
1 parent 8346066 commit 3e68b3e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def run(self, dag: DAGCircuit):
):
continue

decomp = TwoQubitWeylDecomposition(node.op, fidelity=self.requested_fidelity)
decomp = TwoQubitWeylDecomposition(node.matrix, fidelity=self.requested_fidelity)
if (
decomp._inner_decomposition.specialization
== TwoQubitWeylDecomposition._specializations.IdEquiv
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
fixes:
- |
Fixed a bug in :class:`.Split2QUnitaries` where it would fail to run on circuits
with custom gates that didn't implement :meth:`__array__`.
See `#12984 <https://github.com/Qiskit/qiskit/issues/12970>`__.
38 changes: 37 additions & 1 deletion test/python/transpiler/test_split_2q_unitaries.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

from qiskit import QuantumCircuit, QuantumRegister, transpile
from qiskit.circuit.library import UnitaryGate, XGate, ZGate, HGate
from qiskit.circuit import Parameter, CircuitInstruction
from qiskit.circuit import Parameter, CircuitInstruction, Gate
from qiskit.providers.fake_provider import GenericBackendV2
from qiskit.quantum_info import Operator
from qiskit.transpiler import PassManager
Expand Down Expand Up @@ -223,3 +223,39 @@ def test_split_qft(self):
pm.append(Split2QUnitaries())
qc_split = pm.run(qc)
self.assertEqual(26, qc_split.num_nonlocal_gates())

def test_gate_no_array(self):
"""
Test that the pass doesn't fail when the circuit contains a custom gate
with no ``__array__`` implementation.
Reproduce from: https://github.com/Qiskit/qiskit/issues/12970
"""

class MyGate(Gate):
"""Custom gate"""

def __init__(self):
super().__init__("mygate", 2, [])

def to_matrix(self):
return np.eye(4, dtype=complex)

def mygate(self, qubit1, qubit2):
return self.append(MyGate(), [qubit1, qubit2], [])

QuantumCircuit.mygate = mygate

qc = QuantumCircuit(2)
qc.mygate(0, 1)

pm = PassManager()
pm.append(Collect2qBlocks())
pm.append(ConsolidateBlocks())
pm.append(Split2QUnitaries())
qc_split = pm.run(qc)

self.assertTrue(Operator(qc).equiv(qc_split))
self.assertTrue(
matrix_equal(Operator(qc).data, Operator(qc_split).data, ignore_phase=False)
)

0 comments on commit 3e68b3e

Please sign in to comment.