Skip to content

Commit d96a92f

Browse files
authored
Add .pylintrc, update dev-requirements.txt, and address some trivial Pylint warnings (#911)
This adds a `.pylintrc` file based on the one from Cirq, with a change to the line length (80 for this project) and removal of one of the checks because of a difference in conventions. It also adds `pylint` to `dev-requirements.txt` (using the same version that Cirq uses). Finally, it also adjusts a few Python files to address consequent linter warnings.
1 parent f01465c commit d96a92f

File tree

9 files changed

+172
-83
lines changed

9 files changed

+172
-83
lines changed

.pylintrc

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
[MAIN]
2+
load-plugins=pylint.extensions.docstyle,pylint.extensions.docparams
3+
max-line-length=88
4+
ignore-paths=eigen/*,tests/googletest/*
5+
disable=all
6+
output-format=colorized
7+
score=no
8+
reports=no
9+
enable=
10+
abstract-class-instantiated,
11+
anomalous-backslash-in-string,
12+
assert-on-tuple,
13+
assignment-from-no-return,
14+
bad-chained-comparison,
15+
bad-indentation,
16+
bad-mcs-classmethod-argument,
17+
bad-mcs-method-argument,
18+
bad-option-value,
19+
bad-reversed-sequence,
20+
bad-super-call,
21+
consider-merging-isinstance,
22+
consider-using-f-string,
23+
continue-in-finally,
24+
dangerous-default-value,
25+
deprecated-decorator,
26+
docstyle,
27+
duplicate-argument-name,
28+
expression-not-assigned,
29+
f-string-without-interpolation,
30+
function-redefined,
31+
inconsistent-mro,
32+
init-is-generator,
33+
line-too-long,
34+
lost-exception,
35+
method-hidden,
36+
missing-kwoa,
37+
missing-param-doc,
38+
missing-raises-doc,
39+
mixed-line-endings,
40+
no-value-for-parameter,
41+
nonexistent-operator,
42+
not-in-loop,
43+
pointless-statement,
44+
raising-bad-type,
45+
raising-format-tuple,
46+
redefined-builtin,
47+
redefined-slots-in-subclass,
48+
return-arg-in-generator,
49+
return-in-init,
50+
return-outside-function,
51+
self-cls-assignment,
52+
shadowed-import,
53+
simplifiable-condition,
54+
simplifiable-if-statement,
55+
singleton-comparison,
56+
too-many-function-args,
57+
trailing-whitespace,
58+
undefined-variable,
59+
unexpected-keyword-arg,
60+
unhashable-dict-key,
61+
unnecessary-pass,
62+
unnecessary-semicolon,
63+
unneeded-not,
64+
unreachable,
65+
unrecognized-inline-option,
66+
unused-import,
67+
unused-variable,
68+
unused-wildcard-import,
69+
use-maxsplit-arg,
70+
useless-suppression,
71+
using-constant-test,
72+
wildcard-import,
73+
wrong-import-order,
74+
wrong-import-position,
75+
yield-outside-function
76+
77+
# Ignore long lines containing urls or pylint directives.
78+
ignore-long-lines=^(.*#\w*pylint: disable.*|\s*(# )?[<\[\(]?https?://\S+[>\]\)]?)$

dev-requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
cmake~=3.28.1
22
black~=25.9.0
33
flynt~=1.0
4+
pylint~=4.0.2
45
pytest
56
pytest-xdist
67
py-cpuinfo

docs/_scripts/build_api_docs.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
from absl import app
1919
from absl import flags
20-
from tensorflow_docs.api_generator import doc_controls
2120
from tensorflow_docs.api_generator import generate_lib
2221
from tensorflow_docs.api_generator import public_api
2322

qsimcirq/__init__.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# pylint: disable=wrong-import-position
12
# Copyright 2019 Google LLC. All Rights Reserved.
23
#
34
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -13,6 +14,7 @@
1314
# limitations under the License.
1415

1516
import importlib
17+
1618
from qsimcirq import qsim_decide
1719

1820

@@ -53,10 +55,10 @@ def _load_qsim_custatevec():
5355
qsim_gpu = _load_qsim_gpu()
5456
qsim_custatevec = _load_qsim_custatevec()
5557

56-
from .qsim_circuit import add_op_to_opstring, add_op_to_circuit, QSimCircuit
58+
# Note: the following imports must remain at the bottom of this file.
59+
60+
from qsimcirq._version import __version__
61+
62+
from .qsim_circuit import QSimCircuit, add_op_to_circuit, add_op_to_opstring
5763
from .qsim_simulator import QSimOptions, QSimSimulator
5864
from .qsimh_simulator import QSimhSimulator
59-
60-
from qsimcirq._version import (
61-
__version__,
62-
)

qsimcirq/qsim_circuit.py

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import warnings
16-
from typing import Dict, List, Optional, Sequence, Tuple, Union
15+
from typing import Dict, List, Sequence, Tuple, Union
1716

1817
import cirq
1918
import numpy as np
@@ -159,7 +158,7 @@ def _translate_MatrixGate(gate: cirq.MatrixGate):
159158
return qsim.kMatrixGate
160159
raise NotImplementedError(
161160
f"Received matrix on {gate.num_qubits()} qubits; "
162-
+ "only up to 6-qubit gates are supported."
161+
"only up to 6-qubit gates are supported."
163162
)
164163

165164

@@ -219,17 +218,17 @@ def _control_details(
219218
assignments = list(gate.control_values.expand())
220219
if len(qubits) > 1 and len(assignments) > 1:
221220
raise ValueError(
222-
f"Cannot translate controlled gate with multiple assignments for multiple qubits: {gate}"
221+
"Cannot translate controlled gate with multiple assignments for "
222+
f"multiple qubits: {gate}"
223223
)
224224
for q, cvs in zip(qubits, zip(*assignments)):
225225
if 0 in cvs and 1 in cvs:
226226
# This qubit does not affect control.
227227
continue
228228
elif any(cv not in (0, 1) for cv in cvs):
229229
raise ValueError(
230-
f"Cannot translate control values other than 0 and 1: cvs={cvs}"
231-
)
232-
# Either 0 or 1 is in cvs, but not both.
230+
f"Cannot translate control values other than 0 and 1: {cvs=}"
231+
) # Either 0 or 1 is in cvs, but not both.
233232
control_qubits.append(q)
234233
if 0 in cvs:
235234
control_values.append(0)
@@ -247,7 +246,7 @@ def add_op_to_opstring(
247246
"""Adds an operation to an opstring (observable).
248247
249248
Raises:
250-
ValueError if qsim_op is not a single-qubit Pauli (I, X, Y, or Z).
249+
ValueError if qsim_op is not a single-qubit Pauli (I, X, Y, or Z).
251250
"""
252251
qsim_gate = qsim_op.gate
253252
gate_kind = _cirq_gate_kind(qsim_gate)
@@ -258,7 +257,7 @@ def add_op_to_opstring(
258257

259258
is_controlled = isinstance(qsim_gate, cirq.ControlledGate)
260259
if is_controlled:
261-
raise ValueError(f"OpString ops should not be controlled.")
260+
raise ValueError("OpString ops should not be controlled.")
262261

263262
qubits = [qubit_to_index_dict[q] for q in qsim_op.qubits]
264263
qsim.add_gate_to_opstring(gate_kind, qubits, opstring)
@@ -289,7 +288,7 @@ def add_op_to_circuit(
289288
if num_targets > 4:
290289
raise NotImplementedError(
291290
f"Received control gate on {num_targets} target qubits; "
292-
+ "only up to 4-qubit gates are supported."
291+
"only up to 4-qubit gates are supported."
293292
)
294293

295294
qsim_qubits = qubits[qsim_gate.num_controls() :]
@@ -366,8 +365,10 @@ def _resolve_parameters_(
366365

367366
def _check_for_confusion_matrix(self):
368367
"""Checks cirq Circuit for Measurement Gates with confusion matrices.
368+
369369
Returns:
370-
Throws a runtime exception if a MeasurementGate with a confusion matrix is included in the circuit
370+
Throws a runtime exception if a MeasurementGate with a confusion
371+
matrix is included in the circuit.
371372
"""
372373
confusion_maps_on_measurement_gates = [
373374
op.gate.confusion_map
@@ -377,14 +378,14 @@ def _check_for_confusion_matrix(self):
377378
if confusion_maps_on_measurement_gates:
378379
raise ValueError(
379380
"Confusion Matrices are not currently supported in Qsim. "
380-
"See https://github.com/quantumlib/Cirq/issues/6305 for latest status"
381+
"See https://github.com/quantumlib/Cirq/issues/6305 "
382+
"for latest status"
381383
)
382384

383385
def translate_cirq_to_qsim(
384386
self, qubit_order: cirq.QubitOrderOrList = cirq.QubitOrder.DEFAULT
385387
) -> qsim.Circuit:
386-
"""
387-
Translates this Cirq circuit to the qsim representation.
388+
"""Translates this Cirq circuit to the qsim representation.
388389
:qubit_order: Ordering of qubits
389390
:return: a tuple of (C++ qsim Circuit object, moment boundary
390391
gate indices)
@@ -436,8 +437,7 @@ def to_matrix(op: cirq.GateOperation):
436437
def translate_cirq_to_qtrajectory(
437438
self, qubit_order: cirq.QubitOrderOrList = cirq.QubitOrder.DEFAULT
438439
) -> qsim.NoisyCircuit:
439-
"""
440-
Translates this noisy Cirq circuit to the qsim representation.
440+
"""Translates this noisy Cirq circuit to the qsim representation.
441441
:qubit_order: Ordering of qubits
442442
:return: a tuple of (C++ qsim NoisyCircuit object, moment boundary
443443
gate indices)
@@ -476,15 +476,12 @@ def to_matrix(op: cirq.GateOperation):
476476
)
477477
ops_by_gate.append(oplist)
478478
moment_length = max(moment_length, len(oplist))
479-
pass
480479
elif cirq.has_mixture(qsim_op):
481480
ops_by_mix.append(qsim_op)
482481
moment_length = max(moment_length, 1)
483-
pass
484482
elif cirq.has_kraus(qsim_op):
485483
ops_by_channel.append(qsim_op)
486484
moment_length = max(moment_length, 1)
487-
pass
488485
else:
489486
raise ValueError(f"Encountered unparseable op: {qsim_op}")
490487

0 commit comments

Comments
 (0)