Skip to content

Commit

Permalink
🎨🚨 Improved formatting and linting config (#625)
Browse files Browse the repository at this point in the history
## Description

This PR adds a couple further pre-commit checks and other utilities to
ensure the validity of certain configs.
This includes:
- a `pre-commit` check for checking several schemata for their
correctness, such as dependabot and GitHub actions
- an `.editorconfig` file for enabling wide spread support for proper
formatting out of the box
- a switch from `codespell` to the `typos` spellchecker that is more
comprehensive and a lot faster
- a `validate-pyproject` check that makes sure the `pyproject.toml` is
valid

## Checklist:

<!---
This checklist serves as a reminder of a couple of things that ensure
your pull request will be merged swiftly.
-->

- [x] The pull request only contains commits that are related to it.
- [x] I have added appropriate tests and documentation.
- [x] I have made sure that all CI jobs on GitHub pass.
- [x] The pull request introduces no new warnings and follows the
project's style guidelines.
  • Loading branch information
burgholzer authored Jun 5, 2024
2 parents 86d93b5 + 178669d commit 99c8901
Show file tree
Hide file tree
Showing 19 changed files with 103 additions and 61 deletions.
15 changes: 15 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.{py,pyi}]
indent_size = 4

[*.md]
trim_trailing_whitespace = false
1 change: 0 additions & 1 deletion .git_archival.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
node: $Format:%H$
node-date: $Format:%cI$
describe-name: $Format:%(describe:tags=true,match=*[0-9]*)$
ref-names: $Format:%D$
24 changes: 19 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,16 @@ repos:

# Format configuration files with prettier
- repo: https://github.com/pre-commit/mirrors-prettier
rev: v4.0.0-alpha.8
rev: v3.1.0
hooks:
- id: prettier
types_or: [yaml, markdown, html, css, scss, javascript, json]

# Check for spelling
- repo: https://github.com/codespell-project/codespell
rev: v2.3.0
- repo: https://github.com/crate-ci/typos
rev: v1.22.0
hooks:
- id: codespell
args: ["-L", "wille,linz", "--skip", "*.ipynb"]
- id: typos

# Catch common capitalization mistakes
- repo: local
Expand All @@ -129,3 +128,18 @@ repos:
hooks:
- id: sp-repo-review
additional_dependencies: ["repo-review[cli]"]

# Check JSON schemata
- repo: https://github.com/python-jsonschema/check-jsonschema
rev: 0.28.4
hooks:
- id: check-dependabot
- id: check-github-workflows
- id: check-readthedocs

# Check the pyproject.toml file
- repo: https://github.com/abravalheri/validate-pyproject
rev: v0.18
hooks:
- id: validate-pyproject
additional_dependencies: ["validate-pyproject-schema-store[all]"]
8 changes: 4 additions & 4 deletions include/mqt-core/QuantumComputation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class QuantumComputation {

fp globalPhase = 0.;

std::unordered_set<sym::Variable> occuringVariables;
std::unordered_set<sym::Variable> occurringVariables;

void importOpenQASM3(std::istream& is);
void importReal(std::istream& is);
Expand Down Expand Up @@ -255,7 +255,7 @@ class QuantumComputation {
: nqubits(qc.nqubits), nclassics(qc.nclassics), nancillae(qc.nancillae),
name(qc.name), qregs(qc.qregs), cregs(qc.cregs), ancregs(qc.ancregs),
mt(qc.mt), seed(qc.seed), globalPhase(qc.globalPhase),
occuringVariables(qc.occuringVariables),
occurringVariables(qc.occurringVariables),
initialLayout(qc.initialLayout),
outputPermutation(qc.outputPermutation), ancillary(qc.ancillary),
garbage(qc.garbage) {
Expand All @@ -276,7 +276,7 @@ class QuantumComputation {
mt = qc.mt;
seed = qc.seed;
globalPhase = qc.globalPhase;
occuringVariables = qc.occuringVariables;
occurringVariables = qc.occurringVariables;
initialLayout = qc.initialLayout;
outputPermutation = qc.outputPermutation;
ancillary = qc.ancillary;
Expand Down Expand Up @@ -777,7 +777,7 @@ class QuantumComputation {
}

[[nodiscard]] const std::unordered_set<sym::Variable>& getVariables() const {
return occuringVariables;
return occurringVariables;
}

/**
Expand Down
24 changes: 13 additions & 11 deletions include/mqt-core/dd/Package.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -981,27 +981,28 @@ template <class Config> class Package {
std::pair<dd::fp, dd::fp>
determineMeasurementProbabilities(const vEdge& rootEdge, const Qubit index,
const bool assumeProbabilityNormalization) {
std::map<const vNode*, fp> probsMone;
std::map<const vNode*, fp> measurementProbabilities;
std::set<const vNode*> visited;
std::queue<const vNode*> q;

probsMone[rootEdge.p] = ComplexNumbers::mag2(rootEdge.w);
measurementProbabilities[rootEdge.p] = ComplexNumbers::mag2(rootEdge.w);
visited.insert(rootEdge.p);
q.push(rootEdge.p);

while (q.front()->v != index) {
const auto* ptr = q.front();
q.pop();
const fp prob = probsMone[ptr];
const fp prob = measurementProbabilities[ptr];

const auto& s0 = ptr->e[0];
if (const auto s0w = static_cast<ComplexValue>(s0.w);
!s0w.approximatelyZero()) {
const fp tmp1 = prob * s0w.mag2();
if (visited.find(s0.p) != visited.end()) {
probsMone[s0.p] = probsMone[s0.p] + tmp1;
measurementProbabilities[s0.p] =
measurementProbabilities[s0.p] + tmp1;
} else {
probsMone[s0.p] = tmp1;
measurementProbabilities[s0.p] = tmp1;
visited.insert(s0.p);
q.push(s0.p);
}
Expand All @@ -1012,9 +1013,10 @@ template <class Config> class Package {
!s1w.approximatelyZero()) {
const fp tmp1 = prob * s1w.mag2();
if (visited.find(s1.p) != visited.end()) {
probsMone[s1.p] = probsMone[s1.p] + tmp1;
measurementProbabilities[s1.p] =
measurementProbabilities[s1.p] + tmp1;
} else {
probsMone[s1.p] = tmp1;
measurementProbabilities[s1.p] = tmp1;
visited.insert(s1.p);
q.push(s1.p);
}
Expand All @@ -1031,12 +1033,12 @@ template <class Config> class Package {
const auto& s0 = ptr->e[0];
if (const auto s0w = static_cast<ComplexValue>(s0.w);
!s0w.approximatelyZero()) {
pzero += probsMone[ptr] * s0w.mag2();
pzero += measurementProbabilities[ptr] * s0w.mag2();
}
const auto& s1 = ptr->e[1];
if (const auto s1w = static_cast<ComplexValue>(s1.w);
!s1w.approximatelyZero()) {
pone += probsMone[ptr] * s1w.mag2();
pone += measurementProbabilities[ptr] * s1w.mag2();
}
}
} else {
Expand All @@ -1050,12 +1052,12 @@ template <class Config> class Package {
const auto& s0 = ptr->e[0];
if (const auto s0w = static_cast<ComplexValue>(s0.w);
!s0w.approximatelyZero()) {
pzero += probsMone[ptr] * probs[s0.p] * s0w.mag2();
pzero += measurementProbabilities[ptr] * probs[s0.p] * s0w.mag2();
}
const auto& s1 = ptr->e[1];
if (const auto s1w = static_cast<ComplexValue>(s1.w);
!s1w.approximatelyZero()) {
pone += probsMone[ptr] * probs[s1.p] * s1w.mag2();
pone += measurementProbabilities[ptr] * probs[s1.p] * s1w.mag2();
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion include/mqt-core/na/NADefinitions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ struct Point {
return x == other.x && y == other.y;
}
[[maybe_unused]] [[nodiscard]] auto
getEuclidianDistance(const Point& c) const {
getEuclideanDistance(const Point& c) const {
const auto delta = *this - c;
return delta.length();
}
Expand Down
2 changes: 1 addition & 1 deletion include/mqt-core/operations/ClassicControlledOperation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ enum ComparisonKind : std::uint8_t {
Geq,
};

ComparisonKind getInvertedComparsionKind(ComparisonKind kind);
ComparisonKind getInvertedComparisonKind(ComparisonKind kind);

std::string toString(const ComparisonKind& kind);

Expand Down
48 changes: 30 additions & 18 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,23 @@ keywords = ["MQT", "quantum-computing", "design-automation", "decision-diagrams"
license = { file = "LICENSE.md" }

classifiers = [
"Topic :: Scientific/Engineering :: Electronic Design Automation (EDA)",
"Intended Audience :: Science/Research",
"Natural Language :: English",
"Operating System :: POSIX :: Linux",
"Operating System :: MacOS",
"Operating System :: Microsoft :: Windows",
"License :: OSI Approved :: MIT License",
"Programming Language :: C++",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Development Status :: 4 - Beta",
"Typing :: Typed",
"Development Status :: 5 - Production/Stable",
"Intended Audience :: Science/Research",
"License :: OSI Approved :: MIT License",
"Natural Language :: English",
"Operating System :: MacOS",
"Operating System :: Microsoft :: Windows",
"Operating System :: POSIX :: Linux",
"Programming Language :: C++",
"Programming Language :: Python",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Topic :: Scientific/Engineering :: Electronic Design Automation (EDA)",
"Typing :: Typed",
]
requires-python = ">=3.8"
dynamic = ["version"]
Expand Down Expand Up @@ -257,6 +256,19 @@ isort.required-imports = ["from __future__ import annotations"]
convention = "google"


[tool.typos]
[tool.typos.default.extend-words]
wille = "wille"
anc = "anc"
mch = "mch"


[tool.repo-review]
ignore = [
"PC160" # "Uses codespell" -> switched to https://github.com/crate-ci/typos
]


[tool.cibuildwheel]
build = "cp3*"
skip = "*-musllinux_*"
Expand Down
4 changes: 2 additions & 2 deletions src/QuantumComputation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1058,7 +1058,7 @@ void QuantumComputation::addVariable(const SymbolOrNumber& expr) {
if (std::holds_alternative<Symbolic>(expr)) {
const auto& sym = std::get<Symbolic>(expr);
for (const auto& term : sym) {
occuringVariables.insert(term.getVar());
occurringVariables.insert(term.getVar());
}
}
}
Expand All @@ -1081,7 +1081,7 @@ void QuantumComputation::instantiateInplace(
// after an operation is instantiated, the respective parameters can be
// removed from the circuit
for (const auto& [var, _] : assignment) {
occuringVariables.erase(var);
occurringVariables.erase(var);
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/mqt/core/plugins/qiskit.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,16 +285,16 @@ def _parse_symbolic_expression(qiskit_expr: ParameterExpression | float) -> floa
is_const = True
for summand in _SUM_REGEX.findall(expr_str):
sign = 1
summand_no_operaror = summand
summand_no_operator = summand
if summand[0] == "+":
summand_no_operaror = summand[1:]
summand_no_operator = summand[1:]
elif summand[0] == "-":
summand_no_operaror = summand[1:]
summand_no_operator = summand[1:]
sign = -1

coeff = 1.0
var = ""
for factor in _PROD_REGEX.findall(summand_no_operaror):
for factor in _PROD_REGEX.findall(summand_no_operator):
is_div = False
factor_no_operator = factor
if factor[0] == "*":
Expand Down
2 changes: 1 addition & 1 deletion src/operations/ClassicControlledOperation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ std::ostream& operator<<(std::ostream& os, const ComparisonKind& kind) {
return os;
}

ComparisonKind getInvertedComparsionKind(const ComparisonKind kind) {
ComparisonKind getInvertedComparisonKind(const ComparisonKind kind) {
switch (kind) {
case Lt:
return Geq;
Expand Down
6 changes: 3 additions & 3 deletions src/parsers/QASM3Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -785,12 +785,12 @@ class OpenQasm3Parser final : public InstVisitor {
std::move(thenOps), creg->second, rhs->getUInt(), *comparisonKind));
}
if (!ifStatement->elseStatements.empty()) {
const auto invertedComparsionKind =
qc::getInvertedComparsionKind(*comparisonKind);
const auto invertedComparisonKind =
qc::getInvertedComparisonKind(*comparisonKind);
auto elseOps = translateBlockOperations(ifStatement->elseStatements);
qc->emplace_back(std::make_unique<qc::ClassicControlledOperation>(
std::move(elseOps), creg->second, rhs->getUInt(),
invertedComparsionKind));
invertedComparisonKind));
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/zx/ZXDiagram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,8 @@ ZXDiagram& ZXDiagram::concat(const ZXDiagram& rhs) {
}
} // add new edges

const auto nOuptuts = outputs.size();
for (size_t i = 0; i < nOuptuts; ++i) {
const auto nOutputs = outputs.size();
for (size_t i = 0; i < nOutputs; ++i) {
removeVertex(outputs[i]);
outputs[i] = newVs[rhs.outputs[i]];
}
Expand Down
2 changes: 1 addition & 1 deletion test/dd/test_dd_noise_functionality.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ TEST_F(DDNoiseFunctionalityTest, StochSimulateAdder4TrackAPD) {
EXPECT_NEAR(measSummary["1111"], 0.011037316662706232, tolerance);
}

TEST_F(DDNoiseFunctionalityTest, StochSimulateAdder4IdentiyError) {
TEST_F(DDNoiseFunctionalityTest, StochSimulateAdder4IdentityError) {
auto dd = std::make_unique<StochasticNoiseTestPackage>(qc.getNqubits());

std::map<std::string, double, std::less<>> measSummary = {
Expand Down
2 changes: 1 addition & 1 deletion test/na/test_nadefinitions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ TEST(NADefinitions, Point) {
TEST(NADefinitions, PointDistances) {
const Point p1(0, 0);
const Point p2(3, 4);
EXPECT_EQ(p1.getEuclidianDistance(p2), 5);
EXPECT_EQ(p1.getEuclideanDistance(p2), 5);
EXPECT_EQ(p1.getManhattanDistanceX(p2), 3);
EXPECT_EQ(p1.getManhattanDistanceY(p2), 4);
EXPECT_EQ(p2.getManhattanDistanceX(p1), 3);
Expand Down
4 changes: 2 additions & 2 deletions test/test_operation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ TEST(CompoundOperation, GlobalIsInverseOf) {
}

TEST(CompoundOperation, IsInverseOf) {
// This functionality is not implemented yet, the function isInversOf leads to
// false negatives
// This functionality is not implemented yet, the function isInverseOf leads
// to false negatives
qc::CompoundOperation op1;
op1.emplace_back<qc::StandardOperation>(0, qc::OpType::RY,
std::vector{-qc::PI_2});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ TEST(ElidePermutations, simpleInitialLayout) {
EXPECT_EQ(qc.outputPermutation[0], 0);
}

TEST(ElidePermutations, applyPermutionCompound) {
TEST(ElidePermutations, applyPermutationCompound) {
QuantumComputation qc(2);
qc.cx(0, 1);
auto op = qc.asCompoundOperation();
Expand Down
2 changes: 1 addition & 1 deletion test/unittests/test_qasm3_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ TEST_F(Qasm3ParserTest, ImportQasm3IfElseStatement) {
" x q[1];\n"
"}\n"
"if (c " +
toString(getInvertedComparsionKind(comparisonKind)) +
toString(getInvertedComparisonKind(comparisonKind)) +
" 1) {\n"
" x q[0];\n"
" x q[1];\n"
Expand Down
4 changes: 2 additions & 2 deletions test/unittests/test_qfr_functionality.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1715,7 +1715,7 @@ TEST_F(QFRFunctionality, addControlNonUnitaryOperation) {
EXPECT_THROW(op.removeControl(Controls::const_iterator{}), QFRException);
}

TEST_F(QFRFunctionality, addControlCompundOperation) {
TEST_F(QFRFunctionality, addControlCompoundOperation) {
auto op = CompoundOperation();

auto control0 = 0U;
Expand Down Expand Up @@ -1774,7 +1774,7 @@ TEST_F(QFRFunctionality, addTargetAsControl) {
EXPECT_THROW(symbolicOp.addControl(control), QFRException);
}

TEST_F(QFRFunctionality, addControlCompundOperationInvalid) {
TEST_F(QFRFunctionality, addControlCompoundOperationInvalid) {
auto op = CompoundOperation();

auto control1 = 1U;
Expand Down

0 comments on commit 99c8901

Please sign in to comment.