Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

infra: onboard to ruff #153

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,11 @@ jobs:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
pip install --upgrade pip
pip install -e .[test]
pip install tox
- name: Check code format
run: |
# stop the build if there are Python format errors or undefined names
black --check .
flake8
tox -e linters
- name: Run unit tests
run: |
tox -e unit-tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@
"source": [
"import numpy as np\n",
"\n",
"X_1 = [4,3,4,4,3,3,3,3,4,4,4,5,4,3,4]\n",
"X_2 = [3028,1365,2726,2538,1318,1693,1412,1632,2875,3564,4412,4444,4278,3064,3857]\n",
"X_1 = [4, 3, 4, 4, 3, 3, 3, 3, 4, 4, 4, 5, 4, 3, 4]\n",
"X_2 = [3028, 1365, 2726, 2538, 1318, 1693, 1412, 1632, 2875, 3564, 4412, 4444, 4278, 3064, 3857]\n",
"X_1 = (X_1 - np.average(X_1)) / np.std(X_1)\n",
"X_2 = (X_2 - np.average(X_2)) / np.std(X_2)"
]
Expand Down Expand Up @@ -135,9 +135,9 @@
}
],
"source": [
"print('The rescaled feature vectors are')\n",
"print('X_1 = ', X_1)\n",
"print('X_2 = ', X_2)"
"print(\"The rescaled feature vectors are\")\n",
"print(\"X_1 = \", X_1)\n",
"print(\"X_2 = \", X_2)"
]
},
{
Expand Down Expand Up @@ -175,10 +175,7 @@
"source": [
"import pandas as pd\n",
"\n",
"df = pd.DataFrame(\n",
" {'X_1': X_1,\n",
" 'X_2': X_2}\n",
" )"
"df = pd.DataFrame({\"X_1\": X_1, \"X_2\": X_2})"
]
},
{
Expand Down Expand Up @@ -248,8 +245,8 @@
],
"source": [
"sigma_eigenvalues, sigma_eigenvectors = np.linalg.eig(sigma)\n",
"print('sigma_eigenvalues: ', sigma_eigenvalues)\n",
"print('sigma_eigenvectors: ', sigma_eigenvectors)"
"print(\"sigma_eigenvalues: \", sigma_eigenvalues)\n",
"print(\"sigma_eigenvectors: \", sigma_eigenvectors)"
]
},
{
Expand Down Expand Up @@ -444,8 +441,8 @@
],
"source": [
"rho_eig_val, rho_eig_vec = np.linalg.eig(rho)\n",
"print('rho_eig_val: ', rho_eig_val)\n",
"print('rho_eig_vec: ', rho_eig_vec)"
"print(\"rho_eig_val: \", rho_eig_val)\n",
"print(\"rho_eig_vec: \", rho_eig_vec)"
]
},
{
Expand Down Expand Up @@ -610,8 +607,8 @@
}
],
"source": [
"tensor_product1 = np.vstack((np.flip(rho_eig_vec[1]),np.zeros(2))).ravel('F')\n",
"tensor_product2 = np.vstack((np.zeros(2),np.flip(rho_eig_vec[0]))).ravel('F')\n",
"tensor_product1 = np.vstack((np.flip(rho_eig_vec[1]), np.zeros(2))).ravel(\"F\")\n",
"tensor_product2 = np.vstack((np.zeros(2), np.flip(rho_eig_vec[0]))).ravel(\"F\")\n",
"print(tensor_product1)\n",
"print(tensor_product2)"
]
Expand All @@ -633,7 +630,7 @@
}
],
"source": [
"psi = sqrt_eig_val[0]*tensor_product1 + sqrt_eig_val[1]*tensor_product2\n",
"psi = sqrt_eig_val[0] * tensor_product1 + sqrt_eig_val[1] * tensor_product2\n",
"print(psi)"
]
},
Expand Down Expand Up @@ -685,7 +682,7 @@
}
],
"source": [
"rho_partial_trace = np.dot(psi.reshape((4,1)),psi.reshape((4,1)).transpose())\n",
"rho_partial_trace = np.dot(psi.reshape((4, 1)), psi.reshape((4, 1)).transpose())\n",
"print(rho_partial_trace)"
]
},
Expand Down Expand Up @@ -900,7 +897,12 @@
"outputs": [],
"source": [
"def rotation_matrix(value):\n",
" 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)]])"
" return np.array(\n",
" [\n",
" [np.cos(value / 2), -np.exp(1j * value) * np.sin(value / 2)],\n",
" [np.exp(1j * value) * np.sin(value / 2), np.exp(2 * 1j * value) * np.cos(value / 2)],\n",
" ]\n",
" )"
]
},
{
Expand Down Expand Up @@ -942,59 +944,59 @@
"source": [
"circuit = Circuit()\n",
"\n",
"for i in [1,2]:\n",
"for i in [1, 2]:\n",
" circuit.unitary(matrix=rotation_matrix(0.465), targets=[i])\n",
"\n",
"circuit.h(3)\n",
"\n",
"for (i, j) in [[1,0], [2,4]]:\n",
"for i, j in [[1, 0], [2, 4]]:\n",
" circuit.cnot(i, j)\n",
" \n",
"for i in [0,4]:\n",
"\n",
"for i in [0, 4]:\n",
" circuit.unitary(matrix=rotation_matrix(1.570), targets=[i])\n",
"\n",
"for i in [1,2]:\n",
"for i in [1, 2]:\n",
" circuit.unitary(matrix=rotation_matrix(1.950), targets=[i])\n",
"\n",
"for i in [1,2]:\n",
"for i in [1, 2]:\n",
" circuit.h(i)\n",
"\n",
"circuit.cnot(2,1)\n",
"circuit.cnot(2, 1)\n",
"\n",
"circuit.h(2)\n",
"\n",
"circuit.cnot(2,1)\n",
"circuit.cnot(2, 1)\n",
"\n",
"circuit.ti(1)\n",
"\n",
"for (i, j) in [[2,1], [3,2], [2,1]]:\n",
"for i, j in [[2, 1], [3, 2], [2, 1]]:\n",
" circuit.cnot(i, j)\n",
"\n",
"circuit.t(1)\n",
"\n",
"for (i, j) in [[3,2], [2,1]]:\n",
"for i, j in [[3, 2], [2, 1]]:\n",
" circuit.cnot(i, j)\n",
"\n",
"circuit.ti(1)\n",
"\n",
"for (i, j) in [[2,1], [3,2], [2,1]]:\n",
"for i, j in [[2, 1], [3, 2], [2, 1]]:\n",
" circuit.cnot(i, j)\n",
"\n",
"for (i,j,k) in [[1,3,2],[2,3,2]]:\n",
"for i, j, k in [[1, 3, 2], [2, 3, 2]]:\n",
" circuit.t(i)\n",
" circuit.cnot(j,k)\n",
" circuit.cnot(j, k)\n",
"\n",
"circuit.ti(2)\n",
"circuit.t(3)\n",
"\n",
"circuit.cnot(3,2)\n",
"circuit.cnot(3, 2)\n",
"\n",
"for i in [2,3]:\n",
"for i in [2, 3]:\n",
" circuit.h(i)\n",
"\n",
"circuit.cnot(2,1)\n",
"circuit.cnot(2, 1)\n",
"\n",
"#measurement part of the quantum circuit\n",
"# measurement part of the quantum circuit\n",
"circuit.expectation(Observable.Z(), target=[3])\n",
"circuit.sample(observable=Observable.Z(), target=3)\n",
"circuit.probability(target=3)\n",
Expand Down Expand Up @@ -1104,7 +1106,7 @@
],
"source": [
"v_1 = (1 + np.sqrt(2 * purity_sdk - 1)) / 2 * np.trace(sigma)\n",
"print('The first eigenvalue obtained by the quantum PCA using Amazon Braket SDK is: \\n', v_1)"
"print(\"The first eigenvalue obtained by the quantum PCA using Amazon Braket SDK is: \\n\", v_1)"
]
},
{
Expand Down Expand Up @@ -1136,8 +1138,8 @@
}
],
"source": [
"perc_v1 = abs((v_1-1.945575)/1.945575)*100\n",
"print('percent error first eigenvalue (%): ', perc_v1)"
"perc_v1 = abs((v_1 - 1.945575) / 1.945575) * 100\n",
"print(\"percent error first eigenvalue (%): \", perc_v1)"
]
},
{
Expand Down Expand Up @@ -1240,7 +1242,7 @@
],
"source": [
"a_1 = (1 + np.sqrt(2 * purity_sv1 - 1)) / 2 * np.trace(sigma)\n",
"print('The first eigenvalue obtained by the quantum PCA using Amazon Braket SV1 is: \\n', a_1)"
"print(\"The first eigenvalue obtained by the quantum PCA using Amazon Braket SV1 is: \\n\", a_1)"
]
},
{
Expand Down Expand Up @@ -1272,8 +1274,8 @@
}
],
"source": [
"perc_sv1 = abs((a_1-1.945575)/1.945575)*100\n",
"print('percent error first eigenvalue (%): ', perc_sv1)"
"perc_sv1 = abs((a_1 - 1.945575) / 1.945575) * 100\n",
"print(\"percent error first eigenvalue (%): \", perc_sv1)"
]
},
{
Expand Down
5 changes: 1 addition & 4 deletions notebooks/auxiliary_functions/Random_Circuit.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,7 @@
"# Code here\n",
"local_simulator = LocalSimulator()\n",
"gate_set = [CNot, Rx, Rz, CPhaseShift, XY]\n",
"circuit = random_circuit(num_qubits=5, \n",
" num_gates=30,\n",
" gate_set=gate_set,\n",
" seed=42)\n",
"circuit = random_circuit(num_qubits=5, num_gates=30, gate_set=gate_set, seed=42)\n",
"task = local_simulator.run(circuit, shots=100)\n",
"result = task.result()\n",
"print(\"--Circuit--\")\n",
Expand Down
12 changes: 7 additions & 5 deletions notebooks/textbook/Bells_Inequality.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,10 @@
"metadata": {},
"outputs": [],
"source": [
"# # Uncomment to run on a QPU \n",
"# from braket.aws import AwsDevice \n",
"# iqm_garnet = AwsDevice(\"arn:aws:braket:eu-north-1::device/qpu/iqm/Garnet\") \n",
"# iqm_tasks = run_bell_inequality([circAB, circAC, circBC], iqm_garnet, shots=1000) \n",
"# # Uncomment to run on a QPU\n",
"# from braket.aws import AwsDevice\n",
"# iqm_garnet = AwsDevice(\"arn:aws:braket:eu-north-1::device/qpu/iqm/Garnet\")\n",
"# iqm_tasks = run_bell_inequality([circAB, circAC, circBC], iqm_garnet, shots=1000)\n",
"# results, pAB, pAC, pBC = get_bell_inequality_results(iqm_tasks)\n"
]
},
Expand Down Expand Up @@ -199,7 +199,9 @@
"source": [
"print(\"Task Summary\")\n",
"print(f\"{tracker.quantum_tasks_statistics()} \\n\")\n",
"print(f\"Estimated cost to run this example: {tracker.qpu_tasks_cost() + tracker.simulator_tasks_cost():.2f} USD\")"
"print(\n",
" f\"Estimated cost to run this example: {tracker.qpu_tasks_cost() + tracker.simulator_tasks_cost():.2f} USD\"\n",
")"
]
},
{
Expand Down
8 changes: 5 additions & 3 deletions notebooks/textbook/Bernstein_Vazirani_Algorithm.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@
"from braket.experimental.algorithms.bernstein_vazirani import (\n",
" bernstein_vazirani_circuit,\n",
" get_bernstein_vazirani_results,\n",
" run_bernstein_vazirani\n",
" run_bernstein_vazirani,\n",
")\n",
"\n",
"tracker = Tracker().start() # to track Braket costs"
"tracker = Tracker().start() # to track Braket costs"
]
},
{
Expand Down Expand Up @@ -223,7 +223,9 @@
"source": [
"print(\"Task Summary\")\n",
"print(f\"{tracker.quantum_tasks_statistics()} \\n\")\n",
"print(f\"Estimated cost to run this example: {tracker.qpu_tasks_cost() + tracker.simulator_tasks_cost():.2f} USD\")"
"print(\n",
" f\"Estimated cost to run this example: {tracker.qpu_tasks_cost() + tracker.simulator_tasks_cost():.2f} USD\"\n",
")"
]
},
{
Expand Down
Loading