Skip to content

Commit 215e5ab

Browse files
Docs preview for PR #2441.
1 parent 3f13fca commit 215e5ab

File tree

175 files changed

+24888
-14726
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

175 files changed

+24888
-14726
lines changed
Loading
Loading

pr-2441/_images/backends.png

3.86 MB
Loading

pr-2441/_images/circuitsimulators.png

5.89 MB
Loading

pr-2441/_images/mgpu.png

40.9 KB
Loading

pr-2441/_images/mqpu.png

38.7 KB
Loading
78.5 KB
Loading

pr-2441/_images/qpus.png

8.02 MB
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "7aa9cc8f-4e42-401f-a1fd-665e5cda19c7",
6+
"metadata": {},
7+
"source": [
8+
"# The Deutsch-Jozsa Algorithm\n",
9+
"\n",
10+
"Here is the link to the original paper: [Deutsch-Jozsa algorithm](http://rspa.royalsocietypublishing.org/content/439/1907/553). This algorithm is an earlier demonstration of the computational advantage of quantum algorithm over classical one. It addresses the problem of identifying the nature of a hidden Boolean function, which is provided as an oracle. The function is guaranteed to be either:\n",
11+
"\n",
12+
"- **Balanced**, meaning it outputs 0 for exactly half of its possible inputs and 1 for the other half.\n",
13+
"- **Constant**, meaning it outputs the same value (either 0 or 1) for all inputs.\n",
14+
"\n",
15+
"Classically, determining whether the function is balanced or constant requires evaluating the oracle multiple times. In the worst-case scenario, one would need to query at least half of the inputs to distinguish a constant function. However, the Deutsch-Jozsa algorithm demonstrates quantum superiority by solving this problem with a single query to the oracle, regardless of the input size.\n",
16+
"\n",
17+
"This notebook implements the Deutsch-Jozsa algorithm as described in [Cleve et al. 1997](https://arxiv.org/pdf/quant-ph/9708016.pdf). The input for the oracle function $f$ is a $n$-bit string. It means that for $x\\ in \\{0,1\\}^n$, the value of $f(x)$ is either constant, i.e., the same for all $x$, or balanced, i.e., exactly half of the $n$-bit string whose $f(x) = 0$."
18+
]
19+
},
20+
{
21+
"cell_type": "markdown",
22+
"id": "6edbe9a5-2a81-42e4-ac0c-50a0ef4a0dda",
23+
"metadata": {},
24+
"source": [
25+
"## The Theory\n",
26+
"\n",
27+
"Here are the steps to implement the algorithm:\n",
28+
"1. Start with initializing all input qubits and single auxiliary qubits to zero. The first $n-1$ input qubits are used for querying the oracle, and the last auxiliary qubit is used for storing the answer of the oracle\n",
29+
"$$\n",
30+
"|0\\ldots 0\\rangle |0\\rangle\n",
31+
"$$\n",
32+
"2. Create the superposition of all input qubits by applying the Hadamard gate to each qubit.\n",
33+
"$$\n",
34+
"H^{\\otimes^n} |0\\ldots 0\\rangle |0\\rangle = \\frac{1}{\\sqrt{2^n}}\\sum_{i=0}^{2^n-1}|i\\rangle |0\\rangle \n",
35+
"$$\n",
36+
"3. Apply the Pauli-X gate and apply the Hadamard gate to the auxiliary qubit. This is to store the answer of the oracle in the phase.\n",
37+
"$$\n",
38+
"\\frac{1}{\\sqrt{2^n}}\\sum_{i=0}^{2^n-1}|i\\rangle |0\\rangle \\rightarrow \\frac{1}{\\sqrt{2^{n+1}}}\\sum_{i=0}^{2^n-1}|i\\rangle ( |0\\rangle - |1\\rangle )\n",
39+
"$$\n",
40+
"4. Query the oracle.\n",
41+
"$$\n",
42+
"\\frac{1}{\\sqrt{2^{n+1}}}\\sum_{i=0}^{2^n-1}|i\\rangle ( |0\\rangle - |1\\rangle ) \\rightarrow \\frac{1}{\\sqrt{2^{n+1}}}\\sum_{i=0}^{2^n-1}(-1)^{f(i)}|i\\rangle ( |0\\rangle - |1\\rangle ) \n",
43+
"$$\n",
44+
"5. Apply the Hadamard gate to all input gates.\n",
45+
"6. Measure input gates. If measured values are non-zero, then the function is balanced. If not, then it is constant."
46+
]
47+
},
48+
{
49+
"cell_type": "markdown",
50+
"id": "5449f9a7-7b1a-4212-8b26-3edaae1fcabd",
51+
"metadata": {},
52+
"source": [
53+
"## The Algorithm Implementation\n",
54+
"\n",
55+
"Here is the CUDA-Q code following the steps outlined in the above theory section."
56+
]
57+
},
58+
{
59+
"cell_type": "code",
60+
"execution_count": 31,
61+
"id": "08e04d22-535c-4368-a495-dfe7ed5ff567",
62+
"metadata": {},
63+
"outputs": [],
64+
"source": [
65+
"# Import the CUDA-Q package and set the target to run on NVIDIA GPUs.\n",
66+
"\n",
67+
"import cudaq\n",
68+
"import random\n",
69+
"from typing import List\n",
70+
"\n",
71+
"cudaq.set_target(\"nvidia\")\n",
72+
"\n",
73+
"# Number of qubits for the Deutsch-Jozsa algorithm, the last qubit is an auxiliary qubit\n",
74+
"qubit_count = 3"
75+
]
76+
},
77+
{
78+
"cell_type": "code",
79+
"execution_count": 32,
80+
"id": "4d16e25e-d3df-4d07-9e75-a6a046680caa",
81+
"metadata": {},
82+
"outputs": [
83+
{
84+
"name": "stdout",
85+
"output_type": "stream",
86+
"text": [
87+
"Generated fx for function type = constant: [1, 1]\n",
88+
"oracleType = 0\n",
89+
"oracleValue = 1\n"
90+
]
91+
}
92+
],
93+
"source": [
94+
"# Set the function to be \"constant\" or \"balanced\"\n",
95+
"function_type = 'constant'\n",
96+
"\n",
97+
"# Initialize fx depending on whether the function is constant or balanced\n",
98+
"if function_type == 'constant':\n",
99+
" # For a constant function, fx is either all 0's or all 1's\n",
100+
" oracleType = 0 # 0 for constant\n",
101+
" fx_value = random.choice([0, 1]) # Randomly pick 0 or 1\n",
102+
" oracleValue = fx_value # In constant case, fx_value is passed, for balanced it's not used\n",
103+
" fx = [fx_value] * (qubit_count - 1)\n",
104+
"else:\n",
105+
" # For a balanced function, half of fx will be 0's and half will be 1's\n",
106+
" oracleType = 1\n",
107+
" fx = [0] * ((qubit_count - 1) // 2) + [1] * ((qubit_count - 1) - (qubit_count - 1) // 2)\n",
108+
" random.shuffle(fx) # Shuffle to randomize the positions of 0's and 1's\n",
109+
"\n",
110+
"# If needed initialize fx, oracleType, and oracleValue manually\n",
111+
"#oracleType = 0\n",
112+
"#oracleValue = 0\n",
113+
"#fx = [0,0]\n",
114+
"\n",
115+
"print(f\"Generated fx for function type = {function_type}: {fx}\")\n",
116+
"print (\"oracleType = \", oracleType)\n",
117+
"print (\"oracleValue = \", oracleValue)"
118+
]
119+
},
120+
{
121+
"cell_type": "code",
122+
"execution_count": 33,
123+
"id": "caa90b54-16d3-419d-910f-7a36e4e14829",
124+
"metadata": {},
125+
"outputs": [
126+
{
127+
"name": "stdout",
128+
"output_type": "stream",
129+
"text": [
130+
" ╭───╮╭───╮ \n",
131+
"q0 : ┤ h ├┤ h ├─────\n",
132+
" ├───┤├───┤ \n",
133+
"q1 : ┤ h ├┤ h ├─────\n",
134+
" ├───┤├───┤╭───╮\n",
135+
"q2 : ┤ x ├┤ h ├┤ x ├\n",
136+
" ╰───╯╰───╯╰───╯\n",
137+
"\n",
138+
"Input qubits measurement outcome and frequency = { 00:1 }\n",
139+
"\n",
140+
"The oracle function is constant.\n"
141+
]
142+
}
143+
],
144+
"source": [
145+
"# Define kernel\n",
146+
"@cudaq.kernel\n",
147+
"def kernel(fx: List[int], qubit_count: int, oracleType: int, oracleValue: int):\n",
148+
" # Allocate two input qubits\n",
149+
" input_qubits = cudaq.qvector(qubit_count-1)\n",
150+
" # Allocate an auxiliary qubit (initially |0⟩)\n",
151+
" auxiliary_qubit = cudaq.qubit()\n",
152+
"\n",
153+
" # Prepare the auxiliary qubit\n",
154+
" x(auxiliary_qubit)\n",
155+
" h(auxiliary_qubit)\n",
156+
"\n",
157+
" # Place the rest of the register in a superposition state\n",
158+
" h(input_qubits)\n",
159+
"\n",
160+
" # Logic for oracleType == 0 (constant oracle)\n",
161+
" if oracleType == 0:\n",
162+
" if oracleValue == 1:\n",
163+
" # Apply X gate to the auxiliary qubit\n",
164+
" x(auxiliary_qubit)\n",
165+
" elif oracleValue == 0:\n",
166+
" # Apply identity gate (do nothing)\n",
167+
" pass\n",
168+
"\n",
169+
" # Logic for oracleType == 1 (balanced oracle)\n",
170+
" elif oracleType == 1:\n",
171+
" for i in range(len(fx)):\n",
172+
" if fx[i] == 1:\n",
173+
" x.ctrl(input_qubits[i], auxiliary_qubit)\n",
174+
" \n",
175+
" # Apply Hadamard to the input qubit again after querying the oracle\n",
176+
" h(input_qubits)\n",
177+
"\n",
178+
" # Measure the input qubit to yield if the function is constant or balanced.\n",
179+
" mz(input_qubits)\n",
180+
"\n",
181+
"print(cudaq.draw(kernel, fx, qubit_count, oracleType, oracleValue))\n",
182+
"\n",
183+
"result = cudaq.sample(kernel, fx, qubit_count, oracleType, oracleValue, shots_count=1)\n",
184+
"\n",
185+
"# Debugging: Print the raw result dictionary\n",
186+
"print(f\"Input qubits measurement outcome and frequency = {result}\")\n",
187+
"\n",
188+
"# Define the expected constant results for '00' and '11' for the number of input qubits\n",
189+
"expected_constant_results = ['0' * (qubit_count - 1), '1' * (qubit_count - 1)]\n",
190+
"\n",
191+
"# Check if either '00' or '11' (or their equivalent for more qubits) appears in the result\n",
192+
"is_constant = any(result_key in result for result_key in expected_constant_results)\n",
193+
"\n",
194+
"if is_constant:\n",
195+
" print(\"The oracle function is constant.\")\n",
196+
"else:\n",
197+
" print(\"The oracle function is balanced.\")\n"
198+
]
199+
},
200+
{
201+
"cell_type": "code",
202+
"execution_count": null,
203+
"id": "278c6b13-e3a0-4f61-a25b-eed75494b376",
204+
"metadata": {},
205+
"outputs": [],
206+
"source": [
207+
"print(cudaq.__version__)"
208+
]
209+
}
210+
],
211+
"metadata": {
212+
"kernelspec": {
213+
"display_name": "Python 3",
214+
"language": "python",
215+
"name": "python3"
216+
},
217+
"language_info": {
218+
"codemirror_mode": {
219+
"name": "ipython",
220+
"version": 3
221+
},
222+
"file_extension": ".py",
223+
"mimetype": "text/x-python",
224+
"name": "python",
225+
"nbconvert_exporter": "python",
226+
"pygments_lexer": "ipython3",
227+
"version": "3.10.12"
228+
}
229+
},
230+
"nbformat": 4,
231+
"nbformat_minor": 5
232+
}

0 commit comments

Comments
 (0)