Skip to content

Commit

Permalink
fix precommit
Browse files Browse the repository at this point in the history
  • Loading branch information
donghufeng committed Dec 2, 2024
1 parent 4661383 commit 268db18
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 20 deletions.
6 changes: 4 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 5 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
37 changes: 30 additions & 7 deletions quafu/elements/quantum_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 += " "
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
15 changes: 9 additions & 6 deletions quafu/visualisation/circuitPlot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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]])
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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__}")
Expand Down
4 changes: 2 additions & 2 deletions quafu/visualisation/draw_dag.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -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
File renamed without changes.
2 changes: 1 addition & 1 deletion tests/quafu/simulator/basis_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import numpy as np
import pytest
from base import BaseTest
from base_test import BaseTest

from quafu import QuantumCircuit, simulate

Expand Down
2 changes: 1 addition & 1 deletion tests/quafu/simulator/classic_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import unittest

import numpy as np
from base import BaseTest
from base_test import BaseTest

from quafu import QuantumCircuit, simulate

Expand Down

0 comments on commit 268db18

Please sign in to comment.