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
4.83k stars 2.29k forks source link

Simplify QuantumCircuit._from_circuit_data bit handling #12661

Closed mtreinish closed 5 days ago

mtreinish commented 5 days ago

Summary

This commit simplifies the logic around bit handling in the QuantumCircuit._from_circuit_data() constructor. Previously it was calling add_bits() for each bit in the CircuitData object to update the output circuit's accounting for each qubit. But this was needlessly heavy as the CircuitData is already the source of truth for the bits in a circuit and we just need to update the indices dictionary. The add_bits() method attempts to add the bits to the CircuitData too but this is wasted overhead because the CircuitData already has the bits as that's where the came from. This changes the constructor to just directly set the bit indices as needed and return the circuit.

Details and comments

qiskit-bot commented 5 days ago

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

coveralls commented 5 days ago

Pull Request Test Coverage Report for Build 9670599215

Details


Changes Missing Coverage Covered Lines Changed/Added Lines %
qiskit/circuit/quantumcircuit.py 3 4 75.0%
<!-- Total: 3 4 75.0% -->
Files with Coverage Reduction New Missed Lines %
crates/qasm2/src/expr.rs 1 94.02%
crates/qasm2/src/lex.rs 5 91.6%
<!-- Total: 6 -->
Totals Coverage Status
Change from base Build 9661630219: 0.01%
Covered Lines: 63759
Relevant Lines: 71056

💛 - Coveralls
mtreinish commented 5 days ago

It showed up in py-spy profiling I was doing of permutation synthesis. It wasn't a major component but I was surprised to see it there so I pushed this up to fix it. A dict comprehension is a good idea and will make it a bit faster, I'll tweak this shortly.

coveralls commented 5 days ago

Pull Request Test Coverage Report for Build 9678093460

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details


Files with Coverage Reduction New Missed Lines %
crates/qasm2/src/expr.rs 1 94.02%
qiskit/synthesis/discrete_basis/solovay_kitaev.py 4 94.74%
crates/qasm2/src/lex.rs 6 91.86%
qiskit/providers/fake_provider/generic_backend_v2.py 10 94.71%
<!-- Total: 21 -->
Totals Coverage Status
Change from base Build 9661630219: 0.02%
Covered Lines: 63768
Relevant Lines: 71062

💛 - Coveralls