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 pathdeutsch_jozsa_quantum.py
More file actions
201 lines (162 loc) · 5.71 KB
/
deutsch_jozsa_quantum.py
File metadata and controls
201 lines (162 loc) · 5.71 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
"""
Deutsch-Jozsa problem quantum solver.
In this problem the input is a function f:{0,1}^n -> {0,1} which is either
constant (all outputs are the same) or balanced (half the outputs are 0, half
are 1).
The input to this solver is either a list:
[0, 1, 0, 1, ...]
list[i] represents f(i) and must be a power of 2 in size
OR, the input can be a dictionary:
{'nbits': N,
'values': [a list of integers of length 2^N, values[i] represents f(i)]}
The result is a dictionary:
{'answer': 'constant' or 'balanced', or an error message}
"""
import argparse
import math
import requests
from qiskit import QuantumCircuit, qasm2
from qiskit_aer import AerSimulator
def dj_algorithm(function: QuantumCircuit):
"""Determine if a function is constant or balanced."""
qc = compile_circuit(function)
result = AerSimulator().run(qc, shots=1, memory=True).result()
measurements = result.get_memory()
return ("balanced", qc) if "1" in measurements[0] else ("constant", 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 dj_constant(num_qubits, output=True):
"""Return a constant DJ function circuit."""
qc = QuantumCircuit(num_qubits + 1)
if output:
qc.x(num_qubits)
return qc
def dj_balanced(num_qubits, fbits):
"""Return a balanced DJ function circuit."""
qc = QuantumCircuit(num_qubits + 1)
def add_cx(qc, bit_string):
for qubit, bit in enumerate(reversed(bit_string)):
if bit == "1":
qc.x(qubit)
return qc
for state, fbit in enumerate(fbits):
if fbit:
qc.barrier()
qc = add_cx(qc, f"{state:0b}")
qc.mcx(list(range(num_qubits)), num_qubits)
qc = add_cx(qc, f"{state:0b}")
qc.barrier()
return qc
def power_of_two_info(n):
"""Determine if n is a power of 2 and if so, which power of 2."""
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:
"""Solves the Deutsch-Jozsa problem for the given function data.
The input data is a json schema like this (old style):
{'nbits': N,
'f': [list of integers of length 2^nbits: f[x] = f(x) = {0, 1}}
or (new style): just a list [0, 1, ...] where the position i is f(i)
"""
if isinstance(data, list):
# If we're given just a list, assume it is f's values
fbits = [bool(x) for x in data]
ispower2, nbits = power_of_two_info(len(fbits))
if not ispower2:
return {"answer": "invalid function length, need 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}"
}
if sum(fbits) == 0:
func = dj_constant(nbits, output=False)
elif len(fbits) == sum(fbits):
func = dj_constant(nbits, output=True)
else:
func = dj_balanced(nbits, fbits)
(answer, qc) = dj_algorithm(func)
return {"answer": answer, "qasm": qasm2.dumps(qc)}
def testit(url, data, expected, show_circuits=False):
"""Test the deutsch-jozsa solver with given data and expected result."""
if url is None:
result = solve(data)
else:
req = requests.post(url, json=data, timeout=5)
assert req.status_code == 200, f"HTTP error {req.status_code} for data={data}"
result = req.json()
assert (
result["answer"] == expected
), f"expected {expected}, got {result['answer']} for {data}"
if show_circuits and "qasm" in result:
print(f"// Deutsch-Jozsa circuit for input {data}:")
print(result["qasm"])
def main():
"""Internal/server testing of solver"""
parser = argparse.ArgumentParser(description="Deutsch-Jozsa Quantum Solver")
parser.add_argument(
"--baseurl",
type=str,
default=None,
help="Base URL for the quantum solver to test against.",
)
parser.add_argument(
"--endpoint",
type=str,
default="deutsch-jozsa-quantum",
help="Endpoint for the classical solver.",
)
parser.add_argument(
"--show-circuits",
action="store_true",
help="Show the generated quantum circuits.",
)
args = parser.parse_args()
url = None
if args.baseurl is not None:
url = f"{args.baseurl}/{args.endpoint}"
# new style tests
testit(url, [0, 0, 0, 0, 0, 0, 0, 0], "constant", args.show_circuits)
testit(url, [1, 1, 1, 1, 1, 1, 1, 1], "constant", args.show_circuits)
testit(url, [0, 1, 0, 1, 1, 0, 1, 0], "balanced", args.show_circuits)
testit(url, [0, 1, 1, 0, 1, 0, 0, 1], "balanced", args.show_circuits)
# old style tests
testit(
url, {"nbits": 3, "f": [0, 0, 0, 0, 0, 0, 0, 0]}, "constant", args.show_circuits
)
testit(
url, {"nbits": 3, "f": [1, 1, 1, 1, 1, 1, 1, 1]}, "constant", args.show_circuits
)
testit(
url,
{"nbits": 3, "f": [0, 1, 0, 1, 1, 0, 1, 0]},
"balanced",
args.show_circuits,
)
testit(
url,
{"nbits": 3, "f": [0, 1, 1, 0, 1, 0, 0, 1]},
"balanced",
args.show_circuits,
)
if url is None:
url = "local"
print(f"All tests passed ({url})")
if __name__ == "__main__":
main()