forked from NVIDIA/cuda-quantum
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_Infleqtion.py
More file actions
225 lines (175 loc) · 5.53 KB
/
test_Infleqtion.py
File metadata and controls
225 lines (175 loc) · 5.53 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
# ============================================================================ #
# Copyright (c) 2022 - 2026 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, pytest, os
from cudaq import spin
import numpy as np
## NOTE: Comment the following line which skips these tests in order to run in
# local dev environment after setting the API key
## NOTE: Superstaq costs apply
pytestmark = pytest.mark.skip("Infleqtion / Superstaq API key required")
@pytest.fixture(scope="session", autouse=True)
def set_up_target():
cudaq.set_target("infleqtion")
yield "Running the tests."
cudaq.__clearKernelRegistries()
cudaq.reset_target()
def assert_close(want, got, tolerance=1.0e-5) -> bool:
return abs(want - got) < tolerance
def test_simple_kernel():
@cudaq.kernel
def bell():
qubits = cudaq.qvector(2)
h(qubits[0])
x.ctrl(qubits[0], qubits[1])
mz(qubits)
counts = cudaq.sample(bell)
assert len(counts) == 2
assert "00" in counts
assert "11" in counts
def test_all_gates():
@cudaq.kernel
def all_gates():
q = cudaq.qubit()
h(q)
x(q)
y(q)
z(q)
r1(np.pi, q)
rx(np.pi, q)
ry(np.pi, q)
rz(np.pi, q)
s(q)
t(q)
u3(0.0, np.pi / 2, np.pi, q)
mz(q)
qvec = cudaq.qvector(2)
x(qvec[0])
swap(qvec[0], qvec[1])
mz(qvec)
## control modifiers
qubits = cudaq.qvector(2)
h.ctrl(qubits[0], qubits[1])
x.ctrl(qubits[1], qubits[0])
y.ctrl(qubits[0], qubits[1])
z.ctrl(qubits[1], qubits[0])
r1.ctrl(np.pi / 2, qubits[0], qubits[1])
rx.ctrl(np.pi / 4, qubits[1], qubits[0])
ry.ctrl(np.pi / 8, qubits[0], qubits[1])
rz.ctrl(np.pi, qubits[1], qubits[0])
s.ctrl(qubits[0], qubits[1])
t.ctrl(qubits[1], qubits[0])
u3.ctrl(0.0, np.pi / 2, np.pi, qubits[0], qubits[1])
mz(qubits)
qreg = cudaq.qvector(3)
x(qreg[0])
x(qreg[1])
swap.ctrl(qreg[0], qreg[1], qreg[2])
mz(qreg)
## adjoint modifiers
r = cudaq.qubit()
r1.adj(np.pi, r)
rx.adj(np.pi / 2, r)
ry.adj(np.pi / 4, r)
rz.adj(np.pi / 8, r)
s.adj(r)
t.adj(r)
mz(r)
# Test here is that this runs
cudaq.sample(all_gates).dump()
def test_multiple_qvector():
@cudaq.kernel
def kernel():
qubits = cudaq.qvector(2)
ancilla = cudaq.qvector(2)
x(qubits)
h(ancilla)
mz(ancilla)
# Test here is that this runs
cudaq.sample(kernel).dump()
def test_multiple_measure():
@cudaq.kernel
def kernel():
q = cudaq.qvector(4)
a = cudaq.qvector(2)
h(q[0])
cx(q[0], q[1])
h(a)
cx(q[1], a[0])
mz(q[1])
mz(q[0])
mz(a)
# Test here is that this runs
cudaq.sample(kernel).dump()
def test_observe():
cudaq.set_random_seed(13)
@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)
res = cudaq.observe(ansatz, hamiltonian, .59, shots_count=2048)
## Need to adjust expectation value range
# assert assert_close(res.expectation())
print(res.expectation())
def test_state_synthesis():
@cudaq.kernel
def init(n: int):
q = cudaq.qvector(n)
x(q[0])
@cudaq.kernel
def kernel1(s: cudaq.State):
q = cudaq.qvector(s)
x(q[1])
@cudaq.kernel
def kernel2(s: cudaq.State):
q = cudaq.qvector(s)
x(q[1])
mz(q)
s = cudaq.get_state(init, 2)
s = cudaq.get_state(kernel1, s)
counts = cudaq.sample(kernel2, s)
assert '10' in counts
assert len(counts) == 1
def test_state_preparation():
shots = 100
@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, shots_count=shots)
counts.dump()
assert assert_close(shots / 2, counts["00"], shots / 10)
assert assert_close(shots / 2, counts["10"], shots / 10)
def test_state_preparation_builder():
shots = 100
kernel, state = cudaq.make_kernel(list[complex])
qubits = kernel.qalloc(state)
state = [1. / np.sqrt(2.), 1. / np.sqrt(2.), 0., 0.]
counts = cudaq.sample(kernel, state, shots_count=shots)
assert assert_close(shots / 2, counts["00"], shots / 10)
assert assert_close(shots / 2, counts["10"], shots / 10)
def test_exp_pauli():
@cudaq.kernel
def test():
q = cudaq.qvector(2)
exp_pauli(1.0, q, "XX")
mz(q)
counts = cudaq.sample(test)
counts.dump()
assert '00' in counts
assert '11' in counts
assert not '01' in counts
assert not '10' in counts
# leave for gdb debugging
if __name__ == "__main__":
loc = os.path.abspath(__file__)
pytest.main([loc, "-s"])