Pauli Operators

Represent and manipulate Pauli operators. Evaluate Pauli expectation values using ideal statevector simulation. Refer to these Jupyter Notebook tutorials on Forge:

  • GettingStarted/Examples/Circuits/quasar_derivatives.ipynb

  • GettingStarted/Examples/Circuits/quasar_parameters.ipynb

  • GettingStarted/Examples/Circuits/quasar_pauli.ipynb

Classes

Class Pauli

Math

Pauli Summary Attributes

Hilbert Space Representation

OrderedDict Methods

Pauli Starter Objects

Class PauliExpectation

Class PauliOperator

Class PauliString

class quasar.pauli.Pauli(*args, **kwargs)

Class Pauli represents Pauli operators as an OrderedDict of PauliString : float/complex``pairs. Use ``dict access methods to add Pauli strings and access/modify the coefficients of existing strings.

static IXYZ()

Returns four PauliStarter objects corresponding to the I, X, Y, and Z Pauli operators respectively.

When PauliStarter objects are indexed by the [] operator, they generate a corresponding Pauli object on the given qubit index. For instance, after generating PauliStarter objects using I, X, Y, Z = quasar.Pauli.IXYZ(), X[1] generates an X Pauli operator object on qubit index 1. Since indices applied to I PauliStarter objects have no effect, it is convention to refer to the I operator using I[-`]. Use +, -, *, and /, as well as float and complex coefficients to build linear combinations of products of Pauli operators.

Returns

I, X, Y, Z PauliStarter objects, returned in that order

Return type

PauliStarter

property conj

Return a new version of the Pauli object with the values conjugated.

Type

Pauli

dot(other)

Calculate the dot product of coincident Pauli strings in self and another Pauli object.

Parameters

other (Pauli) – the other Pauli object in the dot product with self.

Returns

the dot product of coincident Pauli strings in self and other.

Return type

float

static from_matrix(matrix, qubit_indices=None, cutoff=1e-14)

Convert a computational-basis Hilbert space operator to an equivalent Pauli form.

Parameters
  • param_matrix (np.ndarray) – matrix representing a computational-basis Hilbert space operator.

  • qubit_indices (list of ints) – qubit indices to be occupied by Pauli operators. If default value None, all qubit indices are included in qubit_indices.

  • cutoff (float) – remove a Pauli string from the Pauli form if the absolute value of its coefficient is less than cutoff (default - 1.0E-14).

Returns

matrix expressed in an equivalent Pauli form.

Return type

Pauli

get(k[, d]) → D[k] if k in D, else d. d defaults to None.
matrix_vector_product(statevector, min_qubit=None, nqubit=None, dtype=<class 'numpy.complex128'>)

Evaluate how a Pauli object acts on a statevector in the computational-basis Hilbert space.

Parameters
  • statevector (np.ndarray of shape 2**N) – the statevector on which to evaluate how self acts in the computational-basis Hilbert space.

  • min_qubit (int) – the minimum occupied qubit index (default - None).

  • nqubit (int) – the total number of qubit indices in the circuit, including empty qubiit indices (default - None).

  • dtype (real or complex dtype) – the dtype to perform the computation at (default - None).

Returns

the result of self acting on statevector in the computational-basis Hilbert space.

Return type

np.ndarray, complex dtype

property max_order

The maximum number of one-qubit Pauli operators in the Pauli object.

Type

int

property max_qubit

The maximum occupied qubit index (or -1 if no occupied qubits)

Type

int

property min_qubit

The minimum occupied qubit index (or 0 if no occupied qubits)

Type

int

property norm2

Return the square root of the dot product of self with self.

Type

float

property norminf

Return the magnitude of the largest coefficient of self.

Type

float

property nqubit

The total number of qubit indices in the circuit (including empty qubit indices).

Type

int

property nqubit_sparse

The total number of occupied qubit indices in the circuit (excluding empty qubit indices).

Type

int

property nterm

The number of Pauli strings in the Pauli object.

Type

int

property qubits

The unique occupied qubit indices over all strings in the Pauli object.

Type

SortedSet

setdefault(key, default=None)

Return value for item identified by key in sorted dict.

If key is in the sorted dict then return its value. If key is not in the sorted dict then insert key with value default and return default.

Optional argument default defaults to none.

Runtime complexity: O(log(n)) – approximate.

>>> sd = SortedDict()
>>> sd.setdefault('a', 1)
1
>>> sd.setdefault('a', 10)
1
>>> sd
SortedDict({'a': 1})
Parameters
  • key – key for item

  • default – value for item (default None)

Returns

value for item identified by key

sieved(cutoff=1e-14)

Remove strings which have small or zero coefficients from the self.

Parameters

cutoff (float) – remove a Pauli string if the absolute value of its coefficient is less than cutoff (default - `1.0E-14).

Returns

a new Pauli object with strings removed from self if the absolute value of their coefficient is less than cutoff.

Return type

Pauli

property summary_str

A formatted summary string that displays the values of the nqubit, nterm, and max_order attributes of the Pauli object.

Type

str

to_matrix(min_qubit=None, nqubit=None, dtype=<class 'numpy.complex128'>)

Convert a Pauli object to the computational-basis Hilbert space.

Parameters
  • min_qubit (int) – the minimum occupied qubit index (default - None).

  • nqubit (int) – the total number of qubit indices in the circuit, including empty qubit indices (default - None).

  • dtype (complex dtype) – the dtype to perform the computation at (default - None).

Returns

self expressed in the computational-basis Hilbert space.

Return type

np.ndarray, complex dtype

update(*args, **kwargs)

Update sorted dict with items from args and kwargs.

Overwrites existing items.

Optional arguments args and kwargs may be a mapping, an iterable of pairs or keyword arguments. See SortedDict.__init__() for details.

Parameters
  • args – mapping or iterable of pairs

  • kwargs – keyword arguments mapping

static zero()
Returns

a new Pauli object initialized with no strings.

Return type

Pauli

static zeros_like(x)

Returns a new Pauli object initialized with the strings of another Pauli object, but with coefficient values initialized to zero.

Parameters

x (Pauli) – Pauli object whose strings will be used to initialize the new Pauli object.

Returns

a Pauli object initialized with the strings of x, but with coefficient values initialized to zero.

Return type

Pauli

class quasar.pauli.PauliExpectation(*args, **kwargs)

Class PauliExpectation represents the expectation value of a Pauli-sparse Hermitian operator.

static zero()

Returns: Pauli: a new Pauli object initialized with no strings.

static zeros_like(x)

Returns a new Pauli object initialized with the strings of another Pauli object, but with coefficient values initialized to zero.

Parameters

x (Pauli) – Pauli object whose strings will be used to initialize the new Pauli object.

Returns

a Pauli object initialized with the strings of x, but with coefficient values initialized to zero.

Return type

Pauli

class quasar.pauli.PauliOperator

Class PauliOperator represents a one-qubit Pauli operator with a given character (‘X’, ‘Y’, or ‘Z’) and qubit index.

property char

Return the character representing the type of Pauli operator (‘X’,’Y’, or ‘Z’).

Type

str

static from_string(string)
Parameters

string (str) – string to convert into a PauliOperator object

Returns

a PauliOperator object from a string composed of the character (‘X’,’Y’, or ‘Z’) representing the type of Pauli operator and the qubit index.

Return type

PauliOperator

property qubit

Return qubit index.

Type

int

class quasar.pauli.PauliString

Class PauliString represents a string of one or more PauliOperator objects as a tuple of PauliOperator objects.

property chars

The characters (‘X’, ‘Y’, or ‘Z’) corresponding to all of the PauliOperator objects in the PauliString.

static from_string(string)

Return a PauliString object from a string expressing a linear combination of products of one or more PauliOperator objects.

Parameters

string (str) – string to convert into a PauliOperator object

Returns

a PauliString object from a string expressing a linear combination of products of one or more PauliOperator objects.

Return type

PauliString

property order

The number of PauliOperator objects in the PauliString.

Type

int

property qubits

The qubit indices occupied by all of the PauliOperator objects in the PauliString.

Type

tuple of ints