Skip to content

Commit bf4ace8

Browse files
authored
Fix pylint errors in benchmark directory (#991)
This PR addresses various Pylint errors in the `benchmarks/` directory to ensure CI compliance and improve code quality. The changes include fixing import paths, enforcing naming conventions, and modernizing Python syntax. - Fix Imports: - Converted relative imports to absolute imports (e.g., benchmarks.scripts...) and resolved test_log_pb2 import issues. Grouped imports to fix wrong-import-order and ungrouped-imports errors. - Style & Formatting: - Renamed `TEST_FLAGS` to `test_flags` and converted test method names to snake_case (e.g., test_benchmark_gradient) to comply with PEP8 naming conventions. - Wrapped long lines and docstrings to satisfy the 80-character line length limit. - Documentation: - Added missing module and function docstrings. - Refactoring: - Updated super() calls to the cleaner Python 3 syntax (super().__init__()).
1 parent cfb1873 commit bf4ace8

8 files changed

Lines changed: 43 additions & 27 deletions

benchmarks/scripts/benchmark_clifford_circuit.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,17 @@
2323

2424
from tensorflow_quantum.core.ops import tfq_simulate_ops
2525
from tensorflow_quantum.core.serialize.serializer import serialize_circuit
26-
from models.random_clifford_circuit import random_clifford_circuit
27-
import flags
28-
import benchmark_util
26+
from benchmarks.scripts.models.random_clifford_circuit import (
27+
random_clifford_circuit)
28+
29+
from benchmarks.scripts import flags
30+
from benchmarks.scripts import benchmark_util
2931

3032
SEED = 48510234
3133
SRC = os.path.dirname(os.path.realpath(__file__))
3234
os.environ['TEST_REPORT_FILE_PREFIX'] = os.path.join(SRC, 'reports/')
33-
TEST_PARAMS_1 = flags.TEST_FLAGS(n_qubits=3, n_moments=5, op_density=0.99)
34-
TEST_PARAMS_2 = flags.TEST_FLAGS(n_qubits=4, n_moments=5, op_density=0.99)
35+
TEST_PARAMS_1 = flags.test_flags(n_qubits=3, n_moments=5, op_density=0.99)
36+
TEST_PARAMS_2 = flags.test_flags(n_qubits=4, n_moments=5, op_density=0.99)
3537
ALL_PARAMS = [TEST_PARAMS_1, TEST_PARAMS_2]
3638

3739

@@ -42,7 +44,7 @@ class CliffordBenchmarksTest(tf.test.TestCase, parameterized.TestCase):
4244
("params_1", TEST_PARAMS_1),
4345
("params_2", TEST_PARAMS_2),
4446
)
45-
def testBenchmarkCliffordCircuitEager(self, params):
47+
def test_benchmark_clifford_circuit_eager(self, params):
4648
"""Test that Op constructs and runs correctly."""
4749
proto_file_path = os.path.join(
4850
SRC, "reports/",
@@ -77,7 +79,7 @@ class CliffordBenchmarks(tf.test.Benchmark):
7779

7880
def __init__(self, params=None):
7981
"""Pull in command line flags or use provided flags."""
80-
super(CliffordBenchmarks, self).__init__()
82+
super().__init__()
8183
# Allow input params for testing purposes.
8284
self.params = params if params else flags.FLAGS
8385

benchmarks/scripts/benchmark_op_gradients.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,23 @@
2323
import numpy as np
2424

2525
from tensorflow_quantum.core.ops import tfq_simulate_ops
26-
import benchmark_util
27-
import flags
2826

2927
from tensorflow_quantum.python import util
3028
from tensorflow_quantum.python.differentiators import (
3129
linear_combination,
3230
parameter_shift,
3331
)
3432

33+
from benchmarks.scripts import benchmark_util
34+
from benchmarks.scripts import flags
35+
3536
SRC = os.path.dirname(os.path.realpath(__file__))
3637
os.environ['TEST_REPORT_FILE_PREFIX'] = os.path.join(SRC, 'reports/')
37-
TEST_PARAMS_1 = flags.TEST_FLAGS(n_symbols=4,
38+
TEST_PARAMS_1 = flags.test_flags(n_symbols=4,
3839
n_qubits=3,
3940
n_moments=5,
4041
op_density=0.9)
41-
TEST_PARAMS_2 = flags.TEST_FLAGS(n_symbols=3,
42+
TEST_PARAMS_2 = flags.test_flags(n_symbols=3,
4243
n_qubits=4,
4344
n_moments=5,
4445
op_density=0.6)
@@ -58,7 +59,7 @@ class GradientBenchmarksTest(tf.test.TestCase, parameterized.TestCase):
5859
],
5960
'params': [TEST_PARAMS_1, TEST_PARAMS_2]
6061
})))
61-
def testBenchmarkGradient(self, diff, params):
62+
def test_benchmark_gradient(self, diff, params):
6263
"""Test that op constructs and runs correctly."""
6364

6465
bench_name = "GradientBenchmarks.{}_{}_{}_{}_{}".format(
@@ -93,7 +94,7 @@ class GradientBenchmarks(tf.test.Benchmark):
9394

9495
def __init__(self, params=None):
9596
"""Pull in command line flags or use provided flags."""
96-
super(GradientBenchmarks, self).__init__()
97+
super().__init__()
9798
self.params = params if params else flags.FLAGS
9899
self.setup()
99100

@@ -110,7 +111,8 @@ def setup(self):
110111
replace=True)))
111112
symbol_names = list(symbol_names)
112113

113-
circuit_batch, resolver_batch = util.random_symbol_circuit_resolver_batch(
114+
circuit_batch, resolver_batch = \
115+
util.random_symbol_circuit_resolver_batch(
114116
qubits=qubits,
115117
symbols=symbol_names,
116118
batch_size=self.params.batch_size,

benchmarks/scripts/benchmark_random_circuit.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@
2323

2424
from tensorflow_quantum.core.ops import tfq_simulate_ops
2525
from tensorflow_quantum.core.serialize.serializer import serialize_circuit
26-
import flags
27-
import benchmark_util
26+
from benchmarks.scripts import flags
27+
from benchmarks.scripts import benchmark_util
2828

2929
SEED = 63536323
3030
SRC = os.path.dirname(os.path.realpath(__file__))
3131
os.environ['TEST_REPORT_FILE_PREFIX'] = os.path.join(SRC, 'reports/')
32-
TEST_PARAMS_1 = flags.TEST_FLAGS(n_rows=3, n_cols=5, n_moments=5)
33-
TEST_PARAMS_2 = flags.TEST_FLAGS(n_rows=4, n_cols=4, n_moments=20)
32+
TEST_PARAMS_1 = flags.test_flags(n_rows=3, n_cols=5, n_moments=5)
33+
TEST_PARAMS_2 = flags.test_flags(n_rows=4, n_cols=4, n_moments=20)
3434

3535

3636
def make_random_circuit(n_rows, n_cols, depth):
@@ -49,7 +49,7 @@ class RandomCircuitBenchmarksTest(tf.test.TestCase, parameterized.TestCase):
4949
("params_1", TEST_PARAMS_1),
5050
("params_2", TEST_PARAMS_2),
5151
)
52-
def testBenchmarkRandomCircuit(self, params):
52+
def test_benchmark_random_circuit(self, params):
5353
"""Test that Op constructs and runs correctly."""
5454
proto_file_path = os.path.join(
5555
SRC, "reports/",
@@ -77,7 +77,7 @@ def testBenchmarkRandomCircuit(self, params):
7777
("params_1", TEST_PARAMS_1),
7878
("params_2", TEST_PARAMS_2),
7979
)
80-
def testRandomCircuitParams(self, params):
80+
def test_random_circuit_params(self, params):
8181
"""Ensure that the random circuits are structured as advertised."""
8282
circuit = make_random_circuit(params.n_rows, params.n_cols,
8383
params.n_moments)
@@ -95,7 +95,7 @@ class RandomCircuitBenchmarks(tf.test.Benchmark):
9595

9696
def __init__(self, params=None):
9797
"""Pull in command line flags or use provided flags."""
98-
super(RandomCircuitBenchmarks, self).__init__()
98+
super().__init__()
9999
# Allow input params for testing purposes.
100100
self.params = params if params else flags.FLAGS
101101

@@ -106,7 +106,8 @@ def _simulate_circuit(self, circuit, params):
106106
[[0]] * params.batch_size)
107107

108108
def benchmark_random_circuit(self):
109-
"""Benchmark simulator performance on a classically intractable circuit."""
109+
"""Benchmark simulator performance on
110+
a classically intractable circuit."""
110111

111112
circuit = make_random_circuit(self.params.n_rows, self.params.n_cols,
112113
self.params.n_moments)

benchmarks/scripts/benchmark_util.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,18 @@
1414
# ==============================================================================
1515
"""Utility functions for benchmark tools."""
1616
import tensorflow as tf
17-
import test_log_pb2
17+
from tensorflow.core.util import test_log_pb2
1818

1919

2020
def read_benchmark_entry(f):
21+
"""Reads a benchmark entry from a file.
22+
23+
Args:
24+
f: File path to read from.
25+
26+
Returns:
27+
The first entry in the benchmark file.
28+
"""
2129
s = tf.io.gfile.GFile(f, "rb").read()
2230
entries = test_log_pb2.BenchmarkEntries.FromString(s)
2331
return entries.entry[0]

benchmarks/scripts/benchmark_util_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818

1919
import tensorflow as tf
2020

21-
import test_log_pb2
22-
import benchmark_util
21+
from tensorflow.core.util import test_log_pb2
22+
from benchmarks.scripts import benchmark_util
2323

2424

2525
def _make_dummy_benchmark_report():

benchmarks/scripts/flags.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
lower_bound=0)
8585

8686

87-
def TEST_FLAGS(**kwargs):
87+
def test_flags(**kwargs):
8888
"""Create a set of test flags by kwarg assignment.
8989
9090
This constructs a named tuple that mimics the interface of absl.flags.

benchmarks/scripts/models/random_clifford_circuit.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
# ==============================================================================
15+
"""Module for generating random Clifford circuits."""
1516

1617
from typing import Iterable
1718

benchmarks/scripts/models/random_clifford_circuit_test.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
# ==============================================================================
15+
"""Tests for the random Clifford circuit generator."""
1516

1617
from absl.testing import parameterized
1718
import cirq
1819
import numpy as np
1920
import tensorflow as tf
2021

21-
from random_clifford_circuit import random_clifford_circuit
22+
from benchmarks.scripts.models.random_clifford_circuit import (
23+
random_clifford_circuit)
2224

2325

2426
class RandomCliffordCircuitTest(parameterized.TestCase, tf.test.TestCase):

0 commit comments

Comments
 (0)