pyCTQW.MPI.io

Caution

The functions available in this submodule have been designed to work with classes in the pyCTQW.MPI module; that is, they are being called implicitly by the above classes, invisible to the user.

It is not recommended that they be used by themselves, however it is possible to do so.

Summary

adjToH(adj[, d, amp]) Creates a 1 particle PETSc-type Hamiltonian matrix from a PETSc adjacency matrix.
arrayToMat(matArray) Converts a (global) 2D array to a PETSc matrix over petsc4py.PETSc.COMM_WORLD.
arrayToVec(vecArray) Converts a (global) array to a PETSc vector over petsc4py.PETSc.COMM_WORLD.
exportMat(mat, filename, filetype[, mattype]) Export a PETSc matrix to a file.
exportVec(vec, filename, filetype) Export a PETSc vector to a file.
exportVecToMat(vec, filename, filetype) Export a \(N^2\) element PETSc vector as a \(N\times N\) matrix.
loadMat(filename, filetype[, delimiter]) Import a PETSc matrix from a file.
loadMatToVec(filename, filetype) Load a \(N\times N\) matrix as a \(N^2\) element PETSc vector.
loadVec(filename, filetype) Import a PETSc vector from a file.
matToSparse(mat) Converts a PETSc matrix to a (global) sparse matrix.
vecToArray(obj) Converts a PETSc vector to a numpy array, available on all MPI nodes.
vecToArray0(obj) Converts a PETSc vector to a numpy array available on MPI node 0.

Functions

Input and output functions.

adjToH(adj, d=[0], amp=[0.0])[source]

Creates a 1 particle PETSc-type Hamiltonian matrix from a PETSc adjacency matrix.

Parameters:
  • adj (petsc4py.PETSc.Mat) – input PETSc-type adjacency matrix.
  • d (array of ints) – an array containing integers indicating the nodes where diagonal defects are to be placed (e.g. d=[0,1,4]).
  • amp (array of floats) – an array containing floats indicating the diagonal defect amplitudes corresponding to each element in d (e.g. amp=[0.5,-1,4.2]).
Returns:

: 1 particle Hamiltonian matrix

Return type:

petsc4py.PETSc.Mat()

Warning

  • The size of a and d must be identical

    >>> amp = [0.5,-1.,4.2]
    >>> len(d) == len(amp)
    True
    
  • Elements of d can range from \([0,N-1]\) where the adjacency matrix is \(N\times N\).

arrayToMat(matArray)[source]

Converts a (global) 2D array to a PETSc matrix over petsc4py.PETSc.COMM_WORLD.

Parameters:matArray (array or numpy.array) – input square array.
Return type:petsc4py.PETSc.Mat()

Important

Requires SciPy.

arrayToVec(vecArray)[source]

Converts a (global) array to a PETSc vector over petsc4py.PETSc.COMM_WORLD.

Parameters:vecArray (array or numpy.array) – input vector.
Returns:petsc4py.PETSc.Vec() :
exportMat(mat, filename, filetype, mattype=None)[source]

Export a PETSc matrix to a file.

Parameters:
  • mat (petsc4py.PETSc.Mat) – input matrix.
  • filename (str) – path to desired output file.
  • filetype (str) –

    the filetype of the exported vector.

    • 'txt' - a 2D matrix array in text format.
    • 'bin' - a PETSc binary matrix.
  • mattype (str) – (None,``’adj’) - if set to ``adj, only integers 0 and 1 are written. Note that this only applied in txt mode.
exportVec(vec, filename, filetype)[source]

Export a PETSc vector to a file.

Parameters:
  • vec (petsc4py.PETSc.Vec) – input vector.
  • filename (str) – path to desired output file.
  • filetype (str) –

    the filetype of the exported vector.

    • 'txt' - a column vector in text format.
    • 'bin' - a PETSc binary vector.
exportVecToMat(vec, filename, filetype)[source]

Export a \(N^2\) element PETSc vector as a \(N\times N\) matrix.

This is useful when wanting to view the full statespace of a 2 particle quantum walk.

Parameters:
  • vec (petsc4py.PETSc.Vec) – input \(N^2\) element vector.
  • filename (str) – path to desired output file.
  • filetype (str) –

    the filetype of the exported vector.

    • 'txt' - an \(N\times N\) 2D matrix array in text format.
    • 'bin' - an \(N\times N\) PETSc binary matrix.
loadMat(filename, filetype, delimiter=None)[source]

Import a PETSc matrix from a file.

Parameters:
  • filename (str) – path to input file.
  • filetype (str) –

    the filetype.

    • 'txt' - a 2D matrix array in text format.
    • 'bin' - a PETSc matrix vector.
  • delimiter (str) – this is passed to numpy.genfromtxt in the case of strange delimiters in an imported txt file.
loadMatToVec(filename, filetype)[source]

Load a \(N\times N\) matrix as a \(N^2\) element PETSc vector.

This is useful when wanting to import the full statespace of a 2 particle quantum walk to use for propagation.

Parameters:
  • filename (str) – path to the input file.
  • filetype (str) –

    the filetype

    • 'txt' - an \(N\times N\) 2D matrix array in text format.
    • 'bin' - Not yet implemented! Please use a txt format for this type of import.
loadVec(filename, filetype)[source]

Import a PETSc vector from a file.

Parameters:
  • filename (str) – path to input file.
  • filetype (str) –

    the filetype.

    • 'txt' - a column vector in text format.
    • 'bin' - a PETSc binary vector.
matToSparse(mat)[source]

Converts a PETSc matrix to a (global) sparse matrix.

Parameters:mat (petsc4py.PETSc.Mat) – input PETSc matrix.
Return type:scipy.sparse.csr_matrix

Important

Requires SciPy.

vecToArray(obj)[source]

Converts a PETSc vector to a numpy array, available on all MPI nodes.

Parameters:obj (petsc4py.PETSc.Vec) – input vector.
Returns:numpy.array :
vecToArray0(obj)[source]

Converts a PETSc vector to a numpy array available on MPI node 0.

Parameters:obj (petsc4py.PETSc.Vec) – input vector.
Returns:numpy.array :