Skip to content

Create a new condition that allows easy control by bitmasks and Add a new classical Update the notebook for 'Classical control' to reflect new features" #7166

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

Merged
merged 21 commits into from
Mar 31, 2025

Conversation

NoureldinYosri
Copy link
Collaborator

@NoureldinYosri NoureldinYosri commented Mar 19, 2025

This PR creates BitMaskKeyCondition a condition that can be used to create multiqubit classical conditions with bitmasks. This will be much easier than using sympy indexedbase

cc: @ikd-sci


Examples:

Any bit from 0, 2, 3 => 0b1101 = 13

In [4]: cond = cirq.BitMaskKeyCondition('m', bitmask=13)
   ...: c = cirq.Circuit(
   ...:     cirq.H.on_each(qs),
   ...:     cirq.measure(qs, key='m'),
   ...:     cirq.Y(cirq.q(5)).with_classical_controls(cond),
   ...: 
   ...: )
   ...: c
Out[4]: 
0: ───H───M────────────────────────────
          ║
1: ───H───M────────────────────────────
          ║
2: ───H───M────────────────────────────
          ║
3: ───H───M────────────────────────────
          ║
4: ───H───M────────────────────────────
          ║
5: ───────╫───Y(conditions=[m & 13])───
          ║   ║
m: ═══════@═══^════════════════════════

Contains 0b1011 = 11 as bitmask

In [5]: cond = cirq.BitMaskKeyCondition.create_equal_mask('m', bitmask=11)
   ...: c = cirq.Circuit(
   ...:     cirq.H.on_each(qs),
   ...:     cirq.measure(qs, key='m'),
   ...:     cirq.Y(cirq.q(5)).with_classical_controls(cond),
   ...: 
   ...: )
   ...: c
Out[5]: 
0: ───H───M────────────────────────────────────
          ║
1: ───H───M────────────────────────────────────
          ║
2: ───H───M────────────────────────────────────
          ║
3: ───H───M────────────────────────────────────
          ║
4: ───H───M────────────────────────────────────
          ║
5: ───────╫───Y(conditions=[(m & 11) == 11])───
          ║   ║
m: ═══════@═══^════════════════════════════════

Doesn't contain 11 as a bitmask

In [6]: cond = cirq.BitMaskKeyCondition.create_not_equal_mask('m', bitmask=11)
   ...: c = cirq.Circuit(
   ...:     cirq.H.on_each(qs),
   ...:     cirq.measure(qs, key='m'),
   ...:     cirq.Y(cirq.q(5)).with_classical_controls(cond),
   ...: 
   ...: )
   ...: c
Out[6]: 
0: ───H───M────────────────────────────────────
          ║
1: ───H───M────────────────────────────────────
          ║
2: ───H───M────────────────────────────────────
          ║
3: ───H───M────────────────────────────────────
          ║
4: ───H───M────────────────────────────────────
          ║
5: ───────╫───Y(conditions=[(m & 11) != 11])───
          ║   ║
m: ═══════@═══^════════════════════════════════

update the notebook to reflect the upgraded sympy support from #6914 and the new bitmask condition from #7165

Copy link

codecov bot commented Mar 19, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.14%. Comparing base (c50ce1e) to head (c9cdc67).
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #7166   +/-   ##
=======================================
  Coverage   98.14%   98.14%           
=======================================
  Files        1100     1100           
  Lines       96147    96241   +94     
=======================================
+ Hits        94365    94459   +94     
  Misses       1782     1782           

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@NoureldinYosri NoureldinYosri changed the title Update the notebook for 'Classical control' to reflect new features" Create a new condition that allows easy control by bitmasks and Add a new classical Update the notebook for 'Classical control' to reflect new features" Mar 21, 2025
@NoureldinYosri NoureldinYosri marked this pull request as ready for review March 21, 2025 23:55
@NoureldinYosri NoureldinYosri requested review from vtomole and a team as code owners March 21, 2025 23:55
Copy link
Collaborator

@daxfohl daxfohl left a comment

Choose a reason for hiding this comment

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

Lgtm. All my concerns were addressed in previous PR.

Copy link
Collaborator

@dstrain115 dstrain115 left a comment

Choose a reason for hiding this comment

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

LGTM minus some nits.

def attrs_json_dict(obj: Any) -> Dict[str, Any]:
"""Return a dictionary suitable for `_json_dict_` from an attrs dataclass."""
attribute_names = [f.name for f in attrs.fields(type(obj))]
return obj_to_dict_helper(obj, attribute_names)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you add a unit test for this function?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

"""A multiqubit classical control condition with a bitmask.

The control is based on a single measurement key and allows comparing equality or inequality
after taking the bitwise and with a bitmask.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we have a short blurb about the bit order of the measurement (or a reference to the measurement code that explains)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

added

@staticmethod
def create_equal_mask(
key: 'cirq.MeasurementKey', bitmask: int, *, index: int = -1
) -> 'BitMaskKeyCondition':
Copy link
Collaborator

Choose a reason for hiding this comment

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

Add docstring.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

@staticmethod
def create_not_equal_mask(
key: 'cirq.MeasurementKey', bitmask: int, *, index: int = -1
) -> 'BitMaskKeyCondition':
Copy link
Collaborator

Choose a reason for hiding this comment

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

Add docstring.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

@NoureldinYosri NoureldinYosri added this pull request to the merge queue Mar 31, 2025
Merged via the queue into quantumlib:main with commit 8a3d1ef Mar 31, 2025
38 checks passed
@NoureldinYosri NoureldinYosri deleted the classical_control_docs branch March 31, 2025 19:05
BichengYing pushed a commit to BichengYing/Cirq that referenced this pull request Jun 20, 2025
… new classical Update the notebook for 'Classical control' to reflect new features" (quantumlib#7166)

* Create a new condition that allows easy control by bitmasks

* add tests

* docstring

* add missing files

* swith from dataclasses to attrs

* mypy

* Update the notebook for 'Classical control' to reflect new features

* nit

* nit

* tests

* nit

* fix doc regex

* format

* address comments

* lint
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants