From 5cc8c8c0a4d96e772308fbb1522834c2727f9aa2 Mon Sep 17 00:00:00 2001 From: mhucka Date: Tue, 12 May 2026 03:14:07 +0000 Subject: [PATCH 1/2] Fix #418: add tests for `padded_to_ragged2d` This adds some simple tests for an untested function in `tfq_utility_ops_test.py`. --- .../core/ops/tfq_utility_ops_test.py | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tensorflow_quantum/core/ops/tfq_utility_ops_test.py b/tensorflow_quantum/core/ops/tfq_utility_ops_test.py index 613704fe2..d383006cb 100644 --- a/tensorflow_quantum/core/ops/tfq_utility_ops_test.py +++ b/tensorflow_quantum/core/ops/tfq_utility_ops_test.py @@ -139,6 +139,12 @@ def test_append_circuit(self, max_n_bits, symbols, n_circuits): 'padded_array': [[[0, 0, 0, 0], [1, 1, 1, 1]]] }, { 'padded_array': [[[1, 1, -2, -2], [0, 1, -2, -2], [0, 0, -2, -2]]] + }, { + 'padded_array': [[[-2, -2], [-2, -2]]] + }, { + 'padded_array': [[[1, 1], [1, 1]]] + }, { + 'padded_array': np.empty((0, 2, 2), dtype=np.float32) }]) def test_padded_to_ragged(self, padded_array): """Test for padded_to_ragged utility.""" @@ -148,6 +154,28 @@ def test_padded_to_ragged(self, padded_array): np.array(padded_array, dtype=float)) self.assertAllEqual(expected, actual) + @parameterized.parameters([{ + 'padded_array': [[[1, 0, -2], [0, 1, -2], [-2, -2, -2]], + [[1, -2, -2], [-2, -2, -2], [-2, -2, -2]]] + }, { + 'padded_array': [[[1, 0, 0], [0, 1, 0], [0, 0, 1]]] + }, { + 'padded_array': [[[-2, -2, -2], [-2, -2, -2], [-2, -2, -2]]] + }, { + 'padded_array': np.empty((0, 3, 3), dtype=np.float32) + }]) + def test_padded_to_ragged2d(self, padded_array): + """Test for padded_to_ragged2d utility.""" + tensor_arr = tf.constant(padded_array, dtype=tf.float32) + col_mask = tf.abs(tensor_arr[:, 0]) < 1.1 + masked = tf.ragged.boolean_mask(tensor_arr, col_mask) + mask = tf.abs(masked) < 1.1 + expected = tf.ragged.boolean_mask(masked, mask) + + actual = tfq_utility_ops.padded_to_ragged2d( + np.array(padded_array, dtype=float)) + self.assertAllEqual(expected, actual) + class ResolveParametersOpTest(tf.test.TestCase, parameterized.TestCase): """Test the in-graph parameter resolving op.""" From 5cb90d4dd9f6b7f0cd05fe6434419d028b7ee394 Mon Sep 17 00:00:00 2001 From: mhucka Date: Tue, 12 May 2026 03:20:56 +0000 Subject: [PATCH 2/2] Fix typo --- tensorflow_quantum/core/ops/tfq_utility_ops_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_quantum/core/ops/tfq_utility_ops_test.py b/tensorflow_quantum/core/ops/tfq_utility_ops_test.py index d383006cb..4eab0213b 100644 --- a/tensorflow_quantum/core/ops/tfq_utility_ops_test.py +++ b/tensorflow_quantum/core/ops/tfq_utility_ops_test.py @@ -93,7 +93,7 @@ def test_append_input_checking(self): def test_append_circuit(self, max_n_bits, symbols, n_circuits): """Generate a bunch of circuits of different lengths acting on different numbers of qubits and append them using our op, checking that results - are consistant with the native cirq method. + are consistent with the native cirq method. """ base_circuits = [] circuits_to_append = []