1010from multiprocessing import Process
1111
1212import cudaq
13- from cudaq import spin
13+ import numpy as np
1414import pytest
15+ from cudaq import spin
1516from network_utils import check_server_connection
1617
1718try :
@@ -64,7 +65,6 @@ def test_sample():
6465 kernel .h (qubits [0 ])
6566 kernel .cx (qubits [0 ], qubits [1 ])
6667 kernel .mz (qubits )
67- print (kernel )
6868
6969 # Run sample synchronously
7070 counts = cudaq .sample (kernel )
@@ -80,7 +80,6 @@ def test_sample():
8080 assert "11" in counts
8181
8282 future = cudaq .sample_async (kernel )
83- print (future )
8483
8584 # Persist the future to a file
8685 futureAsString = str (future )
@@ -105,7 +104,7 @@ def ansatz(theta: float):
105104 0 ) * spin .y (1 ) + .21829 * spin .z (0 ) - 6.125 * spin .z (1 )
106105
107106 # Run the observe task on synchronously
108- res = cudaq .observe (ansatz , hamiltonian , .59 , shots_count = 100 )
107+ res = cudaq .observe (ansatz , hamiltonian , .59 , shots_count = 200 )
109108 assert assert_close (res .expectation ())
110109
111110 # Launch it asynchronously, enters the job into the queue
@@ -117,7 +116,6 @@ def ansatz(theta: float):
117116 # Launch the job async, job goes in the queue, and
118117 # we're free to dump the future to file
119118 future = cudaq .observe_async (ansatz , hamiltonian , .59 , shots_count = 100 )
120- print (future )
121119 futureAsString = str (future )
122120
123121 # Later you can come back and read it in
@@ -128,6 +126,129 @@ def ansatz(theta: float):
128126 assert assert_close (res .expectation ())
129127
130128
129+ def test_u3_decomposition ():
130+
131+ @cudaq .kernel
132+ def kernel ():
133+ qubit = cudaq .qubit ()
134+ u3 (0.0 , np .pi / 2 , np .pi , qubit )
135+ mz (qubit )
136+
137+ # Test here is that this runs without error
138+ cudaq .sample (kernel , shots_count = 10 )
139+
140+
141+ def test_u3_ctrl_decomposition ():
142+
143+ @cudaq .kernel
144+ def kernel ():
145+ control = cudaq .qubit ()
146+ target = cudaq .qubit ()
147+ u3 .ctrl (0.0 , np .pi / 2 , np .pi , control , target )
148+ mz (control )
149+ mz (target )
150+
151+ # Test here is that this runs without error
152+ cudaq .sample (kernel , shots_count = 10 )
153+
154+
155+ def test_state_preparation ():
156+
157+ @cudaq .kernel
158+ def kernel (vec : list [complex ]):
159+ qubits = cudaq .qvector (vec )
160+ mz (qubits )
161+
162+ state = [1. / np .sqrt (2. ), 1. / np .sqrt (2. ), 0. , 0. ]
163+ counts = cudaq .sample (kernel , state )
164+ assert '00' in counts
165+ assert '10' in counts
166+ assert not '01' in counts
167+ assert not '11' in counts
168+
169+
170+ def test_state_synthesis_from_simulator ():
171+
172+ @cudaq .kernel
173+ def kernel (state : cudaq .State ):
174+ qubits = cudaq .qvector (state )
175+ mz (qubits )
176+
177+ state = cudaq .State .from_data (
178+ np .array ([1. / np .sqrt (2. ), 1. / np .sqrt (2. ), 0. , 0. ], dtype = complex ))
179+
180+ counts = cudaq .sample (kernel , state )
181+ assert "00" in counts
182+ assert "10" in counts
183+ assert len (counts ) == 2
184+
185+ synthesized = cudaq .synthesize (kernel , state )
186+ counts = cudaq .sample (synthesized )
187+ assert '00' in counts
188+ assert '10' in counts
189+ assert len (counts ) == 2
190+
191+
192+ def test_state_synthesis ():
193+
194+ @cudaq .kernel
195+ def init (n : int ):
196+ q = cudaq .qvector (n )
197+ x (q [0 ])
198+
199+ @cudaq .kernel
200+ def kernel (s : cudaq .State ):
201+ q = cudaq .qvector (s )
202+ x (q [1 ])
203+ mz (q )
204+
205+ s = cudaq .get_state (init , 2 )
206+ counts = cudaq .sample (kernel , s )
207+ assert '11' in counts
208+ assert len (counts ) == 1
209+
210+
211+ def test_exp_pauli ():
212+
213+ @cudaq .kernel
214+ def test ():
215+ q = cudaq .qvector (2 )
216+ exp_pauli (1.0 , q , "XX" )
217+ mz (q )
218+
219+ counts = cudaq .sample (test )
220+ assert '00' in counts
221+ assert '11' in counts
222+ assert not '01' in counts
223+ assert not '10' in counts
224+
225+
226+ def test_run ():
227+
228+ @cudaq .kernel
229+ def simple (numQubits : int ) -> int :
230+ qubits = cudaq .qvector (numQubits )
231+ h (qubits .front ())
232+ for i , qubit in enumerate (qubits .front (numQubits - 1 )):
233+ x .ctrl (qubit , qubits [i + 1 ])
234+ result = 0
235+ for i in range (numQubits ):
236+ if mz (qubits [i ]):
237+ result += 1
238+ return result
239+
240+ shots = 100
241+ qubitCount = 4
242+ results = cudaq .run (simple , qubitCount , shots_count = shots )
243+ assert len (results ) == shots
244+ non_zero_count = 0
245+ for result in results :
246+ assert result == 0 or result == qubitCount # 00..0 or 1...11
247+ if result == qubitCount :
248+ non_zero_count += 1
249+ assert non_zero_count > 0
250+
251+
131252# leave for gdb debugging
132253if __name__ == "__main__" :
133254 loc = os .path .abspath (__file__ )
0 commit comments