【代码贡献】Add quantum counting (BHT) built on the existing Grover operator - #47
Open
mnn31 wants to merge 1 commit into
Open
【代码贡献】Add quantum counting (BHT) built on the existing Grover operator#47mnn31 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.
Adds the Brassard-Hoyer-Tapp quantum counting algorithm as a new module,
pyqpanda_alg.QCounting. It estimates how many items of a search space ofN = 2 ** qnumber are marked by an oracle.
The docstring of
Grover.amp_operatoralready says the operator "Can be part ofGrover/Quantum Count/QAE and other amplitude amplification related algorithm",
and the Grover and QAE parts are in the library while the counting part is not.
This fills that slot and reuses what is already there: the amplitude
amplification operator comes from
Grover.amp_operator, the oracle fromGrover.mark_data_reflectionwhen the marked states are known, and the readoutfrom
plugin.QFT. Nothing is duplicated.How it works. The counting register is put in superposition, the controlled
powers G ** (2 ** k) of the Grover operator are applied, and the inverse QFT
reads the eigenphase back. The eigenphases theta of G satisfy
sin(theta / 2) ** 2 = M / N, so the measured counting value y gives
M = N * sin(pi * y / 2 ** t) ** 2. G has the two eigenphases theta and
2 * pi - theta, and that formula is the same for y and 2 ** t - y, so the two
readout peaks give the same count and the peak does not need to be
disambiguated.
One implementation detail worth flagging:
amp_operatorreturns the product ofthe two reflections without the overall minus sign of the Grover operator. The
sign is unobservable in Grover search but not in phase estimation, where the
operator is controlled, so the module appends
RX(q, 2 * pi)to restore it.That is the same trick
QAE._Q_ciralready uses.API:
The oracle is given either as
mark_data, the marked bit strings, or as a phaseflip circuit through
flip_operator, withancilla_qubitsworkspace qubits ifit needs them.
counting_qubitsdefaults to qnumber + 2, which is the smallestregister for which the error bound pi * N / 2 ** (counting_qubits + 1) equals
pi / 8 and therefore stays below 1/2, so rounding the result returns M. More
counting qubits sharpen the unrounded estimate.
Validation. Every count from 0 to N was checked for qnumber = 2 and 3, plus
spot checks at qnumber = 4, all at the default resolution:
A wider sweep of 150 cases (qnumber 2 to 4, every M, several random subsets per
M, counting registers of qnumber + 2 and qnumber + 3) rounded to the true count
in every case. M = 0 and M = N are not degenerate here: they are the exact
eigenvalues +1 and -1 of G, the counting register lands on y = 0 and
y = 2 ** (t - 1), and the estimates come out as exactly 0 and exactly N.
Tests in
Test_QCounting_QCounting.py: 17 tests covering the sweeps above, thescattered (non contiguous) marked sets, the exact M = 0 and M = N cases, a
flip_operatororacle with a workspace qubit, the relation betweenthetaandthe returned count, the resolution behaviour, and input validation. They are
deterministic because the result is read from
get_prob_dict. The wholetest/suite is green (35 passed, 17 of them new). There are doctests on the class,
cirandrun, and they run clean.example/QAlgBase/testeg_QCounting.pyruns end to end in about 1.4 s and showsboth oracle styles plus the effect of the counting register size.
One limitation: runtime is dominated by the 2 ** counting_qubits - 1 copies of
the Grover operator, so this is meant for small search spaces, same as the other
textbook algorithms in the library.