|
106 | 106 | "source": [
|
107 | 107 | "import numpy as np\n",
|
108 | 108 | "\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", |
111 | 111 | "X_1 = (X_1 - np.average(X_1)) / np.std(X_1)\n",
|
112 | 112 | "X_2 = (X_2 - np.average(X_2)) / np.std(X_2)"
|
113 | 113 | ]
|
|
135 | 135 | }
|
136 | 136 | ],
|
137 | 137 | "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)" |
141 | 141 | ]
|
142 | 142 | },
|
143 | 143 | {
|
|
175 | 175 | "source": [
|
176 | 176 | "import pandas as pd\n",
|
177 | 177 | "\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})" |
182 | 179 | ]
|
183 | 180 | },
|
184 | 181 | {
|
|
248 | 245 | ],
|
249 | 246 | "source": [
|
250 | 247 | "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)" |
253 | 250 | ]
|
254 | 251 | },
|
255 | 252 | {
|
|
444 | 441 | ],
|
445 | 442 | "source": [
|
446 | 443 | "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)" |
449 | 446 | ]
|
450 | 447 | },
|
451 | 448 | {
|
|
610 | 607 | }
|
611 | 608 | ],
|
612 | 609 | "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", |
615 | 612 | "print(tensor_product1)\n",
|
616 | 613 | "print(tensor_product2)"
|
617 | 614 | ]
|
|
633 | 630 | }
|
634 | 631 | ],
|
635 | 632 | "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", |
637 | 634 | "print(psi)"
|
638 | 635 | ]
|
639 | 636 | },
|
|
685 | 682 | }
|
686 | 683 | ],
|
687 | 684 | "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", |
689 | 686 | "print(rho_partial_trace)"
|
690 | 687 | ]
|
691 | 688 | },
|
|
900 | 897 | "outputs": [],
|
901 | 898 | "source": [
|
902 | 899 | "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 | + " )" |
904 | 906 | ]
|
905 | 907 | },
|
906 | 908 | {
|
|
942 | 944 | "source": [
|
943 | 945 | "circuit = Circuit()\n",
|
944 | 946 | "\n",
|
945 |
| - "for i in [1,2]:\n", |
| 947 | + "for i in [1, 2]:\n", |
946 | 948 | " circuit.unitary(matrix=rotation_matrix(0.465), targets=[i])\n",
|
947 | 949 | "\n",
|
948 | 950 | "circuit.h(3)\n",
|
949 | 951 | "\n",
|
950 |
| - "for (i, j) in [[1,0], [2,4]]:\n", |
| 952 | + "for i, j in [[1, 0], [2, 4]]:\n", |
951 | 953 | " circuit.cnot(i, j)\n",
|
952 |
| - " \n", |
953 |
| - "for i in [0,4]:\n", |
| 954 | + "\n", |
| 955 | + "for i in [0, 4]:\n", |
954 | 956 | " circuit.unitary(matrix=rotation_matrix(1.570), targets=[i])\n",
|
955 | 957 | "\n",
|
956 |
| - "for i in [1,2]:\n", |
| 958 | + "for i in [1, 2]:\n", |
957 | 959 | " circuit.unitary(matrix=rotation_matrix(1.950), targets=[i])\n",
|
958 | 960 | "\n",
|
959 |
| - "for i in [1,2]:\n", |
| 961 | + "for i in [1, 2]:\n", |
960 | 962 | " circuit.h(i)\n",
|
961 | 963 | "\n",
|
962 |
| - "circuit.cnot(2,1)\n", |
| 964 | + "circuit.cnot(2, 1)\n", |
963 | 965 | "\n",
|
964 | 966 | "circuit.h(2)\n",
|
965 | 967 | "\n",
|
966 |
| - "circuit.cnot(2,1)\n", |
| 968 | + "circuit.cnot(2, 1)\n", |
967 | 969 | "\n",
|
968 | 970 | "circuit.ti(1)\n",
|
969 | 971 | "\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", |
971 | 973 | " circuit.cnot(i, j)\n",
|
972 | 974 | "\n",
|
973 | 975 | "circuit.t(1)\n",
|
974 | 976 | "\n",
|
975 |
| - "for (i, j) in [[3,2], [2,1]]:\n", |
| 977 | + "for i, j in [[3, 2], [2, 1]]:\n", |
976 | 978 | " circuit.cnot(i, j)\n",
|
977 | 979 | "\n",
|
978 | 980 | "circuit.ti(1)\n",
|
979 | 981 | "\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", |
981 | 983 | " circuit.cnot(i, j)\n",
|
982 | 984 | "\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", |
984 | 986 | " circuit.t(i)\n",
|
985 |
| - " circuit.cnot(j,k)\n", |
| 987 | + " circuit.cnot(j, k)\n", |
986 | 988 | "\n",
|
987 | 989 | "circuit.ti(2)\n",
|
988 | 990 | "circuit.t(3)\n",
|
989 | 991 | "\n",
|
990 |
| - "circuit.cnot(3,2)\n", |
| 992 | + "circuit.cnot(3, 2)\n", |
991 | 993 | "\n",
|
992 |
| - "for i in [2,3]:\n", |
| 994 | + "for i in [2, 3]:\n", |
993 | 995 | " circuit.h(i)\n",
|
994 | 996 | "\n",
|
995 |
| - "circuit.cnot(2,1)\n", |
| 997 | + "circuit.cnot(2, 1)\n", |
996 | 998 | "\n",
|
997 |
| - "#measurement part of the quantum circuit\n", |
| 999 | + "# measurement part of the quantum circuit\n", |
998 | 1000 | "circuit.expectation(Observable.Z(), target=[3])\n",
|
999 | 1001 | "circuit.sample(observable=Observable.Z(), target=3)\n",
|
1000 | 1002 | "circuit.probability(target=3)\n",
|
|
1104 | 1106 | ],
|
1105 | 1107 | "source": [
|
1106 | 1108 | "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)" |
1108 | 1110 | ]
|
1109 | 1111 | },
|
1110 | 1112 | {
|
|
1136 | 1138 | }
|
1137 | 1139 | ],
|
1138 | 1140 | "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)" |
1141 | 1143 | ]
|
1142 | 1144 | },
|
1143 | 1145 | {
|
|
1240 | 1242 | ],
|
1241 | 1243 | "source": [
|
1242 | 1244 | "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)" |
1244 | 1246 | ]
|
1245 | 1247 | },
|
1246 | 1248 | {
|
|
1272 | 1274 | }
|
1273 | 1275 | ],
|
1274 | 1276 | "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)" |
1277 | 1279 | ]
|
1278 | 1280 | },
|
1279 | 1281 | {
|
|
0 commit comments