Skip to content

Validating boolean comparison of MeasurementResults #604

@david-pl

Description

@david-pl

We often want to write something like

q = squin.qalloc(1)
...
m = squin.measure(q)

if m == 1:
    ...

However, m == 1 is always False since m is a MeasurementResult enum, which can be 0, 1 or 2, where 2 represents atom loss.

When there is no loss, you can effectively represent the intended logic by simply writing

if m:
    ...

It is very confusing to users, however, that if m == 1 will never be evaluated. Especially, since there is no error or warning, but it silently evaluates to `False.

Writing if m is also not great since this would be treated as True, when the qubit has been lost (since bool(2) == True). Again, there will be no error, but only a silent bug in the code.

The proper way of writing the condition is

if m == MeasurementResult.One:
    ...

However, any of the above is completely valid syntax and won't issue any warning or error.I 'm not sure how we could fix this. It may even make sense to add a rewrite pass that checks conditions, or more generally, boolean operations, that include measurements and then rewrites that to the "intended" expression. I'm not sure, however, since it may not always be clear what the intention is and rewrites can fail.

Another possible solution would be to use ValidationAnalysis to raise an error or at least print a warning for boolean operations used with measurements. I'm leaning towards that one.

Metadata

Metadata

Assignees

Labels

rfcRequest for Commentssquinsquin related issues

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions