【代码贡献】Fix qft_qubit_comparator geq and s modes returning wrong results - #42
Open
mnn31 wants to merge 1 commit into
Open
【代码贡献】Fix qft_qubit_comparator geq and s modes returning wrong results#42mnn31 wants to merge 1 commit into
mnn31 wants to merge 1 commit into
Conversation
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.
Problem
QCmp.qft_qubit_comparatorbuilds the same circuit forfunction='geq'as for'g', and the same circuit for's'as for'seq'. The two strict/non-strictpairs are indistinguishable, so two of the four documented modes are wrong
whenever the two registers can be equal.
Repro,
q_state_1in uniform superposition over 0..3 andq_state_2 = |2>:qubit_comparatorandqft_comparatoragree with each other on the same case;qft_qubit_comparatordisagrees ongeqands.Root cause
pyqpanda-algorithm/pyqpanda_alg/QCmp/QCmp.py,qft_qubit_comparator(lines 512-592 before this change).
Both QFT comparators work the same way: subtract the compared value from the
q_state + q_cmpregister, then add it back intoq_statealone, which leavesq_cmpholding the borrow bit and restores the state register. The borrow bitis a strict comparison, so the non-strict mode has to shift the subtracted value
by one.
qft_comparatordoes that at line 480:qft_qubit_comparatorhas no equivalent. Its subtracted value comes fromcontrolled
U1phases withq_state_1as controls, so there is nowherefor a
value += 1to go, and the constant term was simply never added. Theborrow bit therefore always evaluates
q_state_1 > q_state_2, and the onlything
functionstill controls is the finalX(q_cmp), givingg==geqands==seq.Fix
Add the constant
1to the same QFT-adder phase construction, for the two modesthat need it. In the
q_state_2 + q_cmpblock it goes in with-factor_all, inthe
q_state_2restore block with+factor_remain, so the state register stillcomes back unchanged:
Uncontrolled gates, so the cost is
2n + 1extraU1s and nothing atall for
gandseq.gandseqproduce byte-identical circuits to before,so the docstring example and
demo16-comparator-qft_qubit_comparator.ipynb(both
function='g') are unaffected.Verification
The repro table above now reads 0.25 / 0.50 / 0.50 / 0.75, matching
qubit_comparatorandqft_comparator.Exhaustive check over computational-basis pairs, asserting the comparison qubit
against the classical predicate for every
(a, b):all of them
a == bundergeqors)Also checked that
q_state_1andq_state_2are returned unchanged: feeding asuperposition through the
geqcircuit leaves the joint distribution over thestate qubits identical to the input.
Tests
test/QAlgBase/Test_comparator_qft_qubit_comparator.pyexisted but every linewas commented out, so nothing covered this. Un-commented it, brought it up to
the current pyqpanda3 API, and extended it to 19 tests: the docstring example,
all four modes on the superposition case with exact expected probabilities, the
a == bboundary that separatesgfromgeqandsfromseq, exhaustivebasis-pair sweeps at 2 and 3 qubits, register restoration, and the
NameErroron an unknown
function.