Add measure propagation rules#369
Conversation
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>
ihincks
left a comment
There was a problem hiding this comment.
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?
| "new_name": obj.new_name, | ||
| "new_type": obj.new_type.value, | ||
| "num_subsystems": obj.num_subsystems, | ||
| "num_subsystems": str(obj.num_subsystems), |
There was a problem hiding this comment.
Is this a bugfix? If so, it should receive a changelog entry. Also, how was it working before??
There was a problem hiding this comment.
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]) |
There was a problem hiding this comment.
This change and the one below it are suspicious. They make it look like this PR breaks serialization for ssv=1 and ssv=2.
| style = super().get_style().append_data("Direction", self.direction.name) | ||
| style.marker = "bowtie" | ||
| style.color = "purple" | ||
| def to_key(self) -> Hashable: |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
.union is order preserving?
There was a problem hiding this comment.
yeah for QubitPartition
| @@ -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. | |||
There was a problem hiding this comment.
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)| from samplomatic.ssv import SSV | ||
|
|
||
| SUPPORTED_SSVS = set(range(1, SSV)) | ||
| SUPPORTED_SSVS = set(range(1, SSV + 1)) |
| 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) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
I undid. It's not necessary anymore, it to ensure ChangeBasis circuits had tests in all SSVs with the restricted measure twirl SSVs.
Summary
This PR refactors how
measureis 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.