Skip to content

Commit

Permalink
integrate linters for jupyter notebooks and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dakk committed Dec 22, 2023
1 parent 472b0ae commit 6375114
Show file tree
Hide file tree
Showing 34 changed files with 219 additions and 159 deletions.
1 change: 1 addition & 0 deletions docs/source/example_big_circuit.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"source": [
"from qlasskit import Qint8, Qlist, boolopt, qlassfa\n",
"\n",
"\n",
"@qlassfa(bool_optimizer=boolopt.fastOptimizer)\n",
"def test(message: Qlist[Qint8, 32]) -> Qint8:\n",
" h_val = Qint8(0)\n",
Expand Down
7 changes: 4 additions & 3 deletions docs/source/example_deutsch_jozsa.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"source": [
"from qlasskit import qlassf, Qint4\n",
"\n",
"\n",
"@qlassf\n",
"def f(a: Qint4) -> bool:\n",
" return a > 7"
Expand All @@ -38,7 +39,7 @@
}
],
"source": [
"f.export('qiskit').draw('mpl')"
"f.export(\"qiskit\").draw(\"mpl\")"
]
},
{
Expand Down Expand Up @@ -70,8 +71,8 @@
}
],
"source": [
"qc = q_algo.export('qiskit')\n",
"qc.draw('mpl')"
"qc = q_algo.export(\"qiskit\")\n",
"qc.draw(\"mpl\")"
]
},
{
Expand Down
9 changes: 5 additions & 4 deletions docs/source/example_grover.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@
"source": [
"from qlasskit import qlassf, Qlist, Qint2\n",
"\n",
"\n",
"@qlassf\n",
"def and_all(a_list: Qlist[bool, 4]) -> bool:\n",
" r = True\n",
" for i in a_list:\n",
" r = r and i\n",
" return r "
" return r"
]
},
{
Expand Down Expand Up @@ -51,7 +52,7 @@
}
],
"source": [
"and_all.export('qiskit').draw('mpl')"
"and_all.export(\"qiskit\").draw(\"mpl\")"
]
},
{
Expand Down Expand Up @@ -97,8 +98,8 @@
}
],
"source": [
"qc = q_algo.export('qiskit')\n",
"qc.draw('mpl')"
"qc = q_algo.export(\"qiskit\")\n",
"qc.draw(\"mpl\")"
]
},
{
Expand Down
6 changes: 4 additions & 2 deletions docs/source/example_grover_factors.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
"from qlasskit import qlassf, Qint2\n",
"from qlasskit.algorithms import Grover\n",
"\n",
"\n",
"@qlassf\n",
"def factorize(a: Tuple[Qint2, Qint2]) -> bool:\n",
" return a[0] * a[1] == 9\n",
"\n",
"\n",
"q_algo = Grover(factorize)"
]
},
Expand All @@ -45,7 +47,7 @@
"from qiskit import Aer, QuantumCircuit, transpile\n",
"from qiskit.visualization import plot_histogram\n",
"\n",
"qc = q_algo.export('qiskit')\n",
"qc = q_algo.export(\"qiskit\")\n",
"qc.measure_all()\n",
"simulator = Aer.get_backend(\"aer_simulator\")\n",
"circ = transpile(qc, simulator)\n",
Expand Down Expand Up @@ -74,7 +76,7 @@
}
],
"source": [
"qc.draw('mpl')"
"qc.draw(\"mpl\")"
]
}
],
Expand Down
17 changes: 10 additions & 7 deletions docs/source/example_grover_hash.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@
"source": [
"from qlasskit import qlassf, Qint4, Qint8, Qlist\n",
"\n",
"\n",
"@qlassf\n",
"def hash_simp(m: Qlist[Qint4, 2]) -> Qint8:\n",
" hv = 0\n",
" for i in m:\n",
" hv = ((hv << 4) ^ (hv >> 1) ^ i) & 0xff\n",
" hv = ((hv << 4) ^ (hv >> 1) ^ i) & 0xFF\n",
"\n",
" return hv"
]
Expand Down Expand Up @@ -51,9 +52,11 @@
"source": [
"from collections import Counter\n",
"\n",
"d = Counter(hex(hash_simp.original_f((x, y))) for x in range(2**4) for y in range(2**4))\n",
"d = Counter(\n",
" hex(hash_simp.original_f((x, y))) for x in range(2**4) for y in range(2**4)\n",
")\n",
"\n",
"print('Hash function output space:', len(d))"
"print(\"Hash function output space:\", len(d))"
]
},
{
Expand Down Expand Up @@ -81,7 +84,7 @@
}
],
"source": [
"hash_simp.export('qiskit').draw('mpl')"
"hash_simp.export(\"qiskit\").draw(\"mpl\")"
]
},
{
Expand All @@ -100,7 +103,7 @@
"source": [
"from qlasskit.algorithms import Grover\n",
"\n",
"q_algo = Grover(hash_simp, Qint8(0xca))"
"q_algo = Grover(hash_simp, Qint8(0xCA))"
]
},
{
Expand Down Expand Up @@ -131,7 +134,7 @@
"from qiskit import Aer, QuantumCircuit, transpile\n",
"from qiskit.visualization import plot_histogram\n",
"\n",
"qc = q_algo.export('qiskit')\n",
"qc = q_algo.export(\"qiskit\")\n",
"qc.measure_all()\n",
"simulator = Aer.get_backend(\"aer_simulator\")\n",
"circ = transpile(qc, simulator)\n",
Expand Down Expand Up @@ -163,7 +166,7 @@
}
],
"source": [
"print(hex(hash_simp.original_f((12,12))))"
"print(hex(hash_simp.original_f((12, 12))))"
]
}
],
Expand Down
3 changes: 2 additions & 1 deletion docs/source/example_grover_subset.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"from qlasskit import qlassf, Qint2, Qint3\n",
"from typing import Tuple\n",
"\n",
"\n",
"@qlassf\n",
"def subset_sum(ii: Tuple[Qint2, Qint2]) -> Qint3:\n",
" l = [0, 5, 2, 3]\n",
Expand Down Expand Up @@ -72,7 +73,7 @@
"from qiskit import Aer, QuantumCircuit, transpile\n",
"from qiskit.visualization import plot_histogram\n",
"\n",
"qc = q_algo.export('qiskit')\n",
"qc = q_algo.export(\"qiskit\")\n",
"qc.measure_all()\n",
"simulator = Aer.get_backend(\"aer_simulator\")\n",
"circ = transpile(qc, simulator)\n",
Expand Down
16 changes: 9 additions & 7 deletions docs/source/example_grover_sudoku.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,18 @@
"from qlasskit import qlassf, Qint2, Qint4, Qmatrix\n",
"from qlasskit.algorithms import Grover\n",
"\n",
"\n",
"@qlassf\n",
"def sudoku_check(m: Qmatrix[bool,2,2]) -> bool:\n",
"def sudoku_check(m: Qmatrix[bool, 2, 2]) -> bool:\n",
" constr = m[0][0]\n",
" sub0 = (m[0][0] ^ m[0][1])\n",
" sub1 = (m[1][0] ^ m[1][1]) \n",
" sub2 = (m[0][0] ^ m[1][0])\n",
" sub3 = (m[0][1] ^ m[1][1])\n",
" sub0 = m[0][0] ^ m[0][1]\n",
" sub1 = m[1][0] ^ m[1][1]\n",
" sub2 = m[0][0] ^ m[1][0]\n",
" sub3 = m[0][1] ^ m[1][1]\n",
" return sub0 and sub1 and sub2 and sub3 and constr\n",
"\n",
"q_algo = Grover(sudoku_check)\n"
"\n",
"q_algo = Grover(sudoku_check)"
]
},
{
Expand Down Expand Up @@ -63,7 +65,7 @@
"from qiskit import Aer, QuantumCircuit, transpile\n",
"from qiskit.visualization import plot_histogram\n",
"\n",
"qc = q_algo.export('qiskit')\n",
"qc = q_algo.export(\"qiskit\")\n",
"qc.measure_all()\n",
"simulator = Aer.get_backend(\"aer_simulator\")\n",
"circ = transpile(qc, simulator)\n",
Expand Down
8 changes: 4 additions & 4 deletions docs/source/example_simon.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"source": [
"from qlasskit import qlassf, Qint4\n",
"\n",
"\n",
"@qlassf\n",
"def f(a: Qint4) -> Qint4:\n",
" return (a >> 3) + 1"
Expand All @@ -38,8 +39,7 @@
}
],
"source": [
"\n",
"f.export('qiskit').draw('mpl')"
"f.export(\"qiskit\").draw(\"mpl\")"
]
},
{
Expand Down Expand Up @@ -71,8 +71,8 @@
}
],
"source": [
"qc = q_algo.export('qiskit')\n",
"qc.draw('mpl')"
"qc = q_algo.export(\"qiskit\")\n",
"qc.draw(\"mpl\")"
]
},
{
Expand Down
8 changes: 5 additions & 3 deletions docs/source/example_unitary_of_f.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,18 @@
"from qiskit.visualization import array_to_latex\n",
"from qlasskit import qlassf\n",
"\n",
"\n",
"@qlassf\n",
"def f(a: bool, b: bool) -> bool:\n",
" return a ^ (not b)\n",
"\n",
"\n",
"print(f\"\\n{f}\\n\")\n",
"\n",
"qc = QuantumCircuit(f.num_qubits, f.num_qubits)\n",
"qc.append(f.gate(), f.qubits)\n",
"\n",
"qc.decompose().draw('mpl')\n"
"qc.decompose().draw(\"mpl\")"
]
},
{
Expand Down Expand Up @@ -85,10 +87,10 @@
}
],
"source": [
"simulator = Aer.get_backend('unitary_simulator')\n",
"simulator = Aer.get_backend(\"unitary_simulator\")\n",
"job = execute(qc, simulator, shots=8192)\n",
"result = job.result()\n",
"array_to_latex(result.get_unitary(qc,3), max_size=16)\n"
"array_to_latex(result.get_unitary(qc, 3), max_size=16)"
]
}
],
Expand Down
11 changes: 6 additions & 5 deletions docs/source/exporter.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"source": [
"from qlasskit import Qint2, qlassf\n",
"\n",
"\n",
"@qlassf\n",
"def hello_world(a: bool, b: Qint2) -> Qint2:\n",
" return b + (1 if a else 0)"
Expand Down Expand Up @@ -52,8 +53,8 @@
}
],
"source": [
"qc = hello_world.export('qiskit')\n",
"qc.draw('mpl')"
"qc = hello_world.export(\"qiskit\")\n",
"qc.draw(\"mpl\")"
]
},
{
Expand Down Expand Up @@ -86,8 +87,8 @@
}
],
"source": [
"qc = hello_world.export('qasm')\n",
"print (qc)"
"qc = hello_world.export(\"qasm\")\n",
"print(qc)"
]
},
{
Expand Down Expand Up @@ -135,7 +136,7 @@
"source": [
"import cirq\n",
"\n",
"qc = hello_world.export('cirq')\n",
"qc = hello_world.export(\"cirq\")\n",
"qc"
]
},
Expand Down
16 changes: 9 additions & 7 deletions docs/source/how_it_works.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@
"from qlasskit import qlassf, Qint2, Qint4\n",
"from qiskit import QuantumCircuit\n",
"\n",
"\n",
"@qlassf\n",
"def f_comp(b: bool, n: Qint2) -> Qint2:\n",
" for i in range(3):\n",
" n += (1 if b else 2)\n",
" return n"
" for i in range(3):\n",
" n += 1 if b else 2\n",
" return n"
]
},
{
Expand Down Expand Up @@ -74,13 +75,14 @@
"def f1(b: bool, n: Qint2) -> Qint2:\n",
" return n + (1 if b else 2)\n",
"\n",
"\n",
"qc = QuantumCircuit(f_comp.num_qubits * 2 - 1)\n",
"\n",
"for i in range(3):\n",
" qc.barrier(label=f\"it_{i}\")\n",
" qc.append(f1.gate(), [0] + list(range(1 + i * 2, 5 + i * 2)))\n",
"\n",
"print('Operations:', qc.decompose().count_ops())\n",
"print(\"Operations:\", qc.decompose().count_ops())\n",
"qc.decompose().draw(\"mpl\")"
]
},
Expand Down Expand Up @@ -119,7 +121,7 @@
"qc = QuantumCircuit(f_comp.num_qubits)\n",
"qc.append(f_comp.gate(), f_comp.qubits)\n",
"\n",
"print('Operations:', qc.decompose().count_ops())\n",
"print(\"Operations:\", qc.decompose().count_ops())\n",
"qc.decompose().draw(\"mpl\")"
]
},
Expand Down Expand Up @@ -150,7 +152,7 @@
"source": [
"@qlassf\n",
"def f(n: Qint4) -> bool:\n",
" return n == 3 "
" return n == 3"
]
},
{
Expand Down Expand Up @@ -222,7 +224,7 @@
}
],
"source": [
"f.export().draw('mpl')"
"f.export().draw(\"mpl\")"
]
}
],
Expand Down
Loading

0 comments on commit 6375114

Please sign in to comment.