forked from NVIDIA/cuda-quantum
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_Quantinuum_LocalEmulation_kernel.py
More file actions
325 lines (244 loc) · 8.86 KB
/
test_Quantinuum_LocalEmulation_kernel.py
File metadata and controls
325 lines (244 loc) · 8.86 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# ============================================================================ #
# Copyright (c) 2022 - 2025 NVIDIA Corporation & Affiliates. #
# All rights reserved. #
# #
# This source code and the accompanying materials are made available under #
# the terms of the Apache License 2.0 which accompanies this distribution. #
# ============================================================================ #
import cudaq
import pytest
import os
from cudaq import spin
import numpy as np
from typing import List
def assert_close(want, got, tolerance=1.0e-1) -> bool:
return abs(want - got) < tolerance
@pytest.fixture(scope="function", autouse=True)
def configureTarget():
# We need a Fake Credentials Config file
credsName = '{}/FakeConfig2.config'.format(os.environ["HOME"])
f = open(credsName, 'w')
f.write('key: {}\nrefresh: {}\ntime: 0'.format("hello", "rtoken"))
f.close()
# Set the targeted QPU
cudaq.set_target('quantinuum', emulate='true')
yield "Running the tests."
# remove the file
os.remove(credsName)
cudaq.reset_target()
def test_quantinuum_sample():
cudaq.set_random_seed(13)
# Create the kernel we'd like to execute on Quantinuum
@cudaq.kernel
def simple():
qubits = cudaq.qvector(2)
h(qubits[0])
x.ctrl(qubits[0], qubits[1])
mz(qubits)
print(simple)
# Run sample synchronously, this is fine
# here in testing since we are targeting a mock
# server. In reality you'd probably not want to
# do this with the remote job queue.
counts = cudaq.sample(simple)
assert (len(counts) == 2)
assert ('00' in counts)
assert ('11' in counts)
# Run sample, but do so asynchronously. This enters
# the execution job into the remote Quantinuum job queue.
future = cudaq.sample_async(simple)
# We could go do other work, but since this
# is a mock server, get the result
counts = future.get()
assert (len(counts) == 2)
assert ('00' in counts)
assert ('11' in counts)
def test_quantinuum_observe():
cudaq.set_random_seed(13)
# Create the parameterized ansatz
@cudaq.kernel
def ansatz(theta: float):
qreg = cudaq.qvector(2)
x(qreg[0])
ry(theta, qreg[1])
x.ctrl(qreg[1], qreg[0])
# Define its spin Hamiltonian.
hamiltonian = 5.907 - 2.1433 * spin.x(0) * spin.x(1) - 2.1433 * spin.y(
0) * spin.y(1) + .21829 * spin.z(0) - 6.125 * spin.z(1)
# Run the observe task on quantinuum synchronously
res = cudaq.observe(ansatz, hamiltonian, .59, shots_count=100000)
assert assert_close(-1.7, res.expectation())
# Launch it asynchronously, enters the job into the queue
future = cudaq.observe_async(ansatz, hamiltonian, .59, shots_count=100000)
# Retrieve the results (since we're emulating)
res = future.get()
assert assert_close(-1.7, res.expectation())
def test_observe():
cudaq.set_random_seed(13)
@cudaq.kernel
def ansatz():
q = cudaq.qvector(1)
molecule = 5.0 - 1.0 * spin.x(0)
res = cudaq.observe(ansatz, molecule, shots_count=10000)
print(res.expectation())
assert assert_close(5.0, res.expectation())
def test_quantinuum_exp_pauli():
cudaq.set_random_seed(13)
# Create the parameterized ansatz
@cudaq.kernel
def ansatz(theta: float):
qreg = cudaq.qvector(2)
x(qreg[0])
exp_pauli(theta, qreg, "XY")
print(ansatz)
# Define its spin Hamiltonian.
hamiltonian = 5.907 - 2.1433 * spin.x(0) * spin.x(1) - 2.1433 * spin.y(
0) * spin.y(1) + .21829 * spin.z(0) - 6.125 * spin.z(1)
# Run the observe task on quantinuum synchronously
res = cudaq.observe(ansatz, hamiltonian, .59, shots_count=100000)
assert assert_close(-1.7, res.expectation())
# Launch it asynchronously, enters the job into the queue
future = cudaq.observe_async(ansatz, hamiltonian, .59, shots_count=100000)
# Retrieve the results (since we're emulating)
res = future.get()
assert assert_close(-1.7, res.expectation())
def test_u3_emulatation():
@cudaq.kernel
def check_x():
q = cudaq.qubit()
u3(np.pi, np.pi, np.pi / 2, q)
counts = cudaq.sample(check_x)
def test_u3_ctrl_emulation():
@cudaq.kernel
def kernel():
control = cudaq.qubit()
target = cudaq.qubit()
u3.ctrl(0.0, np.pi / 2, np.pi, control, target)
result = cudaq.sample(kernel)
def test_quantinuum_state_preparation():
@cudaq.kernel
def kernel(vec: List[complex]):
qubits = cudaq.qvector(vec)
state = [1. / np.sqrt(2.), 1. / np.sqrt(2.), 0., 0.]
counts = cudaq.sample(kernel, state)
assert '00' in counts
assert '10' in counts
assert not '01' in counts
assert not '11' in counts
state = [1. / np.sqrt(2.), 1. / np.sqrt(2.), 0., 0., 0., 0., 0., 0.]
counts = cudaq.sample(kernel, state)
assert '000' in counts
assert '100' in counts
assert not '001' in counts
assert not '010' in counts
assert not '011' in counts
assert not '101' in counts
assert not '110' in counts
assert not '111' in counts
def test_quantinuum_state_synthesis():
@cudaq.kernel
def kernel(state: cudaq.State):
qubits = cudaq.qvector(state)
state = cudaq.State.from_data(
np.array([1. / np.sqrt(2.), 1. / np.sqrt(2.), 0., 0.], dtype=complex))
with pytest.raises(RuntimeError) as e:
counts = cudaq.sample(kernel, state)
assert 'Could not successfully apply quake-synth.' in repr(e)
def test_exp_pauli():
@cudaq.kernel
def test():
q = cudaq.qvector(2)
exp_pauli(1.0, q, "XX")
counts = cudaq.sample(test)
assert '00' in counts
assert '11' in counts
assert not '01' in counts
assert not '10' in counts
def test_exp_pauli_param():
@cudaq.kernel
def test_param(w: cudaq.pauli_word):
q = cudaq.qvector(2)
exp_pauli(1.0, q, w)
# FIXME: should work after new launchKernel becomes default.
with pytest.raises(RuntimeError) as e:
counts = cudaq.sample(test_param, cudaq.pauli_word("XX"))
assert 'Remote rest platform Quake lowering failed.' in repr(e)
def test_1q_unitary_synthesis():
cudaq.register_operation("custom_h",
1. / np.sqrt(2.) * np.array([1, 1, 1, -1]))
cudaq.register_operation("custom_x", np.array([0, 1, 1, 0]))
@cudaq.kernel
def basic_x():
qubit = cudaq.qubit()
custom_x(qubit)
counts = cudaq.sample(basic_x)
assert len(counts) == 1 and "1" in counts
@cudaq.kernel
def basic_h():
qubit = cudaq.qubit()
custom_h(qubit)
counts = cudaq.sample(basic_h)
assert "0" in counts and "1" in counts
@cudaq.kernel
def bell():
qubits = cudaq.qvector(2)
custom_h(qubits[0])
custom_x.ctrl(qubits[0], qubits[1])
counts = cudaq.sample(bell)
assert len(counts) == 2
assert "00" in counts and "11" in counts
cudaq.register_operation("custom_s", np.array([1, 0, 0, 1j]))
cudaq.register_operation("custom_s_adj", np.array([1, 0, 0, -1j]))
@cudaq.kernel
def kernel():
q = cudaq.qubit()
h(q)
custom_s.adj(q)
custom_s_adj(q)
h(q)
counts = cudaq.sample(kernel)
assert counts["1"] == 1000
def test_2q_unitary_synthesis():
cudaq.register_operation(
"custom_cnot",
np.array([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0]))
@cudaq.kernel
def bell_pair():
qubits = cudaq.qvector(2)
h(qubits[0])
custom_cnot(qubits[0], qubits[1])
counts = cudaq.sample(bell_pair)
assert len(counts) == 2
assert "00" in counts and "11" in counts
cudaq.register_operation(
"custom_cz", np.array([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0,
-1]))
@cudaq.kernel
def ctrl_z_kernel():
qubits = cudaq.qvector(5)
controls = cudaq.qvector(2)
custom_cz(qubits[1], qubits[0])
x(qubits[2])
custom_cz(qubits[3], qubits[2])
x(controls)
counts = cudaq.sample(ctrl_z_kernel)
assert counts["0010011"] == 1000
def test_3q_unitary_synthesis():
cudaq.register_operation(
"toffoli",
np.array([
1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0
]))
@cudaq.kernel
def test_toffoli():
q = cudaq.qvector(3)
x(q)
toffoli(q[0], q[1], q[2])
with pytest.raises(RuntimeError):
cudaq.sample(test_toffoli)
# leave for gdb debugging
if __name__ == "__main__":
loc = os.path.abspath(__file__)
pytest.main([loc, "-s"])