This repository was archived by the owner on Jan 22, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbernstein_vazirani_quantum.py
More file actions
209 lines (163 loc) · 5.94 KB
/
bernstein_vazirani_quantum.py
File metadata and controls
209 lines (163 loc) · 5.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
"""The input to this solver is a json schema:
{'nbits': N,
'f': [a list of bits (0's and 1's) where f[x] is f(x)]}
This uses a quantum algorithm to find a binary string S of length N such that
f(x) = S XOR x for all x in 0..2^N
There is a promise that there is such an S.
This code can also generate all possible problem instances for a given N
(in other words, it generates a description of the function f for all possible S).
"""
import argparse
import json
import math
import requests
from qiskit import QuantumCircuit, qasm2
from qiskit_aer import AerSimulator
def bv_query(s):
"""Create a quantum circuit implementing a query gate for the
Bernstein-Vazirani problem."""
qc = QuantumCircuit(len(s) + 1)
for index, bit in enumerate(reversed(s)):
if bit == "1":
qc.cx(index, len(s))
return qc
def compile_circuit(function: QuantumCircuit):
"""Compiles a circuit for use in the Deutsch-Jozsa algorithm."""
n = function.num_qubits - 1
qc = QuantumCircuit(n + 1, n)
qc.x(n)
qc.h(range(n + 1))
qc.compose(function, inplace=True)
qc.h(range(n))
qc.measure(range(n), range(n))
return qc
def bv_algorithm(function: QuantumCircuit):
"""Runs the Bernstein-Vazirani algorithm using the given function"""
qc = compile_circuit(function)
result = AerSimulator().run(qc, shots=1, memory=True).result()
return (result.get_memory()[0], qc)
def power_of_two_info(n):
"""Determine whether n is a power of 2 and which one"""
if n <= 0:
return False, None
# A number is a power of two if it has exactly one bit set
if (n & (n - 1)) == 0:
# log2 gives the exponent
power = int(math.log2(n))
return True, power
return False, None
def solve(data) -> dict:
"""returns the answer as a binary string"""
if isinstance(data, list):
fbits = [bool(x) for x in data]
ispower2, nbits = power_of_two_info(len(fbits))
if not ispower2:
return {"answer": "function length is not power of 2"}
else:
nbits = data["nbits"]
fbits = [bool(x) for x in data["f"]]
if 2**nbits != len(fbits):
return {
"answer": f"invalid function length {len(fbits)} != 2^nbits {2**nbits}"
}
def f(x: int) -> bool:
if x < 0 or x >= len(fbits):
return False
return fbits[x]
bit_string = ["1" if f(1 << i) else "0" for i in range(nbits)]
bit_string = "".join(reversed(bit_string))
answer, circuit = bv_algorithm(bv_query(bit_string))
return {"answer": answer, "qasm": qasm2.dumps(circuit)}
def generate(nbits, s):
"""Generates a Bernstein-Vazirani problem instance with hidden string 's'."""
assert len(s) == nbits
ftrue = set()
si = int(s, 2)
for i in range(2**nbits):
if ((si & i).bit_count() % 2) == 1:
ftrue.add(i)
return (nbits, s, ftrue)
def generate_all(nbits):
"""Yields all possible problem instances for nbits."""
for i in range(2**nbits):
s = f"{i:b}".zfill(nbits)
yield generate(nbits, s)
def tryit(url, nbits, ftrue, expected_s, show_circuits=False):
"""Helper function to test the solver."""
tryit_oldstyle(url, nbits, ftrue, expected_s, show_circuits)
data = ftrue
if url is None:
solution = solve(data)
else:
req = requests.post(url, json=data, timeout=5)
solution = req.json()
answer = solution["answer"]
assert (
answer == expected_s
), f"Failed for nbits={nbits}, ftrue={ftrue}, expected_s={expected_s}, got {answer}"
if show_circuits:
print(f"// Bernstein-Vazirani circuit for nbits={nbits}, s={expected_s}:")
print(solution["qasm"])
def tryit_oldstyle(url, nbits, ftrue, expected_s, show_circuits=False):
"""Helper function to test the solver."""
data = {"nbits": nbits, "f": list(ftrue)}
if url is None:
solution = solve(data)
else:
req = requests.post(url, json=data, timeout=5)
solution = req.json()
answer = solution["answer"]
assert (
answer == expected_s
), f"Failed for nbits={nbits}, ftrue={ftrue}, expected_s={expected_s}, got {answer}"
if show_circuits:
print(f"// Bernstein-Vazirani circuit for nbits={nbits}, s={expected_s}:")
print(solution["qasm"])
def main():
"""Main function to either run tests or generate problem instances."""
parser = argparse.ArgumentParser(
description="Run the Bernstein-Vazirani quantum algorithm."
)
parser.add_argument(
"--generate",
type=int,
required=False,
default=None,
help="The size of the problem to generate.",
)
parser.add_argument(
"--show-circuits",
action="store_true",
help="Show the generated quantum circuits during tests.",
)
parser.add_argument(
"--baseurl",
type=str,
default=None,
help="Base URL for the quantum solver server.",
)
parser.add_argument(
"--endpoint",
type=str,
default="bernstein-vazirani-quantum",
help="Endpoint for the quantum solver.",
)
args = parser.parse_args()
# if we're not generating a problem, run tests
fullurl = None
if args.baseurl is not None:
fullurl = f"{args.baseurl}/{args.endpoint}"
if args.generate is None:
tryit(fullurl, 3, [0, 1, 0, 1, 1, 0, 1, 0], "101", args.show_circuits)
tryit(fullurl, 3, [0, 1, 1, 0, 1, 0, 0, 1], "111", args.show_circuits)
tryit(fullurl, 3, [1, 0, 0, 1, 0, 1, 1, 0], "000", args.show_circuits)
tryit(fullurl, 3, [0, 1, 0, 1, 0, 1, 0, 1], "001", args.show_circuits)
if fullurl is None:
fullurl = "local"
print(f"All tests passed ({fullurl})")
return
for nbits, s, ftrue in generate_all(args.generate):
a = {"nbits": nbits, "f": list(ftrue), "s": s}
print(json.dumps(a))
if __name__ == "__main__":
main()