Skip to content

fix: substitute overlapping linked parameters simultaneously - #302

Open
ddri wants to merge 1 commit into
PsiQ:mainfrom
ddri:fix/bartiq-capture
Open

ddri wants to merge 1 commit into
PsiQ:mainfrom
ddri:fix/bartiq-capture

Conversation

@ddri

@ddri ddri commented Oct 10, 2026 •

Copy link
Copy Markdown

Hi team, this issue is part of a batch from our review of bartiq at Marqov. We routinely review quantum computing tools that our community requests or that we’re exploring for integration with the Marqov SDK and Platform. We’re sharing these findings, reproductions, and proposed fixes in the hope they’re useful. There’s no expectation of an immediate response, and we can maintain any patches we need in our own fork while using the software.

Description

Parameter links can silently overwrite symbols inserted from a parent scope. With child cost x + 2*y and parent links y -> child.x, x -> child.y, compilation returns 3*y instead of 2*x + y.

Substitute linked symbols simultaneously when their replacement values overlap the symbols being replaced, so one linked parameter cannot rewrite another parameter's inserted value. Regression tests cover crossed links in both compilation modes, mapping-order independence, and preservation of bound sequence iterators even when the same symbol is also free outside the sum. Independent replacements retain the existing faster path. This addresses replacements rewriting values inserted by another linked parameter; substitution into a sum can still capture a replacement symbol that shares the name of its bound iterator, a pre-existing limitation that needs a separate fix.

Validation:

  • Six new regression cases fail on unchanged upstream; four additional compatibility cases already pass.
  • Full suite: 1,343 passed, 3 expected failures, including the existing 1,000-child compilation tests.
  • MYPYPATH=src poetry run mypy src --install-types --non-interactive: passes with the lockfile's mypy 1.16.1.
  • poetry run pre-commit run -a: passes.
  • Independent Claude Code review: approved commit e1aa912; independently reran the full suite and core-only symbolic/compiler tests.

Please verify that you have completed the following steps

  • I have self-reviewed my code.
  • I have included test cases validating introduced feature/fix.
  • I have updated documentation. No public API changes are introduced.

@CLAassistant

CLAassistant commented Oct 10, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@ddri ddri changed the title fix: substitute linked parameters without symbol capture fix: substitute overlapping linked parameters simultaneously Oct 10, 2026
@Brendan-Reid1991

Copy link
Copy Markdown
Contributor

Hi @ddri and thank you for opening this pull request! And for signing the CLA :).

Due to the sheer amount of issues / pull requests you opened over the weekend (18 across qref and bartiq, and another 15 issues in psiqdk), it will take us some time to dedicate manpower to reviewing and approving these. You will be glad to know it has sparked an internal discussion on how to handle AI-assisted contributions. While I'm sure it was not your intention, we are wary of being "AI DDoS'd" (see e.g. https://arxiv.org/html/2607.04003v1).

Overall your PRs cover a small number of lines which we greatly appreciate, as it makes it easier to review. For bartiq and qref specifically, I would really appreciate if you could update the PR descriptions to include a "minimal working example" of the bug / edge case in question; this will vastly speed up reviewing.

We're in the works of updating the PR template now to specifically request a MWE.

@Brendan-Reid1991 Brendan-Reid1991 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only some minor changes requested, and some clarification on inline imports and a test

Comment on lines +270 to +274
replaced_symbols = {old for old, _ in restricted_replacements}
simultaneous = len(restricted_replacements) > 1 and any(
replaced_symbols.intersection(sympy.sympify(new).free_symbols) for _, new in restricted_replacements
)
expr = expr.subs(restricted_replacements, simultaneous=simultaneous)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From my own local testing, I can see that having simultaneous=True always is painfully slow, so this guard is justified. My only remaining concern is on the simpyification of new being done twice: once in simultaneous and again in subs. I think this is only a minor optimisation, but if we can avoid repeating work we should.



def test_substitution_preserves_bound_sequence_iterators():
from sympy import Sum

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a good reason to do these imports inline rather than at the top of the file?

assert sympy_backend.substitute(expression, {"i": 7}) == 7 * Sum(i, (i, 1, n))


def test_simultaneous_substitution_accepts_string_values():

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure this test is justified, surely another test is capturing that string input is properly sympyified?

@ddri

ddri commented Oct 14, 2026

Copy link
Copy Markdown
Author

Hi @ddri and thank you for opening this pull request! And for signing the CLA :).

Due to the sheer amount of issues / pull requests you opened over the weekend (18 across qref and bartiq, and another 15 issues in psiqdk), it will take us some time to dedicate manpower to reviewing and approving these. You will be glad to know it has sparked an internal discussion on how to handle AI-assisted contributions. While I'm sure it was not your intention, we are wary of being "AI DDoS'd" (see e.g. https://arxiv.org/html/2607.04003v1).

Overall your PRs cover a small number of lines which we greatly appreciate, as it makes it easier to review. For bartiq and qref specifically, I would really appreciate if you could update the PR descriptions to include a "minimal working example" of the bug / edge case in question; this will vastly speed up reviewing.

We're in the works of updating the PR template now to specifically request a MWE.

This is the catch-22 we're struggling with too. We experienced this first-hand as part of Unitary Foundation's recent hackathon, where we got slammed with PRs and drive-by AI issues. It actually forced us to tighten our release cycles and length the "open source to partners first" so we catch as much as possible.

No rush or pressure to review or process any of these. We're happy to maintain local forks for our users for anything that they are exploring, but the benefit if you do, is that their explorations are because they are doing research towards papers or eventual commercial utility. There's a few FTQC projects that they are evaluating, and we serve whatever they need.

The challenge for Marqov, is that we are becoming the open source standard for quantum-classical orchestration, and every user of our partner release cycle (supercomputing centres and larger enterprises mostly) has pulled in other projects in the open stack. This puts pressure on us to review them, to make sure they are secure and compatible, and build the executors and integrations needed. This is the same challenge a bunch of us had working at Red Hat, but there we had entire teams, while this is handfuls of humans.

In a few cases we've ended up as a development partner, where it's made sense to make this a commercial relationship given the expertise of the team, but we can't scale that too far. We're a small team and there's so few people on the planet right now with quantum computing and enterprise experience. I've recently joined Unitary Foundation as a Fellow, and am trying to solve this on both sides of commercial/community before we all get flooded.

So the extremes of the dilemma are, do we fix all the issues in open sources projects and maintain our own forks, or do we drop a heavy round of PRs/Issues. Or is there a middle-ground we can do as vendors prior to releases? Lots of questions, happy to chat off-list (this will be a dinner conversation at SC26 as we're all struggling with it).

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants