forked from NVIDIA/cuda-quantum
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_Ionq_LocalEmulation_kernel.py
More file actions
80 lines (61 loc) · 2.52 KB
/
test_Ionq_LocalEmulation_kernel.py
File metadata and controls
80 lines (61 loc) · 2.52 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
# ============================================================================ #
# 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 cudaq.kernels
from cudaq import spin
import pytest
import os
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():
# Set the targeted QPU
cudaq.set_target('ionq', emulate='true')
yield "Running the tests."
cudaq.reset_target()
def test_Ionq_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_Ionq_cudaq_uccsd():
num_electrons = 2
num_qubits = 8
thetas = [
-0.00037043841404585794, 0.0003811110195084151, 0.2286823796532558,
-0.00037043841404585794, 0.0003811110195084151, 0.2286823796532558,
-0.00037043841404585794, 0.0003811110195084151, 0.2286823796532558,
-0.00037043841404585794, 0.0003811110195084151, 0.2286823796532558,
-0.00037043841404585794, 0.0003811110195084151, 0.2286823796532558,
-0.00037043841404585794, 0.0003811110195084151, 0.2286823796532558,
-0.00037043841404585794, 0.0003811110195084151, 0.2286823796532558,
-0.00037043841404585794, 0.0003811110195084151, 0.2286823796532558
]
@cudaq.kernel
def kernel():
qubits = cudaq.qvector(num_qubits)
for i in range(num_electrons):
x(qubits[i])
cudaq.kernels.uccsd(qubits, thetas, num_electrons, num_qubits)
counts = cudaq.sample(kernel, shots_count=1000)
assert len(counts) == 6
assert '00000011' in counts
assert '00000110' in counts
assert '00010010' in counts
assert '01000010' in counts
assert '10000001' in counts
assert '11000000' in counts
# leave for gdb debugging
if __name__ == "__main__":
loc = os.path.abspath(__file__)
pytest.main([loc, "-s"])