You're reading the documentation of the v0.6. For the latest released version, please have a look at v0.11.

Matrix

>>> import perceval as pcvl
>>> M = pcvl.Matrix("1 2 3\n4 5 6\n7 8 9")
>>> print(M.pdisplay())
⎡1  2  3⎤
⎢4  5  6⎥
⎣7  8  9⎦
>>> M.is_unitary()
False
class perceval.utils.matrix.Matrix(source, use_symbolic=None)

This parent class is the gateway MatrixN or MatrixS - based on use_symbolic, and checking if input contains any parameter, it will create an instance of one or the other class.

  • MatrixS is a subclass of sympy.Matrix with slight API augmentations for compatibility with numpy

  • MatrixN is a subclass of numpy.ndarray

Both classes have additional utility functions - while Matrix class is also presenting additional static utility functions

static __new__(cls, source, use_symbolic=None)

Constructor for Matrix class

Parameters:
  • source – can be a string, a file, a list, a ndarray, another Matrix, or a integer

  • use_symbolic – True to force use of symbolic, False to force use of numeric, None to select based on source

static eye(n, use_symbolic=False)

Returns an identity matrix

Parameters:
  • n (int) – size of the matrix

  • use_symbolic (bool) – defines if matrix will be symbolic or numeric

Return type:

Matrix

Returns:

an identity matrix

static get_unitary_extension(M)

Embed the input matrix M into an unitary matrix U

U = | M/σ * |
* * |
Parameters:

M (ndarray) – np.ndarray describing a row x col complex matrix

Return type:

MatrixN

Returns:

np.ndarray describing a (row + col) x (row + col) complex unitary matrix

abstract is_symbolic()

check if matrix is symbolic or numeric

Return type:

bool

abstract is_unitary()

check if matrix is unitary

Return type:

bool

static parametrized_unitary(n, parameters)

static method generating a parametrized unitary matrix

Parameters:
  • n (int) – size of the Matrix

  • parameters (Union[ndarray, list]) – \(2n^2\) parameters to use as generator

Return type:

MatrixN

Returns:

a numeric Matrix

static random_unitary(n, parameters=None)

static method generating a random unitary matrix

Parameters:
  • n (int) – size of the Matrix

  • parameters (Union[ndarray, list, None]) – \(2n^2\) random parameters to use a generator

Return type:

MatrixN

Returns:

a numeric Matrix

simp()

Simplify the matrix - only implemented for symbolic matrix

static zeros(shape, use_symbolic=False)

Generate an empty matrix

Parameters:
  • shape (Tuple[int, int]) – 2D shape of the matrix

  • use_symbolic (bool) – defines if matrix will be symbolic or numeric

Return type:

Matrix

Returns:

an empty matrix

Parameter

class perceval.utils.parameter.Parameter(name, value=None, min_v=None, max_v=None, periodic=True, is_expression=False)

A Parameter is a used as a variable in a circuit definition

Parameters are a simple way to introduce named variables in a circuit. They take floating number values. When non defined they are associated to sympy symbols and will be used to perform symbolic calculations.

Parameters:
  • name (str) – name of the parameter

  • value (Optional[float]) – optional value, when the value is provided at initialization, the parameter is considered as fixed

  • min_v (Optional[float]) – minimal value that the parameter can take, is used in circuit optimization

  • max_v (Optional[float]) – maximal value that the parameter can take, is used in circuit optimization

  • is_expression (bool) – for symbolic parameter, the value is an expression to evaluate with context values

property bounds: Tuple[float, float]

Minimal and maximal values for the parameter

property defined: bool

Return True if the parameter has a value (fixed or non fixed)

evalf(subs=None)

Convert the parameter to float, will fail if the parameter has no defined value

fix_value(v)

Fix the value of a non-fixed parameter

Parameters:

v – the value

property fixed: bool

Return True if the parameter is fixed

property is_periodic: bool

Return True if the parameter is defined as a period parameter

property is_variable: bool

“Returns True for a non-fixed parameter

property max: float

The maximal value of the parameter

property min

The minimal value of the parameter

property pid

Unique identifier for the parameter

reset()

Reset the value of a non-fixed parameter

set_periodic(periodic)

set periodic flag

set_value(v, force=False)

Define the value of a non-fixed parameter

Parameters:
  • v (float) – the value

  • force (bool) – enable to set a fixed parameter

Raise:

RuntimeError if the parameter is fixed

property spv: Expr

The current value of the parameter defined as a sympy expression