Skip to content

Add measure propagation rules#369

Merged
joshuasn merged 20 commits into
mainfrom
measure-twirl
Jun 17, 2026
Merged

Add measure propagation rules#369
joshuasn merged 20 commits into
mainfrom
measure-twirl

Conversation

@joshuasn

Copy link
Copy Markdown
Collaborator

Summary

This PR refactors how measure is treated during the samplex build process. Rather than using its own dedicated mechanism, this approach treats the measure as a propagation node.

Details and comments

Some Opus + Sonnet 4.6.

joshuasn and others added 14 commits May 20, 2026 09:05
Add infrastructure for propagating virtual Paulis through measurements:
- PreMeasurePropagate node type in pre_samplex graph
- Z2->Pauli conversion in z2_register
- add_measure_propagate() method on PreSamplex
- Lowering logic (Fork A: Z2 output, Fork B: continued Pauli)
- Twirl emission moved to parse(None) in LeftBoxBuilder with deferred
  leftward connection via connect_emit_leftward()

Current state: the deferred leftward connection works correctly (same
graph topology as original for the left collector). However, emitting
the twirl's rightward dangler at parse(None) causes hard gates to
propagate it rightward, changing what the right collector receives.
This causes 52 integration test failures. The fundamental tension is
that measurements need the rightward dangler to exist during parse(),
but hard gates should NOT propagate it rightward (they should only
create leftward propagation nodes for the adjacent right box).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@joshuasn joshuasn marked this pull request as ready for review June 2, 2026 14:59

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

Thanks for the PR, @joshuasn !

I'd love to see some more integration tests, it seems like we could add some neat ones.

Also, I'm not sure what's going on with the node serializer change, and I'm wondering if that should be pulled out into a separate PR?

Comment thread changelog.d/369.changed.md Outdated
"new_name": obj.new_name,
"new_type": obj.new_type.value,
"num_subsystems": obj.num_subsystems,
"num_subsystems": str(obj.num_subsystems),

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.

Is this a bugfix? If so, it should receive a changelog entry. Also, how was it working before??

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Changed the lowering rule to use SliceRegisterNode for now, bugfix to follow.

assert samplex == samplex_new

@pytest.mark.parametrize("ssv", SUPPORTED_SSVS)
@pytest.mark.parametrize("ssv", [s for s in SUPPORTED_SSVS if s >= 3])

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.

This change and the one below it are suspicious. They make it look like this PR breaks serialization for ssv=1 and ssv=2.

Comment thread samplomatic/virtual_registers/z2_register.py
Comment thread test/integration/test_measurement_twirling.py
style = super().get_style().append_data("Direction", self.direction.name)
style.marker = "bowtie"
style.color = "purple"
def to_key(self) -> Hashable:

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.

Can you remind me who uses it? I can't help but notice the other ones includes things like direction, and I'm wondering if that's relevant here

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

merge_parallel_pre_propagate_nodes and replaced it with a merge_parallel_nodes method that acts on all the nodes of a given type. Direction isn't relevant for measure because it only propagates rightwards. The other attributes are lists of names and offsets which we simply combine then the lowering handles dispatching to separate collect nodes.

We could also rewrite the merging so each pre-measure has one register name.


@classmethod
def from_cluster(cls, nodes: list["PreMeasure"]) -> "PreMeasure":
combined_subsystems = QubitIndicesPartition.union(*(n.subsystems for n in nodes))

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.

.union is order preserving?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

yeah for QubitPartition

Comment thread samplomatic/pre_samplex/graph_data.py
Comment thread samplomatic/pre_samplex/graph_data.py
Comment thread samplomatic/pre_samplex/graph_data.py
Comment thread changelog.d/369.added.md Outdated
Comment thread test/unit/test_serialization/test_samplex_serialization.py Outdated
Comment thread test/unit/test_serialization/test_samplex_serialization.py Outdated
Comment thread changelog.d/369.improved.md Outdated
Comment thread changelog.d/369.changed.md Outdated
@@ -0,0 +1,2 @@
Measurement twirling is now implemented by treating measure operations as virtual gate propagators. Previously, measurement twirling was implemented by collecting bit flips associated with virtual gates exclusively in the right hand side of a left-dressed box. The new approach allows for more flexible measurement twirling, e.g., sequential measurements of the same qubit to different clbits or measurements interwoven with other operations in the same box.

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.

It looks like the changes also remove " measurements interwoven with other operations in the same box". Is that because this is not new behaviour? I see for example this existing integration test:

    def test_gates_and_measure_all(self, save_plot):
        circuit = QuantumCircuit(3)
        with circuit.box([Twirl(dressing="left")]):
            circuit.x(0)
            circuit.h(1)
            circuit.cx(0, 1)
            circuit.measure_all()
        sample_simulate_and_compare_counts(circuit, save_plot)

@joshuasn joshuasn requested a review from ihincks June 17, 2026 12:57
from samplomatic.ssv import SSV

SUPPORTED_SSVS = set(range(1, SSV))
SUPPORTED_SSVS = set(range(1, SSV + 1))

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.

good catch, ty

Comment on lines +103 to +115
with circuit.box([Twirl(dressing="right")]):
circuit.noop(range(2))

_, samplex = build(circuit)
samplex_new = samplex_from_json(samplex_to_json(samplex, ssv=ssv))
samplex_new.finalize()

assert samplex == samplex_new

@pytest.mark.parametrize("ssv", SUPPORTED_SSVS)
def test_measure_twirl_circuit(self, ssv):
"""Test a circuit with measurement twirling."""
circuit = QuantumCircuit(2)

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.

Okay, one last question on the serialization: can we now just undo all changes to these tests that this PR makes (aside from the fix SSV -> SSV+1)? I'm not sure I understand the benefit of splitting this into two cases, one with measurements, and one without, unless perhaps it exercises more nodes in the serial format?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I undid. It's not necessary anymore, it to ensure ChangeBasis circuits had tests in all SSVs with the restricted measure twirl SSVs.

@joshuasn joshuasn merged commit bbd6efd into main Jun 17, 2026
8 checks passed
@joshuasn joshuasn deleted the measure-twirl branch June 17, 2026 14:28
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.

2 participants