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

Fix use of node.op in Split2QUnitaries (backport #13015) #13062

Merged
merged 2 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
)
Loading