bugfix(grover): Initialise MatMultMapBody::contains_zero_val - #10
Open
endaytrer wants to merge 1 commit into
Open
bugfix(grover): Initialise MatMultMapBody::contains_zero_val#10endaytrer wants to merge 1 commit into
endaytrer wants to merge 1 commit into
Conversation
Initialize contains_zero_val to make Grover's algorithm work properly.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
MatMultMapBody::contains_zero_valis never initialised. Its indeterminate value is read inMatrixMultiplyV4WithInfoNode, where a straytruemakes matrix multiplication silently drop path multiplicities. The visible consequence is thattestGroversAlgoreturns wrong answers from 16 qubits up — 0 of 10 seeds correct at 16, 32 and 64 qubits.The defect
CFLOBDD/matmult_map.cpp:39initialises two of the three flags:The field is only ever assigned in
MatMultMapHandle::ForceAdd(matmult_map.cpp:217), when the(-1,-1)zero-sentinel pair is inserted. Every map that never goes through that path carries whatever was in memory — I have observed it reading as208.It is consumed twice in
MatrixMultiplyV4WithInfoNode(CFLOBDD/matrix1234_node.cpp:5776and:5785):v.secondis the path count contributed by the A-connection. When the flag readstruethe scaling is skipped and that multiplicity is lost. The second use, a few lines below, replaces the accumulator instead of adding into it, dropping terms outright.Because the value is indeterminate rather than merely wrong, the failure is order-dependent:
canonicalMatMultMapBodySetinterns bodies by map contents alone —MatMultMapBody::operator==andHash()do not include this flag — so a body interned by one multiply supplies the flag that a later, unrelated multiply reads. In a run oftestGroversAlgo,U_s * U_sis exactlyIwhen computed on its own and stops beingIas soon asM * psi0has run first.Fix
Once initialised, the flag is a pure function of the map contents — true exactly when
(-1,-1)is present — so its omission fromoperator==andHash()becomes harmless and needs no change.Verification
Ten seeds per size through 128 qubits, three at 256 and 512:
The speedup is a side effect of the same defect: dropped multiplicities were splitting entries that should have coincided, inflating the diagrams.
testGHZAlgo,testBVAlgo,testDJAlgoandtestSimonsAlgoare unchanged and still correct.Notes
sequiturbranch, sincemain's Makefile passes the Windows-only-lpsapiand-Wl,--stackand does not build on Linux as-is. The constructor is byte-identical on both branches.wmatrix1234_fb_mul_node.cpp,wmatrix1234_complex_fb_mul_node.cpp,wmatrix1234_fourier_mul_node.cpp) read this same field, several of them testingcontains_zero_val == falseexplicitly. They were not exercised here — that arm was not built — but they rely on the same default.testGroversAlgo 10(1024 qubits) terminates withSIGSEGVon every seed after roughly 2 s. That looks like an unrelated second issue.