From 76f9206edb03b4f717d90cf246066198f689e26a Mon Sep 17 00:00:00 2001 From: Arnav Nagzirkar <113314200+arnavnagzirkar@users.noreply.github.com> Date: Tue, 2 Jun 2026 22:46:55 -0700 Subject: [PATCH 1/2] test: cover random circuits with channels --- tensorflow_quantum/python/util_test.py | 45 ++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/tensorflow_quantum/python/util_test.py b/tensorflow_quantum/python/util_test.py index d22c118a8..3a062f85c 100644 --- a/tensorflow_quantum/python/util_test.py +++ b/tensorflow_quantum/python/util_test.py @@ -139,6 +139,51 @@ def test_random_symbol_circuit_resolver_batch_shapes_and_types( isinstance(value, float) for value in resolver.param_dict.values())) + def test_random_circuit_resolver_batch_channels_present(self): + """Confirm channel ops appear in circuits when include_channels=True.""" + qubits = cirq.GridQubit.rect(1, 3) + channel_types = tuple(type(c) for c in util.get_supported_channels()) + + circuits, _ = util.random_circuit_resolver_batch(qubits, + batch_size=5, + n_moments=20, + include_channels=True) + + def has_channel(circuit): + return any( + isinstance(op.gate, channel_types) + for moment in circuit + for op in moment.operations) + + self.assertTrue( + any(has_channel(c) for c in circuits), + "Expected at least one channel operation across batch when " + "include_channels=True") + # Verify circuits containing channels are serializable. + self.assertIsNotNone(util.convert_to_tensor(circuits)) + + def test_random_symbol_circuit_resolver_batch_channels_present(self): + """Confirm channel ops appear in circuits when include_channels=True.""" + qubits = cirq.GridQubit.rect(1, 3) + symbols = ['alpha', 'beta'] + channel_types = tuple(type(c) for c in util.get_supported_channels()) + + circuits, _ = util.random_symbol_circuit_resolver_batch( + qubits, symbols, batch_size=5, n_moments=20, include_channels=True) + + def has_channel(circuit): + return any( + isinstance(op.gate, channel_types) + for moment in circuit + for op in moment.operations) + + self.assertTrue( + any(has_channel(c) for c in circuits), + "Expected at least one channel operation across batch when " + "include_channels=True") + # Verify circuits containing channels are serializable. + self.assertIsNotNone(util.convert_to_tensor(circuits)) + @parameterized.parameters(_items_to_tensorize()) def test_convert_to_tensor(self, item): """Test that the convert_to_tensor function works correctly by manually From 9b1f9a81691e452aeca2cad945b958c7c5862617 Mon Sep 17 00:00:00 2001 From: Arnav Nagzirkar <113314200+arnavnagzirkar@users.noreply.github.com> Date: Tue, 2 Jun 2026 23:05:07 -0700 Subject: [PATCH 2/2] test: round-trip serialize channel circuits Address review feedback: verify channel-containing circuits survive a full convert_to_tensor/from_tensor round-trip instead of only asserting non-null serialization. --- tensorflow_quantum/python/util_test.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/tensorflow_quantum/python/util_test.py b/tensorflow_quantum/python/util_test.py index 3a062f85c..d198be69e 100644 --- a/tensorflow_quantum/python/util_test.py +++ b/tensorflow_quantum/python/util_test.py @@ -159,8 +159,15 @@ def has_channel(circuit): any(has_channel(c) for c in circuits), "Expected at least one channel operation across batch when " "include_channels=True") - # Verify circuits containing channels are serializable. - self.assertIsNotNone(util.convert_to_tensor(circuits)) + # Verify circuits containing channels are correctly serializable + # round-trip. + serialized = util.convert_to_tensor(circuits, + deterministic_proto_serialize=True) + deserialized = util.from_tensor(serialized) + self.assertAllEqual( + serialized, + util.convert_to_tensor(deserialized, + deterministic_proto_serialize=True)) def test_random_symbol_circuit_resolver_batch_channels_present(self): """Confirm channel ops appear in circuits when include_channels=True.""" @@ -181,8 +188,15 @@ def has_channel(circuit): any(has_channel(c) for c in circuits), "Expected at least one channel operation across batch when " "include_channels=True") - # Verify circuits containing channels are serializable. - self.assertIsNotNone(util.convert_to_tensor(circuits)) + # Verify circuits containing channels are correctly serializable + # round-trip. + serialized = util.convert_to_tensor(circuits, + deterministic_proto_serialize=True) + deserialized = util.from_tensor(serialized) + self.assertAllEqual( + serialized, + util.convert_to_tensor(deserialized, + deterministic_proto_serialize=True)) @parameterized.parameters(_items_to_tensorize()) def test_convert_to_tensor(self, item):