Qiskit / qiskit

Qiskit is an open-source SDK for working with quantum computers at the level of extended quantum circuits, operators, and primitives.
https://www.ibm.com/quantum/qiskit
Apache License 2.0
5.11k stars 2.34k forks source link

Expose `CircuitData` interners and registers to the `qiskit-circuit` crate #13006

Closed raynelfss closed 1 month ago

raynelfss commented 1 month ago

Summary

Tracked by #13001 This commit adds small changes to the visibility of the internal data of the CircuitData object, which would allow for an easier conversion into a DAGCircuit once #13001 is completed.

Details and comments:

Suggestions

Leave any suggestions in a comment here.

qiskit-bot commented 1 month ago

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

coveralls commented 1 month ago

Pull Request Test Coverage Report for Build 10535483243

Details


Changes Missing Coverage Covered Lines Changed/Added Lines %
crates/circuit/src/circuit_data.rs 2 23 8.7%
<!-- Total: 2 23 8.7% -->
Files with Coverage Reduction New Missed Lines %
crates/accelerate/src/two_qubit_decompose.rs 1 90.82%
crates/qasm2/src/lex.rs 5 91.73%
<!-- Total: 6 -->
Totals Coverage Status
Change from base Build 10529709209: -0.02%
Covered Lines: 71504
Relevant Lines: 80192

💛 - Coveralls
raynelfss commented 1 month ago

@kevinhartman I agree with making most of the fields in CircuitData publicly visible except for two of them.

Thus, that's why I think having both interners and the registers (BitData) be the only attributes to be made public to the crate is the correct approach for now.

mtreinish commented 1 month ago

What about global_phase?

kevinhartman commented 1 month ago

exposing it would allow it to be mutable if the CircuitData instance happens to be owned or mutably borrowed.

That's true, but what I'm getting at is having any of the fields public (e.g. the DAG's bits) makes it possible for things within qiskit-circuit to modify DAGCircuit in ways that make it invalid. For example, with the current state of this PR, we could remove the DAG's bits even if it contains operations that use them. It seems odd to force the implementor of circuit_to_dag (and the rest of this crate) to use some parts of the proper / public DAGCircuit (e.g. DAGCircuit::iter) but also expose a select few of its internal fields. My thinking is that because what we're exposing here already gives qiskit-circuit the power to create bad data, we shouldn't try to pretend that the visibility has been designed to protect anything.

kevinhartman commented 1 month ago

I totally closed that by accident!! I'm so sorry @raynelfss >.<

raynelfss commented 1 month ago

Well in that case do you suggest to have functions that return immutable references instead, or functions that can unpack things. I.e. unpack_qarg() or get_qubits(), these could return immutable references and allow for cloning but never in-place modifications.

jakelishman commented 1 month ago

Since I was mentioned: my feeling is probably mostly with Kevin here, on the side of don't make things pub if you can break data coherence by mutating them (including assigning a new value to the field). That includes global_phase: you can break data coherence if you assign to it without suitably updating the parameter table.

Having immutable-view getters and safe setters is great. There'll definitely be places where we need to go further than that, and that'll involve thinking about what APIs we should expose that'll be both useful and efficient.

For CircuitData in particular, I think it's probably a good idea to not let the pub access get out of hand, even if in other places we've fallen more on the side of permissiveness for now - CircuitData is way more core, and the data-coherence requirements much harder to get right.

kevinhartman commented 1 month ago

Well in that case do you suggest to have functions that return immutable references instead

I would also vote for this option. Since PackedInstruction already exposes interner keys, it feels reasonable to me for CircuitData to expose immutable getters for the interners and bits etc. ^^

jakelishman commented 1 month ago

Fwiw, I do think many places will need to be able to query the interners in one way or another for performance, so either special methods, or making sure that there's a way to get at an immutable reference to an interner will be important, I think.

raynelfss commented 1 month ago

Alright then, I will make those changes promptly later in the day. Thank you for your input, team 🚀