Closed jakelishman closed 2 months ago
One or more of the following people are relevant to this code:
@Qiskit/terra-core
@kevinhartman
@mtreinish
Changes Missing Coverage | Covered Lines | Changed/Added Lines | % | ||
---|---|---|---|---|---|
crates/circuit/src/circuit_instruction.rs | 16 | 25 | 64.0% | ||
<!-- | Total: | 17 | 26 | 65.38% | --> |
Files with Coverage Reduction | New Missed Lines | % | ||
---|---|---|---|---|
crates/circuit/src/circuit_instruction.rs | 1 | 87.41% | ||
crates/qasm2/src/expr.rs | 1 | 94.02% | ||
crates/accelerate/src/two_qubit_decompose.rs | 1 | 90.84% | ||
crates/circuit/src/dag_circuit.rs | 3 | 88.86% | ||
crates/qasm2/src/lex.rs | 7 | 91.48% | ||
crates/qasm2/src/parse.rs | 18 | 96.69% | ||
<!-- | Total: | 31 | --> |
Totals | |
---|---|
Change from base Build 10679848479: | -0.02% |
Covered Lines: | 71849 |
Relevant Lines: | 80609 |
Summary
The default value for
Instruction.unit
is"dt"
. Previously, theOperationFromPython
extraction logic would only suppress allocation of the extra instruction attributes if all the contained fields wereNone
, butNone
is not actually a valid value ofInstruction.unit
(which must be a string). This meant thatOperationFromPython
would always allocate and store extra attributes, even for the default cases. This did not affect standard gates appended using their correspondingQuantumCircuit
methods (since no Python-space extraction is performed in that case), but did affect standard calls toappend
, or anything else that entered from Python space.This drastically reduces the memory usage of circuits built by
append
-like methods. Ignoring the inefficiency factor of the heap-allocation implementation, this saves 66 bytes plus small-allocation overhead for 2-byte heap allocations (another 14 bytes on macOS, but will vary depending on the allocator) per standard instruction, which is on the order of 40% memory-usage reduction.Details and comments
I'm using the same sort of microbenchmarking script I've been using since #12730, but now modified to use
append
instead of the special methods onQuantumCircuit
:The memory usage of
main_append
for the parent of this PR is approximately 2.3GB on both macOS, and Linux with glibc, whereas with the PR it drops to 1.35GB on macOS and 1.06GB on Linux/glibc. I suspect there's something additional going on in the macOS one, because while the Linux/glibc one drops to match the memory usage ofmain_methods
(as expected), macOS remains 300MB higher. It might have been different Python versions - I used 3.10 on macOS and 3.12 on Linux.