Quandary
Loading...
Searching...
No Matches
Classes | Functions
util.hpp File Reference
#include <petscmat.h>
#include <cctype>
#include <cstring>
#include <map>
#include <optional>
#include <string>
#include <fstream>
#include <iostream>
#include <vector>
#include "version.hpp"
Include dependency graph for util.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  ParsedArgs
 Structure for parsed command-line arguments. More...
 

Functions

void printHelp ()
 
ParsedArgs parseArguments (int argc, char **argv)
 
double sigmoid (double width, double x)
 Sigmoid function for smooth transitions.
 
double sigmoid_diff (double width, double x)
 Derivative of sigmoid function.
 
double getRampFactor (const double time, const double tstart, const double tstop, const double tramp)
 Computes ramping factor for control pulse shaping.
 
double getRampFactor_diff (const double time, const double tstart, const double tstop, const double tramp)
 Derivative of ramping factor with respect to stop time.
 
PetscInt getVecID (const PetscInt row, const PetscInt col, const PetscInt dim)
 Returns vectorized index for matrix element (row,col).
 
PetscInt mapEssToFull (const PetscInt i, const std::vector< size_t > &nlevels, const std::vector< size_t > &nessential)
 Maps index from essential level system to full-dimension system.
 
PetscInt mapFullToEss (const PetscInt i, const std::vector< size_t > &nlevels, const std::vector< size_t > &nessential)
 Maps index from full dimension to essential dimension system.
 
int isEssential (const int i, const std::vector< size_t > &nlevels, const std::vector< size_t > &nessential)
 Tests if density matrix index corresponds to an essential level.
 
int isGuardLevel (const int i, const std::vector< size_t > &nlevels, const std::vector< size_t > &nessential)
 Tests if density matrix index corresponds to a guard level.
 
PetscErrorCode Ikron (const Mat A, const int dimI, const double alpha, Mat *Out, InsertMode insert_mode)
 Computes Kronecker product \(Id \otimes A\).
 
PetscErrorCode kronI (const Mat A, const int dimI, const double alpha, Mat *Out, InsertMode insert_mode)
 Computes Kronecker product \(A \otimes Id\).
 
PetscErrorCode AkronB (const Mat A, const Mat B, const double alpha, Mat *Out, InsertMode insert_mode)
 Computes general Kronecker product \(A \otimes B\).
 
PetscErrorCode MatIsAntiSymmetric (Mat A, PetscReal tol, PetscBool *flag)
 Tests if matrix A is anti-symmetric (A^T = -A).
 
PetscErrorCode StateIsHermitian (Vec x, PetscReal tol, PetscBool *flag)
 Tests if vectorized state represents a Hermitian matrix.
 
PetscErrorCode StateHasTrace1 (Vec x, PetscReal tol, PetscBool *flag)
 Tests if vectorized state vector x=[u,v] represents matrix with trace 1.
 
PetscErrorCode SanityTests (Vec x, PetscReal time)
 Performs all sanity tests on state vector.
 
int read_vector (const char *filename, double *var, int dim, bool quietmode=false, int skiplines=0, const std::string testheader="")
 Reads data vector from file.
 
int getEigvals (const Mat A, const int neigvals, std::vector< double > &eigvals, std::vector< Vec > &eigvecs)
 Computes eigenvalues and eigenvectors of matrix A.
 
bool isUnitary (const Mat A, const Mat B)
 Tests if complex matrix A+iB is unitary.
 
template<typename Tval >
void copyLast (std::vector< Tval > &fillme, int tosize)
 Extends vector by repeating the last element.
 
std::string toLower (std::string str)
 Returns a lowercase version of the input string.
 
bool hasSuffix (const std::string &str, const std::string &suffix)
 Checks if string ends with specified suffix.
 
template<typename T >
std::optional< T > parseEnum (const std::string &str, const std::map< std::string, T > &enum_map)
 Generic enum parsing utility with case-insensitive lookup.
 
template<typename EnumType >
std::string enumToString (EnumType value, const std::map< std::string, EnumType > &type_map)
 Converts enum value back to string.
 
template<typename T >
parseEnum (const std::optional< std::string > &opt_str, const std::map< std::string, T > &enum_map, const T &default_value)
 Generic enum parsing utility with case-insensitive lookup and default fallback.
 

Function Documentation

◆ AkronB()

PetscErrorCode AkronB ( const Mat  A,
const Mat  B,
const double  alpha,
Mat *  Out,
InsertMode  insert_mode 
)

Computes general Kronecker product \(A \otimes B\).

Computes the Kronecker product of two arbitrary matrices A and B. Works in PETSc serial mode only. Output matrix must be pre-allocated and should be assembled afterwards.

Parameters
AFirst input matrix
BSecond input matrix
alphaScaling factor
OutOutput matrix \((A \otimes B)\)
insert_modeINSERT_VALUES or ADD_VALUES
Returns
PetscErrorCode Error code

◆ copyLast()

template<typename Tval >
void copyLast ( std::vector< Tval > &  fillme,
int  tosize 
)

Extends vector by repeating the last element.

Template function that fills a vector to the specified size by repeating the last element.

Parameters
fillmeVector to extend
tosizeTarget size for the vector

◆ enumToString()

template<typename EnumType >
std::string enumToString ( EnumType  value,
const std::map< std::string, EnumType > &  type_map 
)

Converts enum value back to string.

Parameters
valueEnum value to convert
type_mapMap from string to enum values
Returns
std::string String representation of enum value

◆ getEigvals()

int getEigvals ( const Mat  A,
const int  neigvals,
std::vector< double > &  eigvals,
std::vector< Vec > &  eigvecs 
)

Computes eigenvalues and eigenvectors of matrix A.

Requires compilation with SLEPc for eigenvalue computations.

Parameters
AInput matrix
neigvalsNumber of eigenvalues to compute
eigvalsVector to store eigenvalues
eigvecsVector to store eigenvectors
Returns
int Error code

◆ getRampFactor()

double getRampFactor ( const double  time,
const double  tstart,
const double  tstop,
const double  tramp 
)

Computes ramping factor for control pulse shaping.

Computes smooth ramping factor for interval [tstart, tstop] using sigmoid transitions with specified width tramp.

Parameters
timeCurrent time
tstartStart time of interval
tstopStop time of interval
trampRamping transition width
Returns
double Ramping factor between 0 and 1

◆ getRampFactor_diff()

double getRampFactor_diff ( const double  time,
const double  tstart,
const double  tstop,
const double  tramp 
)

Derivative of ramping factor with respect to stop time.

Parameters
timeCurrent time
tstartStart time of interval
tstopStop time of interval
trampRamping transition width
Returns
double Derivative with respect to tstop

◆ getVecID()

PetscInt getVecID ( const PetscInt  row,
const PetscInt  col,
const PetscInt  dim 
)

Returns vectorized index for matrix element (row,col).

Parameters
rowMatrix row index
colMatrix column index
dimMatrix dimension
Returns
int Vectorized index for element (row,col)

◆ hasSuffix()

bool hasSuffix ( const std::string &  str,
const std::string &  suffix 
)

Checks if string ends with specified suffix.

Parameters
strInput string to check.
suffixSuffix to look for.
Returns
bool True if string ends with suffix, false otherwise.

◆ Ikron()

PetscErrorCode Ikron ( const Mat  A,
const int  dimI,
const double  alpha,
Mat *  Out,
InsertMode  insert_mode 
)

Computes Kronecker product \(Id \otimes A\).

Computes the Kronecker product of an identity matrix with matrix A. Output matrix must be pre-allocated with sufficient non-zeros A * dimI.

Parameters
[in]AInput matrix
[in]dimIDimension of identity matrix
[in]alphaScaling factor
[out]OutOutput matrix \((Id \otimes A)\)
[in]insert_modeINSERT_VALUES or ADD_VALUES
Returns
PetscErrorCode Error code

◆ isEssential()

int isEssential ( const int  i,
const std::vector< size_t > &  nlevels,
const std::vector< size_t > &  nessential 
)

Tests if density matrix index corresponds to an essential level.

Parameters
iRow/column index of density matrix
nlevelsNumber of levels per oscillator
nessentialNumber of essential levels per oscillator
Returns
int Non-zero if index corresponds to essential level

◆ isGuardLevel()

int isGuardLevel ( const int  i,
const std::vector< size_t > &  nlevels,
const std::vector< size_t > &  nessential 
)

Tests if density matrix index corresponds to a guard level.

A guard level is the highest energy level of an oscillator, used for leakage detection and prevention.

Parameters
iRow/column index of density matrix
nlevelsNumber of levels per oscillator
nessentialNumber of essential levels per oscillator
Returns
int Non-zero if index corresponds to guard level

◆ isUnitary()

bool isUnitary ( const Mat  A,
const Mat  B 
)

Tests if complex matrix A+iB is unitary.

Tests whether (A+iB)(A+iB)^dagger = I for real matrices A and B.

Parameters
AReal part of complex matrix
BImaginary part of complex matrix
Returns
bool True if matrix is unitary

◆ kronI()

PetscErrorCode kronI ( const Mat  A,
const int  dimI,
const double  alpha,
Mat *  Out,
InsertMode  insert_mode 
)

Computes Kronecker product \(A \otimes Id\).

Computes the Kronecker product of matrix A with an identity matrix. Output matrix must be pre-allocated with sufficient non-zeros A * dimI.

Parameters
[in]AInput matrix
[in]dimIDimension of identity matrix
[in]alphaScaling factor
[out]OutOutput matrix \((A \otimes Id)\)
[in]insert_modeINSERT_VALUES or ADD_VALUES
Returns
PetscErrorCode Error code

◆ mapEssToFull()

PetscInt mapEssToFull ( const PetscInt  i,
const std::vector< size_t > &  nlevels,
const std::vector< size_t > &  nessential 
)

Maps index from essential level system to full-dimension system.

Parameters
iIndex in essential level system
nlevelsNumber of levels per oscillator
nessentialNumber of essential levels per oscillator
Returns
int Corresponding index in full-dimension system

◆ mapFullToEss()

PetscInt mapFullToEss ( const PetscInt  i,
const std::vector< size_t > &  nlevels,
const std::vector< size_t > &  nessential 
)

Maps index from full dimension to essential dimension system.

Parameters
iIndex in full dimension system
nlevelsNumber of levels per oscillator
nessentialNumber of essential levels per oscillator
Returns
int Corresponding index in essential dimension system

◆ MatIsAntiSymmetric()

PetscErrorCode MatIsAntiSymmetric ( Mat  A,
PetscReal  tol,
PetscBool *  flag 
)

Tests if matrix A is anti-symmetric (A^T = -A).

Parameters
AInput matrix to test
tolTolerance for comparison
flagOutput flag indicating anti-symmetry
Returns
PetscErrorCode Error code

◆ parseArguments()

ParsedArgs parseArguments ( int  argc,
char **  argv 
)

Parses command-line arguments for the Quandary program.

Parameters
argcNumber of command-line arguments.
argvArray of command-line argument strings.
Returns
ParsedArgs structure containing configuration filename, quiet mode flag, and PETSc options.

◆ parseEnum() [1/2]

template<typename T >
T parseEnum ( const std::optional< std::string > &  opt_str,
const std::map< std::string, T > &  enum_map,
const T &  default_value 
)

Generic enum parsing utility with case-insensitive lookup and default fallback.

Parameters
opt_strOptional string value to parse into enum
enum_mapMap from string to enum values
default_valueDefault enum value to return if string is missing or invalid
Returns
T Parsed enum value or default_value if not found

◆ parseEnum() [2/2]

template<typename T >
std::optional< T > parseEnum ( const std::string &  str,
const std::map< std::string, T > &  enum_map 
)

Generic enum parsing utility with case-insensitive lookup.

Parameters
strString value to parse into enum
enum_mapMap from string to enum values
Returns
std::optional<T> Parsed enum value or nullopt if not found

◆ printHelp()

void printHelp ( )

Prints help message for command-line usage.

◆ read_vector()

int read_vector ( const char *  filename,
double *  var,
int  dim,
bool  quietmode = false,
int  skiplines = 0,
const std::string  testheader = "" 
)

Reads data vector from file.

Parameters
filenameName of file to read
varArray to store data
dimDimension of data to read
quietmodeFlag for reduced output
skiplinesNumber of header lines to skip
testheaderExpected header string for validation
Returns
int Error code

◆ SanityTests()

PetscErrorCode SanityTests ( Vec  x,
PetscReal  time 
)

Performs all sanity tests on state vector.

Parameters
xState vector to test
timeCurrent time for diagnostic output
Returns
PetscErrorCode Error code

◆ sigmoid()

double sigmoid ( double  width,
double  x 
)

Sigmoid function for smooth transitions.

Parameters
widthTransition width parameter
xInput value
Returns
double Sigmoid function value

◆ sigmoid_diff()

double sigmoid_diff ( double  width,
double  x 
)

Derivative of sigmoid function.

Parameters
widthTransition width parameter
xInput value
Returns
double Derivative of sigmoid function

◆ StateHasTrace1()

PetscErrorCode StateHasTrace1 ( Vec  x,
PetscReal  tol,
PetscBool *  flag 
)

Tests if vectorized state vector x=[u,v] represents matrix with trace 1.

Parameters
xVectorized state vector
tolTolerance for comparison
flagOutput flag indicating unit trace
Returns
PetscErrorCode Error code

◆ StateIsHermitian()

PetscErrorCode StateIsHermitian ( Vec  x,
PetscReal  tol,
PetscBool *  flag 
)

Tests if vectorized state represents a Hermitian matrix.

For vectorized state x=[u,v] to represent a Hermitian matrix, u must be symmetric and v must be anti-symmetric.

Parameters
xVectorized state vector
tolTolerance for comparison
flagOutput flag indicating Hermiticity
Returns
PetscErrorCode Error code

◆ toLower()

std::string toLower ( std::string  str)

Returns a lowercase version of the input string.

Parameters
strString to convert to lowercase.
Returns
std::string Lowercase string