You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Use singleton matrices for unparametrised standard gates (Qiskit#10296)
* Use singleton matrices for unparametrised standard gates
This makes the array form of standard gates with zero parameters
singleton class attributes that reject modification. The class-level
`__array__` methods are updated to return exactly the same instance,
except in very unusual circumstances, which means that
`Gate.to_matrix()` and `numpy.asarray()` calls on the objects will
return the same instance. This avoids a decent amount of construction
time, and avoids several Python-space list allocations and array
allocations.
The dtypes of the static arrays are all standardised to by complex128.
Gate matrices are in general unitary, `Gate.to_matrix()` already
enforces a cast to `complex128`. For gates that allowed their dtypes to
be inferred, there were several cases where native ints and floats would
be used, meaning that `Gate.to_matrix()` would also involve an extra
matrix allocation to hold the cast, which just wasted time.
For standard controlled gates, we store both the closed- and
open-controlled matrices singly controlled gates. For gates with more
than one control, we only store the "all ones" controlled case, as a
memory/speed trade-off; open controls are much less common than closed
controls.
For the most part this won't have an effect on peak memory usage, since
all the allocated matrices in standard Qiskit usage would be freed by
the garbage collector almost immediately. This will, however, reduce
construction costs and garbage-collector pressure, since fewer
allocations+frees will occur, and no calculations will need to be done.
* Store only all-ones controls for large matrices
* Fix lint
* Use metaprogramming decorator to make `__array__` methods
Instead of defining the array functions manually for each class, this
adds a small amount of metaprogramming that adds them in with the
correct `ndarray` properties set, including for controlled gates.
0 commit comments