pygsti.modelmembers.states.state

The State class and supporting functionality.

Module Contents

Classes

State

TODO: update docstring

class pygsti.modelmembers.states.state.State(rep, evotype)

Bases: pygsti.modelmembers.modelmember.ModelMember

TODO: update docstring A parameterized state preparation OR POVM effect vector (operator).

This class is the common base class for all specific parameterizations of a state vector.

Parameters

repobject

A representation object containing the core data for this spam vector.

evotypeEvotype

The evolution type.

Attributes

sizeint

The number of independent elements in this state vector (when viewed as a dense array).

Initialize a new state Vector

property dim

Return the dimension of this state (when viewed as a dense array)

Returns

int

property hilbert_schmidt_size

Return the number of independent elements in this state as a dense Hilbert-Schmidt super-ket.

Returns

int

property num_params

Get the number of independent parameters which specify this state vector.

Returns
int

the number of independent parameters.

set_dense(vec)

Set the dense-vector value of this state vector.

Attempts to modify this state vector’s parameters so that the raw state vector becomes vec. Will raise ValueError if this operation is not possible.

Parameters
vecarray_like or State

A numpy array representing a state vector, or a State object.

Returns

None

set_time(t)

Sets the current time for a time-dependent operator.

For time-independent operators (the default), this function does absolutely nothing.

Parameters
tfloat

The current time.

Returns

None

abstract to_dense(on_space='minimal', scratch=None)

Return this state vector as a (dense) numpy array.

The memory in scratch maybe used when it is not-None.

Parameters
on_space{‘minimal’, ‘Hilbert’, ‘HilbertSchmidt’}

The space that the returned dense operation acts upon. For unitary matrices and bra/ket vectors, use ‘Hilbert’. For superoperator matrices and super-bra/super-ket vectors use ‘HilbertSchmidt’. ‘minimal’ means that ‘Hilbert’ is used if possible given this operator’s evolution type, and otherwise ‘HilbertSchmidt’ is used.

scratchnumpy.ndarray, optional

scratch space available for use.

Returns

numpy.ndarray

abstract taylor_order_terms(order, max_polynomial_vars=100, return_coeff_polys=False)

Get the order-th order Taylor-expansion terms of this state vector.

This function either constructs or returns a cached list of the terms at the given order. Each term is “rank-1”, meaning that it is a state preparation followed by or POVM effect preceded by actions on a density matrix rho of the form:

rho -> A rho B

The coefficients of these terms are typically polynomials of the State’s parameters, where the polynomial’s variable indices index the global parameters of the State’s parent (usually a Model) , not the State’s local parameter array (i.e. that returned from to_vector).

Parameters
orderint

The order of terms to get.

max_polynomial_varsint, optional

maximum number of variables the created polynomials can have.

return_coeff_polysbool

Whether a parallel list of locally-indexed (using variable indices corresponding to this object’s parameters rather than its parent’s) polynomial coefficients should be returned as well.

Returns
termslist

A list of RankOneTerm objects.

coefficientslist

Only present when return_coeff_polys == True. A list of compact polynomial objects, meaning that each element is a (vtape,ctape) 2-tuple formed by concatenating together the output of Polynomial.compact().

highmagnitude_terms(min_term_mag, force_firstorder=True, max_taylor_order=3, max_polynomial_vars=100)

Get terms with magnitude above min_term_mag.

Get the terms (from a Taylor expansion of this state vector) that have magnitude above min_term_mag (the magnitude of a term is taken to be the absolute value of its coefficient), considering only those terms up to some maximum Taylor expansion order, max_taylor_order.

Note that this function also sets the magnitudes of the returned terms (by calling term.set_magnitude(…)) based on the current values of this state vector’s parameters. This is an essential step to using these terms in pruned-path-integral calculations later on.

Parameters
min_term_magfloat

the threshold for term magnitudes: only terms with magnitudes above this value are returned.

force_firstorderbool, optional

if True, then always return all the first-order Taylor-series terms, even if they have magnitudes smaller than min_term_mag. This behavior is needed for using GST with pruned-term calculations, as we may begin with a guess model that has no error (all terms have zero magnitude!) and still need to compute a meaningful jacobian at this point.

max_taylor_orderint, optional

the maximum Taylor-order to consider when checking whether term- magnitudes exceed min_term_mag.

max_polynomial_varsint, optional

maximum number of variables the created polynomials can have.

Returns
highmag_termslist

A list of the high-magnitude terms that were found. These terms are sorted in descending order by term-magnitude.

first_order_indiceslist

A list of the indices into highmag_terms that mark which of these terms are first-order Taylor terms (useful when we’re forcing these terms to always be present).

taylor_order_terms_above_mag(order, max_polynomial_vars, min_term_mag)

Get the order-th order Taylor-expansion terms of this state vector that have magnitude above min_term_mag.

This function constructs the terms at the given order which have a magnitude (given by the absolute value of their coefficient) that is greater than or equal to min_term_mag. It calls taylor_order_terms() internally, so that all the terms at order order are typically cached for future calls.

Parameters
orderint

The order of terms to get.

max_polynomial_varsint, optional

maximum number of variables the created polynomials can have.

min_term_magfloat

the minimum term magnitude.

Returns

list

frobeniusdist_squared(other_spam_vec, transform=None, inv_transform=None)

Return the squared frobenius difference between this operation and other_spam_vec.

Optionally transforms this vector first using transform and inv_transform.

Parameters
other_spam_vecState

The other spam vector

transformnumpy.ndarray, optional

Transformation matrix.

inv_transformnumpy.ndarray, optional

Inverse of tranform.

Returns

float

residuals(other_spam_vec, transform=None, inv_transform=None)

Return a vector of residuals between this spam vector and other_spam_vec.

Optionally transforms this vector first using transform and inv_transform.

Parameters
other_spam_vecState

The other spam vector

transformnumpy.ndarray, optional

Transformation matrix.

inv_transformnumpy.ndarray, optional

Inverse of tranform.

Returns

float

transform_inplace(s)

Update state preparation (column) vector V as inv(s) * V.

Note that this is equivalent to state preparation vectors getting mapped: rho -> inv(s) * rho.

Generally, the transform function updates the parameters of the state vector such that the resulting vector is altered as described above. If such an update cannot be done (because the gate parameters do not allow for it), ValueError is raised.

Parameters
sGaugeGroupElement

A gauge group element which specifies the “s” matrix (and it’s inverse) used in the above similarity transform.

Returns

None

depolarize(amount)

Depolarize this state vector by the given amount.

Generally, the depolarize function updates the parameters of the State such that the resulting vector is depolarized. If such an update cannot be done (because the gate parameters do not allow for it), ValueError is raised.

Parameters
amountfloat or tuple

The amount to depolarize by. If a tuple, it must have length equal to one less than the dimension of the gate. All but the first element of the spam vector (often corresponding to the identity element) are multiplied by amount (if a float) or the corresponding amount[i] (if a tuple).

Returns

None

to_vector()

Get the state vector parameters as an array of values.

Returns
numpy array

The parameters as a 1D array with length num_params().

from_vector(v, close=False, dirty_value=True)

Initialize the state vector using a 1D array of parameters.

Parameters
vnumpy array

The 1D vector of state vector parameters. Length must == num_params()

closebool, optional

Whether v is close to this state vector’s current set of parameters. Under some circumstances, when this is true this call can be completed more quickly.

dirty_valuebool, optional

The value to set this object’s “dirty flag” to before exiting this call. This is passed as an argument so it can be updated recursively. Leave this set to True unless you know what you’re doing.

Returns

None

deriv_wrt_params(wrt_filter=None)

The element-wise derivative this state vector.

Construct a matrix whose columns are the derivatives of the state vector with respect to a single param. Thus, each column is of length dimension and there is one column per state vector parameter.

Parameters
wrt_filterlist or numpy.ndarray

List of parameter indices to take derivative with respect to. (None means to use all the this operation’s parameters.)

Returns
numpy array

Array of derivatives, shape == (dimension, num_params)

has_nonzero_hessian()

Whether this state vector has a non-zero Hessian with respect to its parameters.

Returns

bool

hessian_wrt_params(wrt_filter1=None, wrt_filter2=None)

Construct the Hessian of this state vector with respect to its parameters.

This function returns a tensor whose first axis corresponds to the flattened operation matrix and whose 2nd and 3rd axes correspond to the parameters that are differentiated with respect to.

Parameters
wrt_filter1list or numpy.ndarray

List of parameter indices to take 1st derivatives with respect to. (None means to use all the this operation’s parameters.)

wrt_filter2list or numpy.ndarray

List of parameter indices to take 2nd derivatives with respect to. (None means to use all the this operation’s parameters.)

Returns
numpy array

Hessian with shape (dimension, num_params1, num_params2)