English
Languages
English
Bengali
French
German
Japanese
Korean
Portuguese
Spanish
Tamil

qiskit.quantum_info.SparsePauliOp.argsort

SparsePauliOp.argsort(weight=False)[source]

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]
Parameters
  • 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) –

Returns

the indices for sorting the table.

Return type

array