Skip to content

Commit

Permalink
Simplify QuantumCircuit._from_circuit_data bit handling (#12661)
Browse files Browse the repository at this point in the history
* Simplify QuantumCircuit._from_circuit_data bit handling

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.

* Use a dict comprehension instead of a for loop
  • Loading branch information
mtreinish authored Jun 26, 2024
1 parent e36027c commit 4d3821b
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions qiskit/circuit/quantumcircuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -1161,9 +1161,9 @@ def __init__(
def _from_circuit_data(cls, data: CircuitData) -> typing.Self:
"""A private constructor from rust space circuit data."""
out = QuantumCircuit()
out.add_bits(data.qubits)
out.add_bits(data.clbits)
out._data = data
out._qubit_indices = {bit: BitLocations(index, []) for index, bit in enumerate(data.qubits)}
out._clbit_indices = {bit: BitLocations(index, []) for index, bit in enumerate(data.clbits)}
return out

@staticmethod
Expand Down

0 comments on commit 4d3821b

Please sign in to comment.