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

Refactor encapsulation of Bit storage #13377

Open
wants to merge 17 commits into
base: main
Choose a base branch
from

Conversation

kevinhartman
Copy link
Contributor

@kevinhartman kevinhartman commented Oct 28, 2024

Summary

This PR follows up #13284 (which added an interface for converting bit types into and out of usize) with encapsulation of the internal representation used for bits. Previously, Qubit and Clbit were transparent wrappers around u32. Their internal representation is now private to encourage users to deal with bits directly or as usize via their index. As part of this, I've changed a lot of API surface in accelerate to deal in terms of usize for bit indices as well.

Details and comments

  • The BitData struct has also been made private to the circuit crate. It was never intended as API surface for other crates, but rather as a short term way for us to track the association between native and Python bits. To support this, a new crate private type WireIndex has been added which is implemented by Qubit, Clbit and Var. It serves as a bound for the type T used in a BitData<T> and provides crate-private conversion between our wire types and usize. With these changes, it is (intentionally) also no longer possible for Var to be constructed outside of the circuit crate.
  • A CircuitData::from_dag function has also been added to support this encapsulation.
  • The From<Qubit> trait is also now implemented for PhysicalQubit and VirtualQubit (though these really also ought to move to the circuit crate soon...).
  • Types like PyInstruction and PyGate still publicly store their num_qubits and num_clbits as u32. We should probably consider making these fields crate(pub) as well.

Needs a rebase on #13368.

kevinhartman and others added 8 commits October 23, 2024 13:46
Co-authored-by: John Lapeyre <[email protected]>
By limiting its visibility to the `qiskit-circuit` crate,
we can hide the storage type used by Qubit and Clbit as
an implementation detail of the circuit crate (also done
in this PR).

As a result of this change, the accelerate crate (and
future crates) are forced to use the `::new` and `::index`
methods of `Qubit` and `Clbit`, which deal in terms of usize.
The BitData type was only ever intended as an implementation detail
of CircuitData and DAGCircuit. As such, its visibility has been
downgraded from pub to pub(crate). By limiting visibility of BitData
to the circuit crate, we can fully encapsulate the storage type of
Qubit, Clbit, and dagcircuit::Var. We also encourage the accelerate
crate to avoid writing code which depends on conversion to and from
the Python bit representations (there were only two existing places
where BitData was being used in accelerate, and it'd be nice to
avoid any more.
Also removes From<u32> and Into<u32> implementations for bit types
(and DAGCircuit's Var type) to improve data encapsulation
outside of the circuit crate. In its place, we now have WireIndex
which is private to the circuit crate, which we implement for our
wire types and use as a bound for types used with BitData going
forward.
@kevinhartman kevinhartman changed the title Improve encapsulation of Bit storage Refactor encapsulation of Bit storage Nov 1, 2024
@kevinhartman kevinhartman marked this pull request as ready for review November 11, 2024 22:39
@qiskit-bot
Copy link
Collaborator

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

  • @Qiskit/terra-core
  • @levbishop

@coveralls
Copy link

Pull Request Test Coverage Report for Build 11787261722

Details

  • 651 of 669 (97.31%) changed or added relevant lines in 43 files are covered.
  • 20 unchanged lines in 4 files lost coverage.
  • Overall coverage increased (+0.006%) to 88.941%

Changes Missing Coverage Covered Lines Changed/Added Lines %
crates/circuit/src/dag_node.rs 1 2 50.0%
crates/accelerate/src/synthesis/clifford/utils.rs 1 3 33.33%
crates/accelerate/src/target_transpiler/mod.rs 6 8 75.0%
crates/accelerate/src/synthesis/evolution/pauli_network.rs 6 9 66.67%
crates/circuit/src/circuit_data.rs 38 41 92.68%
crates/circuit/src/operations.rs 318 321 99.07%
crates/circuit/src/dag_circuit.rs 12 16 75.0%
Files with Coverage Reduction New Missed Lines %
crates/accelerate/src/unitary_synthesis.rs 1 92.19%
crates/circuit/src/operations.rs 1 90.31%
crates/qasm2/src/lex.rs 6 91.73%
crates/qasm2/src/parse.rs 12 97.15%
Totals Coverage Status
Change from base Build 11784569909: 0.006%
Covered Lines: 79178
Relevant Lines: 89023

💛 - Coveralls

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