SparseLabelOp#

class SparseLabelOp(data, *, copy=True, validate=True)[source]#

Bases: LinearMixin, AdjointMixin, GroupMixin, TolerancesMixin, ABC, Mapping

The base class for sparse second-quantized operators.

This class generalizes the storing of operators down to a mapping from string-based labels to coefficients. No assumptions are to be made about the contents of the string keys, which is entirely left up to concrete subclasses of this base.

Since this is an abstract base class, we cannot show concrete computation examples here, but operators implementing this interface will support:

  • linear operations such as addition and scalar multiplication

  • operator multiplication such as composition and tensor products

  • transposition, conjugation and (by extension) the adjoint()

  • equality and equivalence (using the atol and rtol tolerances) comparisons

Furthermore, several general utility methods exist which are documented below.

নোট

A SparseLabelOp can contain qiskit.circuit.ParameterExpression objects as coefficients. However, a SparseLabelOp containing parameters does not support the following methods:

  • equiv

  • induced_norm

প্যারামিটার:
  • data (Mapping[str, _TCoeff]) -- the operator data, mapping string-based keys to numerical values.

  • copy (bool) -- when set to False the data will not be copied and the dictionary will be stored by reference rather than by value (which is the default; copy=True). Note, that this requires you to not change the contents of the dictionary after constructing the operator. This also implies validate=False. Use with care!

  • validate (bool) -- when set to False the data keys will not be validated. Note, that the SparseLabelOp base class, makes no assumption about the data keys, so will not perform any validation by itself. Only concrete subclasses are encouraged to implement a key validation method. Disable this setting with care!

রেইজেস:

QiskitNatureError -- when an invalid key is encountered during validation.

Attributes

atol = 1e-08#
register_length#

Returns the register length

rtol = 1e-05#

Methods

adjoint()#

Return the adjoint of the Operator.

রিটার্ন টাইপ:

Self

argsort(*, weight=False)[source]#

Returns the keys which sort this operator.

প্যারামিটার:

weight (bool) -- when True, the returned keys will sort this operator according to the coefficient weights of the stored terms; when False, the keys will sort the operator by its keys (i.e. lexicographically).

রিটার্নস:

The sequence of keys which sort this operator.

রিটার্ন টাইপ:

Sequence[str]

assign_parameters(parameters)[source]#

Assign parameters to new parameters or values.

প্যারামিটার:

parameters (Mapping[ParameterExpression, complex | ParameterExpression]) -- The mapping from parameters to new parameters or values.

রিটার্নস:

A new operator with the parameters assigned.

রিটার্ন টাইপ:

SparseLabelOp

chop(atol=None)[source]#

Chops the real and imaginary parts of the operator coefficients.

This function separately chops the real and imaginary parts of all coefficients to the provided tolerance. Parameters are chopped only if they are exactly zero.

প্যারামিটার:

atol (float | None) -- the tolerance to which to chop. If None, atol will be used.

রিটার্নস:

The chopped operator.

রিটার্ন টাইপ:

SparseLabelOp

abstract compose(other, qargs=None, front=False)[source]#

Returns the operator composition with another operator.

প্যারামিটার:
  • other (SparseLabelOp) -- the other operator.

  • qargs (None) -- UNUSED.

  • front (bool) -- If True composition uses right operator multiplication, otherwise left multiplication is used (the default).

রিটার্নস:

The operator resulting from the composition.

রিটার্ন টাইপ:

SparseLabelOp

নোট

Composition (&) by default is defined as left matrix multiplication for matrix operators, while @ (equivalent to dot()) is defined as right matrix multiplication. This means that A & B == A.compose(B) is equivalent to B @ A == B.dot(A) when A and B are of the same type.

Setting the front=True keyword argument changes this to right matrix multiplication which is equivalent to the dot() method A.dot(B) == A.compose(B, front=True).

conjugate()[source]#

Returns the conjugate of the SparseLabelOp.

রিটার্নস:

The complex conjugate of the starting SparseLabelOp.

রিটার্ন টাইপ:

SparseLabelOp

dot(other, qargs=None)#

Return the right multiplied operator self * other.

প্যারামিটার:
  • other (Operator) -- an operator object.

  • qargs (list or None) -- Optional, a list of subsystem positions to apply other on. If None apply on all subsystems (default: None).

রিটার্নস:

The right matrix multiplied Operator.

রিটার্ন টাইপ:

Operator

নোট

The dot product can be obtained using the @ binary operator. Hence a.dot(b) is equivalent to a @ b.

equiv(other, *, atol=None, rtol=None)[source]#

Check equivalence of two SparseLabelOp instances up to an accepted tolerance.

প্যারামিটার:
  • other (SparseLabelOp) -- the second SparseLabelOp to compare with this instance.

  • atol (float | None) -- Absolute numerical tolerance. The default behavior is to use self.atol.

  • rtol (float | None) -- Relative numerical tolerance. The default behavior is to use self.rtol.

রিটার্নস:

True if operators are equivalent, False if not.

রেইজেস:

ValueError -- Raised if either operator contains parameters

রিটার্ন টাইপ:

bool

abstract expand(other)[source]#

Returns the reverse-order tensor product with another operator.

প্যারামিটার:

other (SparseLabelOp) -- the other operator.

রিটার্নস:

The operator resulting from the tensor product, \(othr \otimes self\).

রিটার্ন টাইপ:

SparseLabelOp

abstract classmethod from_polynomial_tensor(tensor)[source]#

Constructs the operator from a PolynomialTensor.

প্যারামিটার:

tensor (PolynomialTensor) -- the PolynomialTensor to be expanded.

রিটার্নস:

The constructed operator.

রিটার্ন টাইপ:

SparseLabelOp

abstract classmethod from_terms(terms)[source]#

Constructs a new SparseLabelOp from a sequence returned by terms().

প্যারামিটার:

terms (Sequence[tuple[list[tuple[str, int]], Union[complex, qiskit.circuit.parameterexpression.ParameterExpression]]]) -- a sequence as returned by terms().

রিটার্নস:

The constructed operator.

রিটার্ন টাইপ:

SparseLabelOp

get(k[, d]) D[k] if k in D, else d.  d defaults to None.#
induced_norm(order=1)[source]#

Returns the p-norm induced by the operator coefficients.

If the operator is represented as a sum of terms

\[\sum_i w_i H_i\]

then the induced \(p\)-norm is

\[\left(\sum_i |w_i|^p \right)^{1/p}\]

This is the standard \(p\)-norm of the operator coefficients considered as a vector (see https://en.wikipedia.org/wiki/Norm_(mathematics)#p-norm). Note that this method does not normal-order or simplify the operator before computing the norm; performing either of those operations can affect the result.

প্যারামিটার:

order (int) -- Order \(p\) of the norm. The default value is 1.

রিটার্নস:

The induced norm.

রিটার্ন টাইপ:

float

রেইজেস:

ValueError -- Operator contains parameters.

রিটার্ন টাইপ:

float

is_parameterized()[source]#

Returns whether the operator contains any parameters.

রিটার্ন টাইপ:

bool

is_zero(tol=None)[source]#

Returns true if operator length is zero or all coefficients have value zero.

প্যারামিটার:

tol (int | None) -- tolerance for checking coefficient values. If this is None, atol will be used instead.

রিটার্নস:

If operator length is zero or all coefficients are zero.

রিটার্ন টাইপ:

bool

items() a set-like object providing a view on D's items#
keys() a set-like object providing a view on D's keys#
classmethod one()[source]#

Constructs a unity-operator.

রিটার্নস:

The unity-operator of the given length.

রিটার্ন টাইপ:

SparseLabelOp

parameters()[source]#

Returns a list of the parameters in the operator.

রিটার্নস:

A list of the parameters in the operator.

রিটার্ন টাইপ:

list[qiskit.circuit.parameterexpression.ParameterExpression]

permute_indices(permutation)[source]#

Permutes the indices of the operator.

This method applies the provided index permutation to all labels of this operator. The provided permutation must be a sequence of integers whose length is equal to the register_length of the operator. The integer at any given index of the sequence indicates the new index which that location will be permuted to. For example:

op = SparseLabelOp({"+_0 -_1 +_2 -_3": 1.0})
permuted_op = op.permute_indices([3, 1, 0, 2])
assert permuted_op == SparseLabelOp({"+_3 -_1 +_0 -_2": 1.0})

সতর্কতা

This permutation utility is very powerful. Be mindful of the implications such a permutation might have on other components of the stack. To name an example, the builtin two-qubit reduction of the ParityMapper might not yield the expected results when used on permuted operator.

প্যারামিটার:

permutation (Sequence[int]) -- a sequence of integers indicating the permutation to be applied. See above for an example.

রিটার্নস:

A new operator instance with the permuted indices.

রেইজেস:

ValueError -- if the length of the permutation argument does not equal register_length.

রিটার্ন টাইপ:

SparseLabelOp

power(n)#

Return the compose of a operator with itself n times.

প্যারামিটার:

n (int) -- the number of times to compose with self (n>0).

রিটার্নস:

the n-times composed operator.

রিটার্ন টাইপ:

Clifford

রেইজেস:

QiskitError -- if the input and output dimensions of the operator are not equal, or the power is not a positive integer.

round(decimals=0)[source]#

Rounds the operator coefficients to a specified number of decimal places.

প্যারামিটার:

decimals (int) -- the number of decimal places to round coefficients to. By default this will round to the nearest integer value.

রিটার্নস:

The rounded operator.

রিটার্ন টাইপ:

SparseLabelOp

abstract simplify(atol=None)[source]#

Simplify the operator.

The simplifications implemented by this method should be: - to eliminate terms whose coefficients are close (w.r.t. atol) to 0. - to combine the coefficients which correspond to equivalent terms (see also the note below)

নোট

simplify() should be used to simplify terms whose coefficients are close to zero, up to the specified numerical tolerance. It still differs slightly from chop() because that will chop real and imaginary part components individually.

নোট

The meaning of "equivalence" between multiple terms depends on the specific operator subclass. As a restriction this method is required to preserve the order of appearance of the different components within a term. This avoids some possibly unexpected edge cases. However, this also means that some equivalencies cannot be detected. Check for other methods of a specific subclass which may affect the order of terms and can allow for further simplifications to be implemented.

This method returns a new operator (the original operator is not modified).

প্যারামিটার:

atol (float | None) -- Absolute numerical tolerance. The default behavior is to use self.atol.

রিটার্নস:

The simplified operator.

রিটার্ন টাইপ:

SparseLabelOp

sort(*, weight=False)[source]#

Returns a new sorted operator.

প্যারামিটার:

weight (bool) -- when True, the returned keys will sort this operator according to the coefficient weights of the stored terms; when False, the keys will sort the operator by its keys (i.e. lexicographically).

রিটার্নস:

A new operator instance with its contents sorted.

রিটার্ন টাইপ:

SparseLabelOp

abstract tensor(other)[source]#

Returns the tensor product with another SparseLabelOp.

প্যারামিটার:

other (SparseLabelOp) -- the other SparseLabelOp.

রিটার্নস:

The operator resulting from the tensor product, \(self \otimes other\).

রিটার্ন টাইপ:

SparseLabelOp

নোট

The tensor product can be obtained using the ^ binary operator. Hence a.tensor(b) is equivalent to a ^ b.

abstract terms()[source]#

Provides an iterator analogous to items() but with the labels already split into pairs of operation characters and indices.

Yields:

A tuple with two items; the first one being a list of pairs of the form (char, int) where char represents the operation applied to the register and the integer corresponds to the register index on which the operator gets applied; the second item of the returned tuple is the coefficient of this term.

রিটার্ন টাইপ:

Iterator[tuple[list[tuple[str, int]], Union[complex, qiskit.circuit.parameterexpression.ParameterExpression]]]

abstract transpose()[source]#

Returns the transpose of the operator.

রিটার্নস:

The transpose of the operator.

রিটার্ন টাইপ:

SparseLabelOp

values() an object providing a view on D's values#
classmethod zero()[source]#

Constructs a zero-operator.

রিটার্নস:

The zero-operator of the given length.

রিটার্ন টাইপ:

SparseLabelOp