【代码贡献】Fix QUBO_QAOA.run crashing with default arguments - #45
Open
mnn31 wants to merge 1 commit into
Open
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
QUBO.QUBO_QAOA(f).run()raisesValueError: layer number must be a positive integer. Every argument ofrunis optional, so calling it with nothing butdefaults should work, and instead it fails before any circuit is built.
Root cause
pyqpanda_alg/QUBO/QUBO.py,QUBO_QAOA.run(line 482) declareslayer=Noneand forwards that value straight into
qaoa.QAOA.run:QAOA.runitself declareslayer=1and validates the argument in_check_layer_and_generate_initial_para(pyqpanda_alg/QAOA/qaoa.py, line 748):Noneis not an int, so the wrapper's own default is the only value oflayerthat
QAOA.runrejects outright. The sentinel is never resolved anywherebetween the two calls.
Fix
Give
QUBO_QAOA.runthe same default its callee already documents and uses,layer=1, and mark the parameter optional in the docstring. Nothing else inthe method changes, and any explicit
layerargument behaves exactly asbefore.
Verification
before
after
The highest probability bitstring is
010, the true minimizer of the testfunction, so a single layer already returns a usable answer at the default.
The class docstring example,
run(layer=5, optimizer='SLSQP', optimizer_option={'options': {'eps': 1e-3}}), still runs and still returns010as the most probable bitstring (0.83 under the same seed), unchanged fromdevelop.I checked the sibling
QUBO_GAS_origin.runfor the same class of problem. Itsinit_value=Noneis resolved toself.constantinside the body, andQUBO_GAS_origin(f).run()completes on defaults, so nothing there neededtouching.
Tests
2 cases added to
test/QAlgBase/Test_QUBO_QUBO_QAOA_run.py:run()on puredefaults returns a normalized 8 entry distribution with real weight on the
correct solution, and the default result is identical to
run(layer=1)underthe same seed. Both are seeded and deterministic. Both fail on
developand pass withthis change.
Scope note: this touches
QUBO.pyonly, so it does not overlap #40, whichchanges
qaoa.py(CVaR/Gibbs loss).