quantumlib / Cirq

A Python framework for creating, editing, and invoking Noisy Intermediate Scale Quantum (NISQ) circuits.
Apache License 2.0
4.24k stars 1.01k forks source link

Allow passing `None` to `AbstractCircuit.from_moments` #6637

Closed maffoo closed 3 months ago

maffoo commented 3 months ago

When constructing circuits with Circuit.from_moments we often want to conditionally include a moment and we often end up with awkward expressions like:

Circuit.from_moments(prepare_ops, *([flip_ops] if flip else []), measure_ops)

This PR would allow us to pass None as an arg to from_moments in which case that arg would be skipped and not included in the circuit (not even as an empty moment). The above code can then be written more clearly as:

Circuit.from_moments(prepare_ops, flip_ops if flip else None, measure_ops)
codecov[bot] commented 3 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 97.81%. Comparing base (543d9cd) to head (0cddef6). Report is 1 commits behind head on main.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #6637 +/- ## ======================================= Coverage 97.81% 97.81% ======================================= Files 1067 1067 Lines 91550 91551 +1 ======================================= + Hits 89549 89550 +1 Misses 2001 2001 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.