Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow passing None to AbstractCircuit.from_moments #6637

Merged
merged 2 commits into from
Jun 7, 2024

Conversation

maffoo
Copy link
Contributor

@maffoo maffoo commented Jun 7, 2024

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)

@maffoo maffoo requested review from vtomole, cduck and a team as code owners June 7, 2024 04:40
@CirqBot CirqBot added the size: S 10< lines changed <50 label Jun 7, 2024
Copy link

codecov bot commented Jun 7, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

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

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

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@maffoo maffoo requested a review from senecameeks June 7, 2024 17:16
for m in moments:
if isinstance(m, Moment):
yield m
elif isinstance(m, AbstractCircuit):
yield Moment(m.freeze().to_op())
else:
elif m is not None:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be elif m to encapsulate the empty [] case as well? Since None is expected to replace the [] case, I'd expect them to yield the same output. WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't intend for None to replace []. The former would be skipped, while the latter would produce an empty moment in the circuit.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for clarifying!

for m in moments:
if isinstance(m, Moment):
yield m
elif isinstance(m, AbstractCircuit):
yield Moment(m.freeze().to_op())
else:
elif m is not None:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for clarifying!

@maffoo maffoo merged commit 03aa511 into main Jun 7, 2024
37 checks passed
@maffoo maffoo deleted the u/maffoo/from-moments-none branch June 7, 2024 18:26
harry-phasecraft pushed a commit to PhaseCraft/Cirq that referenced this pull request Oct 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size: S 10< lines changed <50
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants