Skip to content

Commit

Permalink
Fix use of node.op in Split2QUnitaries (#13015)
Browse files Browse the repository at this point in the history
* Replace node.op with node.matrix

* Add docstring to test gate

* Fix black
  • Loading branch information
ElePT authored Aug 30, 2024
1 parent 8e5fab6 commit 623415c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,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>`__.
39 changes: 38 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,40 @@ 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)
# return np.eye(4, dtype=float)

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 623415c

Please sign in to comment.