Skip to content

Commit

Permalink
Replace qubit_pauli_string with qubit_pauli_tensor (#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-mills-cqc authored Dec 10, 2024
1 parent ac0d858 commit 6867816
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 67 deletions.
2 changes: 1 addition & 1 deletion qermit/noise_model/noise_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ def distribution(self) -> Dict[QubitPauliString, float]:
distribution: Dict[QubitPauliString, float] = {}
for stab, count in dict(self.pauli_error_counter).items():
# Note that the phase is ignored here
pauli_string, _ = stab.qubit_pauli_string
pauli_string = stab.qubit_pauli_tensor.string
distribution[pauli_string] = (
distribution.get(pauli_string, 0) + count / self.total
)
Expand Down
31 changes: 10 additions & 21 deletions qermit/noise_model/qermit_pauli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import numpy as np
from numpy.random import Generator
from pytket.circuit import Circuit, OpType, Qubit
from pytket.pauli import Pauli, QubitPauliString
from pytket.pauli import Pauli, QubitPauliString, QubitPauliTensor


class QermitPauli:
Expand Down Expand Up @@ -208,8 +208,7 @@ def __hash__(self):
return hash(key)

def __str__(self) -> str:
qubit_pauli_string, operator_phase = self.qubit_pauli_string
return f"{qubit_pauli_string}, {operator_phase}"
return str(self.qubit_pauli_tensor)

def __eq__(self, other: object) -> bool:
"""Checks for equality by checking all qubits match, and that all
Expand Down Expand Up @@ -538,10 +537,10 @@ def circuit(self) -> Circuit:
return circ

@property
def pauli_string(self) -> Tuple[List[Pauli], complex]:
"""List of Paulis which correspond to Pauli, and the phase.
def qubit_pauli_tensor(self) -> QubitPauliTensor:
"""Return QubitPauliTensor describing Pauli.
:return: [description]
:return: QubitPauliTensor describing Pauli.
"""

operator_phase = self.phase
Expand All @@ -558,21 +557,11 @@ def pauli_string(self) -> Tuple[List[Pauli], complex]:
operator_phase += 3
operator_phase %= 4

return paulis, self.phase_dict[operator_phase]

@property
def qubit_pauli_string(self) -> Tuple[QubitPauliString, complex]:
"""Qubit pauli string corresponding to Pauli,
along with the appropriate phase.
:return: Pauli string and phase corresponding to Pauli.
"""

paulis, operator_phase = self.pauli_string

qubit_pauli_string = QubitPauliString(qubits=self.qubit_list, paulis=paulis)

return qubit_pauli_string, operator_phase
return QubitPauliTensor(
qubits=self.qubit_list,
paulis=paulis,
coeff=self.phase_dict[operator_phase],
)

@classmethod
def from_pauli_iterable(
Expand Down
9 changes: 4 additions & 5 deletions tests/coherent_pauli_checks_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,12 +336,11 @@ def test_logical_error_coherent_pauli_check_workflow():
)

pauli_sampler = OptimalPauliSampler(noise_model=noise_model, n_checks=1)
assert pauli_sampler.sample(circ=cliff_sub_circ)[0].qubit_pauli_string == (
QubitPauliString(
qubits=[Qubit(0), Qubit(1), Qubit(2)], paulis=[Pauli.I, Pauli.I, Pauli.Z]
),
1,
pauli = pauli_sampler.sample(circ=cliff_sub_circ)[0]
assert pauli.qubit_pauli_tensor.string == QubitPauliString(
qubits=[Qubit(0), Qubit(1), Qubit(2)], paulis=[Pauli.I, Pauli.I, Pauli.Z]
)
assert pauli.qubit_pauli_tensor.coeff == 1

cliff_circ = Circuit()
cliff_circ.add_q_register(name="my_reg", size=3)
Expand Down
72 changes: 32 additions & 40 deletions tests/noise_model_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ def test_qermit_pauli_from_iterable() -> None:
pauli_iterable=qubit_pauli_string.map.values(),
qubit_list=list(qubit_pauli_string.map.keys()),
)
pauli.qubit_pauli_string == (qubit_pauli_string, 1 + 0j)
pauli.qubit_pauli_tensor.string == qubit_pauli_string
pauli.qubit_pauli_tensor.coeff == 1 + 0j


def test_qermit_pauli_commute_coeff() -> None:
Expand Down Expand Up @@ -606,26 +607,27 @@ def test_identity_clifford() -> None:
)
pauli.apply_circuit(circ)

qubit_pauli_string, phase = pauli.qubit_pauli_string
assert qubit_pauli_string == QubitPauliString(
assert pauli.qubit_pauli_tensor.string == QubitPauliString(
qubits=qubit_list, paulis=[Pauli.Z, Pauli.Z]
)
assert phase == 1
assert pauli.qubit_pauli_tensor.coeff == 1


def test_H() -> None:
qubit_list = [Qubit(0)]
pauli = QermitPauli(Z_list=[1], X_list=[0], qubit_list=qubit_list)

pauli.H(qubit=qubit_list[0])
qubit_pauli_string, phase = pauli.qubit_pauli_string
assert qubit_pauli_string == QubitPauliString(qubits=qubit_list, paulis=[Pauli.X])
assert phase == 1
assert pauli.qubit_pauli_tensor.string == QubitPauliString(
qubits=qubit_list, paulis=[Pauli.X]
)
assert pauli.qubit_pauli_tensor.coeff == 1

pauli.H(qubit=qubit_list[0])
qubit_pauli_string, phase = pauli.qubit_pauli_string
assert qubit_pauli_string == QubitPauliString(qubits=qubit_list, paulis=[Pauli.Z])
assert phase == 1
assert pauli.qubit_pauli_tensor.string == QubitPauliString(
qubits=qubit_list, paulis=[Pauli.Z]
)
assert pauli.qubit_pauli_tensor.coeff == 1


def test_h_series_gates() -> None:
Expand All @@ -643,11 +645,10 @@ def test_apply_circuit() -> None:

pauli.apply_circuit(circ)

qubit_pauli_string, phase = pauli.qubit_pauli_string
assert qubit_pauli_string == QubitPauliString(
assert pauli.qubit_pauli_tensor.string == QubitPauliString(
qubits=qubit_list, paulis=[Pauli.X, Pauli.Y]
)
assert phase == 1
assert pauli.qubit_pauli_tensor.coeff == 1


def test_apply_gate() -> None:
Expand All @@ -660,11 +661,10 @@ def test_apply_gate() -> None:

pauli.apply_gate(op_type=OpType.H, qubits=[qubit_list[0]])

qermit_qubit_pauli_string, phase = pauli.qubit_pauli_string
assert qermit_qubit_pauli_string == QubitPauliString(
assert pauli.qubit_pauli_tensor.string == QubitPauliString(
qubits=qubit_list, paulis=[Pauli.X, Pauli.Z, Pauli.Z]
)
assert phase == 1
assert pauli.qubit_pauli_tensor.coeff == 1

pauli.apply_gate(
op_type=OpType.CX,
Expand All @@ -674,15 +674,13 @@ def test_apply_gate() -> None:
qubit_pauli_string = QubitPauliString(
qubits=qubit_list, paulis=[Pauli.X, Pauli.I, Pauli.Z]
)
qermit_qubit_pauli_string, phase = pauli.qubit_pauli_string
assert qermit_qubit_pauli_string == qubit_pauli_string
assert phase == 1
assert pauli.qubit_pauli_tensor.string == qubit_pauli_string
assert pauli.qubit_pauli_tensor.coeff == 1

pauli.apply_gate(op_type=OpType.S, qubits=[qubit_list[1]])
qermit_qubit_pauli_string, phase = pauli.qubit_pauli_string

assert qermit_qubit_pauli_string == qubit_pauli_string
assert phase == 1
assert pauli.qubit_pauli_tensor.string == qubit_pauli_string
assert pauli.qubit_pauli_tensor.coeff == 1


def test_qubit_pauli_string() -> None:
Expand All @@ -696,47 +694,42 @@ def test_qubit_pauli_string() -> None:
qubit_pauli_string = QubitPauliString(
qubits=qubit_list, paulis=[Pauli.Z, Pauli.Z, Pauli.Z]
)
qermit_qubit_pauli_string, phase = pauli.qubit_pauli_string
assert qermit_qubit_pauli_string == qubit_pauli_string
assert phase == 1
assert pauli.qubit_pauli_tensor.string == qubit_pauli_string
assert pauli.qubit_pauli_tensor.coeff == 1

pauli.H(qubit_list[0])
pauli.S(qubit_list[0])

qubit_pauli_string = QubitPauliString(
qubits=qubit_list, paulis=[Pauli.Y, Pauli.Z, Pauli.Z]
)
qermit_qubit_pauli_string, phase = pauli.qubit_pauli_string
assert qermit_qubit_pauli_string == qubit_pauli_string
assert phase == 1
assert pauli.qubit_pauli_tensor.string == qubit_pauli_string
assert pauli.qubit_pauli_tensor.coeff == 1

pauli.CX(qubit_list[0], qubit_list[1])

qubit_pauli_string = QubitPauliString(
qubits=qubit_list, paulis=[Pauli.X, Pauli.Y, Pauli.Z]
)
qermit_qubit_pauli_string, phase = pauli.qubit_pauli_string
assert qermit_qubit_pauli_string == qubit_pauli_string
assert phase == 1
assert pauli.qubit_pauli_tensor.string == qubit_pauli_string
assert pauli.qubit_pauli_tensor.coeff == 1

pauli.S(qubit_list[1])

qubit_pauli_string = QubitPauliString(
qubits=qubit_list, paulis=[Pauli.X, Pauli.X, Pauli.Z]
)
qermit_qubit_pauli_string, phase = pauli.qubit_pauli_string
assert qermit_qubit_pauli_string == qubit_pauli_string
assert phase == -1
assert pauli.qubit_pauli_tensor.string == qubit_pauli_string
assert pauli.qubit_pauli_tensor.coeff == -1

pauli.S(qubit_list[0])
pauli.CX(qubit_list[0], qubit_list[2])

qubit_pauli_string = QubitPauliString(
qubits=qubit_list, paulis=[Pauli.X, Pauli.X, Pauli.Y]
)
qermit_qubit_pauli_string, phase = pauli.qubit_pauli_string
assert qermit_qubit_pauli_string == qubit_pauli_string
assert phase == -1
assert pauli.qubit_pauli_tensor.string == qubit_pauli_string
assert pauli.qubit_pauli_tensor.coeff == -1


def test_clifford_incremental() -> None:
Expand Down Expand Up @@ -801,9 +794,8 @@ def test_to_from_qps() -> None:
paulis=paulis,
)
stab = QermitPauli.from_qubit_pauli_string(qubit_pauli_string)
stab_qps, stab_phase = stab.qubit_pauli_string
assert stab_qps == qubit_pauli_string
assert stab_phase == 1 + 0j
assert stab.qubit_pauli_tensor.string == qubit_pauli_string
assert stab.qubit_pauli_tensor.coeff == 1 + 0j


def test_is_measureable() -> None:
Expand Down

0 comments on commit 6867816

Please sign in to comment.