Skip to content

Commit

Permalink
Docs preview for PR #1844.
Browse files Browse the repository at this point in the history
  • Loading branch information
cuda-quantum-bot committed Aug 20, 2024
1 parent a95341e commit 0cbeba5
Show file tree
Hide file tree
Showing 23 changed files with 362 additions and 412 deletions.
Binary file modified pr-1844/_images/examples_python_tutorials_afqmc_22_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 18 additions & 16 deletions pr-1844/_sources/examples/python/tutorials/afqmc.ipynb.txt

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,12 @@
" Args:\n",
" n_qubits (int): The number of qubits in the quantum circuit.\n",
" probs (Union[float, list[float]]): Probabilities of a bit flip error occurring on each qubit.\n",
" If a single probability value is provided, it will be applied to all qubits. If a list of\n",
" probabilities is provided, each qubit will have its own probability.\n",
" If a single probability value is provided, it will be applied to all qubits. If a list of\n",
" probabilities is provided, each qubit will have its own probability.\n",
"\n",
" Returns:\n",
" cudaq noise model\n",
" \"\"\"\n",
" \n",
" noise = cudaq.NoiseModel()\n",
"\n",
" if not isinstance(probs, list):\n",
Expand Down Expand Up @@ -131,7 +130,9 @@
" for i, val in enumerate(state_label):\n",
" if val == 1:\n",
" x(qvector[i])\n",
" rx(0.0, qvector[i]) # Identity gate at the end on each qubit to model readout error\n",
" rx(\n",
" 0.0, qvector[i]\n",
" ) # Identity gate at the end on each qubit to model readout error\n",
" mz(qvector)"
]
},
Expand Down Expand Up @@ -228,14 +229,8 @@
"\n",
" # Constraint: all elements of p must be positive, and the distribution must sum to 1\n",
" cons = (\n",
" {\n",
" \"type\": \"ineq\",\n",
" \"fun\": lambda p: p\n",
" },\n",
" {\n",
" \"type\": \"eq\",\n",
" \"fun\": lambda p: np.sum(p) - 1\n",
" },\n",
" {\"type\": \"ineq\", \"fun\": lambda p: p},\n",
" {\"type\": \"eq\", \"fun\": lambda p: np.sum(p) - 1},\n",
" )\n",
" bnds = [(0, 1) for _ in range(len(empirical_dist))]\n",
" initial_value = np.random.uniform(size=len(empirical_dist))\n",
Expand Down Expand Up @@ -337,14 +332,10 @@
"\n",
"noise_1 = get_noise(n_qubits, p)\n",
"\n",
"ghz_result = cudaq.sample(ghz_kernel,\n",
" n_qubits,\n",
" shots_count=shots,\n",
" noise_model=noise_1)\n",
"ghz_result = cudaq.sample(ghz_kernel, n_qubits, shots_count=shots, noise_model=noise_1)\n",
"noisy_dict_1 = dict(list(ghz_result.items()))\n",
"\n",
"noisy_res_1 = np.array(\n",
" [noisy_dict_1.get(state, 0) for i, state in enumerate(states)])\n",
"noisy_res_1 = np.array([noisy_dict_1.get(state, 0) for i, state in enumerate(states)])\n",
"\n",
"noisy_dict_1"
]
Expand Down Expand Up @@ -387,14 +378,10 @@
"\n",
"noise_2 = get_noise(n_qubits, p)\n",
"\n",
"ghz_result = cudaq.sample(ghz_kernel,\n",
" n_qubits,\n",
" shots_count=shots,\n",
" noise_model=noise_2)\n",
"ghz_result = cudaq.sample(ghz_kernel, n_qubits, shots_count=shots, noise_model=noise_2)\n",
"noisy_dict_2 = dict(list(ghz_result.items()))\n",
"\n",
"noisy_res_2 = np.array(\n",
" [noisy_dict_2.get(state, 0) for i, state in enumerate(states)])\n",
"noisy_res_2 = np.array([noisy_dict_2.get(state, 0) for i, state in enumerate(states)])\n",
"\n",
"noisy_dict_2"
]
Expand Down Expand Up @@ -498,8 +485,7 @@
"source": [
"A = reduce(np.kron, [A_1] * n_qubits) # joint confusion matrix\n",
"A_pinv = np.linalg.pinv(A) # Generate pseudoinverse confusion matrix.\n",
"mitigated = np.array(np.dot(A_pinv, noisy_res_1),\n",
" dtype=int) # Obtain mitigated counts\n",
"mitigated = np.array(np.dot(A_pinv, noisy_res_1), dtype=int) # Obtain mitigated counts\n",
"print(f\"Mitigated counts:\\n{mitigated}\")\n",
"\n",
"if not np.all(mitigated >= 0):\n",
Expand All @@ -526,16 +512,15 @@
}
],
"source": [
"df = pd.DataFrame({\n",
" \"states\": states,\n",
" \"noisy\": np.around(noisy_res_1 / sum(noisy_res_1), 3),\n",
" \"mitigated_sg\": np.around(mitigated / sum(mitigated), 3),\n",
"})\n",
"df = pd.DataFrame(\n",
" {\n",
" \"states\": states,\n",
" \"noisy\": np.around(noisy_res_1 / sum(noisy_res_1), 3),\n",
" \"mitigated_sg\": np.around(mitigated / sum(mitigated), 3),\n",
" }\n",
")\n",
"\n",
"ax = df.plot(x=\"states\",\n",
" y=[\"noisy\", \"mitigated_sg\"],\n",
" kind=\"bar\",\n",
" figsize=(8, 5))\n",
"ax = df.plot(x=\"states\", y=[\"noisy\", \"mitigated_sg\"], kind=\"bar\", figsize=(8, 5))\n",
"ax.bar_label(ax.containers[0], labels=df[\"noisy\"])\n",
"ax.bar_label(ax.containers[1], labels=df[\"mitigated_sg\"])\n",
"ax.set_ylabel(\"probabilities\")\n",
Expand Down Expand Up @@ -576,11 +561,8 @@
"local_states = [\"0\" * n_qubits, \"1\" * n_qubits]\n",
"\n",
"results = [\n",
" cudaq.sample(kernel,\n",
" n_qubits,\n",
" label,\n",
" shots_count=shots,\n",
" noise_model=noise_2) for label in local_labels\n",
" cudaq.sample(kernel, n_qubits, label, shots_count=shots, noise_model=noise_2)\n",
" for label in local_labels\n",
"]\n",
"\n",
"for i, state in enumerate(local_states):\n",
Expand All @@ -603,9 +585,7 @@
"metadata": {},
"outputs": [],
"source": [
"counts = [\n",
" dict(list(results[i].items())) for i, state in enumerate(local_states)\n",
"]\n",
"counts = [dict(list(results[i].items())) for i, state in enumerate(local_states)]\n",
"matrices = []\n",
"\n",
"for k in range(n_qubits):\n",
Expand All @@ -620,8 +600,7 @@
" # matrix[i][j] is the probability of counting i for expected j\n",
" for i in range(2):\n",
" for j in range(2):\n",
" matrix[i][j] = marginalized_counts[j].get(str(i),\n",
" 0) / total_shots[j]\n",
" matrix[i][j] = marginalized_counts[j].get(str(i), 0) / total_shots[j]\n",
" matrices.append(matrix)"
]
},
Expand Down Expand Up @@ -718,16 +697,15 @@
}
],
"source": [
"df = pd.DataFrame({\n",
" \"states\": states,\n",
" \"noisy\": np.around(noisy_res_2 / sum(noisy_res_2), 3),\n",
" \"mitigated_k_local\": np.around(mitigated / sum(mitigated), 3),\n",
"})\n",
"df = pd.DataFrame(\n",
" {\n",
" \"states\": states,\n",
" \"noisy\": np.around(noisy_res_2 / sum(noisy_res_2), 3),\n",
" \"mitigated_k_local\": np.around(mitigated / sum(mitigated), 3),\n",
" }\n",
")\n",
"\n",
"ax = df.plot(x=\"states\",\n",
" y=[\"noisy\", \"mitigated_k_local\"],\n",
" kind=\"bar\",\n",
" figsize=(8, 5))\n",
"ax = df.plot(x=\"states\", y=[\"noisy\", \"mitigated_k_local\"], kind=\"bar\", figsize=(8, 5))\n",
"ax.bar_label(ax.containers[0], labels=df[\"noisy\"])\n",
"ax.bar_label(ax.containers[1], labels=df[\"mitigated_k_local\"])\n",
"ax.set_ylabel(\"probabilities\")\n",
Expand Down Expand Up @@ -767,11 +745,8 @@
],
"source": [
"results = [\n",
" cudaq.sample(kernel,\n",
" n_qubits,\n",
" label,\n",
" shots_count=shots,\n",
" noise_model=noise_2) for label in labels\n",
" cudaq.sample(kernel, n_qubits, label, shots_count=shots, noise_model=noise_2)\n",
" for label in labels\n",
"]\n",
"\n",
"for i, state in enumerate(states):\n",
Expand Down Expand Up @@ -891,7 +866,7 @@
" x=\"states\",\n",
" y=[\"noisy\", \"mitigated_k_local\", \"mitigated_full\"],\n",
" kind=\"bar\",\n",
" figsize=(12, 5)\n",
" figsize=(12, 5),\n",
")\n",
"ax.bar_label(ax.containers[0], labels=df[\"noisy\"])\n",
"ax.bar_label(ax.containers[1], labels=df[\"mitigated_k_local\"])\n",
Expand All @@ -917,7 +892,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
"version": "3.10.0"
}
},
"nbformat": 4,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
"\n",
"What are the possible states a qubit can be in and how can we build up a visual cue to help us make sense of quantum states and their evolution?\n",
"\n",
"We know our qubit can have two distinct states: $\\ket{0}$ and $\\ket{1}$. Maybe we need a one-dimensional line whose vertices can\n",
"represent each of the states. We also know that qubits can be in an equal superposition states: $\\ket{+}$ and $\\ket{-}$. This now forces us to extend our 1D line to a 2D Cartesian coordinate system. If you dive deeper you will learn about the existence of states like \n",
"$\\ket{+i}$ and $\\ket{-i}$, this calls for a 3D extension.\n",
"We know our qubit can have two distinct states: $\\ket{0}$ and $\\ket{1}$. If these were the only two states, we could represent them as two vectors on a one-dimensional line (i.e., the z-axis in the image below). We also know that qubits can be in an equal superposition of states: $\\ket{+}$ and $\\ket{-}$. In order to capture all of the states in equal superposition, we will need a 2D plane (i.e., the $xy$-plane in the image below). If you dive deeper you will learn about the existence of other states that will call for a 3D extension. \n",
"\n",
"It turns out that a sphere is able to depict all the possible states of a single qubit. This is called a Bloch sphere. \n",
"In general, a quantum state can be written in the form $\\ket{\\psi} = \\cos(\\frac{\\theta}{2})\\ket{0}+e^{i\\varphi}\\sin(\\frac{\\theta}{2})\\ket{1}$ where $\\theta$ is a real number between $0$ and $\\pi$ and $\\varphi$ is a real value between $0$ and $2\\pi$. For example, the minus state, $\\ket{-} = \\frac{1}{\\sqrt{2}}\\ket{0}- \\frac{1}{\\sqrt{2}}\\ket{1}$, can be rewritten as\n",
"$$\\ket{-} = \\cos(\\frac{\\theta}{2})\\ket{0}+e^{i\\varphi}\\sin(\\frac{\\theta}{2})\\ket{1}\\text{ with }\\theta = \\frac{\\pi}{2}\\text{ and }\\varphi = \\pi.$$ \n",
"This can be visualized in the image below as a unit vector pointing in the direction of the negative $x$-axis.\n",
"\n",
"Using spherical coordinates, it is possible to depict all the possible states of a single qubit on a sphere. This is called a Bloch sphere. \n",
"\n",
"<img src=\"images/Bloch_sphere.png\" alt=\"Bloch Sphere\" width=\"300\" height=\"300\">\n"
]
Expand Down
4 changes: 2 additions & 2 deletions pr-1844/_sources/examples/python/tutorials/vqe.ipynb.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"import numpy as np\n",
"\n",
"# Single precision\n",
"# cudaq.set_target(\"nvidia\")\n",
"cudaq.set_target(\"nvidia\")\n",
"# Double precision\n",
"#cudaq.set_target(\"nvidia-fp64\")"
]
Expand Down Expand Up @@ -123,7 +123,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"-1.1371744305855904\n"
"-1.1371744305855906\n"
]
}
],
Expand Down
2 changes: 1 addition & 1 deletion pr-1844/_sources/specification/quake-dialect.md.txt
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ is as follows: In value form we need to follow a chain of values to
know the qubit operators are being applied to, in this example:

```text
Memory Value
Mmeory Value
%q0 [%q0_0, %q0_1 ... %q0_L, %q0_M; %q0_P ... %q0_Y, %q0_Z]

```
Expand Down
2 changes: 1 addition & 1 deletion pr-1844/api/languages/python_api.html
Original file line number Diff line number Diff line change
Expand Up @@ -2094,7 +2094,7 @@ <h2>Data Types<a class="headerlink" href="#data-types" title="Permalink to this
<em class="property"><span class="pre">static</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">random</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#cudaq.SpinOperator.random" title="Permalink to this definition"></a></dt>
<dd><dl class="py function">
<dt class="sig sig-object py">
<span class="sig-name descname"><span class="pre">random</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">qubit_count</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><a class="reference external" href="https://docs.python.org/3/library/functions.html#int" title="(in Python v3.12)"><span class="pre">int</span></a></span></em>, <em class="sig-param"><span class="n"><span class="pre">term_count</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><a class="reference external" href="https://docs.python.org/3/library/functions.html#int" title="(in Python v3.12)"><span class="pre">int</span></a></span></em>, <em class="sig-param"><span class="n"><span class="pre">seed</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><a class="reference external" href="https://docs.python.org/3/library/functions.html#int" title="(in Python v3.12)"><span class="pre">int</span></a></span><span class="w"> </span><span class="o"><span class="pre">=</span></span><span class="w"> </span><span class="default_value"><span class="pre">3349907914</span></span></em><span class="sig-paren">)</span> <span class="sig-return"><span class="sig-return-icon">&#x2192;</span> <span class="sig-return-typehint"><a class="reference internal" href="#cudaq.SpinOperator" title="cudaq.mlir._mlir_libs._quakeDialects.cudaq_runtime.SpinOperator"><span class="pre">cudaq.mlir._mlir_libs._quakeDialects.cudaq_runtime.SpinOperator</span></a></span></span></dt>
<span class="sig-name descname"><span class="pre">random</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">qubit_count</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><a class="reference external" href="https://docs.python.org/3/library/functions.html#int" title="(in Python v3.12)"><span class="pre">int</span></a></span></em>, <em class="sig-param"><span class="n"><span class="pre">term_count</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><a class="reference external" href="https://docs.python.org/3/library/functions.html#int" title="(in Python v3.12)"><span class="pre">int</span></a></span></em>, <em class="sig-param"><span class="n"><span class="pre">seed</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><a class="reference external" href="https://docs.python.org/3/library/functions.html#int" title="(in Python v3.12)"><span class="pre">int</span></a></span><span class="w"> </span><span class="o"><span class="pre">=</span></span><span class="w"> </span><span class="default_value"><span class="pre">994175775</span></span></em><span class="sig-paren">)</span> <span class="sig-return"><span class="sig-return-icon">&#x2192;</span> <span class="sig-return-typehint"><a class="reference internal" href="#cudaq.SpinOperator" title="cudaq.mlir._mlir_libs._quakeDialects.cudaq_runtime.SpinOperator"><span class="pre">cudaq.mlir._mlir_libs._quakeDialects.cudaq_runtime.SpinOperator</span></a></span></span></dt>
<dd></dd></dl>

<p>Return a random <a class="reference internal" href="#cudaq.SpinOperator" title="cudaq.SpinOperator"><code class="xref py py-class docutils literal notranslate"><span class="pre">SpinOperator</span></code></a> on the given number of qubits (<code class="code docutils literal notranslate"><span class="pre">qubit_count</span></code>) and composed of the given number of terms (<code class="code docutils literal notranslate"><span class="pre">term_count</span></code>). An optional seed value may also be provided.</p>
Expand Down
Loading

0 comments on commit 0cbeba5

Please sign in to comment.