Fix TemplateOptimization dropping the global_phase on substitution#15943
Conversation
…ubstitution circuit_to_dagdependency did not copy global_phase when converting a template QuantumCircuit to DAGDependency, so template phases were lost before matching. TemplateSubstitution.run_dag_opt also constructed a new DAGDependency for the optimised output without inheriting the original circuit's global_phase, resetting it to zero whenever at least one substitution was applied. Fixed by copying global_phase in circuit_to_dagdependency and seeding dag_dep_opt.global_phase from the circuit DAG at construction time. Also added the per-match phase subtraction (Fix 3) for when templates with nonzero global_phase become accepted via the identity check fix in Qiskit#14538. Fixes Qiskit#14537. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…ubstitution
circuit_to_dagdependency did not copy global_phase when converting a
template QuantumCircuit to DAGDependency, so template phases were lost
before matching. TemplateSubstitution.run_dag_opt also constructed a
new DAGDependency for the optimised output without inheriting the
original circuit's global_phase, resetting it to zero whenever at
least one substitution was applied. Additionally, when a template
carries a nonzero global_phase phi_T (gate content implements
e^{-i*phi_T} * I while the full operator is I), the per-match phase
contribution was not being subtracted from the output circuit.
Fixes Qiskit#14537.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
One or more of the following people are relevant to this code:
|
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
4e62215 to
bdeeb62
Compare
The :issue: role is not available in this project's Sphinx config. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…on test Covers the case where both the circuit and the template carry a nonzero global_phase simultaneously, confirming that fixes Qiskit#2 (circuit phase preserved across substitution) and Qiskit#3 (per-match template phase subtracted) compose correctly. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
Thanks for the fix! |
|
Thanks @ShellyGarion! |
ShellyGarion
left a comment
There was a problem hiding this comment.
LGTM. Thanks for fixing this bug!
I won't merge it yet since I want to see if there are further comments.
alexanderivrii
left a comment
There was a problem hiding this comment.
Thank you for doing this, two minor comments, otherwise LGTM.
…e tests Replace assertTrue(Operator(x) == Operator(y)) with assertEqual(Operator(x), Operator(y)) so that assertion failures show the actual vs expected values rather than just "False is not True". Also shorten multi-line test docstrings to concise one-liners. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
alexanderivrii
left a comment
There was a problem hiding this comment.
LGTM. Julien/Shelly - do you have any further comments?
Cryoris
left a comment
There was a problem hiding this comment.
Thanks for picking this up! I left a few comments, most are typical LLM things like being too verbose, touching unrelated things and being a bit redundant.
| # Operator equivalence confirms the full unitary (including phase) is preserved. | ||
| self.assertEqual(Operator(circuit_in), Operator(result)) | ||
|
|
||
| def test_circuit_and_template_both_have_nonzero_global_phase(self): |
There was a problem hiding this comment.
This tests the same thing as the previous test, right? Do we need it?
There was a problem hiding this comment.
I think given that the previous test was changed to 4 gates and therefore avoids the substitution is testing partial match, this test is now testing a different thing, which is the phase subtraction upon full substitution. So I would say we keep it?
- Simplify comment on global phase update in template_substitution.py - Split release note into per-bug files; add fix-circuit-to-dagdependency-global-phase-14537.yaml - Update tests: use issue reproducer (4-gate circuit) in test_template_nonzero_global_phase_applied_to_circuit, merge single/multiple match tests, drop modulo from assertAlmostEqual Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- Fix test_circuit_and_template_both_have_nonzero_global_phase expected value from np.pi/4 to 7*np.pi/12 (pi/3 + pi/4) - Remove duplicate test_circuit_global_phase_preserved_with_multiple_template_matches - Update test_template_nonzero_global_phase_applied_to_circuit to use Operator equivalence instead of phase/count assertions since partial template match does occur Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
FYI -- I was surprised to see the Claude as co-author didn't have to explicitly sign the CLA. Turns out we have an active rule that excludes bots from requiring to sign (which made sense pre-AI). We'll likely want to discuss in what form such a rule still makes sense before merging this PR. |
…4537.yaml Co-authored-by: Julien Gacon <gaconju@gmail.com>
…7.yaml Co-authored-by: Julien Gacon <gaconju@gmail.com>
Co-authored-by: Julien Gacon <gaconju@gmail.com>
Actually I removed all reference to "Regression test" in the test descriptions based on this comment Qiskit#15943 (comment) Co-authored-by: Julien Gacon <gaconju@gmail.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
What's your take on #15688, where Claude was also used? |
|
Commits where LLMs have been used have been merged to Qiskit already, though until now humans (as far as we can tell) were the sole listed author. With signing the CLA they take responsibility that they can give Qiskit the right to use the code. With the present PR, it's the first time that an LLM is actually listed as co-author. Normally we require all authors to sign the CLA -- yet a bot cannot sign the CLA. So there's an open question on whether our guidelines are explicit enough that you, as human author, take responsibility of the LLMs output by signing the CLA or whether we need something additional. We've discussed this in the team meeting but didn't reach a conclusion yet! |
TemplateOptimization dropping the global_phase on substitution
Cryoris
left a comment
There was a problem hiding this comment.
LGTM thanks for the contribution!
Summary
Fixes #14537.
TemplateOptimizationwas silently dropping or miscalculatingglobal_phasein three places:circuit_to_dagdependencydid not copyglobal_phasewhen converting a templateQuantumCircuittoDAGDependency.TemplateSubstitution.run_dag_optconstructed the optimizedDAGDependencywithout inheriting the original circuit'sglobal_phase.global_phaseφ_T (gate content implementse^{-i*φ_T} * Iwhile the full operator isI), the per-match phase contribution was not being subtracted from the output circuit.Test plan
test_circuit_global_phase_preserved_after_template_match— circuit phase survives a substitutiontest_template_nonzero_global_phase_applied_to_circuit— template phase correctly accumulated; includesOperatorequivalence checktest_circuit_global_phase_preserved_with_multiple_template_matches— circuit phase preserved across multiple matches🤖 Generated with Claude Code