SparsePauliOp#
- class qiskit.quantum_info.SparsePauliOp(data, coeffs=None, *, ignore_pauli_phase=False, copy=True)[ソース]#
ベースクラス:
LinearOp
Sparse N-qubit operator in a Pauli basis representation.
This is a sparse representation of an N-qubit matrix
Operator
in terms of N-qubitPauliList
and complex coefficients.It can be used for performing operator arithmetic for hundred of qubits if the number of non-zero Pauli basis terms is sufficiently small.
The Pauli basis components are stored as a
PauliList
object and can be accessed using thepaulis
attribute. The coefficients are stored as a complex Numpy array vector and can be accessed using thecoeffs
attribute.Data type of coefficients
The default
dtype
of the internalcoeffs
Numpy array iscomplex128
. Users can configure this by passingnp.ndarray
with a different dtype. For example, a parameterizedSparsePauliOp
can be made as follows:>>> import numpy as np >>> from qiskit.circuit import ParameterVector >>> from qiskit.quantum_info import SparsePauliOp >>> SparsePauliOp(["II", "XZ"], np.array(ParameterVector("a", 2))) SparsePauliOp(['II', 'XZ'], coeffs=[ParameterExpression(1.0*a[0]), ParameterExpression(1.0*a[1])])
注釈
Parameterized
SparsePauliOp
does not support the following methods:to_matrix(sparse=True)
sincescipy.sparse
cannot have objects as elements.to_operator()
sinceOperator
does not support objects.sort
,argsort
sinceParameterExpression
does not support comparison.equiv
sinceParameterExpression
cannot be converted into complex.chop
sinceParameterExpression
does not support absolute value.
Initialize an operator object.
- パラメータ:
data (PauliList or SparsePauliOp or Pauli or list or str) – Pauli list of terms. A list of Pauli strings or a Pauli string is also allowed.
coeffs (np.ndarray) –
complex coefficients for Pauli terms.
注釈
If
data
is aSparsePauliOp
andcoeffs
is notNone
, the value of theSparsePauliOp.coeffs
will be ignored, and only the passed keyword argumentcoeffs
will be used.ignore_pauli_phase (bool) – if true, any
phase
component of a givenPauliList
will be assumed to be zero. This is more efficient in cases where aPauliList
has been constructed purely for this object, and it is already known that the phases in the ZX-convention are zero. It only makes sense to pass this option when givingPauliList
data. (Default: False)copy (bool) – copy the input data if True, otherwise assign it directly, if possible. (Default: True)
- 例外:
QiskitError – If the input data or coeffs are invalid.
Attributes
- atol = 1e-08#
- coeffs#
Return the Pauli coefficients.
- dim#
Return tuple (input_shape, output_shape).
- num_qubits#
Return the number of qubits if a N-qubit operator or None otherwise.
- parameters#
Return the free
Parameter
s in the coefficients.
- paulis#
Return the PauliList.
- qargs#
Return the qargs for the operator.
- rtol = 1e-05#
- settings#
Return settings.
- size#
The number of Pauli of Pauli terms in the operator.
Methods
- argsort(weight=False)[ソース]#
Return indices for sorting the rows of the table.
Returns the composition of permutations in the order of sorting by coefficient and sorting by Pauli. By using the weight kwarg the output can additionally be sorted by the number of non-identity terms in the Pauli, where the set of all Pauli’s of a given weight are still ordered lexicographically.
Example
Here is an example of how to use SparsePauliOp argsort.
import numpy as np from qiskit.quantum_info import SparsePauliOp # 2-qubit labels labels = ["XX", "XX", "XX", "YI", "II", "XZ", "XY", "XI"] # coeffs coeffs = [2.+1.j, 2.+2.j, 3.+0.j, 3.+0.j, 4.+0.j, 5.+0.j, 6.+0.j, 7.+0.j] # init spo = SparsePauliOp(labels, coeffs) print('Initial Ordering') print(spo) # Lexicographic Ordering srt = spo.argsort() print('Lexicographically sorted') print(srt) # Lexicographic Ordering srt = spo.argsort(weight=False) print('Lexicographically sorted') print(srt) # Weight Ordering srt = spo.argsort(weight=True) print('Weight sorted') print(srt)
Initial Ordering SparsePauliOp(['XX', 'XX', 'XX', 'YI', 'II', 'XZ', 'XY', 'XI'], coeffs=[2.+1.j, 2.+2.j, 3.+0.j, 3.+0.j, 4.+0.j, 5.+0.j, 6.+0.j, 7.+0.j]) Lexicographically sorted [4 7 0 1 2 6 5 3] Lexicographically sorted [4 7 0 1 2 6 5 3] Weight sorted [4 7 3 0 1 2 6 5]
- パラメータ:
weight (bool) – optionally sort by weight if True (Default: False).
sorted (By using the weight kwarg the output can additionally be) –
Pauli. (by the number of non-identity terms in the) –
- 戻り値:
the indices for sorting the table.
- 戻り値の型:
array
- assign_parameters(parameters, inplace=False)[ソース]#
Bind the free
Parameter
s in the coefficients to provided values.- パラメータ:
parameters (Mapping[Parameter, complex | ParameterExpression] | Sequence[complex | ParameterExpression]) – The values to bind the parameters to.
inplace (bool) – If
False
, a copy of the operator with the bound parameters is returned. IfTrue
the operator itself is modified.
- 戻り値:
A copy of the operator with bound parameters, if
inplace
isFalse
, otherwiseNone
.- 戻り値の型:
SparsePauliOp | None
- chop(tol=1e-14)[ソース]#
Set real and imaginary parts of the coefficients to 0 if
< tol
in magnitude.For example, the operator representing
1+1e-17j X + 1e-17 Y
with a tolerance larger than1e-17
will be reduced to1 X
whereasSparsePauliOp.simplify()
would return1+1e-17j X
.If a both the real and imaginary part of a coefficient is 0 after chopping, the corresponding Pauli is removed from the operator.
- パラメータ:
tol (float) – The absolute tolerance to check whether a real or imaginary part should be set to 0.
- 戻り値:
This operator with chopped coefficients.
- 戻り値の型:
- compose(other, qargs=None, front=False)[ソース]#
Return the operator composition with another SparsePauliOp.
- パラメータ:
other (SparsePauliOp) – a SparsePauliOp object.
qargs (list or None) – Optional, a list of subsystem positions to apply other on. If None apply on all subsystems (default: None).
front (bool) – If True compose using right operator multiplication, instead of left multiplication [default: False].
- 戻り値:
The composed SparsePauliOp.
- 戻り値の型:
- 例外:
QiskitError – if other cannot be converted to an operator, or has incompatible dimensions for specified subsystems.
注釈
Composition (
&
) by default is defined as left matrix multiplication for matrix operators, while@
(equivalent todot()
) is defined as right matrix multiplication. That is thatA & B == A.compose(B)
is equivalent toB @ A == B.dot(A)
whenA
andB
are of the same type.Setting the
front=True
kwarg changes this to right matrix multiplication and is equivalent to thedot()
methodA.dot(B) == A.compose(B, front=True)
.
- copy()#
Make a deep copy of current operator.
- dot(other, qargs=None)#
Return the right multiplied operator self * other.
- パラメータ:
- 戻り値:
The right matrix multiplied Operator.
- 戻り値の型:
注釈
The dot product can be obtained using the
@
binary operator. Hencea.dot(b)
is equivalent toa @ b
.
- equiv(other, atol=None)[ソース]#
Check if two SparsePauliOp operators are equivalent.
- パラメータ:
other (SparsePauliOp) – an operator object.
atol (float | None) – Absolute numerical tolerance for checking equivalence.
- 戻り値:
True if the operator is equivalent to
self
.- 戻り値の型:
- expand(other)[ソース]#
Return the reverse-order tensor product with another SparsePauliOp.
- パラメータ:
other (SparsePauliOp) – a SparsePauliOp object.
- 戻り値:
- the tensor product \(b \otimes a\), where \(a\)
is the current SparsePauliOp, and \(b\) is the other SparsePauliOp.
- 戻り値の型:
- static from_list(obj, dtype=<class 'complex'>)[ソース]#
Construct from a list of Pauli strings and coefficients.
For example, the 5-qubit Hamiltonian
\[H = Z_1 X_4 + 2 Y_0 Y_3\]can be constructed as
# via tuples and the full Pauli string op = SparsePauliOp.from_list([("XIIZI", 1), ("IYIIY", 2)])
- パラメータ:
- 戻り値:
The SparsePauliOp representation of the Pauli terms.
- 戻り値の型:
- 例外:
QiskitError – If the list of Paulis is empty.
- static from_operator(obj, atol=None, rtol=None)[ソース]#
Construct from an Operator objector.
Note that the cost of this construction is exponential as it involves taking inner products with every element of the N-qubit Pauli basis.
- パラメータ:
- 戻り値:
the SparsePauliOp representation of the operator.
- 戻り値の型:
- 例外:
QiskitError – if the input operator is not an N-qubit operator.
- static from_sparse_list(obj, num_qubits, do_checks=True, dtype=<class 'complex'>)[ソース]#
Construct from a list of local Pauli strings and coefficients.
Each list element is a 3-tuple of a local Pauli string, indices where to apply it, and a coefficient.
For example, the 5-qubit Hamiltonian
\[H = Z_1 X_4 + 2 Y_0 Y_3\]can be constructed as
# via triples and local Paulis with indices op = SparsePauliOp.from_sparse_list([("ZX", [1, 4], 1), ("YY", [0, 3], 2)], num_qubits=5) # equals the following construction from "dense" Paulis op = SparsePauliOp.from_list([("XIIZI", 1), ("IYIIY", 2)])
- パラメータ:
- 戻り値:
The SparsePauliOp representation of the Pauli terms.
- 戻り値の型:
- 例外:
QiskitError – If the list of Paulis is empty.
QiskitError – If the number of qubits is incompatible with the indices of the Pauli terms.
QiskitError – If the designated qubit is already assigned.
- group_commuting(qubit_wise=False)[ソース]#
Partition a SparsePauliOp into sets of commuting Pauli strings.
- パラメータ:
qubit_wise (bool) –
whether the commutation rule is applied to the whole operator, or on a per-qubit basis. For example:
>>> op = SparsePauliOp.from_list([("XX", 2), ("YY", 1), ("IZ",2j), ("ZZ",1j)]) >>> op.group_commuting() [SparsePauliOp(["IZ", "ZZ"], coeffs=[0.+2.j, 0.+1j]), SparsePauliOp(["XX", "YY"], coeffs=[2.+0.j, 1.+0.j])] >>> op.group_commuting(qubit_wise=True) [SparsePauliOp(['XX'], coeffs=[2.+0.j]), SparsePauliOp(['YY'], coeffs=[1.+0.j]), SparsePauliOp(['IZ', 'ZZ'], coeffs=[0.+2.j, 0.+1.j])]
- 戻り値:
- List of SparsePauliOp where each SparsePauliOp contains
commuting Pauli operators.
- 戻り値の型:
- input_dims(qargs=None)#
Return tuple of input dimension for specified subsystems.
- label_iter()[ソース]#
Return a label representation iterator.
This is a lazy iterator that converts each term in the SparsePauliOp into a tuple (label, coeff). To convert the entire table to labels use the
to_labels()
method.- 戻り値:
label iterator object for the SparsePauliOp.
- 戻り値の型:
LabelIterator
- matrix_iter(sparse=False)[ソース]#
Return a matrix representation iterator.
This is a lazy iterator that converts each term in the SparsePauliOp into a matrix as it is used. To convert to a single matrix use the
to_matrix()
method.- パラメータ:
sparse (bool) – optionally return sparse CSR matrices if True, otherwise return Numpy array matrices (Default: False)
- 戻り値:
matrix iterator object for the PauliList.
- 戻り値の型:
MatrixIterator
- output_dims(qargs=None)#
Return tuple of output dimension for specified subsystems.
- 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.
- 戻り値の型:
- 例外:
QiskitError – if the input and output dimensions of the operator are not equal, or the power is not a positive integer.
- reshape(input_dims=None, output_dims=None, num_qubits=None)#
Return a shallow copy with reshaped input and output subsystem dimensions.
- パラメータ:
input_dims (None or tuple) – new subsystem input dimensions. If None the original input dims will be preserved [Default: None].
output_dims (None or tuple) – new subsystem output dimensions. If None the original output dims will be preserved [Default: None].
num_qubits (None or int) – reshape to an N-qubit operator [Default: None].
- 戻り値:
returns self with reshaped input and output dimensions.
- 戻り値の型:
BaseOperator
- 例外:
QiskitError – if combined size of all subsystem input dimension or subsystem output dimensions is not constant.
- simplify(atol=None, rtol=None)[ソース]#
Simplify PauliList by combining duplicates and removing zeros.
- パラメータ:
- 戻り値:
the simplified SparsePauliOp operator.
- 戻り値の型:
- sort(weight=False)[ソース]#
Sort the rows of the table.
After sorting the coefficients using numpy’s argsort, sort by Pauli. Pauli sort takes precedence. If Pauli is the same, it will be sorted by coefficient. By using the weight kwarg the output can additionally be sorted by the number of non-identity terms in the Pauli, where the set of all Pauli’s of a given weight are still ordered lexicographically.
Example
Here is an example of how to use SparsePauliOp sort.
import numpy as np from qiskit.quantum_info import SparsePauliOp # 2-qubit labels labels = ["XX", "XX", "XX", "YI", "II", "XZ", "XY", "XI"] # coeffs coeffs = [2.+1.j, 2.+2.j, 3.+0.j, 3.+0.j, 4.+0.j, 5.+0.j, 6.+0.j, 7.+0.j] # init spo = SparsePauliOp(labels, coeffs) print('Initial Ordering') print(spo) # Lexicographic Ordering srt = spo.sort() print('Lexicographically sorted') print(srt) # Lexicographic Ordering srt = spo.sort(weight=False) print('Lexicographically sorted') print(srt) # Weight Ordering srt = spo.sort(weight=True) print('Weight sorted') print(srt)
Initial Ordering SparsePauliOp(['XX', 'XX', 'XX', 'YI', 'II', 'XZ', 'XY', 'XI'], coeffs=[2.+1.j, 2.+2.j, 3.+0.j, 3.+0.j, 4.+0.j, 5.+0.j, 6.+0.j, 7.+0.j]) Lexicographically sorted SparsePauliOp(['II', 'XI', 'XX', 'XX', 'XX', 'XY', 'XZ', 'YI'], coeffs=[4.+0.j, 7.+0.j, 2.+1.j, 2.+2.j, 3.+0.j, 6.+0.j, 5.+0.j, 3.+0.j]) Lexicographically sorted SparsePauliOp(['II', 'XI', 'XX', 'XX', 'XX', 'XY', 'XZ', 'YI'], coeffs=[4.+0.j, 7.+0.j, 2.+1.j, 2.+2.j, 3.+0.j, 6.+0.j, 5.+0.j, 3.+0.j]) Weight sorted SparsePauliOp(['II', 'XI', 'YI', 'XX', 'XX', 'XX', 'XY', 'XZ'], coeffs=[4.+0.j, 7.+0.j, 3.+0.j, 2.+1.j, 2.+2.j, 3.+0.j, 6.+0.j, 5.+0.j])
- パラメータ:
weight (bool) – optionally sort by weight if True (Default: False).
sorted (By using the weight kwarg the output can additionally be) –
Pauli. (by the number of non-identity terms in the) –
- 戻り値:
a sorted copy of the original table.
- 戻り値の型:
- static sum(ops)[ソース]#
Sum of SparsePauliOps.
This is a specialized version of the builtin
sum
function for SparsePauliOp with smaller overhead.- パラメータ:
ops (list[SparsePauliOp]) – a list of SparsePauliOps.
- 戻り値:
the SparsePauliOp representing the sum of the input list.
- 戻り値の型:
- 例外:
QiskitError – if the input list is empty.
QiskitError – if the input list includes an object that is not SparsePauliOp.
QiskitError – if the numbers of qubits of the objects in the input list do not match.
- tensor(other)[ソース]#
Return the tensor product with another SparsePauliOp.
- パラメータ:
other (SparsePauliOp) – a SparsePauliOp object.
- 戻り値:
- the tensor product \(a \otimes b\), where \(a\)
is the current SparsePauliOp, and \(b\) is the other SparsePauliOp.
- 戻り値の型:
注釈
The tensor product can be obtained using the
^
binary operator. Hencea.tensor(b)
is equivalent toa ^ b
.
- to_list(array=False)[ソース]#
Convert to a list Pauli string labels and coefficients.
For operators with a lot of terms converting using the
array=True
kwarg will be more efficient since it allocates memory for the full Numpy array of labels in advance.