Complex Algebra

qucumber.utils.cplx.absolute_value(x)[source]

Returns the complex absolute value elementwise.

Parameters

x (torch.Tensor) – A complex tensor.

Returns

A real tensor.

Return type

torch.Tensor

qucumber.utils.cplx.conj(x)[source]

Returns the element-wise complex conjugate of the argument.

Parameters

x (torch.Tensor) – A complex tensor.

Returns

The complex conjugate of x.

Return type

torch.Tensor

qucumber.utils.cplx.conjugate(x)[source]

Returns the conjugate transpose of the argument.

In the case of a scalar or vector, only the complex conjugate is taken. In the case of a rank-2 or higher tensor, the complex conjugate is taken, then the first two indices of the tensor are swapped.

Parameters

x (torch.Tensor) – A complex tensor.

Returns

The conjugate of x.

Return type

torch.Tensor

qucumber.utils.cplx.einsum(equation, a, b, real_part=True, imag_part=True)[source]

Complex-aware version of einsum. See the torch documentation for more details.

Parameters
  • equation (str) – The index equation. Passed directly to torch.einsum.

  • a (torch.Tensor) – A complex tensor.

  • b (torch.Tensor) – A complex tensor.

  • real_part (bool) – Whether to compute and return the real part of the result.

  • imag_part (bool) – Whether to compute and return the imaginary part of the result.

Returns

The Einstein summation of the input tensors performed according to the given equation. If both real_part and imag_part are true, the result will be a complex tensor, otherwise a real tensor.

Return type

torch.Tensor

qucumber.utils.cplx.elementwise_division(x, y)[source]

Element-wise division of x by y.

Parameters
Return type

torch.Tensor

qucumber.utils.cplx.elementwise_mult(x, y)[source]

Alias for scalar_mult().

qucumber.utils.cplx.imag(x)[source]

Returns the imaginary part of a complex tensor.

Parameters

x (torch.Tensor) – The complex tensor

Returns

The imaginary part of x; will have one less dimension than x.

Return type

torch.Tensor

qucumber.utils.cplx.inner_prod(x, y)[source]

A function that returns the inner product of two complex vectors, x and y (<x|y>).

Parameters
Raises

ValueError – If x and y are not complex vectors with their first dimensions being 2, then the function will not execute.

Returns

The inner product, \langle x\vert y\rangle.

Return type

torch.Tensor

qucumber.utils.cplx.inverse(z)[source]

Returns the multiplicative inverse of z. Acts elementwise.

Parameters

z (torch.Tensor) – The complex tensor.

Returns

1 / z

Return type

torch.Tensor

qucumber.utils.cplx.kronecker_prod(x, y)[source]

Returns the tensor / Kronecker product of 2 complex matrices, x and y.

Parameters
Raises

ValueError – If x and y do not have 3 dimensions or their first dimension is not 2, the function cannot execute.

Returns

The Kronecker product of x and y, x \otimes y.

Return type

torch.Tensor

qucumber.utils.cplx.make_complex(x, y=None)[source]

A function that creates a torch compatible complex tensor.

Note

x and y must have the same shape.

Parameters
  • x (torch.Tensor or numpy.ndarray) – The real part or a complex numpy array. If a numpy array, will ignore y.

  • y (torch.Tensor) – The imaginary part. Can be None, in which case, the resulting complex tensor will have imaginary part equal to zero.

Returns

The tensor [x,y] = x + iy.

Return type

torch.Tensor

qucumber.utils.cplx.matmul(x, y)[source]

A function that computes complex matrix-matrix and matrix-vector products.

Note

If one wishes to do matrix-vector products, the vector must be the second argument (y).

Parameters
Returns

The product between x and y.

Return type

torch.Tensor

qucumber.utils.cplx.norm(x)[source]

Returns the norm of the argument.

Parameters

x (torch.Tensor) – A complex scalar.

Returns

|x|.

Return type

torch.Tensor

qucumber.utils.cplx.norm_sqr(x)[source]

Returns the squared norm of the argument.

Parameters

x (torch.Tensor) – A complex scalar.

Returns

|x|^2.

Return type

torch.Tensor

qucumber.utils.cplx.numpy(x)[source]

Converts a complex torch tensor into a numpy array

Parameters

x (torch.Tensor) – The tensor to convert.

Returns

A complex numpy array containing the data from x.

Return type

numpy.ndarray

qucumber.utils.cplx.outer_prod(x, y)[source]

A function that returns the outer product of two complex vectors, x and y.

Parameters
Raises

ValueError – If x and y are not complex vectors with their first dimensions being 2, then an error will be raised.

Returns

The outer product between x and y, \vert x \rangle\langle y\vert.

Return type

torch.Tensor

qucumber.utils.cplx.real(x)[source]

Returns the real part of a complex tensor.

Parameters

x (torch.Tensor) – The complex tensor

Returns

The real part of x; will have one less dimension than x.

Return type

torch.Tensor

qucumber.utils.cplx.scalar_divide(x, y)[source]

Divides x by y. If x and y have the same shape, then acts elementwise. If y is a complex scalar, then performs a scalar division.

Parameters
Returns

x / y

Return type

torch.Tensor

qucumber.utils.cplx.scalar_mult(x, y, out=None)[source]

A function that computes the product between complex matrices and scalars, complex vectors and scalars or two complex scalars.

Parameters
Returns

The product between x and y. Either overwrites out, or returns a new tensor.

Return type

torch.Tensor

qucumber.utils.cplx.sigmoid(x, y)[source]

Returns the sigmoid function of a complex number. Acts elementwise.

Parameters
  • x (torch.Tensor) – The real part of the complex number

  • y (torch.Tensor) – The imaginary part of the complex number

Returns

The complex sigmoid of x + iy

Return type

torch.Tensor