Skip to content

Commit

Permalink
Add pylint to workflow tests (#520)
Browse files Browse the repository at this point in the history
* updat format workflow

* add install instruction

* update `.pylintrc` files

* add `pylint` fixes

* tweaks

* tweaks

* workflow name

* copyright dates

* Update tests/conftest.py

* apply suggestions

* minor tweak
  • Loading branch information
obliviateandsurrender authored Apr 30, 2024
1 parent a8fb1bd commit d826caa
Show file tree
Hide file tree
Showing 18 changed files with 331 additions and 83 deletions.
16 changes: 13 additions & 3 deletions .github/workflows/format.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ on:
- pull_request

jobs:
black:
black-pylint:
runs-on: ubuntu-latest
steps:
- name: Cancel Previous Runs
Expand All @@ -17,9 +17,19 @@ jobs:
python-version: 3.9

- name: Install dependencies
run: pip install black
run: pip install black pylint==2.7.4

- uses: actions/checkout@v2

- name: Run Black
run: black -l 100 pennylane_qiskit/ --check
run: |
black -l 100 pennylane_qiskit/ --check
black -l 100 tests/ --check
- name: Run Pylint (source files)
if: always()
run: pylint --rcfile .pylintrc $(find pennylane_qiskit -name "*.py")

- name: Run Pylint (test files)
if: always()
run: pylint --rcfile tests/.pylintrc $(find tests -name "*.py")
22 changes: 18 additions & 4 deletions .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
# A comma-separated list of packages or module names from where C extensions may
# be loaded. Extensions are loaded into the active Python interpreter and may
# run arbitrary code
extension-pkg-whitelist=numpy,scipy,autograd,qiskit
extension-pkg-whitelist=numpy,scipy,autograd,qiskit,pennylane,sympy,qiskit_ibm_runtime,qiskit_ibm_provider,semantic_version

[TYPECHECK]

# List of module names for which member attributes should not be checked
# (useful for modules/projects where namespaces are manipulated during runtime
# and thus existing member attributes cannot be deduced by static analysis. It
# supports qualified module names, as well as Unix pattern matching.
ignored-modules=numpy,scipy,autograd,qiskit
ignored-modules=numpy,scipy,autograd,qiskit,pennylane,sympy,qiskit_ibm_runtime,qiskit_ibm_provider,semantic_version

# List of class names for which member attributes should not be checked
# (useful for classes with attributes dynamically set). This can work
# with qualified names.
ignored-classes=numpy,scipy,autograd,qiskit
ignored-classes=numpy,scipy,autograd,qiskit,pennylane,sympy,qiskit_ibm_runtime,qiskit_ibm_provider,semantic_version

[MESSAGES CONTROL]

Expand All @@ -28,4 +28,18 @@ ignored-classes=numpy,scipy,autograd,qiskit
# can either give multiple identifiers separated by comma (,) or put this option
# multiple times (only on the command line, not in the configuration file where
# it should appear only once).
disable=line-too-long,invalid-name,too-many-lines,redefined-builtin,too-many-locals,duplicate-code,bad-continuation
disable=
line-too-long,
invalid-name,
too-many-lines,
redefined-builtin,
too-many-locals,
duplicate-code,
cyclic-import,
import-error,
bad-option-value

[MISCELLANEOUS]

# List of note tags to take in consideration, separated by a comma.
notes=
1 change: 1 addition & 0 deletions pennylane_qiskit/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@ def _function(*args, params: dict = None, wires: list = None, **kwargs):
mid_circ_regs = {}

# Processing the dictionary of parameters passed
# pylint: disable=too-many-nested-blocks
for idx, circuit_instruction in enumerate(qc.data):
(instruction, qargs, cargs) = circuit_instruction
# the new Singleton classes have different names than the objects they represent,
Expand Down
7 changes: 6 additions & 1 deletion pennylane_qiskit/qiskit_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ def backend(self):
return self._backend

def reset(self):
"""Reset the Qiskit backend device"""
# Reset only internal data, not the options that are determined on
# device creation
self._reg = QuantumRegister(self.num_wires, "q")
Expand Down Expand Up @@ -305,6 +306,7 @@ def create_circuit_object(self, operations, **kwargs):
self._circuit.save_state()

def apply(self, operations, **kwargs):
"""Build the circuit object and apply the operations"""
self.create_circuit_object(operations, **kwargs)

# These operations need to run for all devices
Expand Down Expand Up @@ -448,9 +450,11 @@ def generate_samples(self, circuit=None):

@property
def state(self):
"""Get state of the device"""
return self._state

def analytic_probability(self, wires=None):
"""Get the analytic probability of the device"""
if self._state is None:
return None

Expand Down Expand Up @@ -482,7 +486,7 @@ def compile_circuits(self, circuits):
return compiled_circuits

def batch_execute(self, circuits, timeout: int = None):
# pylint: disable=missing-function-docstring
"""Batch execute the circuits on the device"""

compiled_circuits = self.compile_circuits(circuits)

Expand All @@ -500,6 +504,7 @@ def batch_execute(self, circuits, timeout: int = None):
result = self._current_job.result()

# increment counter for number of executions of qubit device
# pylint: disable=no-member
self._num_executions += 1

# Compute statistics using the state and/or samples
Expand Down
1 change: 1 addition & 0 deletions pennylane_qiskit/runtime_devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ def batch_execute(self, circuits):

return results

# pylint: disable=arguments-differ
def generate_samples(self, circuit_id=None):
r"""Returns the computational basis samples generated for all wires.
Expand Down
58 changes: 58 additions & 0 deletions tests/.pylintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
[MASTER]
# A comma-separated list of package or module names from where C extensions may
# be loaded. Extensions are loading into the active Python interpreter and may
# run arbitrary code
extension-pkg-whitelist=numpy,scipy,autograd,qiskit,pennylane,sympy,qiskit_ibm_runtime,qiskit_ibm_provider,semantic_version

[TYPECHECK]

# List of module names for which member attributes should not be checked
# (useful for modules/projects where namespaces are manipulated during runtime
# and thus existing member attributes cannot be deduced by static analysis. It
# supports qualified module names, as well as Unix pattern matching.
ignored-modules=numpy,scipy,autograd,qiskit,pennylane,sympy,qiskit_ibm_runtime,qiskit_ibm_provider,semantic_version

# List of classes names for which member attributes should not be checked
# (useful for classes with attributes dynamically set). This supports can work
# with qualified names.
ignored-classes=numpy,scipy,autograd,qiskit,pennylane,sympy,qiskit_ibm_runtime,qiskit_ibm_provider,semantic_version

[MESSAGES CONTROL]

# Enable the message, report, category or checker with the given id(s). You can
# either give multiple identifier separated by comma (,) or put this option
# multiple time.
#enable=

# Disable the message, report, category or checker with the given id(s). You
# can either give multiple identifier separated by comma (,) or put this option
# multiple time (only on the command line, not in the configuration file where
# it should appear only once).
# Cyclical import checks are disabled for now as they are frequently used in
# the code base, but this can be removed in the future once cycles are resolved.
disable=
line-too-long,
invalid-name,
too-many-lines,
redefined-builtin,
too-many-locals,
duplicate-code,
cyclic-import,
import-error,
bad-option-value,
import-outside-toplevel,
missing-class-docstring,
missing-function-docstring,
no-self-use,
no-member, # because of qnode decorator
comparison-with-callable,
unsubscriptable-object, # because of qnode decorator
not-callable, # because of qnode decorator
unexpected-keyword-arg,
arguments-differ,
no-value-for-parameter

[MISCELLANEOUS]

# List of note tags to take in consideration, separated by a comma.
notes=
10 changes: 6 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2021-2022 Xanadu Quantum Technologies Inc.
# Copyright 2021-2024 Xanadu Quantum Technologies Inc.

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
r"""
This module contains tests for PennyLane runtime programs.
This module contains some configuration for PennyLane IBMQ devices.
"""

import os
Expand All @@ -22,9 +22,11 @@

import pennylane as qml
from semantic_version import Version
from qiskit_ibm_provider import IBMProvider
from qiskit_ibm_provider import IBMProvider, IBMProviderValueError
from pennylane_qiskit import AerDevice, BasicAerDevice, BasicSimulatorDevice

# pylint: disable=protected-access, unused-argument, redefined-outer-name

np.random.seed(42)

U = np.array(
Expand Down Expand Up @@ -61,7 +63,7 @@ def skip_if_no_account():
t = os.getenv("IBMQX_TOKEN", None)
try:
IBMProvider(token=t)
except Exception:
except IBMProviderValueError:
missing = "token" if t else "account"
pytest.skip(f"Skipping test, no IBMQ {missing} available")

Expand Down
23 changes: 19 additions & 4 deletions tests/test_apply.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
# Copyright 2021-2024 Xanadu Quantum Technologies Inc.

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
r"""
This module contains tests for applying operations on PennyLane IBMQ devices.
"""
import pytest

import numpy as np
import pennylane as qml
from scipy.linalg import block_diag

from pennylane_qiskit import AerDevice, BasicAerDevice

from conftest import U, U2

np.random.seed(42)
# pylint: disable=protected-access, too-many-arguments, too-few-public-methods

np.random.seed(42)

# global variables and rotations
I = np.identity(2)
Expand All @@ -26,7 +41,7 @@
toffoli[6:8, 6:8] = np.array([[0, 1], [1, 0]])
CSWAP = block_diag(I, I, SWAP)


# pylint: disable=unnecessary-lambda-assignment
phase_shift = lambda phi: np.array([[1, 0], [0, np.exp(1j * phi)]])
rx = lambda theta: np.cos(theta / 2) * I + 1j * np.sin(-theta / 2) * X
ry = lambda theta: np.cos(theta / 2) * I + 1j * np.sin(-theta / 2) * Y
Expand Down
Loading

0 comments on commit d826caa

Please sign in to comment.