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

[DAGCircuit Oxidation] Refactor bit management in CircuitData #12372

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

Conversation

kevinhartman
Copy link
Contributor

@kevinhartman kevinhartman commented May 8, 2024

Summary

This is part of the work we're doing to port DAGCircuit to Rust. It's work I've pulled out of a local branch where I'm doing that port, in an effort to reduce the size of that eventual PR.

Details and comments

  • Added zero-cost struct types for our native Qubit and Clbit representation.
  • Added a new struct BitData which encapsulates maintaining a mapping between native Rust bit types and Python bit types. This significantly cleans up the code in CircuitData and makes it reusable for DAGCircuit.
  • Made InternContext into a generic SlottedCache data structure, which implements slotted caching for any T which is Hash and Eq. This is used to implement the new trait Interner<Vec<Qubit>> and Interner<Vec<Clbit>> for CircuitData.
  • Added Interner trait, for defining interner semantics for containers like CircuitData (and DAGCircuit).
  • Added PyNativeMapper trait, for defining a mapping between Python and native Rust types for containers like CircuitData.
  • Added trait InstructionPacker, implemented for containers which provide intern semantics for bit lists and mappings from Python bits to native bits. This allows us to share our instruction packing implementation between CircuitData and DAGCircuit.

@kevinhartman kevinhartman added the Changelog: None Do not include in changelog label May 8, 2024
@kevinhartman kevinhartman requested a review from a team as a code owner May 8, 2024 20:36
@qiskit-bot
Copy link
Collaborator

One or more of the the following people are requested to review this:

@coveralls
Copy link

coveralls commented May 8, 2024

Pull Request Test Coverage Report for Build 9019232369

Details

  • 234 of 248 (94.35%) changed or added relevant lines in 6 files are covered.
  • 17 unchanged lines in 3 files lost coverage.
  • Overall coverage increased (+0.02%) to 89.648%

Changes Missing Coverage Covered Lines Changed/Added Lines %
crates/circuit/src/slotted_cache.rs 20 23 86.96%
crates/circuit/src/packed_instruction.rs 57 61 93.44%
crates/circuit/src/bit_data.rs 68 75 90.67%
Files with Coverage Reduction New Missed Lines %
qiskit/transpiler/passes/synthesis/unitary_synthesis.py 1 88.38%
crates/qasm2/src/lex.rs 4 92.88%
crates/qasm2/src/parse.rs 12 97.15%
Totals Coverage Status
Change from base Build 9004289325: 0.02%
Covered Lines: 62248
Relevant Lines: 69436

💛 - Coveralls

Comment on lines +31 to +36
struct BitAsKey {
/// Python's `hash()` of the wrapped instance.
hash: isize,
/// The wrapped instance.
bit: PyObject,
}
Copy link
Member

Choose a reason for hiding this comment

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

I'm wondering if we maybe should remove one of the layers of abstraction here. It's something I've been thinking about in #12205 is that the source of truth for bits is still python but for interaction from rust space this isn't super ergonomic. I'm wondering if instead of playing games like this we try flattening the two representations. I guess the complexity is things like register and index (although #11596 will take care of that)

Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure if this is what you were getting at, but I was thinking longer term that we'll want/need to make it possible to construct circuits from Rust space without creating any Python objects at all. We might not be at that point yet, but it might to be thinking about it.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, that's basically what I was getting at, having a pattern for making a circuit without any python interaction is what we'll want longer term. The example I have for making circuits from rust space right now still needs a py handle because we need to instantiate Qubit objects.

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'd agree as well. BitAsKey isn't new here, though. I've only moved it from CircuitData.

Also, the source of truth for bits is CircuitData. It owns its bits, and QuantumCircuit simply "references" them.

@mtreinish mtreinish added the Rust This PR or issue is related to Rust code in the repository label May 16, 2024
@kevinhartman
Copy link
Contributor Author

I'm rethinking a bit of this PR (specifically the stuff with packing and interning) so I'm going to put this into draft for now.

The BitData stuff is likely to stay, but I'm likely to reduce the traiting stuff around packing and interning a bit.

@kevinhartman kevinhartman marked this pull request as draft May 17, 2024 15:33
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 Rust This PR or issue is related to Rust code in the repository
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants