[Stretch] Add +, -, *, / operators for classical expressions.#13850
[Stretch] Add +, -, *, / operators for classical expressions.#13850jakelishman merged 121 commits intoQiskit:mainfrom
+, -, *, / operators for classical expressions.#13850Conversation
This reverts commit e2b3221.
Types that have some natural order no longer have an ordering when one of them is strictly greater but has an incompatible const-ness (i.e. when the greater type is const but the other type is not).
We need to reject types with const=True in QPY until it supports them. For now, I've also made the Index and shift operator constructors lift their RHS to the same const-ness as the target to make it less likely that existing users of expr run into issues when serializing to older QPY versions.
This is probably a better default in general, since we don't really have much use for const except for timing stuff.
Since we're going for using a Cast node when const-ness differs, this will be fine.
I wasn't going to have this, but since we have DANGEROUS Float => Int, and we have Int => Bool, I think this makes the most sense.
A Stretch can always represent a Duration (it's just an expression without any unresolved stretch variables, in this case), so we allow implicit conversion from Duration => Stretch. The reason for a separate Duration type is to support things like Duration / Duration => Float. This is not valid for stretches in OpenQASM (to my knowledge).
Also adds support to expr.lift to create a value expression of type types.Duration from an instance of qiskit.circuit.Duration.
jakelishman
left a comment
There was a problem hiding this comment.
I haven't looked at the test files (need to go to dinner), but here's a super quick scan over the implementation.
jakelishman
left a comment
There was a problem hiding this comment.
Thanks for the changes! I think everything here is all right (though I don't trust that I read every test exactly correctly haha).
| """ | ||
| left, right = _lift_binary_operands(left, right) | ||
| type: types.Type | ||
| if left.type.kind is right.type.kind is types.Duration: |
There was a problem hiding this comment.
ooh, chained is comparisons. Not a Python feature you see every day haha
| right-hand operand. In all cases, the output bit width is the same as the input, and zeros | ||
| fill in the "exposed" spaces. | ||
|
|
||
| The binary arithmetic operators :data:`ADD`, :data:`SUB:, :data:`MUL`, and :data:`DIV` |
There was a problem hiding this comment.
| The binary arithmetic operators :data:`ADD`, :data:`SUB:, :data:`MUL`, and :data:`DIV` | |
| The binary arithmetic operators :data:`ADD`, :data:`SUB`, :data:`MUL`, and :data:`DIV` |
There was a problem hiding this comment.
Maybe apply this in the next PR, just so I can get this one in the merge queue already?
| opcode, expr.Var(cr, types.Uint(8)), expr.Value(200, types.Uint(8)), types.Uint(8) | ||
| ), | ||
| ) | ||
| self.assertFalse(function(cr, 200).const) |
There was a problem hiding this comment.
Super minor, but the expr.Binary equality check should already be doing the const checks. Could we have written the tests such that the const is set explicitly in the expr.Binary constructor?
(No need to change all the tests now.)
| # Multiply timing expressions by non-const floats: | ||
| non_const_float = expr.Var.new("a", types.Float()) | ||
| with self.assertRaisesRegex(ValueError, "would result in a non-const"): | ||
| expr.mul(Duration.dt(1000), non_const_float) | ||
| with self.assertRaisesRegex(ValueError, "would result in a non-const"): | ||
| expr.mul(non_const_float, Duration.dt(1000)) |
There was a problem hiding this comment.
In a world where we have a use and a way of functioning with runtime-specified delay, I can imagine us relaxing this restriction, but I think it's good to assert right now.
Summary
Adds new arithmetic operators for use with numeric and a duration expressions. These are needed to express relationships between stretch variables and durations.
Details and comments
Based on #13844. More readable diff here..UintandFloat, but these cannot be intermixed. You must first cast one to the other explicitly, sinceFloat => UintandUint => Floatare both considered dangerous cast kinds. If twoUints of different width are used, the wider one is chosen for the expression's output type.Duration.DurationandFloatoperands. Currently, aUintmust first be cast to aFloatto be used as a timing multiplier.Duration, but theexprconstructors will raise aValueErrorif a user tries to produce such an expression (this was not possible in the original design of const-types, but what we have now feels more future-proof than baking this as a hard requirement into the type system).Varwith aDurationtype, which would not be a constant expression. If desired, we can block users from adding such variables to a circuit.DurationandFloatoperands, but only where the duration is in theleftposition. It is also supported between twoDurationtypes, in which case the result is aFloat. Dividing aDurationby a non-const expression will raise aValueError.To-do