Updated validation.py - #67
Open
sannesg wants to merge 2 commits into
Open
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This pull request updates the exact phase-separator validation helper to better support problems where only a subset of the computational basis states are considered valid/feasible (i.e., not all (2^n) bitstrings are meaningful), by allowing invalid states to be omitted from the error metrics.
Changes:
- Adds support for omitting invalid states during validation via masking.
- Introduces new parameters (
global_phase,omit_invalid_states) and updates the docstring to describe feasible-subspace behavior. - Adjusts magnitude/phase error calculations to apply only to unmasked states.
Suppressed comments (4)
qaoa/utils/validation.py:66
- When omit_invalid_states=True, the global phase alignment currently always uses ref_idx=0 even if that state was masked out. This can align against an omitted/invalid basis state and distort the residual phase calculation. Pick ref_idx from the first unmasked entry, and handle the edge case where all states are masked out.
# Remove global phase by aligning first nonzero expected
ref_idx = 0
g = diag[ref_idx] / expected[ref_idx] # global phase factor
ratios = diag / (expected * g)
qaoa/utils/validation.py:86
- The "worst offender" examples are sorted using all ratios, including masked-out states, which can make the report misleading when omit_invalid_states=True. Sort using only unmasked indices so the examples correspond to the states actually being validated.
# include a few worst offenders
idx_sorted = np.argsort(-np.abs(np.angle(ratios)))
bad = []
for k in idx_sorted[:8]:
qaoa/utils/validation.py:91
- The report key "raw expected" contains a space, which is awkward to consume programmatically and inconsistent with the other snake_case keys in the report. Consider renaming it to "raw_expected".
"bitstring": list(_bitstring(k, n, flip=flip)),
"diag_entry": complex(diag[k]),
"raw expected": complex(expected[k]),
"expected": complex(expected[k]*g),
qaoa/utils/validation.py:34
- Docstring spelling: "anzats'" should be "ansatzes" (and the possessive apostrophe is incorrect here).
Works for anzats' that remain in a feasible subspace. If the anzats' can reach infeasible spaces,
the problem must define a suitable penalty for such solutions.
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+27
to
29
| Exact check that the problem's circuit represents the problem's cost function. | ||
| This tests checks that the unitary operator represented by the quantum circuit is | ||
| equal to the expected matrix with diagonal elements |
| return check_phase_separator_exact_problem(qaoa.problem, *arg, **kwarg) | ||
|
|
||
| def check_phase_separator_exact_problem(problem, t=1, flip=True, atol=1e-8, rtol=1e-8): | ||
| def check_phase_separator_exact_problem(problem, t=1, flip=True, atol=1e-7, rtol=1e-7, global_phase = 0.0, omit_invalid_states = False): |
Member
|
@copilot Fix the issue described in the Copilot review comment above. |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
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.
Updated validation.py so that it now works also for problems where not all 2^n solutions are valid.