@@ -77,7 +77,7 @@ def lift_legacy_condition(
77
77
return Binary (Binary .Op .EQUAL , left , right , types .Bool ())
78
78
79
79
80
- def lift (value : typing .Any , / , type : types .Type | None = None , * , try_const : bool = False ) -> Expr :
80
+ def lift (value : typing .Any , / , type : types .Type | None = None ) -> Expr :
81
81
"""Lift the given Python ``value`` to a :class:`~.expr.Value` or :class:`~.expr.Var`.
82
82
83
83
By default, lifted scalars are not const. To lift supported scalars to const-typed
@@ -106,31 +106,16 @@ def lift(value: typing.Any, /, type: types.Type | None = None, *, try_const: boo
106
106
Var(ClassicalRegister(3, "c"), Uint(5, const=False))
107
107
>>> expr.lift(5, types.Uint(4))
108
108
Value(5, Uint(4))
109
-
110
- Lifting non-classical resource scalars to const values::
111
-
112
- >>> from qiskit.circuit.classical import expr, types
113
- >>> expr.lift(7)
114
- Value(7, Uint(3, const=False))
115
- >>> expr.lift(7, try_const=True)
116
- Value(7, Uint(3, const=True))
117
- >>> expr.lift(7, types.Uint(8, const=True))
118
- Value(7, Uint(8, const=True))
119
-
120
109
"""
121
110
if isinstance (value , Expr ):
122
111
if type is not None :
123
112
raise ValueError ("use 'cast' to cast existing expressions, not 'lift'" )
124
113
return value
125
114
from qiskit .circuit import Clbit , ClassicalRegister # pylint: disable=cyclic-import
126
115
127
- if type is not None :
128
- # If a type was specified, the inferred type must be the same
129
- # const-ness.
130
- try_const = type .const
131
116
inferred : types .Type
132
117
if value is True or value is False :
133
- inferred = types .Bool (const = try_const )
118
+ inferred = types .Bool ()
134
119
constructor = Value
135
120
elif isinstance (value , Clbit ):
136
121
inferred = types .Bool ()
@@ -141,7 +126,7 @@ def lift(value: typing.Any, /, type: types.Type | None = None, *, try_const: boo
141
126
elif isinstance (value , int ):
142
127
if value < 0 :
143
128
raise ValueError ("cannot represent a negative value" )
144
- inferred = types .Uint (width = value .bit_length () or 1 , const = try_const )
129
+ inferred = types .Uint (width = value .bit_length () or 1 )
145
130
constructor = Value
146
131
else :
147
132
raise TypeError (f"failed to infer a type for '{ value } '" )
@@ -218,7 +203,7 @@ def logic_not(operand: typing.Any, /) -> Expr:
218
203
Bool(const=False))
219
204
"""
220
205
operand = lift (operand )
221
- operand = _coerce_lossless (operand , types .Bool (const = operand . type . const ))
206
+ operand = _coerce_lossless (operand , types .Bool ())
222
207
return Unary (Unary .Op .LOGIC_NOT , operand , operand .type )
223
208
224
209
@@ -233,24 +218,13 @@ def _lift_binary_operands(left: typing.Any, right: typing.Any) -> tuple[Expr, Ex
233
218
to be interoperable.
234
219
* If both operands are expressions, they are returned as-is, and may require a cast node.
235
220
"""
236
- left_bool = isinstance (left , bool )
237
221
left_int = isinstance (left , int ) and not isinstance (left , bool )
238
- right_bool = isinstance (right , bool )
239
- right_int = isinstance (right , int ) and not right_bool
222
+ right_int = isinstance (right , int ) and not isinstance (right , bool )
240
223
if not (left_int or right_int ):
241
- if left_bool == right_bool :
242
- # They're either both bool, or neither are, so we lift them
243
- # independently.
244
- left = lift (left )
245
- right = lift (right )
246
- elif not right_bool :
247
- # Left is a bool, which should only be const if right is const.
248
- right = lift (right )
249
- left = lift (left , try_const = right .type .const )
250
- elif not left_bool :
251
- # Right is a bool, which should only be const if left is const.
252
- left = lift (left )
253
- right = lift (right , try_const = left .type .const )
224
+ # They're either both bool, or neither are, so we lift them
225
+ # independently.
226
+ left = lift (left )
227
+ right = lift (right )
254
228
elif not right_int :
255
229
# Left is an int.
256
230
right = lift (right )
@@ -262,7 +236,7 @@ def _lift_binary_operands(left: typing.Any, right: typing.Any) -> tuple[Expr, Ex
262
236
# Left will share const-ness of right.
263
237
left = Value (left , right .type )
264
238
else :
265
- left = lift (left , try_const = right . type . const )
239
+ left = lift (left )
266
240
elif not left_int :
267
241
# Right is an int.
268
242
left = lift (left )
@@ -274,7 +248,7 @@ def _lift_binary_operands(left: typing.Any, right: typing.Any) -> tuple[Expr, Ex
274
248
# Right will share const-ness of left.
275
249
right = Value (right , left .type )
276
250
else :
277
- right = lift (right , try_const = left . type . const )
251
+ right = lift (right )
278
252
else :
279
253
# Both are `int`, so we take our best case to make things work.
280
254
# If the caller needs a const type, they should lift one side to
@@ -289,14 +263,14 @@ def _binary_bitwise(op: Binary.Op, left: typing.Any, right: typing.Any) -> Expr:
289
263
left , right = _lift_binary_operands (left , right )
290
264
type : types .Type
291
265
if left .type .kind is right .type .kind is types .Bool :
292
- type = types .Bool (const = ( left . type . const and right . type . const ) )
266
+ type = types .Bool ()
293
267
elif left .type .kind is types .Uint and right .type .kind is types .Uint :
294
268
if left .type .width != right .type .width :
295
269
raise TypeError (
296
270
"binary bitwise operations are defined between unsigned integers of the same width,"
297
271
f" but got { left .type .width } and { right .type .width } ."
298
272
)
299
- type = types .Uint (width = left .type .width , const = ( left . type . const and right . type . const ) )
273
+ type = types .Uint (width = left .type .width )
300
274
else :
301
275
raise TypeError (f"invalid types for '{ op } ': '{ left .type } ' and '{ right .type } '" )
302
276
return Binary (op , _coerce_lossless (left , type ), _coerce_lossless (right , type ), type )
@@ -362,7 +336,7 @@ def bit_xor(left: typing.Any, right: typing.Any, /) -> Expr:
362
336
def _binary_logical (op : Binary .Op , left : typing .Any , right : typing .Any ) -> Expr :
363
337
left = lift (left )
364
338
right = lift (right )
365
- type = types .Bool (const = ( left . type . const and right . type . const ) )
339
+ type = types .Bool ()
366
340
left = _coerce_lossless (left , type )
367
341
right = _coerce_lossless (right , type )
368
342
return Binary (op , left , right , type )
@@ -416,7 +390,7 @@ def _equal_like(op: Binary.Op, left: typing.Any, right: typing.Any) -> Expr:
416
390
op ,
417
391
_coerce_lossless (left , type ),
418
392
_coerce_lossless (right , type ),
419
- types .Bool (const = type . const ),
393
+ types .Bool (),
420
394
)
421
395
422
396
@@ -469,7 +443,7 @@ def _binary_relation(op: Binary.Op, left: typing.Any, right: typing.Any) -> Expr
469
443
op ,
470
444
_coerce_lossless (left , type ),
471
445
_coerce_lossless (right , type ),
472
- types .Bool (const = type . const ),
446
+ types .Bool (),
473
447
)
474
448
475
449
@@ -554,14 +528,14 @@ def _shift_like(
554
528
left = _coerce_lossless (left , type ) if type is not None else left
555
529
else :
556
530
left = lift (left , type )
557
- right = lift (right , try_const = left . type . const )
531
+ right = lift (right )
558
532
if left .type .kind != types .Uint or right .type .kind != types .Uint :
559
533
raise TypeError (f"invalid types for '{ op } ': '{ left .type } ' and '{ right .type } '" )
560
534
return Binary (
561
535
op ,
562
536
left ,
563
537
right ,
564
- types .Uint (width = left .type .width , const = ( left . type . const and right . type . const ) ),
538
+ types .Uint (width = left .type .width ),
565
539
)
566
540
567
541
@@ -631,7 +605,7 @@ def index(target: typing.Any, index: typing.Any, /) -> Expr:
631
605
Bool(const=False))
632
606
"""
633
607
target = lift (target )
634
- index = lift (index , try_const = target . type . const )
608
+ index = lift (index )
635
609
if target .type .kind is not types .Uint or index .type .kind is not types .Uint :
636
610
raise TypeError (f"invalid types for indexing: '{ target .type } ' and '{ index .type } '" )
637
- return Index (target , index , types .Bool (const = target . type . const and index . type . const ))
611
+ return Index (target , index , types .Bool ())
0 commit comments