randomsymplectic.DnaryArray

Bases: ndarray

A subclass of numpy.ndarray for arrays over the integers mod . Users can specify using the classmethod DnaryArray.set_d, which will return a subclass of DnaryArray specialized to the given modulus. Instances of that subclass behave as normal numpy arrays, but all outputs will be computed using arithmetic in .

Instances can be created from any array-like data acceptable by np.array(...).

Within a given d-nary class, symplectic algebra on matrices and vectors over , the modulo- integers, can be performed:

Note

For a given , the symplectic group is the group of matrices such that where and is the identity matrix.

The matrix may be defined differently in other contexts. The definition used here follows arXiv:quant-ph/0408190. See here for other definitions.

Similarly, the symplectic inner product between length- vectors is defined as

If , we say is a symplectic pair.

An equivalent definition for a symplectic matrix is the requirement that, for each , the matrix's th and th columns form a symplectic pair.

Examples:

>>> D3 = DnaryArray.set_d(3)
>>> D3([1, 2, 3, 4])
DnaryArray(d=3)([1, 2, 0, 1])

>>> D3.eye(4)
D3([[1, 0, 0, 0],
    [0, 1, 0, 0],
    [0, 0, 1, 0],
    [0, 0, 0, 1]])

>>> D3.eye(4)-2
D3([[2, 1, 1, 1],
    [1, 2, 1, 1],
    [1, 1, 2, 1],
    [1, 1, 1, 2]])

>>> (D3.eye(4)-2).inverse()
D3([[2, 1, 1, 1],
    [1, 2, 1, 1],
    [1, 1, 2, 1],
    [1, 1, 1, 2]])

>>> import numpy as np
>>> np.linalg.matrix_power(D3.eye(4)-2, 2)
D3([[1, 0, 0, 0],
    [0, 1, 0, 0],
    [0, 0, 1, 0],
    [0, 0, 0, 1]])

>>> MyD6Array = DnaryArray.set_d(6)
>>> (2 * MyD6Array.eye(2)) @ [2,3]
MyD6Array([4, 0])

Lambda property

Returns:
  • Self

    The defining symplectic array matching the invoking object's dimension ().

    See .LambdaN.

LambdaN(n) classmethod

Returns the defining symplectic array for any given using the class modulus .

See DnaryArray.

Parameters:
  • n (int) –

    is a array.

Returns:
  • Self

    The -nary (2n, 2n) array that defines the symplectic group and symplectic inner product.

Transvection

A class representing transvections, a class of linear transformations on vectors. Transvections are defined by a vector and a constant , and act as . All arithmetic is mod d. Transvections applied to matrices apply the above transformation column-wise.

Parameters:
  • u (DnaryArray) –

    The vector to transvect by.

  • c (int) –

    The constant of the transvection.

__call__(vector_or_matrix)

Apply the transvection to a vector or matrix of the same type as the transvection's vector.

Parameters:
  • vector_or_matrix (DnaryArray) –

    A vector or matrix of the same type as the Transvection's vector.

Returns:
  • DnaryArray( DnaryArray ) –

    The transvection applied to the vector or matrix.

find_transvection(u, v) classmethod

Returns a tuple of transvections such that .

Parameters:
  • u (DnaryArray) –

    Vector you are transvecting from.

  • v (DnaryArray) –

    Vector you are transvecting to.

Returns:

transvect_matrix_columns(A)

Apply the transvection to a matrix of the same type as the transvection's vector.

Parameters:
  • A (DnaryArray) –

    A matrix of the same type as the Transvection's vector.

Returns:
  • DnaryArray( DnaryArray ) –

    The transvection applied to the matrix.

transvect_vector(v)

Apply the transvection to a vector of the same type as the transvection's vector.

Parameters:
  • v (DnaryArray) –

    A vector of the same type as the Transvection's vector.

Returns:
  • DnaryArray( DnaryArray ) –

    The transvection applied to the vector.

basis_vector(i, n) classmethod

Generates the th standard basis vector of length . This is the vector of all zeros except the th component equal to .

Parameters:
  • i (int) –

    The component of the basis vector.

  • n (int) –

    The length of the vector

Returns:
  • Self

    The th standard basis vector.

Examples:

>>> class D3(DnaryArray): d=3
>>> D3.basis_vector(1, 5)
D3([0, 1, 0, 0, 0])

det()

Alias for .determinant.

Returns:
  • int

    The determinant of the array modulo .

determinant()

Returns:
  • int

    The determinant of the array modulo d.

dnary_inverse(n) classmethod

Alias for dnary_inverse(n, cls.d). Calculates the multiplicative inverse of n in if it exists.

If is prime and , the integer is guaranteed to exist. For general , exists if .

Parameters:
  • n (int) –

    The integer to invert.

Returns:
  • int

    The integer such that , or None if such an integer does not exist.

dnary_to_int(digits) classmethod

Alias for int_to_dnary(n, cls.d). Given a list of the -nary digits of a number, return that number in base 10.

See also [randomsymplectic.DnaryArray.int_to_dnary].

Parameters:
  • digits (list[int]) –

    A list of the -nary digits of a number.

Returns:
  • int

    The number in base 10.

embed_symplectic()

Embeds a array into a array via the symplecticity-preserving block embedding which inserts identity rows/columns at indices and (Python indexing) and fills the gaps with zeros.

Returns:
  • Self

    The embedded array.

eye(n) classmethod

Returns:
  • Self

    The (n, n) identity matrix with integer type.

from_index(index, n) classmethod

Generate an element of the symplectic group from that element's index.

Parameters:
  • index (int) –

    An integer from 0 (inclusive) to the size of the symplectic group (exclusive). See .symplectic_group_size.

  • n (int) –

    Half the size of the symplectic matrix to be generated, or the number of qubits in the equivalent Clifford group.

Returns:
  • Self( Self ) –

    The symplectic matrix corresponding to the input index.

inner_product(v1, v2) classmethod

Computes the symplectic inner product of the given vectors.

The symplectic inner product between length- vectors is defined as

Parameters:
  • v1 (Self | ndarray) –

    The first vector. Will be coerced into the type of the calling class.

  • v2 (Self | ndarray) –

    The second vector. Will be coerced into the type of the calling class.

Raises:
  • ValueError

    Input arrays are not one-dimensional.

  • ValueError

    Input arrays are not the same size.

Returns:
  • int

    The symplectic inner product of v1 and v2.

Tip

If you've already created a vector v, you can call v.inner_product_with(w) method to compute .

Note

The modulus is defined by the calling class. Thus, v1 and v2 can be any array-like data, but they will be coerced into the type of the calling class (by coercing components to integers and modding by ).

inner_product_with(other)

Compute the symplectic inner product of the calling vector with the other vector.

See the .inner_product classmethod.

Parameters:
  • other (Self) –

    The other vector.

Returns:
  • int

    The symplectic inner product of the calling vector with the other vector.

Tip

Python's pipe operator | is overloaded with this method, so you can call v1.inner_product_with(v2) as v1 | v2.

Examples:

>>> D3 = DnaryArray.set_d(3)
>>> v1 = D3([1, 0, 0, 0])
>>> v2 = D3([0, 0, 1, 0])
>>> v1 | v2
2

int_to_dnary(n, result_list_size=None) classmethod

Alias for int_to_dnary(n, cls.d).

Decomposes a base 10 integer into a list of its -nary digits.

See also [randomsymplectic.DnaryArray.dnary_to_int].

Parameters:
  • n (int) –

    The base 10 integer to be factored into its -nary digits.

  • result_list_size (int | None, default: None ) –

    The length of the list to return; if None, will use the minimum number of digits to result the input integer in -nary. Defaults to None.

Raises:
  • ValueError

    Invalid input parameters.

Returns:
  • list[int]

    A list of the -nary digits of n The list has elements such that

inv(**kwargs)

Alias for mod_matrix_inv.

Returns:
  • Self | None

    The array's inverse, or None if the array is not invertible.

inverse(**kwargs)

Alias for mod_matrix_inv.

Returns:
  • Self | None

    The array's inverse, or None if the array is not invertible.

is_invertible()

Determines if an array is invertible by checking if its determinant is invertible in .

Returns:
  • bool

    Whether the array is invertible.

is_matrix property

Returns:
  • bool

    Whether this is a 2D array.

is_nonzero()

Returns:
  • bool

    Whether the array has any nonzero values in it.

is_symplectic_matrix()

Returns:
  • bool

    Whether the calling array is a symplectic matrix.

is_symplectic_pair(v1, v2) classmethod

Determine whether a pair of vectors form a symplectic pair.

Two vectors form a symplectic pair if

See also .is_symplectic_pair_with.

Parameters:
  • v1 (Self) –

    The first vector.

  • v2 (Self) –

    The second vector.

Returns:
  • bool

    Whether the vectors form a symplectic pair.

is_symplectic_pair_with(other)

If the calling object is a vector (i.e. 1D array), determine whether it forms a symplectic pair with another vector.

See the .is_symplectic_pair classmethod.

Raises an exception if the calling object is not a vector.

Parameters:
  • other (Self) –

    The other vector.

Raises:
  • TypeError

    Both the calling object and the input must be 1d arrays.

Returns:
  • bool

    Whether the two vectors form a symplectic pair.

is_vector property

Returns:
  • bool

    Whether this is a 1D array.

is_zero()

Returns:
  • bool

    Whether the array is all zero.

mod_matrix_inv(validate=True, suppress_warnings=True)

Calculates the matrix inverse in -arithmetic using where is the matrix adjugate of and is the determinant.

Parameters:
  • validate (bool, default: True ) –

    Whether to validate the result through multiplication. Defaults to True.

  • suppress_warnings (bool, default: True ) –

    If False, warnings will be raise if the array is not invertible. Defaults to True.

Raises:
  • RuntimeError

    If the inversion fails for an invertible array. Theoretically this should never happen.

Returns:
  • Self | None

    The array's inverse, or None if the array is not invertible.

n property

Half of the array size. Will raise an exception for odd-sized arrays.

Defines the defining symplectic array . See the .Lambda property and the .LambdaN method.

Raises:
  • ValueError

    Not accessible for odd-sized arrays.

Returns:
  • int

    Half of the array size.

nn property

Returns:
  • int

    The symplectic array size .

random_array(shape, allow_zero=True) classmethod

Returns an array of the given shape with uniform random integers mod , including the all-zero array unless specified otherwise.

Parameters:
  • shape (tuple[int]) –

    The shape of the array.

  • allow_zero (bool, default: True ) –

    Whether to allow the zero matrix. Defaults to True.

Returns:
  • Self

    A uniform random -nary array.

random_symplectic(n) classmethod

Generate a uniformly random element of the symplectic group.

Parameters:
  • n (int) –

    Half the size of the symplectic matrix to be generated, or the number of qubits in the equivalent Clifford group.

Returns:
  • Self( Self ) –

    A uniform random -nary symplectic matrix.

set_d(d) classmethod

Classmethod for creating a child class with a specific d.

Parameters:
  • d (int) –

    The modulus of the class to create.

Returns:

symplectic_group_size(n) classmethod

Returns the size of the symplectic group for a given . That number is

Parameters:
  • n (int) –

    Half the size of the symplectic matrix, or the number of qubits in the equivalent Clifford group.

Returns:
  • int

    The number of different symplectic matrices over .

zeros(shape) classmethod

Returns an all-zero array of the given shape.

Parameters:
  • shape (int | tuple[int]) –

    The shape of the desired array.

Returns:
  • Self

    The zero array of the given shape.