You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"""Function parametrizing the QUA `for_` loop from a python array.
11
+
12
+
Taken from qualang_tools to avoid the dependency.
13
+
14
+
Args:
15
+
var: the QUA variable that will be looped over (int or fixed).
16
+
array: a Python list or numpy array containing the values over which `a` will be looping.
17
+
The spacing must be even in linear or logarithmic scales and it cannot be a QUA array.
18
+
19
+
Returns:
20
+
QUA for_ loop parameters (var, init, cond, update) as defined in https://qm-docs.qualang.io/api_references/qua/dsl_main?highlight=for_#qm.qua._dsl.for_.
21
+
"""
22
+
23
+
# Check for array length
24
+
iflen(array) ==0:
25
+
raiseException("The array must be of length > 0.")
26
+
eliflen(array) ==1:
27
+
returnvar, array[0], var<=array[0], var+1
28
+
# Check QUA vs python variables
29
+
ifnotisinstance(var, Variable):
30
+
raiseException("The first argument must be a QUA variable.")
31
+
if (notisinstance(array[0], (np.generic, int, float))) or (
32
+
isinstance(array[0], bool)
33
+
):
34
+
raiseException("The array must be an array of python variables.")
raiseException("fixed numbers are bounded to [-8, 8).")
72
+
73
+
# Generate the loop parameters for positive and negative steps
74
+
ifstep>0:
75
+
return (
76
+
var,
77
+
float(start),
78
+
var<float(stop) +float(step) /2,
79
+
var+float(step),
80
+
)
81
+
else:
82
+
return (
83
+
var,
84
+
float(start),
85
+
var>float(stop) +float(step) /2,
86
+
var+float(step),
87
+
)
88
+
else:
89
+
raiseException(
90
+
"This variable type is not supported. Please use a QUA 'int' or 'fixed' or contact a QM member for assistance."
91
+
)
92
+
# Logarithmic increment
93
+
elifincrement=="log":
94
+
step=array[1] /array[0]
95
+
96
+
ifvar.is_int():
97
+
warnings.warn(
98
+
"When using logarithmic increments with QUA integers, the resulting values will slightly differ from the ones in numpy.logspace() because of rounding errors. \n Please use the get_equivalent_log_array() function to get the exact values taken by the QUA variable and note that the number of points may also change."
"Two successive values in the scan are equal after being cast to integers which will make the QUA for_ loop fail. \nEither increase the logarithmic step or use for_each_(): https://docs.quantum-machines.co/1.1.6/qm-qua-sdk/docs/Guides/features/?h=for_ea#for_each."
104
+
)
105
+
else:
106
+
return (
107
+
var,
108
+
round(start),
109
+
var<round(stop) *np.sqrt(float(step)),
110
+
Cast.mul_int_by_fixed(var, float(step)),
111
+
)
112
+
else:
113
+
return (
114
+
var,
115
+
round(start),
116
+
var>round(stop) /np.sqrt(float(step)),
117
+
Cast.mul_int_by_fixed(var, float(step)),
118
+
)
119
+
120
+
elifvar.is_fixed():
121
+
# Check for fixed number overflows
122
+
ifnot (-8<=start<8) andnot (-8<=stop<8):
123
+
raiseException("fixed numbers are bounded to [-8, 8).")
124
+
125
+
ifstep>1:
126
+
return (
127
+
var,
128
+
float(start),
129
+
var<float(stop) *np.sqrt(float(step)),
130
+
var*float(step),
131
+
)
132
+
else:
133
+
return (
134
+
var,
135
+
float(start),
136
+
var>float(stop) *np.sqrt(float(step)),
137
+
var*float(step),
138
+
)
139
+
else:
140
+
raiseException(
141
+
"This variable type is not supported. Please use a QUA 'int' or 'fixed' or contact a QM member for assistance."
0 commit comments