Skip to content

Remove wire sizes - #10531

Open
pragati903 wants to merge 31 commits into
mainfrom
remove-wire_sizes
Open

pragati903 wants to merge 31 commits into
mainfrom
remove-wire_sizes

Conversation

@pragati903

Copy link
Copy Markdown

Context:
As part of the ongoing work on future-facing design abstractions for program expression (PL2), the Operator2 base class was originally implemented with a wire_sizes property to specify the expected sizes of each wire argument. However, with the subsequent addition of the comprehensive arg_specs property—which handles the expected shape and data type of every argument including wires—wire_sizes has become fully redundant.

Description of the Change:

  • Removed the wire_sizes class attribute declaration from the Operator2 base class interface.
  • Updated init_wires logic within Operator2 to eliminate any dependencies or fallbacks related to wire_sizes.
  • Removed wire_sizes specifications from subclass initializations.
  • Cleaned up and removed obsolete tests specifically testing wire_sizes.

Benefits:

  • Simplifies the Operator2 interface by removing redundant abstractions.
  • Aligns the codebase with the streamlined PL2 design architecture.

Possible Drawbacks:

  • Makes a breaking change

Related GitHub Issues:
#124345

@comp-phys-marc

Copy link
Copy Markdown
Contributor

Hello, and thanks so much for your contribution!! 🎉

Could you please resolve the merge conflicts so that we can run CI and make sure that there are no failures in the test suite? 🙂

Thanks so much!

Comment on lines -613 to 617
# make sure wires have the right sizes
error_msg = f"Wires argument {name} has an invalid dimension."
assert dim is None or len(val) == dim, error_msg


Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should we replace this check with a check against arg_specs?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks, I have added a test for the same.

Comment on lines -298 to -308
"""Test that when ``wire_sizes`` is not declared, it defaults to a tuple
of ``None`` matching the length of ``wire_argnames``."""

class Op(Operator2):
wire_argnames = ("wires", "ctrl_wires")

def __init__(self, wires, ctrl_wires):
super().__init__(wires=wires, ctrl_wires=ctrl_wires)

assert Op.wire_sizes == (None, None)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is this behaviour matched with arg_specs? Do we need a test for it?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks, I have added a test for this.

Comment thread tests/core/operator/test_operator2.py Resolved

@lillian542 lillian542 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks, Pragati! This is looking good. I left a few questions/suggestions regarding docstrings and tests :)


class Op(Operator2):
wire_sizes = (2,)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Some of the tests from here onward in this file look like they are testing behaviour that we still want in terms of validating the wire input matches the expected input for the operator - we just want it specified via arg_specs instead.

Are these same things already tested using arg_specs elsewhere? If not, let's keep the tests and update them to use arg_specs.

For example, we still want an operator whose arg_specs indicate 2 is the correct wire size to raise the same error as before 3 wires are passed - we're only changing how you specify that 2 is the correct size when defining the operator.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I have added these tests back and replaced wire_sizes with arg_specs where applicable

@@ -107,8 +108,6 @@ class CRot(Controlled2):

wire_argnames = ("wires",)

wire_sizes = (2,)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think in this example this needs to be replaced with arg_specs rather than just removed, since we still need to define the appropriate number of wires for the operator

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I have replaced with arg_specs


class SX2(Operator2):
"""A new SX gate."""

wire_sizes = (1,)

is_verified_hermitian = True

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It doesn't actually change anything about the current tests, but its probably best to replace this with arg_specs instead of just removing, as I understand it that would be the intended way of defining these operators going forward. Otherwise this new SX can have more than one wire.

Same goes for the operator definitions in the rest of the tests below :)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Updated by adding arg_specs = {"wires": Wire[1]}

@JerryChen97 JerryChen97 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Would you mind adding an item to changelog as well as your name to the contributor list? Thanks!

@codecov

codecov Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.38%. Comparing base (96640cc) to head (22c9835).
⚠️ Report is 7 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #10531      +/-   ##
==========================================
- Coverage   99.40%   99.38%   -0.03%     
==========================================
  Files         614      613       -1     
  Lines       67812    67616     -196     
==========================================
- Hits        67411    67202     -209     
- Misses        401      414      +13     

☔ View full report in Codecov by Harness.
📢 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.

@comp-phys-marc

Copy link
Copy Markdown
Contributor

Hi @pragati903 thanks so much for continuing to work on this! I wonder if I could please ask however for a little more descriptive commit messages, so the reviewers can see what is being changed and what might need our attention. Thanks! 🙂

Comment on lines 68 to 72
class RX2(Operator2):
"""A simplified RX operator that inherit from Operator2."""

dynamic_argnames = ("theta",)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Does it need an arg specs as well

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ah I think I got it it's all only for test dummy op2

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It's technically doesn't change anything about how the tests behave right now, but it would feel more consistent to me if we replaced wire_sizes with arg_specs in all these test classes, especially the ones that get reused and might be used in additional tests in the future. A simplified RX operator should only accept one wire, even if it's just created for testing; it seems like its a standard part of how the operator would be defined normally, so it should be in the test op definition too.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I have added arg_specs in each place where there was wire_sizes initially in testing operators

Comment thread doc/releases/changelog-dev.rst Resolved

@lillian542 lillian542 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks, @pragati903! I left a couple additional comments.

To me it seems like we should replace wire_sizes with arg_specs in the test ops throughout, instead of just removing them. I know it doesn't modify current test behaviour, but it's part of the standard definition for an operator with a set number of wires, which is what all of those tests are creating and testing.

validation of an operators inputs during construction. Additionally, when defining
decomposition rules for an operator, operator types with ``arg_specs`` that spans
all the arguments with static types can be placed in the rules' resources without needing
to fully construct abstract operators.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

should we add info in the docstring here about how to define wire sizes in arg_specs (i.e. any of the removed info above about how to support arbitrary size versus set wire size, etc, that might still be relevant, just defined in a new place now)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks, I have updated the docstring.

Comment on lines 68 to 72
class RX2(Operator2):
"""A simplified RX operator that inherit from Operator2."""

dynamic_argnames = ("theta",)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It's technically doesn't change anything about how the tests behave right now, but it would feel more consistent to me if we replaced wire_sizes with arg_specs in all these test classes, especially the ones that get reused and might be used in additional tests in the future. A simplified RX operator should only accept one wire, even if it's just created for testing; it seems like its a standard part of how the operator would be defined normally, so it should be in the test op definition too.

plt.savefig(folder / "mid_measure.png")
plt.close()


Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

These 3 files seem to have been reformatted and then accidentally added to the PR - they aren't related, so let's not change them here.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks. The formatting checks were failing until I ran black on these files, so I think it wouldn't be possible to merge the PR unless these files are reformatted.

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.

4 participants