Skip to content

Commit 5f0e5e6

Browse files
authored
infra: onboard to ruff (#153)
1 parent e30611b commit 5f0e5e6

32 files changed

+525
-488
lines changed

.github/workflows/python-package.yml

+2-4
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,11 @@ jobs:
2828
python-version: ${{ matrix.python-version }}
2929
- name: Install dependencies
3030
run: |
31-
pip install --upgrade pip
32-
pip install -e .[test]
31+
pip install tox
3332
- name: Check code format
3433
run: |
3534
# stop the build if there are Python format errors or undefined names
36-
black --check .
37-
flake8
35+
tox -e linters
3836
- name: Run unit tests
3937
run: |
4038
tox -e unit-tests

notebooks/advanced_algorithms/Quantum_Principal_Component_Analysis.ipynb

+43-41
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@
106106
"source": [
107107
"import numpy as np\n",
108108
"\n",
109-
"X_1 = [4,3,4,4,3,3,3,3,4,4,4,5,4,3,4]\n",
110-
"X_2 = [3028,1365,2726,2538,1318,1693,1412,1632,2875,3564,4412,4444,4278,3064,3857]\n",
109+
"X_1 = [4, 3, 4, 4, 3, 3, 3, 3, 4, 4, 4, 5, 4, 3, 4]\n",
110+
"X_2 = [3028, 1365, 2726, 2538, 1318, 1693, 1412, 1632, 2875, 3564, 4412, 4444, 4278, 3064, 3857]\n",
111111
"X_1 = (X_1 - np.average(X_1)) / np.std(X_1)\n",
112112
"X_2 = (X_2 - np.average(X_2)) / np.std(X_2)"
113113
]
@@ -135,9 +135,9 @@
135135
}
136136
],
137137
"source": [
138-
"print('The rescaled feature vectors are')\n",
139-
"print('X_1 = ', X_1)\n",
140-
"print('X_2 = ', X_2)"
138+
"print(\"The rescaled feature vectors are\")\n",
139+
"print(\"X_1 = \", X_1)\n",
140+
"print(\"X_2 = \", X_2)"
141141
]
142142
},
143143
{
@@ -175,10 +175,7 @@
175175
"source": [
176176
"import pandas as pd\n",
177177
"\n",
178-
"df = pd.DataFrame(\n",
179-
" {'X_1': X_1,\n",
180-
" 'X_2': X_2}\n",
181-
" )"
178+
"df = pd.DataFrame({\"X_1\": X_1, \"X_2\": X_2})"
182179
]
183180
},
184181
{
@@ -248,8 +245,8 @@
248245
],
249246
"source": [
250247
"sigma_eigenvalues, sigma_eigenvectors = np.linalg.eig(sigma)\n",
251-
"print('sigma_eigenvalues: ', sigma_eigenvalues)\n",
252-
"print('sigma_eigenvectors: ', sigma_eigenvectors)"
248+
"print(\"sigma_eigenvalues: \", sigma_eigenvalues)\n",
249+
"print(\"sigma_eigenvectors: \", sigma_eigenvectors)"
253250
]
254251
},
255252
{
@@ -444,8 +441,8 @@
444441
],
445442
"source": [
446443
"rho_eig_val, rho_eig_vec = np.linalg.eig(rho)\n",
447-
"print('rho_eig_val: ', rho_eig_val)\n",
448-
"print('rho_eig_vec: ', rho_eig_vec)"
444+
"print(\"rho_eig_val: \", rho_eig_val)\n",
445+
"print(\"rho_eig_vec: \", rho_eig_vec)"
449446
]
450447
},
451448
{
@@ -610,8 +607,8 @@
610607
}
611608
],
612609
"source": [
613-
"tensor_product1 = np.vstack((np.flip(rho_eig_vec[1]),np.zeros(2))).ravel('F')\n",
614-
"tensor_product2 = np.vstack((np.zeros(2),np.flip(rho_eig_vec[0]))).ravel('F')\n",
610+
"tensor_product1 = np.vstack((np.flip(rho_eig_vec[1]), np.zeros(2))).ravel(\"F\")\n",
611+
"tensor_product2 = np.vstack((np.zeros(2), np.flip(rho_eig_vec[0]))).ravel(\"F\")\n",
615612
"print(tensor_product1)\n",
616613
"print(tensor_product2)"
617614
]
@@ -633,7 +630,7 @@
633630
}
634631
],
635632
"source": [
636-
"psi = sqrt_eig_val[0]*tensor_product1 + sqrt_eig_val[1]*tensor_product2\n",
633+
"psi = sqrt_eig_val[0] * tensor_product1 + sqrt_eig_val[1] * tensor_product2\n",
637634
"print(psi)"
638635
]
639636
},
@@ -685,7 +682,7 @@
685682
}
686683
],
687684
"source": [
688-
"rho_partial_trace = np.dot(psi.reshape((4,1)),psi.reshape((4,1)).transpose())\n",
685+
"rho_partial_trace = np.dot(psi.reshape((4, 1)), psi.reshape((4, 1)).transpose())\n",
689686
"print(rho_partial_trace)"
690687
]
691688
},
@@ -900,7 +897,12 @@
900897
"outputs": [],
901898
"source": [
902899
"def rotation_matrix(value):\n",
903-
" return np.array([[np.cos(value/2), -np.exp(1j*value)*np.sin(value/2)],[np.exp(1j*value)*np.sin(value/2), np.exp(2*1j*value)*np.cos(value/2)]])"
900+
" return np.array(\n",
901+
" [\n",
902+
" [np.cos(value / 2), -np.exp(1j * value) * np.sin(value / 2)],\n",
903+
" [np.exp(1j * value) * np.sin(value / 2), np.exp(2 * 1j * value) * np.cos(value / 2)],\n",
904+
" ]\n",
905+
" )"
904906
]
905907
},
906908
{
@@ -942,59 +944,59 @@
942944
"source": [
943945
"circuit = Circuit()\n",
944946
"\n",
945-
"for i in [1,2]:\n",
947+
"for i in [1, 2]:\n",
946948
" circuit.unitary(matrix=rotation_matrix(0.465), targets=[i])\n",
947949
"\n",
948950
"circuit.h(3)\n",
949951
"\n",
950-
"for (i, j) in [[1,0], [2,4]]:\n",
952+
"for i, j in [[1, 0], [2, 4]]:\n",
951953
" circuit.cnot(i, j)\n",
952-
" \n",
953-
"for i in [0,4]:\n",
954+
"\n",
955+
"for i in [0, 4]:\n",
954956
" circuit.unitary(matrix=rotation_matrix(1.570), targets=[i])\n",
955957
"\n",
956-
"for i in [1,2]:\n",
958+
"for i in [1, 2]:\n",
957959
" circuit.unitary(matrix=rotation_matrix(1.950), targets=[i])\n",
958960
"\n",
959-
"for i in [1,2]:\n",
961+
"for i in [1, 2]:\n",
960962
" circuit.h(i)\n",
961963
"\n",
962-
"circuit.cnot(2,1)\n",
964+
"circuit.cnot(2, 1)\n",
963965
"\n",
964966
"circuit.h(2)\n",
965967
"\n",
966-
"circuit.cnot(2,1)\n",
968+
"circuit.cnot(2, 1)\n",
967969
"\n",
968970
"circuit.ti(1)\n",
969971
"\n",
970-
"for (i, j) in [[2,1], [3,2], [2,1]]:\n",
972+
"for i, j in [[2, 1], [3, 2], [2, 1]]:\n",
971973
" circuit.cnot(i, j)\n",
972974
"\n",
973975
"circuit.t(1)\n",
974976
"\n",
975-
"for (i, j) in [[3,2], [2,1]]:\n",
977+
"for i, j in [[3, 2], [2, 1]]:\n",
976978
" circuit.cnot(i, j)\n",
977979
"\n",
978980
"circuit.ti(1)\n",
979981
"\n",
980-
"for (i, j) in [[2,1], [3,2], [2,1]]:\n",
982+
"for i, j in [[2, 1], [3, 2], [2, 1]]:\n",
981983
" circuit.cnot(i, j)\n",
982984
"\n",
983-
"for (i,j,k) in [[1,3,2],[2,3,2]]:\n",
985+
"for i, j, k in [[1, 3, 2], [2, 3, 2]]:\n",
984986
" circuit.t(i)\n",
985-
" circuit.cnot(j,k)\n",
987+
" circuit.cnot(j, k)\n",
986988
"\n",
987989
"circuit.ti(2)\n",
988990
"circuit.t(3)\n",
989991
"\n",
990-
"circuit.cnot(3,2)\n",
992+
"circuit.cnot(3, 2)\n",
991993
"\n",
992-
"for i in [2,3]:\n",
994+
"for i in [2, 3]:\n",
993995
" circuit.h(i)\n",
994996
"\n",
995-
"circuit.cnot(2,1)\n",
997+
"circuit.cnot(2, 1)\n",
996998
"\n",
997-
"#measurement part of the quantum circuit\n",
999+
"# measurement part of the quantum circuit\n",
9981000
"circuit.expectation(Observable.Z(), target=[3])\n",
9991001
"circuit.sample(observable=Observable.Z(), target=3)\n",
10001002
"circuit.probability(target=3)\n",
@@ -1104,7 +1106,7 @@
11041106
],
11051107
"source": [
11061108
"v_1 = (1 + np.sqrt(2 * purity_sdk - 1)) / 2 * np.trace(sigma)\n",
1107-
"print('The first eigenvalue obtained by the quantum PCA using Amazon Braket SDK is: \\n', v_1)"
1109+
"print(\"The first eigenvalue obtained by the quantum PCA using Amazon Braket SDK is: \\n\", v_1)"
11081110
]
11091111
},
11101112
{
@@ -1136,8 +1138,8 @@
11361138
}
11371139
],
11381140
"source": [
1139-
"perc_v1 = abs((v_1-1.945575)/1.945575)*100\n",
1140-
"print('percent error first eigenvalue (%): ', perc_v1)"
1141+
"perc_v1 = abs((v_1 - 1.945575) / 1.945575) * 100\n",
1142+
"print(\"percent error first eigenvalue (%): \", perc_v1)"
11411143
]
11421144
},
11431145
{
@@ -1240,7 +1242,7 @@
12401242
],
12411243
"source": [
12421244
"a_1 = (1 + np.sqrt(2 * purity_sv1 - 1)) / 2 * np.trace(sigma)\n",
1243-
"print('The first eigenvalue obtained by the quantum PCA using Amazon Braket SV1 is: \\n', a_1)"
1245+
"print(\"The first eigenvalue obtained by the quantum PCA using Amazon Braket SV1 is: \\n\", a_1)"
12441246
]
12451247
},
12461248
{
@@ -1272,8 +1274,8 @@
12721274
}
12731275
],
12741276
"source": [
1275-
"perc_sv1 = abs((a_1-1.945575)/1.945575)*100\n",
1276-
"print('percent error first eigenvalue (%): ', perc_sv1)"
1277+
"perc_sv1 = abs((a_1 - 1.945575) / 1.945575) * 100\n",
1278+
"print(\"percent error first eigenvalue (%): \", perc_sv1)"
12771279
]
12781280
},
12791281
{

notebooks/auxiliary_functions/Random_Circuit.ipynb

+1-4
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,7 @@
6767
"# Code here\n",
6868
"local_simulator = LocalSimulator()\n",
6969
"gate_set = [CNot, Rx, Rz, CPhaseShift, XY]\n",
70-
"circuit = random_circuit(num_qubits=5, \n",
71-
" num_gates=30,\n",
72-
" gate_set=gate_set,\n",
73-
" seed=42)\n",
70+
"circuit = random_circuit(num_qubits=5, num_gates=30, gate_set=gate_set, seed=42)\n",
7471
"task = local_simulator.run(circuit, shots=100)\n",
7572
"result = task.result()\n",
7673
"print(\"--Circuit--\")\n",

notebooks/textbook/Bells_Inequality.ipynb

+7-5
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,10 @@
166166
"metadata": {},
167167
"outputs": [],
168168
"source": [
169-
"# # Uncomment to run on a QPU \n",
170-
"# from braket.aws import AwsDevice \n",
171-
"# iqm_garnet = AwsDevice(\"arn:aws:braket:eu-north-1::device/qpu/iqm/Garnet\") \n",
172-
"# iqm_tasks = run_bell_inequality([circAB, circAC, circBC], iqm_garnet, shots=1000) \n",
169+
"# # Uncomment to run on a QPU\n",
170+
"# from braket.aws import AwsDevice\n",
171+
"# iqm_garnet = AwsDevice(\"arn:aws:braket:eu-north-1::device/qpu/iqm/Garnet\")\n",
172+
"# iqm_tasks = run_bell_inequality([circAB, circAC, circBC], iqm_garnet, shots=1000)\n",
173173
"# results, pAB, pAC, pBC = get_bell_inequality_results(iqm_tasks)\n"
174174
]
175175
},
@@ -199,7 +199,9 @@
199199
"source": [
200200
"print(\"Task Summary\")\n",
201201
"print(f\"{tracker.quantum_tasks_statistics()} \\n\")\n",
202-
"print(f\"Estimated cost to run this example: {tracker.qpu_tasks_cost() + tracker.simulator_tasks_cost():.2f} USD\")"
202+
"print(\n",
203+
" f\"Estimated cost to run this example: {tracker.qpu_tasks_cost() + tracker.simulator_tasks_cost():.2f} USD\"\n",
204+
")"
203205
]
204206
},
205207
{

notebooks/textbook/Bernstein_Vazirani_Algorithm.ipynb

+5-3
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@
4444
"from braket.experimental.algorithms.bernstein_vazirani import (\n",
4545
" bernstein_vazirani_circuit,\n",
4646
" get_bernstein_vazirani_results,\n",
47-
" run_bernstein_vazirani\n",
47+
" run_bernstein_vazirani,\n",
4848
")\n",
4949
"\n",
50-
"tracker = Tracker().start() # to track Braket costs"
50+
"tracker = Tracker().start() # to track Braket costs"
5151
]
5252
},
5353
{
@@ -223,7 +223,9 @@
223223
"source": [
224224
"print(\"Task Summary\")\n",
225225
"print(f\"{tracker.quantum_tasks_statistics()} \\n\")\n",
226-
"print(f\"Estimated cost to run this example: {tracker.qpu_tasks_cost() + tracker.simulator_tasks_cost():.2f} USD\")"
226+
"print(\n",
227+
" f\"Estimated cost to run this example: {tracker.qpu_tasks_cost() + tracker.simulator_tasks_cost():.2f} USD\"\n",
228+
")"
227229
]
228230
},
229231
{

0 commit comments

Comments
 (0)