Fix #324: use match for better readability and typing#434
Open
thierry-martinez wants to merge 2 commits intoTeamGraphix:masterfrom
Open
Fix #324: use match for better readability and typing#434thierry-martinez wants to merge 2 commits intoTeamGraphix:masterfrom
match for better readability and typing#434thierry-martinez wants to merge 2 commits intoTeamGraphix:masterfrom
Conversation
This commit replaces `if`/`elif`/`else` chains with `match` when the
conditions perform equality comparisons on the same subject. These
changes improve readibility and avoid `mypy` shortcomings related to
type narrowing.
Before this commit, PLR1714 suggested rewriting code such as:
```python
if cmd.kind == CommandKind.X or cmd.kind == CommandKind.Z: ...
```
into:
```python
if cmd.kind in {CommandKind.X, CommandKind.Z}: ...
```
However, `mypy` does not currently infer that `cmd` is an instance of
`X` or `Z` within the `if` block when set membership is used.
With this commit, the condition is expressed using an or-pattern, and
type checking behaves as expected:
```python
match cmd.kind:
case CommandKind.X | CommandKind.Z: ...
```
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #434 +/- ##
==========================================
- Coverage 86.72% 86.33% -0.40%
==========================================
Files 44 44
Lines 6163 6243 +80
==========================================
+ Hits 5345 5390 +45
- Misses 818 853 +35 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
emlynsg
reviewed
Feb 16, 2026
Contributor
emlynsg
left a comment
There was a problem hiding this comment.
I've suggested additional places to replace if else with match. Happy to discuss.
I also found a few other locations:
ops.pyfrom L221pauli.pyfrom L71 (eigenstateand_repr_impland_matmul_implfunctions).
| circuit = Circuit(width=self.width) | ||
| for instr in self.instruction: | ||
| if instr.kind == InstructionKind.M: | ||
| if instr.axis == Axis.X: |
Contributor
There was a problem hiding this comment.
Minor but slight readability improvement:
match instr.axis:
case Axis.X:
circuit.h(instr.target)
circuit.m(instr.target, Axis.Z)
case Axis.Y:
circuit.rx(instr.target, ANGLE_PI / 2)
circuit.m(instr.target, Axis.Z)
case _:
circuit.add(instr)
Could possibly do the same to the outer if else.
| else: | ||
| raise ValueError(f"invalid command {cmd}") | ||
| yield f"h q{cmd.node};\n" | ||
| if cmd.angle != 0: |
Contributor
There was a problem hiding this comment.
Similar here:
match cmd.plane:
case Plane.XY:
gate = "rx"
angle = -cmd.angle
case Plane.XZ:
gate = "ry"
angle = -cmd.angle
case Plane.YZ:
gate = "rx"
angle = cmd.angle
case _:
assert_never(cmd.plane)
| non_pauli_list.append( | ||
| command.M(node=cmd.node, angle=cmd.angle, plane=cmd.plane, s_domain=s_domain, t_domain=t_domain) | ||
| ) | ||
| else: |
| target = self._find_op_to_be_moved(CommandKind.S, rev=True) | ||
| continue | ||
| cmd = self.__seq[target + 1] | ||
| kind = cmd.kind |
Contributor
There was a problem hiding this comment.
Can replace these with match too
| pm = PauliMeasurement.try_from(cmd.plane, cmd.angle) # None returned if the measurement is not in Pauli basis | ||
| if pm is not None: | ||
| # Pauli measurement to be removed | ||
| if pm.axis == Axis.X: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This commit replaces
if/elif/elsechains withmatchwhen the conditions perform equality comparisons on the same subject. These changes improve readibility and avoidmypyshortcomings related to type narrowing.Before this commit, PLR1714 suggested rewriting code such as:
into:
However,
mypydoes not currently infer thatcmdis an instance ofXorZwithin theifblock when set membership is used.With this commit, the condition is expressed using an or-pattern, and type checking behaves as expected: