Pauli

class Pauli(z=None, x=None, label=None)[source]

Bases: object

A simple class representing Pauli Operators.

The form is P_zx = (-i)^dot(z,x) Z^z X^x where z and x are elements of Z_2^n. That is, there are 4^n elements (no phases in this group).

For example, for 1 qubit P_00 = Z^0 X^0 = I P_01 = X P_10 = Z P_11 = -iZX = (-i) iY = Y

The overload __mul__ does not track the sign: P1*P2 = Z^(z1+z2) X^(x1+x2) but sgn_prod does __mul__ and track the phase: P1*P2 = (-i)^dot(z1+z2,x1+x2) Z^(z1+z2) X^(x1+x2) where the sums are taken modulo 2.

Pauli vectors z and x are supposed to be defined as boolean numpy arrays.

Ref. Jeroen Dehaene and Bart De Moor Clifford group, stabilizer states, and linear and quadratic operations over GF(2) Phys. Rev. A 68, 042318 – Published 20 October 2003

Make the Pauli object.

Note that, for the qubit index:
  • Order of z, x vectors is q_0 … q_{n-1},

  • Order of pauli label is q_{n-1} … q_0

E.g.,
  • z and x vectors: z = [z_0 … z_{n-1}], x = [x_0 … x_{n-1}]

  • a pauli is $P_{n-1} otimes … otimes P_0$

Parameters
  • z (numpy.ndarray) – boolean, z vector

  • x (numpy.ndarray) – boolean, x vector

  • label (str) – pauli label

Attributes Summary

numberofqubits

Number of qubits.

x

Getter of x.

z

Getter of z.

Methods Summary

append_paulis([paulis, pauli_labels])

Append pauli at the end.

delete_qubits(indices)

Delete pauli at the indices.

from_label(label)

Take pauli string to construct pauli.

insert_paulis([indices, paulis, pauli_labels])

Insert or append pauli to the targeted indices.

kron(other)

Kronecker product of two paulis.

pauli_single(num_qubits, index, pauli_label)

Generate single qubit pauli at index with pauli_label with length num_qubits.

random(num_qubits[, seed])

Return a random Pauli on number of qubits.

sgn_prod(p1, p2)

Multiply two Paulis and track the phase.

to_instruction()

Convert to Pauli circuit instruction.

to_label()

Present the pauli labels in I, X, Y, Z format.

to_matrix()

Convert Pauli to a matrix representation.

to_operator()

Convert to Operator object.

to_spmatrix()

Convert Pauli to a sparse matrix representation (CSR format).

update_x(x[, indices])

Update partial or entire x.

update_z(z[, indices])

Update partial or entire z.

Attributes Documentation

numberofqubits

Number of qubits.

x

Getter of x.

z

Getter of z.

Methods Documentation

append_paulis(paulis=None, pauli_labels=None)[source]

Append pauli at the end.

Parameters
  • paulis (Pauli) – the to-be-inserted or appended pauli

  • pauli_labels (list[str]) – the to-be-inserted or appended pauli label

Returns

self

Return type

Pauli

delete_qubits(indices)[source]

Delete pauli at the indices.

Parameters

indices (list[int]) – the indices of to-be-deleted paulis

Returns

self

Return type

Pauli

classmethod from_label(label)[source]

Take pauli string to construct pauli.

The qubit index of pauli label is q_{n-1} … q_0. E.g., a pauli is $P_{n-1} otimes … otimes P_0$

Parameters

label (str) – pauli label

Returns

the constructed pauli

Return type

Pauli

Raises

QiskitError – invalid character in the label

insert_paulis(indices=None, paulis=None, pauli_labels=None)[source]

Insert or append pauli to the targeted indices.

If indices is None, it means append at the end.

Parameters
  • indices (list[int]) – the qubit indices to be inserted

  • paulis (Pauli) – the to-be-inserted or appended pauli

  • pauli_labels (list[str]) – the to-be-inserted or appended pauli label

Note

the indices refers to the location of original paulis, e.g. if indices = [0, 2], pauli_labels = [‘Z’, ‘I’] and original pauli = ‘ZYXI’ the pauli will be updated to ZY’I’XI’Z’ ‘Z’ and ‘I’ are inserted before the qubit at 0 and 2.

Returns

self

Return type

Pauli

Raises

QiskitError – provide both paulis and pauli_labels at the same time

kron(other)[source]

Kronecker product of two paulis.

Order is $P_2 (other) otimes P_1 (self)$

Parameters

other (Pauli) – P2

Returns

self

Return type

Pauli

classmethod pauli_single(num_qubits, index, pauli_label)[source]

Generate single qubit pauli at index with pauli_label with length num_qubits.

Parameters
  • num_qubits (int) – the length of pauli

  • index (int) – the qubit index to insert the single qubit

  • pauli_label (str) – pauli

Returns

single qubit pauli

Return type

Pauli

classmethod random(num_qubits, seed=None)[source]

Return a random Pauli on number of qubits.

Parameters
  • num_qubits (int) – the number of qubits

  • seed (int) – Optional. To set a random seed.

Returns

the random pauli

Return type

Pauli

static sgn_prod(p1, p2)[source]

Multiply two Paulis and track the phase.

$P_3 = P_1 otimes P_2$: X*Y

Parameters
Returns

the multiplied pauli complex: the sign of the multiplication, 1, -1, 1j or -1j

Return type

Pauli

to_instruction()[source]

Convert to Pauli circuit instruction.

to_label()[source]

Present the pauli labels in I, X, Y, Z format.

Order is $q_{n-1} …. q_0$

Returns

pauli label

Return type

str

to_matrix()[source]

Convert Pauli to a matrix representation.

Order is q_{n-1} …. q_0, i.e., $P_{n-1} otimes … P_0$

Returns

a matrix that represents the pauli.

Return type

numpy.array

to_operator()[source]

Convert to Operator object.

to_spmatrix()[source]

Convert Pauli to a sparse matrix representation (CSR format).

Order is q_{n-1} …. q_0, i.e., $P_{n-1} otimes … P_0$

Returns

a sparse matrix with CSR format that represents the pauli.

Return type

scipy.sparse.csr_matrix

update_x(x, indices=None)[source]

Update partial or entire x.

Parameters
  • x (numpy.ndarray or list) – to-be-updated x

  • indices (numpy.ndarray or list or optional) – to-be-updated qubit indices

Returns

self

Return type

Pauli

Raises

QiskitError – when updating whole x, the number of qubits must be the same.

update_z(z, indices=None)[source]

Update partial or entire z.

Parameters
  • z (numpy.ndarray or list) – to-be-updated z

  • indices (numpy.ndarray or list or optional) – to-be-updated qubit indices

Returns

self

Return type

Pauli

Raises

QiskitError – when updating whole z, the number of qubits must be the same.

append_paulis(paulis=None, pauli_labels=None)[source]

Append pauli at the end.

Parameters
  • paulis (Pauli) – the to-be-inserted or appended pauli

  • pauli_labels (list[str]) – the to-be-inserted or appended pauli label

Returns

self

Return type

Pauli

delete_qubits(indices)[source]

Delete pauli at the indices.

Parameters

indices (list[int]) – the indices of to-be-deleted paulis

Returns

self

Return type

Pauli

classmethod from_label(label)[source]

Take pauli string to construct pauli.

The qubit index of pauli label is q_{n-1} … q_0. E.g., a pauli is $P_{n-1} otimes … otimes P_0$

Parameters

label (str) – pauli label

Returns

the constructed pauli

Return type

Pauli

Raises

QiskitError – invalid character in the label

insert_paulis(indices=None, paulis=None, pauli_labels=None)[source]

Insert or append pauli to the targeted indices.

If indices is None, it means append at the end.

Parameters
  • indices (list[int]) – the qubit indices to be inserted

  • paulis (Pauli) – the to-be-inserted or appended pauli

  • pauli_labels (list[str]) – the to-be-inserted or appended pauli label

Note

the indices refers to the location of original paulis, e.g. if indices = [0, 2], pauli_labels = [‘Z’, ‘I’] and original pauli = ‘ZYXI’ the pauli will be updated to ZY’I’XI’Z’ ‘Z’ and ‘I’ are inserted before the qubit at 0 and 2.

Returns

self

Return type

Pauli

Raises

QiskitError – provide both paulis and pauli_labels at the same time

kron(other)[source]

Kronecker product of two paulis.

Order is $P_2 (other) otimes P_1 (self)$

Parameters

other (Pauli) – P2

Returns

self

Return type

Pauli

property numberofqubits

Number of qubits.

classmethod pauli_single(num_qubits, index, pauli_label)[source]

Generate single qubit pauli at index with pauli_label with length num_qubits.

Parameters
  • num_qubits (int) – the length of pauli

  • index (int) – the qubit index to insert the single qubit

  • pauli_label (str) – pauli

Returns

single qubit pauli

Return type

Pauli

classmethod random(num_qubits, seed=None)[source]

Return a random Pauli on number of qubits.

Parameters
  • num_qubits (int) – the number of qubits

  • seed (int) – Optional. To set a random seed.

Returns

the random pauli

Return type

Pauli

static sgn_prod(p1, p2)[source]

Multiply two Paulis and track the phase.

$P_3 = P_1 otimes P_2$: X*Y

Parameters
Returns

the multiplied pauli complex: the sign of the multiplication, 1, -1, 1j or -1j

Return type

Pauli

to_instruction()[source]

Convert to Pauli circuit instruction.

to_label()[source]

Present the pauli labels in I, X, Y, Z format.

Order is $q_{n-1} …. q_0$

Returns

pauli label

Return type

str

to_matrix()[source]

Convert Pauli to a matrix representation.

Order is q_{n-1} …. q_0, i.e., $P_{n-1} otimes … P_0$

Returns

a matrix that represents the pauli.

Return type

numpy.array

to_operator()[source]

Convert to Operator object.

to_spmatrix()[source]

Convert Pauli to a sparse matrix representation (CSR format).

Order is q_{n-1} …. q_0, i.e., $P_{n-1} otimes … P_0$

Returns

a sparse matrix with CSR format that represents the pauli.

Return type

scipy.sparse.csr_matrix

update_x(x, indices=None)[source]

Update partial or entire x.

Parameters
  • x (numpy.ndarray or list) – to-be-updated x

  • indices (numpy.ndarray or list or optional) – to-be-updated qubit indices

Returns

self

Return type

Pauli

Raises

QiskitError – when updating whole x, the number of qubits must be the same.

update_z(z, indices=None)[source]

Update partial or entire z.

Parameters
  • z (numpy.ndarray or list) – to-be-updated z

  • indices (numpy.ndarray or list or optional) – to-be-updated qubit indices

Returns

self

Return type

Pauli

Raises

QiskitError – when updating whole z, the number of qubits must be the same.

property x

Getter of x.

property z

Getter of z.