SpinOp#

class SpinOp(data, spin=Fraction(1, 2), num_spins=None, *, copy=True, validate=True)[ソース]#

ベースクラス: SparseLabelOp

XYZ Spin operator.

A SpinOp represents a weighted sum of spin operator terms with a certain spin value associated to them. This value can be an integer for bosonic particles or a half-integer (fraction) for fermions.

These operator terms are encoded as sparse labels, strings consisting of a space-separated list of expressions. Each expression must look like [XYZ]_<index> or [XYZ]_<index>^<power>, where the <index> is a non-negative integer representing the index of the spin mode where the X, Y or Z component of the spin operator is to be applied.

The value of index is bound by the number of spins(num_spins) of the operator (Note: since Python indices are 0-based, the maximum value an index can take is given by num_spins-1). The <power> is a positive integer indicating the number of times the given operator is applied to the mode at <index>. You can omit <power>, implying a single application of the operator (power = 1).

Initialization

A SpinOp is initialized with a dictionary, mapping terms to their respective coefficients. For example:

from qiskit_nature.second_q.operators import SpinOp

x = SpinOp({"X_0": 1}, spin=3/2)
y = SpinOp({"Y_0": 1}, spin=3/2)
z = SpinOp({"Z_0": 1}, spin=3/2)

are \(S^x, S^y, S^z\) for spin 3/2 system. The two qutrit Heisenberg model with transverse magnetic field is

SpinOp({
        "X_0 X_1": -1,
        "Y_0 Y_1": -1,
        "Z_0 Z_1": -1,
        "Z_0": -0.3,
        "Z_1": -0.3,
    },
    spin=1
)

This means \(- S^x_0 S^x_1 - S^y_0 S^y_1 - S^z_0 S^z_1 - 0.3 S^z_0 - 0.3 S^z_1\).

An example using labels with powers would be:

from qiskit_nature.second_q.operators import SpinOp

op = SpinOp({"X_0^2 Y_1^3 Z_0": 1})

By default, this way of initializing will create a full copy of the dictionary of coefficients. If you have very restricted memory resources available, or would like to avoid the additional copy, the dictionary will be stored by reference if you disable copy like so:

some_big_data = {
    "X_0 Y_0": 1.0,
    "X_1 Y_1": -1.0,
    # ...
}

op = SpinOp(
    some_big_data,
    num_spins=2,
    copy=False,
)

注釈

It is the users』 responsibility, that in the above scenario, some_big_data is not changed after initialization of the SpinOp, since the operator contents are not guaranteed to remain unaffected by such changes.

Algebra

SpinOp supports the following basic arithmetic operations: addition, subtraction, scalar multiplication, adjoint, composition and tensoring.

As of now, operations that involve two different instances of SpinOp (i.e. addition, subtraction, composition, and tensoring) are only supported for identical spins (op_1.num_spins == op_2.num_spins).

For example,

Addition

SpinOp({"X_1": 1}, num_spins=2) + SpinOp({"X_0": 1}, num_spins=2)

Sum

sum(SpinOp({label: 1}, num_spins=3) for label in ["X_0", "Z_1", "X_2 Z_2"])

Scalar multiplication

0.5 * SpinOp({"X_1": 1}, num_spins=2)

Operator multiplication

op1 = SpinOp({"X_0 Z_1": 1}, num_spins=2)
op2 = SpinOp({"Z_0 X_0 X_1": 1}, num_spins=2)
print(op1 @ op2)

Tensor multiplication

op = SpinOp({"X_0 Z_1": 1}, num_spins=2)
print(op ^ op)

Adjoint

SpinOp({"X_0 Z_1": 1j}, num_spins=2).adjoint()

Iteration

Instances of SpinOp are iterable. Iterating a SpinOp yields (term, coefficient) pairs describing the terms contained in the operator. Labels containing powers/exponents will be expanded into multiple (term, coefficient) pairs.

The following attributes can be set via the initializer but can also be read and updated once the SpinOp object has been constructed.

num_spins#

the number of spins on which this operator acts. This is considered a lower bound, which means that mathematical operations acting on two or more operators will result in a new operator with the maximum number of spins of any of the involved operators.

Type:

int | None

spin#

positive half-integer (integer or half-odd-integer) that represents spin.

Type:

float | Fraction

パラメータ:
  • data (Mapping[str, _TCoeff]) – label string, list of labels and coefficients. See the label section in the documentation of SpinOp for more details.

  • spin (float | Fraction) – positive half-integer (integer or half-odd-integer) that represents spin.

  • num_spins (int | None) – the number spins on which this operator acts.

  • 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!

例外:

Attributes

atol = 1e-08#
register_length#
rtol = 1e-05#

Methods

adjoint()#

Return the adjoint of the Operator.

戻り値の型:

Self

argsort(*, weight=False)#

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)#

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)#

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

compose(other, qargs=None, front=False)[ソース]#

Returns the operator composition with another operator.

パラメータ:
  • other (SpinOp) – the other operator.

  • qargs – UNUSED.

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

戻り値:

The operator resulting from the composition.

戻り値の型:

SpinOp

注釈

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()[ソース]#

Returns the conjugate of the SpinOp.

戻り値:

The complex conjugate of this SpinOp.

戻り値の型:

SpinOp

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)#

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

expand(other)[ソース]#

Returns the reverse-order tensor product with another operator.

パラメータ:

other (SpinOp) – the other operator.

戻り値:

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

戻り値の型:

SpinOp

classmethod from_polynomial_tensor(tensor)[ソース]#

Constructs the operator from a PolynomialTensor.

パラメータ:

tensor (PolynomialTensor) – the PolynomialTensor to be expanded.

戻り値:

The constructed operator.

戻り値の型:

SpinOp

classmethod from_terms(terms)[ソース]#

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.

戻り値の型:

SpinOp

get(k[, d]) D[k] if k in D, else d.  d defaults to None.#
index_order()[ソース]#

Convert to the equivalent operator with the terms of each label ordered by index.

Returns a new operator (the original operator is not modified).

戻り値:

The index ordered operator.

戻り値の型:

SpinOp

induced_norm(order=1)#

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()#

Returns whether the operator contains any parameters.

戻り値の型:

bool

is_zero(tol=None)#

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(spin=Fraction(1, 2))[ソース]#

Constructs the 「one」 spin operator for a given spin.

戻り値:

The 「one」 spin operator for spin.

戻り値の型:

SpinOp

parameters()#

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)#

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)#

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

simplify(atol=None)[ソース]#

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.

戻り値の型:

SpinOp

sort(*, weight=False)#

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

tensor(other)[ソース]#

Returns the tensor product with another SparseLabelOp.

パラメータ:

other (SpinOp) – the other SparseLabelOp.

戻り値:

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

戻り値の型:

SpinOp

注釈

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

terms()[ソース]#

Provides an iterator analogous to items() but with the labels already split into pairs of operation characters and indices. If the labels contain an exponent, they will be expanded into as many elements as indicated by the exponent. For example, label "X_0^3" will yield ([("X", 0), ("X", 0), ("X", 0)], coeff).

列挙:

A tuple with two items; the first one being a list of pairs of the form (char, int) where char is either X, Y or Z and the integer corresponds to the 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]]]

to_matrix()[ソース]#

Convert to dense matrix.

戻り値:

The matrix (numpy.ndarray with dtype=numpy.complex128)

戻り値の型:

ndarray

transpose()[ソース]#

Returns the transpose of the SpinOp.

戻り値:

The transpose of the SpinOp.

戻り値の型:

SpinOp

values() an object providing a view on D's values#
classmethod x(spin=Fraction(1, 2))[ソース]#

Constructs the X spin operator for a given spin.

戻り値:

The X spin operator for spin.

戻り値の型:

SpinOp

classmethod y(spin=Fraction(1, 2))[ソース]#

Constructs the Y spin operator for a given spin.

戻り値:

The Y spin operator for spin.

戻り値の型:

SpinOp

classmethod z(spin=Fraction(1, 2))[ソース]#

Constructs the Z spin operator for a given spin.

戻り値:

The Z spin operator for spin.

戻り値の型:

SpinOp

classmethod zero(spin=Fraction(1, 2))[ソース]#

Constructs the 「zero」 spin operator for a given spin.

戻り値:

The 「zero」 spin operator for spin.

戻り値の型:

SpinOp