@@ -138,6 +138,87 @@ def test_complex_numbers_declaration():
138
138
139
139
assert prog .to_qasm () == expected
140
140
141
+ def test_array_declaration ():
142
+ b = ArrayVar (name = "b" , init_expression = [True , False ], dimensions = [2 ], base_type = BoolVar )
143
+ i = ArrayVar (name = "i" , init_expression = [0 , 1 , 2 , 3 , 4 ], dimensions = [5 ], base_type = IntVar )
144
+ i55 = ArrayVar (name = "i55" , init_expression = [0 , 1 , 2 , 3 , 4 ], dimensions = [5 ], base_type = IntVar [55 ])
145
+ u = ArrayVar (name = "u" , init_expression = [0 , 1 , 2 , 3 , 4 ], dimensions = [5 ], base_type = UintVar )
146
+ x = ArrayVar (name = "x" , init_expression = [0e-9 , 1e-9 , 2e-9 ], dimensions = [3 ], base_type = DurationVar )
147
+ y = ArrayVar (name = "y" , init_expression = [0.0 , 1.0 , 2.0 , 3.0 ], dimensions = [4 ], base_type = FloatVar )
148
+ ang = ArrayVar (name = "ang" , init_expression = [0.0 , 1.0 , 2.0 , 3.0 ], dimensions = [4 ], base_type = AngleVar )
149
+ comp = ArrayVar (name = "comp" , init_expression = [0 , 1 + 1j ], dimensions = [2 ], base_type = ComplexVar )
150
+ comp55 = ArrayVar (name = "comp55" , init_expression = [0 , 1 + 1j ], dimensions = [2 ], base_type = ComplexVar [float_ (55 )])
151
+ ang_partial = ArrayVar [AngleVar , 2 ](name = "ang_part" , init_expression = [oqpy .pi , oqpy .pi / 2 ])
152
+ simple = ArrayVar [FloatVar ](name = "no_init" , dimensions = [5 ])
153
+ multidim = ArrayVar [FloatVar [32 ], 3 , 2 ](name = "multiDim" , init_expression = [[1.1 , 1.2 ], [2.1 , 2.2 ], [3.1 , 3.2 ]])
154
+
155
+ vars = [b , i , i55 , u , x , y , ang , comp , comp55 , ang_partial , simple , multidim ]
156
+
157
+ prog = oqpy .Program (version = None )
158
+ prog .declare (vars )
159
+ prog .set (i [1 ], 0 ) # Set with literal values
160
+ idx = IntVar (name = "idx" , init_expression = 5 )
161
+ val = IntVar (name = "val" , init_expression = 10 )
162
+ prog .set (i [idx ], val )
163
+
164
+ expected = textwrap .dedent (
165
+ """
166
+ int[32] idx = 5;
167
+ int[32] val = 10;
168
+ array[bool, 2] b = {true, false};
169
+ array[int[32], 5] i = {0, 1, 2, 3, 4};
170
+ array[int[55], 5] i55 = {0, 1, 2, 3, 4};
171
+ array[uint[32], 5] u = {0, 1, 2, 3, 4};
172
+ array[duration, 3] x = {0.0ns, 1.0ns, 2.0ns};
173
+ array[float[64], 4] y = {0.0, 1.0, 2.0, 3.0};
174
+ array[angle[32], 4] ang = {0.0, 1.0, 2.0, 3.0};
175
+ array[complex[float[64]], 2] comp = {0, 1.0 + 1.0im};
176
+ array[complex[float[55]], 2] comp55 = {0, 1.0 + 1.0im};
177
+ array[angle[32], 2] ang_part = {pi, pi / 2};
178
+ array[float[64], 5] no_init;
179
+ array[float[32], 3, 2] multiDim = {{1.1, 1.2}, {2.1, 2.2}, {3.1, 3.2}};
180
+ i[1] = 0;
181
+ i[idx] = val;
182
+ """
183
+ ).strip ()
184
+
185
+ assert prog .to_qasm () == expected
186
+
187
+ def test_non_trivial_array_access ():
188
+ prog = oqpy .Program ()
189
+ port = oqpy .PortVar (name = "my_port" )
190
+ frame = oqpy .FrameVar (name = "my_frame" , port = port , frequency = 1e9 , phase = 0 )
191
+
192
+ zero_to_one = oqpy .ArrayVar (
193
+ name = 'duration_array' ,
194
+ init_expression = [0.0 , 0.25 , 0.5 , 0.75 , 1 ],
195
+ dimensions = [5 ],
196
+ base_type = oqpy .DurationVar
197
+ )
198
+ one_second = oqpy .DurationVar (init_expression = 1 , name = "one_second" )
199
+
200
+ one = oqpy .IntVar (name = "one" , init_expression = 1 )
201
+
202
+ with oqpy .ForIn (prog , range (4 ), "idx" ) as idx :
203
+ prog .delay (zero_to_one [idx + one ] + one_second , frame )
204
+ prog .set (zero_to_one [idx ], 5 )
205
+
206
+ expected = textwrap .dedent (
207
+ """
208
+ OPENQASM 3.0;
209
+ port my_port;
210
+ array[duration, 5] duration_array = {0.0ns, 250000000.0ns, 500000000.0ns, 750000000.0ns, 1000000000.0ns};
211
+ int[32] one = 1;
212
+ duration one_second = 1000000000.0ns;
213
+ frame my_frame = newframe(my_port, 1000000000.0, 0);
214
+ for int idx in [0:3] {
215
+ delay[duration_array[idx + one] + one_second] my_frame;
216
+ duration_array[idx] = 5;
217
+ }
218
+ """
219
+ ).strip ()
220
+
221
+ assert prog .to_qasm () == expected
141
222
142
223
def test_non_trivial_variable_declaration ():
143
224
prog = Program ()
@@ -389,6 +470,26 @@ def test_for_in_var_types():
389
470
"""
390
471
).strip ()
391
472
473
+ # Test indexing over an ArrayVar
474
+ program = oqpy .Program ()
475
+ pyphases = [0 ] + [oqpy .pi / i for i in range (10 , 1 , - 2 )]
476
+ phases = ArrayVar (name = "phases" , dimensions = [len (pyphases )], init_expression = pyphases , base_type = AngleVar )
477
+
478
+ with oqpy .ForIn (program , range (len (pyphases )), "idx" ) as idx :
479
+ program .shift_phase (phases [idx ], frame )
480
+
481
+ expected = textwrap .dedent (
482
+ """
483
+ OPENQASM 3.0;
484
+ port my_port;
485
+ array[angle[32], 6] phases = {0, pi / 10, pi / 8, pi / 6, pi / 4, pi / 2};
486
+ frame my_frame = newframe(my_port, 3000000000.0, 0);
487
+ for int idx in [0:5] {
488
+ shift_phase(phases[idx], my_frame);
489
+ }
490
+ """
491
+ ).strip ()
492
+
392
493
assert program .to_qasm () == expected
393
494
394
495
0 commit comments