Skip to content

Commit

Permalink
Update pre commit hooks and fix failing CentOS CI builds (#429)
Browse files Browse the repository at this point in the history
* Update pre-commit hook versions

* Fix linter/formatter warnings

* Fix issues with CentOS build on the CI
  • Loading branch information
Takishima committed Apr 11, 2022
1 parent 0d32c16 commit 6f2c2be
Show file tree
Hide file tree
Showing 36 changed files with 147 additions and 151 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,18 @@ jobs:
/var/cache/dnf/
key: ${{ runner.os }}-centos${{ matrix.centos }}-yum-${{ secrets.yum_cache }}

- name: Fix repository URLs (CentOS 8 only)
if: matrix.centos == 8
run: |
sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/CentOS-*
sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g' /etc/yum.repos.d/CentOS-*
- name: Add Python 3 and other dependencies
run: yum update -y && yum install -y python3-devel gcc-c++ make

- name: Setup Endpoint repository (CentOS 7 only)
if: matrix.centos == 7
run: yum -y install https://packages.endpoint.com/rhel/7/os/x86_64/endpoint-repo-1.7-1.x86_64.rpm
run: yum -y install https://packages.endpointdev.com/rhel/7/os/x86_64/endpoint-repo.x86_64.rpm

- name: Install Git > 2.18
run: yum install -y git
Expand Down
10 changes: 5 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ repos:

# Changes tabs to spaces
- repo: https://github.com/Lucas-C/pre-commit-hooks
rev: v1.1.10
rev: v1.1.13
hooks:
- id: remove-tabs

Expand All @@ -50,15 +50,15 @@ repos:
name: isort (python)

- repo: https://github.com/psf/black
rev: 21.12b0
rev: 22.3.0
hooks:
- id: black
language_version: python3
# This is a slow hook, so only run this if --hook-stage manual is passed
stages: [manual]

- repo: https://gitlab.com/PyCQA/flake8
rev: 4.0.1
rev: 3.9.2
hooks:
- id: flake8
name: flake8-strict
Expand All @@ -73,7 +73,7 @@ repos:
files: ^(.*_test\.py)$

- repo: https://github.com/pre-commit/mirrors-pylint
rev: 'v3.0.0a3'
rev: 'v3.0.0a4'
hooks:
- id: pylint
args: ['--score=n']
Expand All @@ -82,7 +82,7 @@ repos:
additional_dependencies: [pybind11>=2.6, numpy, requests, boto3, matplotlib, networkx, sympy]

- repo: https://github.com/mgedmin/check-manifest
rev: '0.47'
rev: '0.48'
hooks:
- id: check-manifest
additional_dependencies: ['setuptools-scm', 'pybind11>=2.6']
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Repository

- Fix issues with building on CentOS 7 & 8
- Update `Lucas-C/pre-commit-hooks` hook to v1.1.13
- Update `flake8` hook to v4.0.1
- Update `pylint` hook to v3.0.0a4
- Update `black` hook to v22.3.0
- Update `check-manifest` to v0.48

## [0.7.1] - 2022-01-10

### Added
Expand Down
2 changes: 1 addition & 1 deletion examples/unitary_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def main():

# Output the final state of the qubits (assuming they all start in state |0>)
print('The final state of the qubits is:')
print(eng.backend.unitary @ np.array([1] + ([0] * (2 ** n_qubits - 1))))
print(eng.backend.unitary @ np.array([1] + ([0] * (2**n_qubits - 1))))
print('\n')

# Show the unitaries separated by measurement:
Expand Down
3 changes: 1 addition & 2 deletions projectq/backends/_aqt/_aqt.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,10 @@ def get_probabilities(self, qureg):
raise RuntimeError("Please, run the circuit first!")

probability_dict = {}
for state in self._probabilities:
for state, probability in self._probabilities.items():
mapped_state = ['0'] * len(qureg)
for i, qubit in enumerate(qureg):
mapped_state[i] = state[self._logical_to_physical(qubit.id)]
probability = self._probabilities[state]
mapped_state = "".join(mapped_state)

probability_dict[mapped_state] = probability_dict.get(mapped_state, 0) + probability
Expand Down
110 changes: 50 additions & 60 deletions projectq/backends/_awsbraket/_awsbraket.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,49 +172,43 @@ def is_available(self, cmd): # pylint: disable=too-many-return-statements,too-m
if get_control_count(cmd) == 1:
return isinstance(gate, (R, ZGate, XGate, SwapGate))
if get_control_count(cmd) == 0:
return (
isinstance(
gate,
(
R,
Rx,
Ry,
Rz,
XGate,
YGate,
ZGate,
HGate,
SGate,
TGate,
SwapGate,
),
)
or gate in (Sdag, Tdag)
)
return isinstance(
gate,
(
R,
Rx,
Ry,
Rz,
XGate,
YGate,
ZGate,
HGate,
SGate,
TGate,
SwapGate,
),
) or gate in (Sdag, Tdag)

if self.device == 'IonQ Device':
if get_control_count(cmd) == 1:
return isinstance(gate, XGate)
if get_control_count(cmd) == 0:
return (
isinstance(
gate,
(
Rx,
Ry,
Rz,
XGate,
YGate,
ZGate,
HGate,
SGate,
TGate,
SqrtXGate,
SwapGate,
),
)
or gate in (Sdag, Tdag)
)
return isinstance(
gate,
(
Rx,
Ry,
Rz,
XGate,
YGate,
ZGate,
HGate,
SGate,
TGate,
SqrtXGate,
SwapGate,
),
) or gate in (Sdag, Tdag)

if self.device == 'SV1':
if get_control_count(cmd) == 2:
Expand All @@ -224,26 +218,23 @@ def is_available(self, cmd): # pylint: disable=too-many-return-statements,too-m
if get_control_count(cmd) == 0:
# TODO: add MatrixGate to cover the unitary operation
# TODO: Missing XY gate in ProjectQ
return (
isinstance(
gate,
(
R,
Rx,
Ry,
Rz,
XGate,
YGate,
ZGate,
HGate,
SGate,
TGate,
SqrtXGate,
SwapGate,
),
)
or gate in (Sdag, Tdag)
)
return isinstance(
gate,
(
R,
Rx,
Ry,
Rz,
XGate,
YGate,
ZGate,
HGate,
SGate,
TGate,
SqrtXGate,
SwapGate,
),
) or gate in (Sdag, Tdag)
return False

def _reset(self):
Expand Down Expand Up @@ -372,13 +363,12 @@ def get_probabilities(self, qureg):
raise RuntimeError("Please, run the circuit first!")

probability_dict = {}
for state in self._probabilities:
for state, probability in self._probabilities.items():
mapped_state = ['0'] * len(qureg)
for i, qubit in enumerate(qureg):
if self._logical_to_physical(qubit.id) >= len(state): # pragma: no cover
raise IndexError('Physical ID {} > length of internal probabilities array'.format(qubit.id))
mapped_state[i] = state[self._logical_to_physical(qubit.id)]
probability = self._probabilities[state]
mapped_state = "".join(mapped_state)
if mapped_state not in probability_dict:
probability_dict[mapped_state] = probability
Expand Down
4 changes: 2 additions & 2 deletions projectq/backends/_circuits/_drawer.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,10 @@ def get_latex(self, ordered=False, draw_gates_in_parallel=True):
"""
qubit_lines = {}

for line in range(len(self._qubit_lines)):
for line, qubit_line in self._qubit_lines.items():
new_line = self._map[line]
qubit_lines[new_line] = []
for cmd in self._qubit_lines[line]:
for cmd in qubit_line:
lines = [self._map[qb_id] for qb_id in cmd.lines]
ctrl_lines = [self._map[qb_id] for qb_id in cmd.ctrl_lines]
gate = cmd.gate
Expand Down
8 changes: 4 additions & 4 deletions projectq/backends/_circuits/_drawer_matplotlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def _process(self, cmd): # pylint: disable=too-many-branches
# considering the qubit axes that are between the topmost and
# bottommost qubit axes of the current command.
if len(targets) + len(controls) > 1:
max_depth = max(len(self._qubit_lines[qubit_id]) for qubit_id in self._qubit_lines)
max_depth = max(len(line) for qubit_id, line in self._qubit_lines.items())

for qb_id in itertools.chain(targets, controls):
depth = len(self._qubit_lines[qb_id])
Expand Down Expand Up @@ -206,9 +206,9 @@ def draw(self, qubit_labels=None, drawing_order=None, **kwargs):
- wire_height (1): Vertical spacing between two qubit
wires (roughly in inches)
"""
max_depth = max(len(self._qubit_lines[qubit_id]) for qubit_id in self._qubit_lines)
for qubit_id in self._qubit_lines:
depth = len(self._qubit_lines[qubit_id])
max_depth = max(len(line) for qubit_id, line in self._qubit_lines.items())
for qubit_id, line in self._qubit_lines.items():
depth = len(line)
if depth < max_depth:
self._qubit_lines[qubit_id] += [None] * (max_depth - depth)

Expand Down
2 changes: 1 addition & 1 deletion projectq/backends/_circuits/_to_latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ def to_tikz( # pylint: disable=too-many-branches,too-many-locals,too-many-state
tikz_code.append(connections)

if not draw_gates_in_parallel:
for _line in range(len(self.pos)):
for _line, _ in enumerate(self.pos):
if _line != line:
self.pos[_line] = self.pos[line]

Expand Down
3 changes: 1 addition & 2 deletions projectq/backends/_ibm/_ibm.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,10 @@ def get_probabilities(self, qureg):
raise RuntimeError("Please, run the circuit first!")

probability_dict = {}
for state in self._probabilities:
for state, probability in self._probabilities.items():
mapped_state = ['0'] * len(qureg)
for i, val in enumerate(qureg):
mapped_state[i] = state[self._logical_to_physical(val.id)]
probability = self._probabilities[state]
mapped_state = "".join(mapped_state)
if mapped_state not in probability_dict:
probability_dict[mapped_state] = probability
Expand Down
3 changes: 1 addition & 2 deletions projectq/backends/_ionq/_ionq.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,15 +283,14 @@ def get_probabilities(self, qureg):
raise RuntimeError("Please, run the circuit first!")

probability_dict = {}
for state in self._probabilities:
for state, probability in self._probabilities.items():
mapped_state = ['0'] * len(qureg)
for i, qubit in enumerate(qureg):
try:
meas_idx = self._measured_ids.index(qubit.id)
except ValueError:
continue
mapped_state[i] = state[meas_idx]
probability = self._probabilities[state]
mapped_state = "".join(mapped_state)
probability_dict[mapped_state] = probability_dict.get(mapped_state, 0) + probability
return probability_dict
Expand Down
14 changes: 7 additions & 7 deletions projectq/backends/_sim/_pysim.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ def emulate_math(self, func, qubit_ids, ctrlqubit_ids): # pylint: disable=too-m
qb_locs[-1].append(self._map[qubit_id])

newstate = _np.zeros_like(self._state)
for i in range(0, len(self._state)):
for i, state in enumerate(self._state):
if (mask & i) == mask:
arg_list = [0] * len(qb_locs)
for qr_i, qr_loc in enumerate(qb_locs):
Expand All @@ -218,9 +218,9 @@ def emulate_math(self, func, qubit_ids, ctrlqubit_ids): # pylint: disable=too-m
for qb_i, qb_loc in enumerate(qr_loc):
if not ((new_i >> qb_loc) & 1) == ((res[qr_i] >> qb_i) & 1):
new_i ^= 1 << qb_loc
newstate[new_i] = self._state[i]
newstate[new_i] = state
else:
newstate[i] = self._state[i]
newstate[i] = state

self._state = newstate

Expand Down Expand Up @@ -286,7 +286,7 @@ def get_probability(self, bit_string, ids):
probability = 0.0
for i, state in enumerate(self._state):
if (i & mask) == bit_str:
probability += state.real ** 2 + state.imag ** 2
probability += state.real**2 + state.imag**2
return probability

def get_amplitude(self, bit_string, ids):
Expand Down Expand Up @@ -482,13 +482,13 @@ def collapse_wavefunction(self, ids, values):
mask |= 1 << pos
val |= int(values[i]) << pos
nrm = 0.0
for i in range(len(self._state)):
for i, state in enumerate(self._state):
if (mask & i) == val:
nrm += _np.abs(self._state[i]) ** 2
nrm += _np.abs(state) ** 2
if nrm < 1.0e-12:
raise RuntimeError("collapse_wavefunction(): Invalid collapse! Probability is ~0.")
inv_nrm = 1.0 / _np.sqrt(nrm)
for i in range(len(self._state)):
for i in range(len(self._state)): # pylint: disable=consider-using-enumerate
if (mask & i) != val:
self._state[i] = 0.0
else:
Expand Down
4 changes: 2 additions & 2 deletions projectq/backends/_sim/_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def is_available(self, cmd):
try:
matrix = cmd.gate.matrix
# Allow up to 5-qubit gates
if len(matrix) > 2 ** 5:
if len(matrix) > 2**5:
return False
return True
except AttributeError:
Expand Down Expand Up @@ -402,7 +402,7 @@ def _handle(self, cmd): # pylint: disable=too-many-branches,too-many-locals,too
qubitids = [qb.id for qb in cmd.qubits[0]]
ctrlids = [qb.id for qb in cmd.control_qubits]
self._simulator.emulate_time_evolution(op, time, qubitids, ctrlids)
elif len(cmd.gate.matrix) <= 2 ** 5:
elif len(cmd.gate.matrix) <= 2**5:
matrix = cmd.gate.matrix
ids = [qb.id for qureg in cmd.qubits for qb in qureg]
if not 2 ** len(ids) == len(cmd.gate.matrix):
Expand Down

0 comments on commit 6f2c2be

Please sign in to comment.