Skip to content

Provide workaround for TwoQubitWeylDecomposition error for large circuits (backport #100)#125

Merged
garrison merged 2 commits into
stable/0.2from
mergify/bp/stable/0.2/pr-100
Oct 1, 2025
Merged

Provide workaround for TwoQubitWeylDecomposition error for large circuits (backport #100)#125
garrison merged 2 commits into
stable/0.2from
mergify/bp/stable/0.2/pr-100

Conversation

@mergify
Copy link
Copy Markdown

@mergify mergify Bot commented Sep 30, 2025

Hi!

When using the generate_ansatz_from_circuit function with a large circuit, I've encountered an error with Qiskit's TwoQubitWeylDecomposition. See the below error message.

qiskit.exceptions.QiskitError: 'TwoQubitWeylDecomposition: failed to diagonalize M2. Please report this at https://github.com/Qiskit/qiskit-terra/issues/4159. 

This originates from the lines:

mat = Operator(couple_qc).data
d = TwoQubitWeylDecomposition(mat)

in from_connectivity.py (304-305). You can recreate the error using:

from qiskit import QuantumCircuit
from qiskit_addon_aqc_tensor import generate_ansatz_from_circuit

N = 50
qc = QuantumCircuit(N)

for _ in range(10):
    for i in range(0, N, 2):
        qc.cx(i, i + 1)
    for i in range(1, N - 1, 2):
        qc.cx(i, i + 1)


_, _ = generate_ansatz_from_circuit(qc, qubits_initially_zero=True)

Many of the calls to TwoQubitWeylDecomposition occur without error, but for large circuits, there comes a point where one of the matrices fails to diagonalise. Interestingly, this is reproducible behaviour, and always occurs at the same point for a given circuit.

This error has been mentioned in quite a few issues on GitHub/StackExchange (Qiskit/qiskit#4159, Qiskit/qiskit#14233, Qiskit/qiskit#7120, https://quantumcomputing.stackexchange.com/questions/41866/twoqubitweyldecomposition-failed-to-diagonalize-m2). I believe it usually arises when the matrix being decomposed is not unitary, however in all the cases I tested, the matrix was unitary to numerical precision (and returned Operator(couple_qc).is_unitary() = True. It seems others have had this issue as well.

I found a work-around by transpiling couple_qc before passing it to TwoQubitWeylDecomposition. From playing around, it looks like the matrices produced by Operator(couple_qc).data are identical to numerical precision before and after this transpilation, however one causes an error, and the other doesn't. I'm not sure exactly why this is the case, or if it works all the time, but it seems to work for the cases I looked into.


This is an automatic backport of pull request #100 done by Mergify.

…uits (#100)

* add transpilation of couple_qc

* unit test

* rename

* Update to transpile onlyu if a QiskitError has been thrown

* add release note for TwoQubitWeylDecomposition fix

* Show warning by default

* Tweak the release note

* Update warning message to clarify that it is not an error

* Update qiskit_addon_aqc_tensor/ansatz_generation/from_connectivity.py

---------

Co-authored-by: Jim Garrison <garrison@ibm.com>
Co-authored-by: Jim Garrison <jim@garrison.cc>
(cherry picked from commit e926eed)
@coveralls
Copy link
Copy Markdown

coveralls commented Sep 30, 2025

Pull Request Test Coverage Report for Build 18148242511

Details

  • 13 of 13 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 100.0%

Totals Coverage Status
Change from base Build 15401394333: 0.0%
Covered Lines: 667
Relevant Lines: 667

💛 - Coveralls

@garrison garrison merged commit 62a5cbc into stable/0.2 Oct 1, 2025
20 checks passed
@garrison garrison deleted the mergify/bp/stable/0.2/pr-100 branch October 1, 2025 01:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants