cuquantum.densitymat.DenseOperator¶
- class cuquantum.densitymat.DenseOperator(data, callback=None, copy=True)[source]¶
Dense elementary operator from data buffer and optional callback.
- Parameters
data – Data buffer for operator elements.
callback – An inplace or out-of-place callback function that modifies CPU or GPU buffer.
Note
If number of dimensions in
data
is odd, the last dimension is assumed to be the batch dimension.If
copy=True
, a copy will be created on the data buffer and can be accessed through thedata
attribute. Note that if a np.ndarray is passed, it will be copied to GPU at a later stage.If
copy=False
, the provided data buffer is required to be a cp.ndarray and Fortran-contiguous.The current underlying data buffer is accessible via the
data
attribute. Modification of the underlying data buffer by the user will lead to undefined behaviour.If an out-of-place callback is provided, it needs to return an array needs that is consistent with the provided data buffer in terms of shape and data type.
If an inplace callback is provided, it needs perform an inplace modification on an array that is provided as its third positional argument.
The data buffer will be updated when this instance is involved in a
compute
method of anOperator
orOperatorAction
if a callback is passed.
Examples
>>> import numpy as np >>> from cuquantum.densitymat import DenseOperator
Suppose we want to construct a creation operator on a Hilbert space of dimension 3 as a
DenseOperator
. It can be constructed from the data buffer directly as>>> data = np.array([ >>> [0, 0, 0], >>> [1.0, 0, 0], >>> [0, np.sqrt(2), 0], >>> ]) >>> dense_op = DenseOperator(data)
Methods
- __add__(other: Union[DenseOperator, MultidiagonalOperator]) DenseOperator [source]¶
Add an elementary operator to this instance and return a new
DenseOperator
.
- __init__(data: Union[numpy.ndarray, cupy.ndarray], callback: Optional[Callback] = None, copy: bool = True) None [source]¶
Initialize a dense elementary operator from data buffer and optional callback.
- __matmul__(other: Union[DenseOperator, MultidiagonalOperator]) DenseOperator [source]¶
Perform matrix multiplication between this instance and an elementary operator and return a new
DenseOperator
.
- __mul__(scalar: Union[numbers.Number, numpy.ndarray, cupy.ndarray]) DenseOperator [source]¶
Multiply this instance with a scalar on the left.
- __rmul__(scalar: Union[numbers.Number, numpy.ndarray]) DenseOperator [source]¶
Multiply this instance with a scalar on the right.
- __sub__(other: Union[DenseOperator, MultidiagonalOperator]) DenseOperator [source]¶
Subtract an elementary operator from this instance and return a new
DenseOperator
.
- dag() DenseOperator [source]¶
Return the conjugate complex transpose of this instance.
- to_array(t: Optional[float] = None, args: Optional[Union[numpy.ndarray, cupy.ndarray]] = None, device: Optional[Union[int, str]] = None) Union[numpy.ndarray, cupy.ndarray] [source]¶
Return the array form of the dense elementary operator.
- Parameters
t – Time variable in callback, only required if callback is not
None
.args – Additional arguments in callback, only required if callback is not
None
.device – Device on which to return the array. Defaults to
"cpu"
.
Note
If the device is not specified, an
ElementaryOperator
without callback will return a reference to its current underlying data. Otherwise, the return location will match that of theCallback
instance.If this instance has a callback, the callback arguments
t
andBeyond sys.argv
are required.This call is blocking if it involves device-to-host or device-to-device transfer. Otherwise, it is stream-ordered on the current stream.
Attributes
- data¶
Data buffer of the elementary operator.
- device_id¶
Return device ID if stored on GPU and
None
if stored on CPU.