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

Create a new condition that allows easy control by bitmasks #7165

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from

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: ═══════@═══^════════════════════════════════


@property
def qasm(self):
raise ValueError('QASM is defined only for SympyConditions of type key == constant.')
Copy link
Collaborator

Choose a reason for hiding this comment

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

Looks like this is doable in OpenQASM 3.0.

Copy link
Collaborator

Choose a reason for hiding this comment

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

(FWIW I wish now that I hadn't made qasm abstract, and instead just implemented _qasm_ directly in the subclasses. As-is, qasm doesn't contain the args to check OpenQASM version, so you'd have to implement _qasm_ here anyway, but you still have to implement qasm because it's abstract. I'd recommend deprecating and removing qasm if it's possible.)

Copy link
Collaborator Author

@NoureldinYosri NoureldinYosri Mar 19, 2025

Choose a reason for hiding this comment

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

for now I plan to just raise UnImplementedError and let the qasm decision for the people working on qasm integration

Copy link

codecov bot commented Mar 19, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.13%. Comparing base (3ae4ddb) to head (6cfb326).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #7165   +/-   ##
=======================================
  Coverage   98.13%   98.13%           
=======================================
  Files        1093     1093           
  Lines       95579    95666   +87     
=======================================
+ Hits        93797    93883   +86     
- Misses       1782     1783    +1     

☔ 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.

)
index: int = -1
target_value: int = 0
equal_target: bool = False
Copy link
Collaborator

@daxfohl daxfohl Mar 20, 2025

Choose a reason for hiding this comment

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

Maybe make this an enum instead of bool in case it needs extended?

@@ -135,6 +136,107 @@ def _qasm_(self, args: 'cirq.QasmArgs', **kwargs) -> Optional[str]:
return f'{key}==1'


@attrs.frozen
class BitMaskKeyCondition(Condition):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Curious why create a new class rather than add the functionality to KeyCondition?

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.

2 participants