Skip to content

Commit

Permalink
Fix performance issue in parsing bit indices in QPY (#11944)
Browse files Browse the repository at this point in the history
* Fix performance issue in parsing bit indices in QPY

This commit fixes a performance issue in the deserialization of QPY
payloads. Previously when parsing every instruction the deserialization
would build up a dictionary of indices to bits so that when we load the
bit index in the qargs and cargs for each instruction. This mean the
worst case scaling of this section was O(n * m) for n instructions and
m bits, which for very large circuits could end up dominating the time
spent in deserialization. However, this was unecessary work because it
is rebuilding a mapping from index to bit as a dictionary, but that
mapping already existed in the bit lists the circuit is carrying around.
This commit fixes the performance issue by removing the dictionary
generation and just using the bit lists directly.

* Add release note

* Simplify logic
  • Loading branch information
mtreinish committed Mar 4, 2024
1 parent 85411bb commit f4b50fb
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
11 changes: 2 additions & 9 deletions qiskit/qpy/binary_io/circuits.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,6 @@ def _read_instruction(
cregs=registers["c"],
use_symengine=use_symengine,
)
if circuit is not None:
qubit_indices = dict(enumerate(circuit.qubits))
clbit_indices = dict(enumerate(circuit.clbits))
else:
qubit_indices = {}
clbit_indices = {}

# Load Arguments
if circuit is not None:
for _qarg in range(instruction.num_qargs):
Expand All @@ -243,7 +236,7 @@ def _read_instruction(
)
if qarg.type.decode(common.ENCODE) == "c":
raise TypeError("Invalid input carg prior to all qargs")
qargs.append(qubit_indices[qarg.size])
qargs.append(circuit.qubits[qarg.size])
for _carg in range(instruction.num_cargs):
carg = formats.CIRCUIT_INSTRUCTION_ARG._make(
struct.unpack(
Expand All @@ -253,7 +246,7 @@ def _read_instruction(
)
if carg.type.decode(common.ENCODE) == "q":
raise TypeError("Invalid input qarg after all qargs")
cargs.append(clbit_indices[carg.size])
cargs.append(circuit.clbits[carg.size])

# Load Parameters
for _param in range(instruction.num_parameters):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
fixes:
- |
Fixed a performance issue in the :func:`.qpy.load` function when
deserializing QPY payloads with large numbers of qubits or clbits in
a circuit.

0 comments on commit f4b50fb

Please sign in to comment.