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

qml.lie_closure often raises LinAlgError: Singular matrix #6065

Open
Qottmann opened this issue Aug 5, 2024 · 6 comments · May be fixed by #6075 or #6232
Open

qml.lie_closure often raises LinAlgError: Singular matrix #6065

Qottmann opened this issue Aug 5, 2024 · 6 comments · May be fixed by #6075 or #6232

Comments

@Qottmann
Copy link
Contributor

Qottmann commented Aug 5, 2024

Running into this problem a lot when using sums of paulis, wonder how we can make this more reliable and stable (ideally with no significant performance regression).

In instances where the code breaks, I often find myself with large sums of Paulis with large coefficients, which makes me wonder whether picking up this old suggestion by @vincentmr could help?

An example:

n = 4
ZZs = [Z(i) @ Z(i+1) for i in range(n-1)]
Xs = [X(i) for i in range(n)]

H_TFIM = qml.dot(np.ones(2*n-1, dtype=float), ZZs + Xs) # np.random.rand(2*n-1)
H_TFIM = H_TFIM.pauli_rep

S = [Z(i) for i in range(n)]
S = [op.pauli_rep for op in S]

vspace = qml.pauli.PauliVSpace(S, dtype=float)

while True:
    len_before = len(vspace.basis)
    i = 1
    for m, Om in enumerate(vspace.basis.copy()):
        com = H_TFIM.commutator(Om)
        com.simplify()

        if len(com) == 0:  # skip because operators commute
            continue

        # result is always purely imaginary
        # remove common factor 2 with Pauli commutators
        for pw, val in com.items():
            com[pw] = val.imag / 2.

        vspace.add(com, tol=1e-8)
    
    print("----------------\n")
    print(f"After {i} epochs, the following candidate support:")
    print(vspace.basis)
    print("----------------\n")

    i += 1
    
    if len_before == len(vspace):
        break
LinAlgError: Singular matrix
@Qottmann Qottmann linked a pull request Aug 7, 2024 that will close this issue
@josh146
Copy link
Member

josh146 commented Aug 18, 2024

Thanks @Qottmann! Just double checking, does #6075 aim to resolve this? It wasn't clear to me from the PR (as the description is blank)

@Qottmann
Copy link
Contributor Author

Thanks @Qottmann! Just double checking, does #6075 aim to resolve this? It wasn't clear to me from the PR (as the description is blank)

Yes! Though it is currently not a full fix and we are still not sure how best to resolve it. Linear independence in a stable fashion is hard apparently 😢

@dwierichs
Copy link
Contributor

@josh146 Sorry for the blank description, I thought this was going to be a very quick fix that is not even worth triaging.
However, it turned out to be more complex after discussions on the PR.

We need to re-triage this issue, in my opinion, as it was found that neither @Qottmann nor I can guarantee to work on this soon.

@josh146
Copy link
Member

josh146 commented Aug 20, 2024

Thanks! I guess one question; does the open PR improve the behaviour even if it doesn't resolve it fully?

@dwierichs
Copy link
Contributor

Yep I think so, because a previously failing case is fixed by it (see test addition).
However, as this is about numerical precision and the like, there might be a new edge case lingering somewhere that stops working due to the PR, I suppose 🤔 Although it would surprise me.

@josh146
Copy link
Member

josh146 commented Aug 20, 2024

Got it, thanks @dwierichs!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants