Skip to content

Commit 42b6fe8

Browse files
committed
cleanup notebook
1 parent f2841a6 commit 42b6fe8

File tree

2 files changed

+274
-2526
lines changed

2 files changed

+274
-2526
lines changed
Lines changed: 274 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,274 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"id": "0",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"import numpy as np"
11+
]
12+
},
13+
{
14+
"cell_type": "markdown",
15+
"id": "1",
16+
"metadata": {},
17+
"source": [
18+
"### Implementing [Quartic quantum speedups for planted inference](https://arxiv.org/abs/2406.19378v1)"
19+
]
20+
},
21+
{
22+
"cell_type": "markdown",
23+
"id": "2",
24+
"metadata": {},
25+
"source": [
26+
"### Problem Definition"
27+
]
28+
},
29+
{
30+
"cell_type": "markdown",
31+
"id": "3",
32+
"metadata": {},
33+
"source": [
34+
"**Definition 2.2 (simplified):**\n",
35+
"A kXOR instance $\\mathcal{I}$ on $n$ variables in ${0, 1}$ is a collection of $m$ constraints, each of the form\n",
36+
"$$ x_{c_1} \\oplus x_{c_2} \\oplus ... x_{c_k} = b $$ "
37+
]
38+
},
39+
{
40+
"cell_type": "markdown",
41+
"id": "4",
42+
"metadata": {},
43+
"source": [
44+
"**Notation 2.3**\n",
45+
"Random Instance: Pick each clause independently:\n",
46+
"- Pick $C$, a $k$ subset of $[n] = \\{1, ... n\\}$ uniformly at random.\n",
47+
"- Pick $b \\in {0, 1}$ uniformly at random."
48+
]
49+
},
50+
{
51+
"cell_type": "code",
52+
"execution_count": null,
53+
"id": "5",
54+
"metadata": {},
55+
"outputs": [],
56+
"source": [
57+
"from qualtran.bloqs.optimization.k_xor_sat import KXorInstance, Constraint\n",
58+
"\n",
59+
"n, k = 10, 4\n",
60+
"cs = (\n",
61+
" Constraint((0, 1, 2, 3), -1), # read: x_0 ^ x_1 ^ x_2 ^ x_3 = 0\n",
62+
" Constraint((0, 2, 4, 5), 1),\n",
63+
" Constraint((0, 3, 4, 5), 1),\n",
64+
" Constraint((0, 3, 4, 5), 1),\n",
65+
" Constraint((1, 2, 3, 4), -1),\n",
66+
" Constraint((1, 3, 4, 5), -1),\n",
67+
" Constraint((1, 3, 4, 5), -1),\n",
68+
" Constraint((2, 3, 4, 5), 1),\n",
69+
")\n",
70+
"simple_inst = KXorInstance(n, k, cs)\n",
71+
"simple_inst"
72+
]
73+
},
74+
{
75+
"cell_type": "markdown",
76+
"id": "6",
77+
"metadata": {},
78+
"source": [
79+
"**Notation 2.4 (simplified)** Planted Instance:\n",
80+
"Given $\\rho \\in [0, 1]$ (the _planted advantage_),\n",
81+
"\n",
82+
"first pick a secret assignment $z \\in \\{0, 1\\}^n$.\n",
83+
"Now pick each clause independently by: \n",
84+
"- Pick $C$, a $k$ subset of $[n]$ uniformly at random.\n",
85+
"- Pick noise $\\eta \\in {0, 1}$, s.t. $\\eta = 0$ with probability $(1 + \\rho)/2$\n",
86+
"- Set $b = C(z) \\oplus \\eta$\n",
87+
"\n",
88+
"Note: when $\\rho = 0$, the noise is random, and when $\\rho = 1$, there is no noise."
89+
]
90+
},
91+
{
92+
"cell_type": "code",
93+
"execution_count": null,
94+
"id": "7",
95+
"metadata": {},
96+
"outputs": [],
97+
"source": [
98+
"random_inst = KXorInstance.random_instance(\n",
99+
" n=10, \n",
100+
" m=20, \n",
101+
" k=4,\n",
102+
" planted_advantage=0.8,\n",
103+
" rng=np.random.default_rng(42),\n",
104+
")\n",
105+
"random_inst"
106+
]
107+
},
108+
{
109+
"cell_type": "markdown",
110+
"id": "8",
111+
"metadata": {},
112+
"source": [
113+
"## Problem\n",
114+
"\n",
115+
"**Problem 2.6 (Planted Noisy kXOR)**\n",
116+
"Given $\\rho \\in (0, 1)$, and an instance $\\mathcal{I}$ that is promised to be either drawn from the random distribution or planted distribution (with $\\rho$), distinguish which case it is."
117+
]
118+
},
119+
{
120+
"cell_type": "markdown",
121+
"id": "9",
122+
"metadata": {},
123+
"source": [
124+
"## Kikuchi Method\n",
125+
"This is a technique to reduce $k$XOR problems to $2$XOR problems, on an exponentially larger set of variables (say, $O(n^k)$).\n",
126+
"The 2XOR is known to be efficiently solvable by some spectral analysis.\n",
127+
"\n",
128+
"For this, we pick our new variables as subsets of $[n]$ of size $k$, call them $X_S$ for each subset $S$.\n",
129+
"There are ${n \\choose k}$ variables now, and for $k \\ll n$, this is about $O(n^k)$.\n",
130+
"\n",
131+
"The equations are of the form $X_S \\oplus X_T = b(S, T)$ for every $S, T$ with $|S \\Delta T| = k$.\n",
132+
"Here $b(S, T)$ is the xor of all variables in S and T (common ones cancel out, leaving only the $k$ as above)"
133+
]
134+
},
135+
{
136+
"cell_type": "markdown",
137+
"id": "10",
138+
"metadata": {},
139+
"source": [
140+
"## Quantum Algorithm\n",
141+
"\n",
142+
"**Theorem 4.18 (simplified)**\n",
143+
"Let $k$ (even) and $\\rho \\in (0, 1)$ be known constants.\n",
144+
"\n",
145+
"We are given an instance $\\mathcal{I}$ which is either random or planted (with advantage $\\rho$),\n",
146+
"where the number of constraints $m$ is picked above a given threshold (see Alice Theorem).\n",
147+
"\n",
148+
"For a parameter $\\ell$, if we have a classical _Kikuchi style_ algorithm with complexity $\\tilde{O}(n^\\ell)$,\n",
149+
"then there is a quantum algorithm with $\\tilde{O}(n^{\\ell/4} m \\ell^{O{\\ell}} \\log^{\\ell/2k}n)$."
150+
]
151+
},
152+
{
153+
"cell_type": "code",
154+
"execution_count": null,
155+
"id": "11",
156+
"metadata": {},
157+
"outputs": [],
158+
"source": [
159+
"from qualtran.bloqs.optimization.k_xor_sat.planted_noisy_kxor import PlantedNoisyKXOR\n",
160+
"from qualtran.drawing import show_bloq, show_call_graph, show_counts_sigma"
161+
]
162+
},
163+
{
164+
"cell_type": "code",
165+
"execution_count": null,
166+
"id": "12",
167+
"metadata": {},
168+
"outputs": [],
169+
"source": [
170+
"def make_algo_example():\n",
171+
" k = 4\n",
172+
" n, m = 100, 1000\n",
173+
" rho = 0.8\n",
174+
" \n",
175+
" c = 2 # Kikuchi param: ell = c * k\n",
176+
" \n",
177+
" # generate instance\n",
178+
" rng = np.random.default_rng(142)\n",
179+
" ell = c * k\n",
180+
" inst = KXorInstance.random_instance(n=n, m=m, k=k, planted_advantage=rho, rng=rng)\n",
181+
" algo_bloq = PlantedNoisyKXOR.from_inst(inst=inst, ell=ell, rho=rho, zeta=1 / np.log(n), rng=rng)\n",
182+
"\n",
183+
" expected_complexity = n ** (ell/4) * m * ell**ell * np.log(n)**(c//2)\n",
184+
"\n",
185+
" return algo_bloq, expected_complexity"
186+
]
187+
},
188+
{
189+
"cell_type": "code",
190+
"execution_count": null,
191+
"id": "13",
192+
"metadata": {},
193+
"outputs": [],
194+
"source": [
195+
"bloq, cost_O_tilde = make_algo_example()"
196+
]
197+
},
198+
{
199+
"cell_type": "code",
200+
"execution_count": null,
201+
"id": "14",
202+
"metadata": {},
203+
"outputs": [],
204+
"source": [
205+
"show_bloq(bloq)"
206+
]
207+
},
208+
{
209+
"cell_type": "code",
210+
"execution_count": null,
211+
"id": "15",
212+
"metadata": {},
213+
"outputs": [],
214+
"source": [
215+
"g, sigma = bloq.call_graph(max_depth=6)\n",
216+
"show_call_graph(g)"
217+
]
218+
},
219+
{
220+
"cell_type": "code",
221+
"execution_count": null,
222+
"id": "16",
223+
"metadata": {},
224+
"outputs": [],
225+
"source": [
226+
"from qualtran.resource_counting import get_cost_value, QECGatesCost\n",
227+
"\n",
228+
"gc = get_cost_value(bloq, QECGatesCost())\n",
229+
"gc.asdict()"
230+
]
231+
},
232+
{
233+
"cell_type": "code",
234+
"execution_count": null,
235+
"id": "17",
236+
"metadata": {},
237+
"outputs": [],
238+
"source": [
239+
"(gc * (1/cost_O_tilde)).asdict()"
240+
]
241+
},
242+
{
243+
"cell_type": "code",
244+
"execution_count": null,
245+
"id": "18",
246+
"metadata": {},
247+
"outputs": [],
248+
"source": [
249+
"f\"{cost_O_tilde:e}\""
250+
]
251+
}
252+
],
253+
"metadata": {
254+
"kernelspec": {
255+
"display_name": "Python 3 (ipykernel)",
256+
"language": "python",
257+
"name": "python3"
258+
},
259+
"language_info": {
260+
"codemirror_mode": {
261+
"name": "ipython",
262+
"version": 3
263+
},
264+
"file_extension": ".py",
265+
"mimetype": "text/x-python",
266+
"name": "python",
267+
"nbconvert_exporter": "python",
268+
"pygments_lexer": "ipython3",
269+
"version": "3.11.9"
270+
}
271+
},
272+
"nbformat": 4,
273+
"nbformat_minor": 5
274+
}

0 commit comments

Comments
 (0)