diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 3df3f8bb..08e2e61d 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -18,14 +18,16 @@ repos: - id: trailing-whitespace - id: requirements-txt-fixer - id: sort-simple-yaml + - repo: https://gitee.com/mirrors_PyCQA/pylint rev: v3.3.1 hooks: - id: pylint name: pylint-strict - exclude: (test_.*\.py)$ + exclude: (.*_test\.py)$ args: [--score=n, --load-plugins=pylint_secure_coding_standard] - additional_dependencies: [pylint-secure-coding-standard, numpy, pytest, matplotlib, scipy, torch] + additional_dependencies: [pylint-secure-coding-standard, numpy, pytest, matplotlib, scipy, torch, requests] + - repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks.git rev: v2.14.0 hooks: diff --git a/pyproject.toml b/pyproject.toml index 78d6ea97..c5837e0e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -35,6 +35,11 @@ skip-string-normalization = true max-line-length = 120 +[tool.pylint] +# TODO:disable doc related checks, should be remove in the future: "C0114", "R0801", "C0116" +disable = ["W0511", "R0914", "C0114", "R0801", "C0116"] +ignore = ["conftest.py", "doc/run.py", "doc/source/conf.py", "setup.py"] + [tool.isort] profile = "black" diff --git a/quafu/elements/quantum_gate.py b/quafu/elements/quantum_gate.py index 39688a1a..38380d13 100644 --- a/quafu/elements/quantum_gate.py +++ b/quafu/elements/quantum_gate.py @@ -91,7 +91,12 @@ def __str__(self): properties_values = [getattr(self, x) for x in properties_names] return "%s:\n%s" % ( self.__class__.__name__, - "\n".join([f"{x} = {repr(properties_values[i])}" for i, x in enumerate(properties_names)]), + "\n".join( + [ + f"{x} = {repr(properties_values[i])}" + for i, x in enumerate(properties_names) + ] + ), ) def __repr__(self): @@ -157,7 +162,11 @@ def symbol(self) -> str: """Symbol used in text-drawing.""" # TODO: Use latex repr for Parameter if len(self.paras) > 0: - return "%s(" % self.name + ",".join(["%.3f" % para for para in self._paras]) + ")" + return ( + "%s(" % self.name + + ",".join(["%.3f" % para for para in self._paras]) + + ")" + ) return "%s" % self.name @symbol.setter @@ -188,7 +197,11 @@ def to_qasm(self, with_para=False) -> str: qstr = "%s" % self.name.lower() if self.paras: if with_para: - qstr += "(" + ",".join(["%s" % handle_expression(para) for para in self.paras]) + ")" + qstr += ( + "(" + + ",".join(["%s" % handle_expression(para) for para in self.paras]) + + ")" + ) else: qstr += "(" + ",".join(["%s" % para for para in self._paras]) + ")" qstr += " " @@ -242,7 +255,9 @@ def power(self, n) -> "QuantumGate": name = self.name + "^%d" % n raw_matrix = self._raw_matrix if isinstance(self._raw_matrix, Callable): - raw_matrix = lambda paras: np.linalg.matrix_power(self._raw_matrix(paras), n) + raw_matrix = lambda paras: np.linalg.matrix_power( + self._raw_matrix(paras), n + ) else: raw_matrix = np.linalg.matrix_power(self._raw_matrix, n) return QuantumGate(name, self.pos, self.paras, raw_matrix) @@ -362,7 +377,9 @@ def power(self, n) -> "ControlledGate": name = self.name + "^%d" % n targ_matrix = self._targ_matrix if isinstance(self._targ_matrix, Callable): - targ_matrix = lambda paras: np.linalg.matrix_power(self._targ_matrix(paras), n) + targ_matrix = lambda paras: np.linalg.matrix_power( + self._targ_matrix(paras), n + ) else: targ_matrix = np.linalg.matrix_power(self._targ_matrix, n) return ControlledGate( @@ -405,7 +422,11 @@ def dagger(self) -> "ControlledGate": @property def symbol(self): if len(self.paras) > 0: - return "%s(" % self._targ_name + ",".join(["%.3f" % para for para in self._paras]) + ")" + return ( + "%s(" % self._targ_name + + ",".join(["%.3f" % para for para in self._paras]) + + ")" + ) return "%s" % self._targ_name @property @@ -441,7 +462,9 @@ class ControlledU(ControlledGate): def __init__(self, name, ctrls: List[int], U: QuantumGate): self.targ_gate = U targs = U.pos - super().__init__(name, U.name, ctrls, targs, U.paras, targ_matrix=self.targ_gate._raw_matrix) + super().__init__( + name, U.name, ctrls, targs, U.paras, targ_matrix=self.targ_gate._raw_matrix + ) class CircuitWrapper(QuantumGate): diff --git a/quafu/visualisation/circuitPlot.py b/quafu/visualisation/circuitPlot.py index cb341c57..bc1541c8 100644 --- a/quafu/visualisation/circuitPlot.py +++ b/quafu/visualisation/circuitPlot.py @@ -78,7 +78,7 @@ mc_gate_names = ["mcx", "mcy", "mcz"] operation_names = ["barrier", "delay"] - +# pylint: disable=too-few-public-methods class CircuitPlotManager: """ A class to manage the plotting of quantum circuits. @@ -143,7 +143,7 @@ def __init__(self, qc): self.depth = np.max(self.dorders) + 1 - for q, c in qc.measures.items(): + for q, _ in qc.measures.items(): self._proc_measure(self.depth - 1, q) # step2: initialize bit-label @@ -154,6 +154,7 @@ def __init__(self, qc): self.xs = np.arange(-3 / 2, self.depth + 3 / 2) self.ys = np.arange(-2, self.used_qbit_num + 1 / 2) + # pylint: disable=too-many-arguments def __call__( self, title=None, @@ -252,7 +253,7 @@ def _circuit_wires(self): """ plot horizontal circuit wires """ - for pos, y in self.used_qbit_y.items(): + for _, y in self.used_qbit_y.items(): x0 = self.xs[0] + 1 x1 = self.xs[-1] - 1 self._h_wire_points.append([[x0, y], [x1, y]]) @@ -295,7 +296,7 @@ def _measured_label(self, labels: Dict[int, str] = None): def _gate_bbox(self, x, y, fc: str): """Single qubit gate box""" a = self._a - from matplotlib.patches import FancyBboxPatch + from matplotlib.patches import FancyBboxPatch # pylint: disable=import-outside-toplevel bbox = FancyBboxPatch( (-a / 2 + x, -a / 2 + y), @@ -340,7 +341,9 @@ def _para_label(self, x, y, para_txt): self._text_list.append(text) def _measure_label(self, x, y): - from matplotlib.patches import FancyArrow + from matplotlib.patches import ( + FancyArrow, + ) # pylint: disable=import-outside-toplevel a = self._a r = 1.1 * a @@ -453,7 +456,7 @@ def _proc_ctrl(self, depth, ins: ControlledGate, ctrl_type: bool = True): elif name == "ccx": self._not_points.append((depth, self.used_qbit_y[ins.targs[0]])) else: - from quafu.elements.element_gates import ControlledU + from quafu.elements.quantum_gate import ControlledU if not isinstance(ins, ControlledU): raise ValueError(f"unknown gate: {name}, {ins.__class__.__name__}") diff --git a/quafu/visualisation/draw_dag.py b/quafu/visualisation/draw_dag.py index 9098f3d0..d3eb61ae 100644 --- a/quafu/visualisation/draw_dag.py +++ b/quafu/visualisation/draw_dag.py @@ -63,8 +63,8 @@ def draw_dag( for edge in dag.edges(data=True): node1, node2, link = edge - name1, label1 = _extract_node_info(node1) - name2, label2 = _extract_node_info(node2) + name1, _ = _extract_node_info(node1) + name2, _ = _extract_node_info(node2) dot.edge(name1, name2, label=link["label"]) dot.render(format=output_format, cleanup=True) diff --git a/setup.cfg b/setup.cfg index a0a08d07..c4a6b92b 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,5 +1,5 @@ [flake8] -# ignore doc related rule, should remove in the future: D100, D103, D415, D104, D101, D102, D200, D205, D403, D107, D105, D411, D405, D301, D202, D208, D209, D417, D300, D419, SCS108, D210, W191, E101, C092, D206, D207 +# TODO: ignore doc related rule, should remove in the future: D100, D103, D415, D104, D101, D102, D200, D205, D403, D107, D105, D411, D405, D301, D202, D208, D209, D417, D300, D419, SCS108, D210, W191, E101, C092, D206, D207 ignore = E203, E741, E731, N803, N806, N802, W503, D212, D100, D103, D415, D104, D101, D102, D200, D205, D403, D107, D105, D411, D405, D301, D202, D208, D209, D417, D300, D419, SCS108, D210, W191, E101, C092, D206, D207 max-line-length = 120 exclude = doc/run.py, doc/source/conf.py diff --git a/tests/quafu/simulator/base.py b/tests/quafu/simulator/base_test.py similarity index 100% rename from tests/quafu/simulator/base.py rename to tests/quafu/simulator/base_test.py diff --git a/tests/quafu/simulator/basis_test.py b/tests/quafu/simulator/basis_test.py index 24e34d33..a40d33a9 100644 --- a/tests/quafu/simulator/basis_test.py +++ b/tests/quafu/simulator/basis_test.py @@ -17,7 +17,7 @@ import numpy as np import pytest -from base import BaseTest +from base_test import BaseTest from quafu import QuantumCircuit, simulate diff --git a/tests/quafu/simulator/classic_test.py b/tests/quafu/simulator/classic_test.py index 6dec5ab2..6df7e136 100644 --- a/tests/quafu/simulator/classic_test.py +++ b/tests/quafu/simulator/classic_test.py @@ -15,7 +15,7 @@ import unittest import numpy as np -from base import BaseTest +from base_test import BaseTest from quafu import QuantumCircuit, simulate