Skip to content

Fix negative sign in circuit extraction strings - #645

Merged
thierry-martinez merged 4 commits into
TeamGraphix:masterfrom
pmitos:codex/fix-negative-pauli-input-sign
Nov 5, 2026
Merged

thierry-martinez merged 4 commits into
TeamGraphix:masterfrom
pmitos:codex/fix-negative-pauli-input-sign

Conversation

@pmitos

@pmitos pmitos commented Oct 29, 2026

Copy link
Copy Markdown
Contributor

Context (if applicable):
For inputs measured as (-X) or (-Y), circuit extraction produces the wrong sign in the extraction string. For example, two connected vertices with a (-X) input and an output produce circuit HY instead of HZ. If the input is instead represented as XY(1), the result is correct.

Description of the change:
Exclude the corrected vertex itself when counting the negative Pauli measurements. Add a small regression test and a changelog entry.

Related issue:
None

pmitos and others added 2 commits October 29, 2026 16:38
Exclude the corrected vertex when absorbing negative Pauli measurement signs,
since it remains unprojected in its own extraction string. Add the two-node
negative-X regression that implements HZ.

Agent: Codex
Requested-by: pmitos
Co-authored-by: Codex <noreply@openai.com>
Add the Unreleased changelog entry requested by the pull request template.

Agent: Codex
Requested-by: pmitos
Co-authored-by: Codex <noreply@openai.com>
@codecov

codecov Bot commented Nov 1, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.96%. Comparing base (790c6a2) to head (a2a4fad).

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #645   +/-   ##
=======================================
  Coverage   89.96%   89.96%           
=======================================
  Files          49       49           
  Lines        8462     8462           
=======================================
  Hits         7613     7613           
  Misses        849      849           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@matulni matulni 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.

Very nice, thank you very much for the bug-fix @pmitos !

I just made a comment suggesting to extend the test, otherwise it looks good to merge to me (the policy of the repository is two approvals to merge).

I have a question, though. The contribution to the phase was calculated following the formula

$$ c = b + |(p(v) \cup \text{Odd}[p(v)]) \cap \lbrace u \in \overline{O} | \lambda(u) \in \lbrace X, Y, Z\rbrace \land \alpha(u) = \pi \rbrace |,$$

defined right after Eq. 35 in Simmons 21. The loop

 for n in (c_set | odd_c_set):

corresponded to the term $(p(v) \cup \text{Odd}[p(v)])$. Is it obvious that we shouldn't iterate over $v$ (hence your fix - {node}), or is there a mistake in the equation ?

Comment thread tests/test_circ_extraction.py Outdated
Comment on lines +440 to +451
def test_extract_negative_x_input(self) -> None:
og = OpenGraph(
graph=nx.path_graph(2),
input_nodes=[0],
output_nodes=[1],
measurements={0: -Measurement.X},
)
clifford_map = og.to_pauliflow().extract_circuit().clifford_map

# The outcome-zero branch implements HZ.
assert clifford_map.x_map == (PauliString.from_str("-Z"),)
assert clifford_map.z_map == (PauliString.from_str("+X"),)

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.

I suggest adding both previously failing cases -Y and -X. For completeness, I also suggest adding the full round-trip simulation check, that is, comparing the output from simulating OpenGraph -> CausalFlow -> Pattern and OpenGraph -> PauliFlow -> Circuit.

The code below requires
from graphix.random_objects import rand_state_vector (on L19)
and
from graphix.measurements import PauliMeasurement (under the TYPE_CHECKING block, L25)

Suggested change
def test_extract_negative_x_input(self) -> None:
og = OpenGraph(
graph=nx.path_graph(2),
input_nodes=[0],
output_nodes=[1],
measurements={0: -Measurement.X},
)
clifford_map = og.to_pauliflow().extract_circuit().clifford_map
# The outcome-zero branch implements HZ.
assert clifford_map.x_map == (PauliString.from_str("-Z"),)
assert clifford_map.z_map == (PauliString.from_str("+X"),)
@pytest.mark.parametrize(
("meas", "x_map"),
[
(-Measurement.X, (PauliString.from_str("-Z"),)), # The outcome-zero branch implements HZ.
(-Measurement.Y, (PauliString.from_str("-Y"),)), # The outcome-zero branch implements HY.
],
)
def test_extract_negative_x_input(
self, meas: PauliMeasurement, x_map: tuple[PauliString, ...], fx_rng: Generator
) -> None:
og = OpenGraph(
graph=nx.path_graph(2),
input_nodes=[0],
output_nodes=[1],
measurements={0: meas},
)
clifford_map = og.to_pauliflow().extract_circuit().clifford_map
assert clifford_map.x_map == x_map
assert clifford_map.z_map == (PauliString.from_str("+X"),)
input_state = rand_state_vector(nqubits=1, rng=fx_rng)
state_qc = og.to_circuit().simulate(rng=fx_rng, input_state=input_state).state
state_pattern = og.simulate(rng=fx_rng, input_state=input_state)
assert state_qc.isclose(state_pattern)

…nput measurements.

Agent: Codex
Requested-by: pmitos
Co-authored-by: Codex <noreply@openai.com>
@pmitos

pmitos commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor Author

Hello @matulni, and thanks for the review.

Yes, the equation for c after (35) in Simmons should instead read:

$$ c = b + |(p(v) \cup \text{Odd}[p(v)]) \cap \lbrace u \in \overline{O}\setminus \lbrace v \rbrace | \lambda(u) \in \lbrace X, Y, Z\rbrace \land \alpha(u) = \pi \rbrace | $$

i.e., omitting v itself when counting negative Pauli measurements. This follows from Definition 4.2, which doesn't project v.

Updated the code as suggested, with some renaming since we no longer only cover (-X). One small thing: for (-Y), your Pauli images are correct, but they correspond to HS instead of HY.

@thierry-martinez thierry-martinez left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thank you very much, @pmitos. I would just suggest adding a comment to document this discussion in the code.

Comment thread graphix/circ_ext/extraction.py Resolved
Co-authored-by: thierry-martinez <thierry.martinez@inria.fr>
@thierry-martinez
thierry-martinez merged commit 62bc3af into TeamGraphix:master Nov 5, 2026
24 checks passed
@pmitos
pmitos deleted the codex/fix-negative-pauli-input-sign branch November 5, 2026 13:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants