Skip to content

Commit

Permalink
ci: updates some linting rules
Browse files Browse the repository at this point in the history
- mypy did not run in CI at all
- black was not run on the docs folder

I also updated the rules in the Makefile to respect the currently active
Python environment (by prepending the `python -m`). This was also done
in Qiskit Nature a while back.
  • Loading branch information
mrossinek committed Sep 8, 2023
1 parent 4464447 commit c1b068f
Show file tree
Hide file tree
Showing 16 changed files with 315 additions and 222 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ jobs:
- uses: ./.github/actions/install-algorithms
- run: make lint
shell: bash
- run: make mypy
shell: bash
- name: Algorithms Unit Tests under Python ${{ matrix.python-version }}
uses: ./.github/actions/run-tests
with:
Expand Down
21 changes: 12 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,29 +35,32 @@ endif
# You can set this variable from the command line.
SPHINXOPTS =

.PHONY: lint style black test test_ci spell copyright html doctest clean_sphinx coverage clean
.PHONY: lint mypy style black test test_ci spell copyright html doctest clean_sphinx coverage clean

all_check: spell style lint copyright clean_sphinx html doctest
all_check: spell style lint copyright mypy clean_sphinx html doctest

lint:
pylint -rn qiskit_algorithms test tools
python tools/verify_headers.py qiskit_algorithms test tools

mypy:
python -m mypy qiskit_algorithms test tools

style:
black --check qiskit_algorithms test tools
python -m black --check qiskit_algorithms test tools docs

black:
black qiskit_algorithms test tools
python -m black qiskit_algorithms test tools docs

test:
python -m unittest discover -v test

test_ci:
echo "Detected $(NPROCS) CPUs running with $(CONCURRENCY) workers"
stestr run --concurrency $(CONCURRENCY)
python -m stestr run --concurrency $(CONCURRENCY)

spell:
pylint -rn --disable=all --enable=spelling --spelling-dict=en_US qiskit_algorithms test tools
python -m pylint -rn --disable=all --enable=spelling --spelling-dict=en_US qiskit_algorithms test tools
sphinx-build -M spelling docs docs/_build -W -T --keep-going $(SPHINXOPTS)

copyright:
Expand All @@ -73,10 +76,10 @@ clean_sphinx:
make -C docs clean

coverage:
coverage3 run --source qiskit_algorithms -m unittest discover -s test -q
coverage3 report
python -m coverage3 run --source qiskit_algorithms -m unittest discover -s test -q
python -m coverage3 report

coverage_erase:
coverage erase
python -m coverage erase

clean: clean_sphinx coverage_erase;
9 changes: 5 additions & 4 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
sys.path.append(os.path.abspath("."))

project = "Qiskit Algorithms"
copyright = f"2017-{datetime.date.today().year}, Qiskit Algorithms Development Team" # pylint: disable=redefined-builtin
copyright = f"2017-{datetime.date.today().year}, Qiskit Algorithms Development Team" # pylint: disable=redefined-builtin
author = "Qiskit Algorithms Development Team"

# The short X.Y version
Expand Down Expand Up @@ -55,7 +55,7 @@
"matplotlib.sphinxext.plot_directive",
"sphinx.ext.doctest",
"qiskit_sphinx_theme",
"nbsphinx"
"nbsphinx",
]

rst_prolog = """
Expand All @@ -81,7 +81,9 @@
__"""

vers = version.split(".")
link_str = f" https://github.com/qiskit-community/qiskit-algorithms/blob/stable/{vers[0]}.{vers[1]}/docs/"
link_str = (
f" https://github.com/qiskit-community/qiskit-algorithms/blob/stable/{vers[0]}.{vers[1]}/docs/"
)
nbsphinx_prolog += link_str + "{{ docname }}"

nbsphinx_timeout = 360
Expand Down Expand Up @@ -162,4 +164,3 @@
# >> code
# output
doctest_test_doctest_blocks = ""

19 changes: 11 additions & 8 deletions docs/tutorials/01_algorithms_introduction.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,15 @@
"source": [
"from qiskit.quantum_info import SparsePauliOp\n",
"\n",
"H2_op = SparsePauliOp.from_list([\n",
" (\"II\", -1.052373245772859),\n",
" (\"IZ\", 0.39793742484318045),\n",
" (\"ZI\", -0.39793742484318045),\n",
" (\"ZZ\", -0.01128010425623538),\n",
" (\"XX\", 0.18093119978423156)\n",
"])"
"H2_op = SparsePauliOp.from_list(\n",
" [\n",
" (\"II\", -1.052373245772859),\n",
" (\"IZ\", 0.39793742484318045),\n",
" (\"ZI\", -0.39793742484318045),\n",
" (\"ZZ\", -0.01128010425623538),\n",
" (\"XX\", 0.18093119978423156),\n",
" ]\n",
")"
]
},
{
Expand Down Expand Up @@ -305,8 +307,9 @@
],
"source": [
"import qiskit.tools.jupyter\n",
"\n",
"%qiskit_version_table\n",
"%qiskit_copyright\n"
"%qiskit_copyright"
]
}
],
Expand Down
10 changes: 3 additions & 7 deletions docs/tutorials/02_vqe_advanced_options.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,7 @@
" values.append(mean)\n",
"\n",
"\n",
"vqe = VQE(\n",
" estimator, ansatz, optimizer, callback=store_intermediate_result, gradient=gradient\n",
")\n",
"vqe = VQE(estimator, ansatz, optimizer, callback=store_intermediate_result, gradient=gradient)\n",
"\n",
"result = vqe.compute_minimum_eigenvalue(operator=H2_op)\n",
"print(f\"Value using Gradient: {result.eigenvalue.real:.5f}\")"
Expand Down Expand Up @@ -435,9 +433,7 @@
"ansatz1 = TwoLocal(rotation_blocks=\"ry\", entanglement_blocks=\"cz\")\n",
"optimizer1 = SLSQP(maxiter=1000)\n",
"\n",
"vqe1 = VQE(\n",
" estimator1, ansatz1, optimizer1, gradient=gradient1, initial_point=initial_pt\n",
")\n",
"vqe1 = VQE(estimator1, ansatz1, optimizer1, gradient=gradient1, initial_point=initial_pt)\n",
"result1 = vqe1.compute_minimum_eigenvalue(operator=H2_op)\n",
"print(result1)\n",
"\n",
Expand Down Expand Up @@ -507,7 +503,7 @@
"import qiskit.tools.jupyter\n",
"\n",
"%qiskit_version_table\n",
"%qiskit_copyright\n"
"%qiskit_copyright"
]
}
],
Expand Down
15 changes: 5 additions & 10 deletions docs/tutorials/03_vqe_simulation_with_noise.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
"counts = []\n",
"values = []\n",
"\n",
"\n",
"def store_intermediate_result(eval_count, parameters, mean, std):\n",
" counts.append(eval_count)\n",
" values.append(mean)"
Expand Down Expand Up @@ -162,15 +163,11 @@
"# instantiate and run VQE\n",
"from qiskit_algorithms import VQE\n",
"\n",
"vqe = VQE(\n",
" noiseless_estimator, ansatz, optimizer=spsa, callback=store_intermediate_result\n",
")\n",
"vqe = VQE(noiseless_estimator, ansatz, optimizer=spsa, callback=store_intermediate_result)\n",
"result = vqe.compute_minimum_eigenvalue(operator=H2_op)\n",
"\n",
"print(f\"VQE on Aer qasm simulator (no noise): {result.eigenvalue.real:.5f}\")\n",
"print(\n",
" f\"Delta from reference energy value is {(result.eigenvalue.real - ref_value):.5f}\"\n",
")"
"print(f\"Delta from reference energy value is {(result.eigenvalue.real - ref_value):.5f}\")"
]
},
{
Expand Down Expand Up @@ -324,9 +321,7 @@
"result1 = vqe.compute_minimum_eigenvalue(operator=H2_op)\n",
"\n",
"print(f\"VQE on Aer qasm simulator (with noise): {result1.eigenvalue.real:.5f}\")\n",
"print(\n",
" f\"Delta from reference energy value is {(result1.eigenvalue.real - ref_value):.5f}\"\n",
")"
"print(f\"Delta from reference energy value is {(result1.eigenvalue.real - ref_value):.5f}\")"
]
},
{
Expand Down Expand Up @@ -442,7 +437,7 @@
"import qiskit.tools.jupyter\n",
"\n",
"%qiskit_version_table\n",
"%qiskit_copyright\n"
"%qiskit_copyright"
]
}
],
Expand Down
12 changes: 7 additions & 5 deletions docs/tutorials/04_vqd.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
"ansatz = TwoLocal(2, rotation_blocks=[\"ry\", \"rz\"], entanglement_blocks=\"cz\", reps=1)\n",
"\n",
"optimizer = SLSQP()\n",
"ansatz.decompose().draw('mpl')"
"ansatz.decompose().draw(\"mpl\")"
]
},
{
Expand Down Expand Up @@ -148,6 +148,7 @@
"values = []\n",
"steps = []\n",
"\n",
"\n",
"def callback(eval_count, params, value, meta, step):\n",
" counts.append(eval_count)\n",
" values.append(value)\n",
Expand All @@ -170,8 +171,8 @@
"from qiskit_algorithms import VQD\n",
"\n",
"vqd = VQD(estimator, fidelity, ansatz, optimizer, k=k, betas=betas, callback=callback)\n",
"result = vqd.compute_eigenvalues(operator = H2_op)\n",
"vqd_values = result.eigenvalues\n"
"result = vqd.compute_eigenvalues(operator=H2_op)\n",
"vqd_values = result.eigenvalues"
]
},
{
Expand Down Expand Up @@ -237,7 +238,7 @@
"counts = np.asarray(counts)\n",
"values = np.asarray(values)\n",
"\n",
"for i in range(1,4):\n",
"for i in range(1, 4):\n",
" _counts = counts[np.where(steps == i)]\n",
" _values = values[np.where(steps == i)]\n",
" pylab.plot(_counts, _values, label=f\"State {i-1}\")\n",
Expand Down Expand Up @@ -338,8 +339,9 @@
],
"source": [
"import qiskit.tools.jupyter\n",
"\n",
"%qiskit_version_table\n",
"%qiskit_copyright\n"
"%qiskit_copyright"
]
}
],
Expand Down
28 changes: 17 additions & 11 deletions docs/tutorials/05_qaoa.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@
"import networkx as nx\n",
"\n",
"num_nodes = 4\n",
"w = np.array([[0., 1., 1., 0.],\n",
" [1., 0., 1., 1.],\n",
" [1., 1., 0., 1.],\n",
" [0., 1., 1., 0.]])\n",
"w = np.array(\n",
" [[0.0, 1.0, 1.0, 0.0], [1.0, 0.0, 1.0, 1.0], [1.0, 1.0, 0.0, 1.0], [0.0, 1.0, 1.0, 0.0]]\n",
")\n",
"G = nx.from_numpy_array(w)"
]
},
Expand All @@ -56,9 +55,9 @@
],
"source": [
"layout = nx.random_layout(G, seed=10)\n",
"colors = ['r', 'g', 'b', 'y']\n",
"colors = [\"r\", \"g\", \"b\", \"y\"]\n",
"nx.draw(G, layout, node_color=colors)\n",
"labels = nx.get_edge_attributes(G, 'weight')\n",
"labels = nx.get_edge_attributes(G, \"weight\")\n",
"nx.draw_networkx_edge_labels(G, pos=layout, edge_labels=labels);"
]
},
Expand Down Expand Up @@ -95,10 +94,12 @@
" w_01 = np.where(w != 0, 1, 0)\n",
" return np.sum(w_01 * X)\n",
"\n",
"\n",
"def bitfield(n, L):\n",
" result = np.binary_repr(n, L)\n",
" return [int(digit) for digit in result] # [2:] to chop off the \"0b\" part\n",
"\n",
"\n",
"# use the brute-force way to generate the oracle\n",
"L = num_nodes\n",
"max = 2**L\n",
Expand All @@ -114,7 +115,7 @@
" if cur_v < sol:\n",
" sol = cur_v\n",
"\n",
"print(f'Objective value computed by the brute-force method is {sol}')"
"print(f\"Objective value computed by the brute-force method is {sol}\")"
]
},
{
Expand All @@ -132,6 +133,7 @@
"source": [
"from qiskit.quantum_info import Pauli, SparsePauliOp\n",
"\n",
"\n",
"def get_operator(weight_matrix):\n",
" r\"\"\"Generate Hamiltonian for the graph partitioning\n",
" Notes:\n",
Expand Down Expand Up @@ -179,6 +181,7 @@
"\n",
" return SparsePauliOp(pauli_list, coeffs=coeffs), shift\n",
"\n",
"\n",
"qubit_op, offset = get_operator(w)"
]
},
Expand Down Expand Up @@ -215,6 +218,7 @@
"\n",
"sampler = Sampler()\n",
"\n",
"\n",
"def sample_most_likely(state_vector):\n",
" \"\"\"Compute the most likely binary string from state vector.\n",
" Args:\n",
Expand All @@ -233,6 +237,7 @@
" x.reverse()\n",
" return np.asarray(x)\n",
"\n",
"\n",
"algorithm_globals.random_seed = 10598\n",
"\n",
"optimizer = COBYLA()\n",
Expand All @@ -243,7 +248,7 @@
"x = sample_most_likely(result.eigenstate)\n",
"\n",
"print(x)\n",
"print(f'Objective value computed by QAOA is {objective_value(x, w)}')"
"print(f\"Objective value computed by QAOA is {objective_value(x, w)}\")"
]
},
{
Expand Down Expand Up @@ -277,7 +282,7 @@
"x = sample_most_likely(result.eigenstate)\n",
"\n",
"print(x)\n",
"print(f'Objective value computed by the NumPyMinimumEigensolver is {objective_value(x, w)}')"
"print(f\"Objective value computed by the NumPyMinimumEigensolver is {objective_value(x, w)}\")"
]
},
{
Expand Down Expand Up @@ -318,7 +323,7 @@
"x = sample_most_likely(result.eigenstate)\n",
"\n",
"print(x)\n",
"print(f\"Objective value computed by SamplingVQE is {objective_value(x, w)}\")\n"
"print(f\"Objective value computed by SamplingVQE is {objective_value(x, w)}\")"
]
},
{
Expand Down Expand Up @@ -357,8 +362,9 @@
],
"source": [
"import qiskit.tools.jupyter\n",
"\n",
"%qiskit_version_table\n",
"%qiskit_copyright\n"
"%qiskit_copyright"
]
}
],
Expand Down
Loading

0 comments on commit c1b068f

Please sign in to comment.