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

Move Option<Box<_>> into ExtraInstructionAttributes. #13127

Merged
merged 3 commits into from
Sep 12, 2024

Conversation

kevinhartman
Copy link
Contributor

@kevinhartman kevinhartman commented Sep 10, 2024

Summary

The primary motivation here is to encapsulate the hairy logic of managing memory for extra attributes. By moving the Option<Box<_>> into the ExtraInstructionAttributes struct itself, clients are no longer burdened with the responsibility of checking if the Option contains a value, unwrapping it etc.; the semantics of that aren't relevant to clients like DAGCircuit and CircuitData. Instead, the ExtraInstructionAttributes acts as a container for optional attributes, and can easily drop the Box internally if all attributes are cleared.

A give-away that this pattern could be helpful was ExtraInstructionAttributes::new's return type, which was previously Option<Self>.

Details and comments

The size of ExtraInstructionAttributes (now held by value by instructions) should be the same size as the old Option<Box<ExtraInstructionAttributes>>, so this should not have memory implications regarding the size of instructions.

Note that ExtraAttrs is completely private to the circuit_instruction module, i.e. it's only an implementation detail and in no way part of the interface.

@kevinhartman kevinhartman requested a review from a team as a code owner September 10, 2024 22:25
@qiskit-bot
Copy link
Collaborator

One or more of the following people are relevant to this code:

  • @Qiskit/terra-core
  • @kevinhartman
  • @mtreinish

@coveralls
Copy link

coveralls commented Sep 10, 2024

Pull Request Test Coverage Report for Build 10837025267

Details

  • 107 of 164 (65.24%) changed or added relevant lines in 8 files are covered.
  • 21 unchanged lines in 6 files lost coverage.
  • Overall coverage decreased (-0.02%) to 88.89%

Changes Missing Coverage Covered Lines Changed/Added Lines %
crates/circuit/src/packed_instruction.rs 2 3 66.67%
crates/circuit/src/dag_node.rs 5 7 71.43%
crates/circuit/src/circuit_instruction.rs 54 108 50.0%
Files with Coverage Reduction New Missed Lines %
crates/circuit/src/dag_circuit.rs 1 88.54%
crates/qasm2/src/expr.rs 1 94.02%
qiskit/transpiler/passes/synthesis/unitary_synthesis.py 2 88.26%
crates/qasm2/src/lex.rs 4 92.23%
crates/qasm2/src/parse.rs 6 97.61%
qiskit/synthesis/two_qubit/xx_decompose/decomposer.py 7 90.91%
Totals Coverage Status
Change from base Build 10834414649: -0.02%
Covered Lines: 73365
Relevant Lines: 82535

💛 - Coveralls

Copy link
Member

@jakelishman jakelishman left a comment

Choose a reason for hiding this comment

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

Thanks for doing this, it looks like a very sensible idea.

crates/circuit/src/circuit_instruction.rs Outdated Show resolved Hide resolved
crates/circuit/src/circuit_instruction.rs Outdated Show resolved Hide resolved
crates/circuit/src/circuit_instruction.rs Outdated Show resolved Hide resolved
crates/circuit/src/operations.rs Outdated Show resolved Hide resolved
Previously, CircuitInstruction and PackedInstruction held
the ExtraInstructionAttributes struct within an Option<Box<_>>.
By putting the Option<Box<_>> inside ExtraInstructionAttributes,
we can use the struct itself to manage its memory and provide
access to the attributes behind a nicer interface.

The size of ExtraInstructionAttributes should be the same size
as the old Option<Box<ExtraInstructionAttributes>>, so this
should not have memory implications.
- Use tuple struct.
- Use 'Attributes' over 'Attrs'.
- Add doc comment for internal 'ExtraAttributes' struct.
- Add doc comments for methods.
- Add setters for unit and duration.
Copy link
Member

@jakelishman jakelishman left a comment

Choose a reason for hiding this comment

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

Thanks for this and the changes. I'll leave unmerged because I saw elsewhere that Ray was going to look too.

Copy link
Contributor

@raynelfss raynelfss left a comment

Choose a reason for hiding this comment

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

This is all sensible to me, my only comment was about the usage of a map-like struct instead of a simple tuple struct but it seems it's already been corrected :)

This should be good as is. Thanks for working on it! 🚀

@raynelfss raynelfss enabled auto-merge September 12, 2024 19:52
@raynelfss raynelfss added this pull request to the merge queue Sep 12, 2024
Merged via the queue into Qiskit:main with commit c655acb Sep 12, 2024
15 checks passed
ElePT pushed a commit to ElePT/qiskit that referenced this pull request Sep 18, 2024
* Move Option<Box<_>> into ExtraInstructionAttributes.

Previously, CircuitInstruction and PackedInstruction held
the ExtraInstructionAttributes struct within an Option<Box<_>>.
By putting the Option<Box<_>> inside ExtraInstructionAttributes,
we can use the struct itself to manage its memory and provide
access to the attributes behind a nicer interface.

The size of ExtraInstructionAttributes should be the same size
as the old Option<Box<ExtraInstructionAttributes>>, so this
should not have memory implications.

* Address review comments.

- Use tuple struct.
- Use 'Attributes' over 'Attrs'.
- Add doc comment for internal 'ExtraAttributes' struct.
- Add doc comments for methods.
- Add setters for unit and duration.

* Fix performance regression from unnecessary dict creation.
@raynelfss raynelfss added the Changelog: None Do not include in changelog label Nov 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: None Do not include in changelog
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants