Skip to content

Commit

Permalink
Switch PauliTable to PauliList in SparsePauliOp (#6826)
Browse files Browse the repository at this point in the history
* Switch PauliTable to PauliList in SparsePauliOp

* more replacement

* filter deprecation warnings

* fix lint

* more filter deprecation warning

* rm unused import

* upgrade visualization/utils.py

* DeprecationWarning correct way

* update doc and compatibility of pauli_basis

* fix lint

* add test

* add releasenote

* move deprecations into separate section

Co-authored-by: Julien Gacon <gaconju@gmail.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Aug 28, 2021
1 parent 9057472 commit 75e06dc
Show file tree
Hide file tree
Showing 11 changed files with 218 additions and 123 deletions.
8 changes: 4 additions & 4 deletions qiskit/opflow/converters/pauli_basis_change.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,12 @@ def convert(self, operator: OperatorBase) -> OperatorBase:
and operator.primitive.grouping_type == "TPB"
):
primitive = operator.primitive.primitive.copy()
origin_x = reduce(np.logical_or, primitive.table.X)
origin_z = reduce(np.logical_or, primitive.table.Z)
origin_x = reduce(np.logical_or, primitive.paulis.x)
origin_z = reduce(np.logical_or, primitive.paulis.z)
origin_pauli = Pauli((origin_z, origin_x))
cob_instr_op, _ = self.get_cob_circuit(origin_pauli)
primitive.table.Z = np.logical_or(primitive.table.X, primitive.table.Z)
primitive.table.X = False
primitive.paulis.z = np.logical_or(primitive.paulis.x, primitive.paulis.z)
primitive.paulis.x = False
dest_pauli_sum_op = PauliSumOp(primitive, coeff=operator.coeff, grouping_type="TPB")
return self._replacement_fn(cob_instr_op, dest_pauli_sum_op)

Expand Down
4 changes: 2 additions & 2 deletions qiskit/opflow/primitive_ops/pauli_sum_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ def permute(self, permutation: List[int]) -> "PauliSumOp":
spop = self.primitive.tensor(SparsePauliOp(Pauli("I" * (length - self.num_qubits))))
permutation = [i for i in range(length) if i not in permutation] + permutation
permu_arr = np.arange(length)[np.argsort(permutation)]
permu_arr = np.hstack([permu_arr, permu_arr + length])
spop.table.array = spop.table.array[:, permu_arr]
spop.paulis.x = spop.paulis.x[:, permu_arr]
spop.paulis.z = spop.paulis.z[:, permu_arr]
return PauliSumOp(spop, self.coeff)

def compose(
Expand Down
6 changes: 4 additions & 2 deletions qiskit/quantum_info/operators/symplectic/pauli.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
# pylint: disable=invalid-name
# pylint: disable=bad-docstring-quotes # for deprecate_function decorator

from typing import Dict
import re
from typing import Dict

import numpy as np

Expand Down Expand Up @@ -1024,7 +1024,9 @@ def random(cls, num_qubits, seed=None):
Pauli: the random pauli
"""
# pylint: disable=cyclic-import
from qiskit.quantum_info.operators.symplectic.random import random_pauli
from qiskit.quantum_info.operators.symplectic.random import (
random_pauli,
)

return random_pauli(num_qubits, group_phase=False, seed=seed)

Expand Down
5 changes: 0 additions & 5 deletions qiskit/quantum_info/operators/symplectic/pauli_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -632,11 +632,6 @@ def tensor(self, other):
"""
if not isinstance(other, PauliList):
other = PauliList(other)
if len(other) not in [1, len(self)]:
raise QiskitError(
"Incompatible PauliLists. Other list must "
"have either 1 or the same number of Paulis."
)
return PauliList(super().tensor(other))

def expand(self, other):
Expand Down
31 changes: 23 additions & 8 deletions qiskit/quantum_info/operators/symplectic/pauli_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,42 @@
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.
"""
PauliTable utility functions.
PauliList utility functions.
"""

import warnings

import numpy as np
from .pauli_table import PauliTable

from qiskit.quantum_info.operators.symplectic.pauli_list import PauliList
from qiskit.quantum_info.operators.symplectic.pauli_table import PauliTable


def pauli_basis(num_qubits, weight=False):
"""Return the ordered PauliTable for the n-qubit Pauli basis.
def pauli_basis(num_qubits, weight=False, pauli_list=False):
"""Return the ordered PauliTable or PauliList for the n-qubit Pauli basis.
Args:
num_qubits (int): number of qubits
weight (bool): if True optionally return the basis sorted by Pauli weight
rather than lexicographic order (Default: False)
pauli_list (bool): if True, the return type becomes PauliList, otherwise PauliTable.
Returns:
PauliTable: the PauliTable for the basis
PauliTable, PauliList: the Paulis for the basis
"""
pauli_1q = PauliTable(
np.array([[False, False], [True, False], [True, True], [False, True]], dtype=bool)
)
if pauli_list:
pauli_1q = PauliList(["I", "X", "Y", "Z"])
else:
warnings.warn(
"The pauli_basis function with PauliTable output is deprecated as of Qiskit Terra "
"0.19.0 and will be removed no sooner than 3 months after the releasedate. "
"Use PauliList by pauli_list=True instead.",
DeprecationWarning,
stacklevel=2,
)
pauli_1q = PauliTable(
np.array([[False, False], [True, False], [True, True], [False, True]], dtype=bool)
)
if num_qubits == 1:
return pauli_1q
pauli = pauli_1q
Expand Down
Loading

0 comments on commit 75e06dc

Please sign in to comment.