Skip to content

Commit

Permalink
✨ 🚨 Added ruff and nb-clean to repository (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
nquetschlich authored Feb 10, 2023
1 parent cd2e329 commit 923bcb3
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 430 deletions.
18 changes: 0 additions & 18 deletions .flake8

This file was deleted.

52 changes: 12 additions & 40 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,6 @@ repos:
- id: fix-ligatures
- id: fix-smartquotes

# Sort includes
- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: isort
args: ["--profile", "black", "--filter-files"]

# Upgrade old Python syntax
- repo: https://github.com/asottile/pyupgrade
rev: "v3.3.1"
hooks:
- id: pyupgrade
args: ["--py37-plus"]

# Run code formatting with Black
- repo: https://github.com/psf/black
rev: "23.1.0" # Keep in sync with blacken-docs
Expand All @@ -64,32 +50,6 @@ repos:
additional_dependencies:
- black==23.1.0 # keep in sync with black hook

# Check for common mistakes
- repo: https://github.com/pre-commit/pygrep-hooks
rev: "v1.10.0"
hooks:
- id: python-check-blanket-noqa
- id: python-check-blanket-type-ignore
- id: python-no-log-warn
- id: python-no-eval
- id: python-use-type-annotations
- id: rst-backticks
- id: rst-directive-colons
- id: rst-inline-touching-normal

# Run Flake8 checks
- repo: https://github.com/PyCQA/flake8
rev: "6.0.0"
hooks:
- id: flake8
additional_dependencies:
- flake8-bugbear
- flake8-comprehensions
- flake8-future-annotations
- flake8-new-union-types
- flake8-simplify
- flake8-2020

# Check for spelling
- repo: https://github.com/codespell-project/codespell
rev: "v2.2.2"
Expand All @@ -107,3 +67,15 @@ repos:
hooks:
- id: prettier
types_or: [yaml, markdown, html, css, javascript, json]

- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.237
hooks:
- id: ruff
args: ["--fix"]

# Clean jupyter notebooks
- repo: https://github.com/srstevenson/nb-clean
rev: "2.4.0"
hooks:
- id: nb-clean
65 changes: 9 additions & 56 deletions paper_figures.ipynb

Large diffs are not rendered by default.

58 changes: 58 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,61 @@ Discussions = "https://github.com/cda-tum/mqtproblemsolver/discussions"
Research = "https://www.cda.cit.tum.de/research/quantum/"

[tool.setuptools_scm]


[tool.black]
line-length = 120

[tool.ruff]
select = [
"E", "F", "W", # flake8
"A", # flake8-builtins
"B", "B904", # flake8-bugbear
"I", # isort
"ARG", # flake8-unused-arguments
"C4", # flake8-comprehensions
"EM", # flake8-errmsg
"EXE", # flake8-executable
"ICN", # flake8-import-conventions
"ISC", # flake8-implicit-str-concat
"PGH", # pygrep-hooks
"PIE", # flake8-pie
"PL", # pylint
"PT", # flake8-pytest-style
"PTH", # flake8-use-pathlib
"Q", # flake8-quotes
"RET", # flake8-return
"RUF", # Ruff-specific
"SIM", # flake8-simplify
"TCH", # flake8-type-checking
"UP", # pyupgrade
"YTT", # flake8-2020
]
ignore = ["E501"]
target-version = "py39"

# Exclude a variety of commonly ignored directories.
exclude = [
"__init__.py",
".bzr",
".direnv",
".eggs",
".git",
".hg",
".mypy_cache",
".nox",
".pants.d",
".ruff_cache",
".svn",
".tox",
".venv",
"__pypackages__",
"_build",
"buck-out",
"build",
"dist",
"node_modules",
"venv",
]

line-length = 120
61 changes: 20 additions & 41 deletions src/mqt/problemsolver/csp.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from __future__ import annotations

import numpy as np
from qiskit import QuantumCircuit, QuantumRegister, execute

from mqt import ddsim
from qiskit import QuantumCircuit, QuantumRegister, execute


class CSP:
Expand All @@ -23,27 +22,21 @@ def solve(
"""
if quantum_algorithm == "Grover":
qc, anc, anc_mct, flag, nqubits, nancilla, (a, b, c, d) = self.init_qc()
qc, mct_list = self.encode_constraints(
qc, a, b, c, d, anc, constraints=constraints
)
qc, mct_list = self.encode_constraints(qc, a, b, c, d, anc, constraints=constraints)
oracle = self.create_oracle(qc, mct_list, flag, anc_mct)
for m in (5, 6, 7, 8, 12):
qc = self.create_grover(
oracle, nqubits, nancilla, ninputs=nqubits - 1, grover_iterations=m
)
qc = self.create_grover(oracle, nqubits, nancilla, ninputs=nqubits - 1, grover_iterations=m)
solution = self.simulate(qc)
if solution:
break
if solution:
return solution
else:
return False

else:
print("ERROR: Selected quantum algorithm is not implemented.")
return False

def print(
print("ERROR: Selected quantum algorithm is not implemented.")
return False

def print_problem(
self,
sum_s0: str | int = "s0",
sum_s1: str | int = "s1",
Expand All @@ -68,9 +61,7 @@ def print(
print(" ", sum_s3, " | ", c, " | ", d, " |")
print("------------------\n")

def check_inequality(
self, qc: QuantumCircuit, x: tuple, y: tuple, res_anc: QuantumRegister
):
def check_inequality(self, qc: QuantumCircuit, x: tuple, y: tuple, res_anc: QuantumRegister):
x_low, x_high = x
y_low, y_high = y

Expand All @@ -88,9 +79,7 @@ def check_inequality(
qc.x(y_high)
qc.cx(x_high, y_high)

def check_equality(
self, qc: QuantumCircuit, x: tuple, s: str, res_anc: QuantumRegister
):
def check_equality(self, qc: QuantumCircuit, x: tuple, s: str, res_anc: QuantumRegister):
x_low, x_mid, x_high = x

if s[-1] == "0":
Expand Down Expand Up @@ -159,25 +148,17 @@ def encode_constraints(
anc_index = 0
for constraint in constraints:
if constraint.get("type") == "inequality":
first_qreg = dict_variable_to_quantumregister.get(
constraint.get("operand_one")
)
second_qreg = dict_variable_to_quantumregister.get(
constraint.get("operand_two")
)
first_qreg = dict_variable_to_quantumregister.get(constraint.get("operand_one"))
second_qreg = dict_variable_to_quantumregister.get(constraint.get("operand_two"))

self.check_inequality(qc, first_qreg, second_qreg, anc[anc_index])
mct_list.append(anc[anc_index])
qc.barrier()
anc_index += anc_needed_per_constraint.get(constraint.get("type"))

elif constraint.get("type") == "addition_equality":
first_qreg = dict_variable_to_quantumregister.get(
constraint.get("operand_one")
)
second_qreg = dict_variable_to_quantumregister.get(
constraint.get("operand_two")
)
first_qreg = dict_variable_to_quantumregister.get(constraint.get("operand_one"))
second_qreg = dict_variable_to_quantumregister.get(constraint.get("operand_two"))
tmp_1 = self.add_two_numbers(
qc,
first_qreg,
Expand Down Expand Up @@ -219,8 +200,7 @@ def create_oracle(
uncompute.name = "uncompute"
qc.append(uncompute, range(qc.num_qubits))

oracle = qc.to_instruction(label="oracle")
return oracle
return qc.to_instruction(label="oracle")

def init_qc(self):
a_low = QuantumRegister(1, "a_low")
Expand Down Expand Up @@ -291,28 +271,27 @@ def simulate(self, qc: QuantumCircuit):
mean_counts = np.mean(list(counts.values()))

found_sol = False
for entry in counts.keys():
for entry in counts:
if counts.get(entry) > 5 * mean_counts:
found_sol = True
break
if found_sol:
for entry in counts.keys():
for entry in counts:
d = int(entry[0:2], 2)
c = int(entry[2:4], 2)
b = int(entry[4:6], 2)
a = int(entry[6:8], 2)
if counts.get(entry) > 5 * mean_counts:
return (a, b, c, d)
else:
print("Simulation was unsuccessful.")

print("Simulation was unsuccessful.")
return None

def get_available_quantum_algorithms(self):
"""Method to get all available quantum algorithms in a list."""
return ["Grover"]

def get_kakuro_constraints(
self, sum_s0: int, sum_s1: int, sum_s2: int, sum_s3: int
):
def get_kakuro_constraints(self, sum_s0: int, sum_s1: int, sum_s2: int, sum_s3: int):
"""Method to get a list of constraints for the inserted sums."""
list_of_constraints = []
constraint_1 = {
Expand Down
Loading

0 comments on commit 923bcb3

Please sign in to comment.